-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_test.go
More file actions
77 lines (61 loc) · 1.82 KB
/
context_test.go
File metadata and controls
77 lines (61 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package hostlib
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewHostContext(t *testing.T) {
ctx := context.Background()
hc := NewHostContext(ctx, "dns_lookup")
require.NotNil(t, hc)
assert.Equal(t, "dns_lookup", hc.FunctionName())
}
func TestHostContext_SetGetValue(t *testing.T) {
hc := NewHostContext(context.Background(), "test_func")
// Initially no value
_, ok := hc.GetValue("key1")
assert.False(t, ok)
// Set a value
hc.SetValue("key1", "value1")
val, ok := hc.GetValue("key1")
assert.True(t, ok)
assert.Equal(t, "value1", val)
// Set another value
hc.SetValue("key2", 42)
val2, ok := hc.GetValue("key2")
assert.True(t, ok)
assert.Equal(t, 42, val2)
// First value still there
val, ok = hc.GetValue("key1")
assert.True(t, ok)
assert.Equal(t, "value1", val)
}
func TestHostContext_ImplementsContext(t *testing.T) {
parent := context.Background()
hc := NewHostContext(parent, "http_request")
// Verify it implements context.Context
var ctx context.Context = hc
assert.NotNil(t, ctx)
// Verify context methods work
assert.Nil(t, hc.Done())
assert.Nil(t, hc.Err())
assert.Nil(t, hc.Value("nonexistent"))
}
func TestHostContextFrom(t *testing.T) {
t.Run("wraps plain context", func(t *testing.T) {
ctx := context.Background()
hc := HostContextFrom(ctx, "tcp_connect")
assert.Equal(t, "tcp_connect", hc.FunctionName())
})
t.Run("returns existing HostContext unchanged", func(t *testing.T) {
original := NewHostContext(context.Background(), "original")
original.SetValue("marker", true)
returned := HostContextFrom(original, "different")
// Should return the same HostContext
assert.Equal(t, "original", returned.FunctionName())
val, ok := returned.GetValue("marker")
assert.True(t, ok)
assert.Equal(t, true, val)
})
}