Skip to content

Commit 5cfd3da

Browse files
authored
fix: fix context value setting for the function instance (#139)
1 parent 4def3fb commit 5cfd3da

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

fs/instance_impl.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/functionstream/function-stream/fs/api"
2424
"github.com/functionstream/function-stream/fs/contube"
2525
"github.com/pkg/errors"
26-
"github.com/sirupsen/logrus"
2726
"log/slog"
2827
)
2928

@@ -37,6 +36,8 @@ type FunctionInstanceImpl struct {
3736
index int32
3837
}
3938

39+
type CtxKey string
40+
4041
type DefaultInstanceFactory struct{}
4142

4243
func NewDefaultInstanceFactory() api.FunctionInstanceFactory {
@@ -45,10 +46,8 @@ func NewDefaultInstanceFactory() api.FunctionInstanceFactory {
4546

4647
func (f *DefaultInstanceFactory) NewFunctionInstance(definition *model.Function, sourceFactory contube.SourceTubeFactory, sinkFactory contube.SinkTubeFactory, index int32) api.FunctionInstance {
4748
ctx, cancelFunc := context.WithCancel(context.Background())
48-
ctx.Value(logrus.Fields{
49-
"function-name": definition.Name,
50-
"function-index": index,
51-
})
49+
ctx = context.WithValue(ctx, CtxKey("function-name"), definition.Name)
50+
ctx = context.WithValue(ctx, CtxKey("function-index"), index)
5251
return &FunctionInstanceImpl{
5352
ctx: ctx,
5453
cancelFunc: cancelFunc,

fs/instance_impl_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2024 Function Stream Org.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package fs
18+
19+
import (
20+
"github.com/functionstream/function-stream/common/model"
21+
"testing"
22+
)
23+
24+
func TestFunctionInstanceContextSetting(t *testing.T) {
25+
defaultInstanceFactory := DefaultInstanceFactory{}
26+
definition := &model.Function{
27+
Name: "test-function",
28+
}
29+
index := int32(1)
30+
instance := defaultInstanceFactory.NewFunctionInstance(definition, nil, nil, index)
31+
32+
if instance == nil {
33+
t.Error("FunctionInstance should not be nil")
34+
}
35+
36+
if ctxValue, ok := instance.Context().Value(CtxKey("function-name")).(string); !ok || ctxValue != definition.Name {
37+
t.Errorf("Expected 'function-name' in ctx to be '%s'", definition.Name)
38+
}
39+
40+
if ctxValue, ok := instance.Context().Value(CtxKey("function-index")).(int32); !ok || ctxValue != index {
41+
t.Errorf("Expected 'function-index' in ctx to be '%d'", index)
42+
}
43+
44+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
github.com/bmizerany/perks v0.0.0-20230307044200-03f9df79da1e
88
github.com/gorilla/mux v1.8.1
99
github.com/pkg/errors v0.9.1
10-
github.com/sirupsen/logrus v1.9.3
1110
github.com/spf13/cobra v1.8.0
1211
github.com/tetratelabs/wazero v1.6.0
1312
golang.org/x/net v0.21.0
@@ -53,6 +52,7 @@ require (
5352
github.com/prometheus/common v0.46.0 // indirect
5453
github.com/prometheus/procfs v0.12.0 // indirect
5554
github.com/rogpeppe/go-internal v1.11.0 // indirect
55+
github.com/sirupsen/logrus v1.9.3 // indirect
5656
github.com/spaolacci/murmur3 v1.1.0 // indirect
5757
github.com/spf13/pflag v1.0.5 // indirect
5858
go.uber.org/atomic v1.11.0 // indirect

0 commit comments

Comments
 (0)