-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctx_shared_test.go
More file actions
126 lines (114 loc) · 2.96 KB
/
ctx_shared_test.go
File metadata and controls
126 lines (114 loc) · 2.96 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package context
import "testing"
// RunContextTests runs all context tests. Called from environment-specific test files.
func RunContextTests(t *testing.T) {
t.Run("Background", func(t *testing.T) {
ctx := Background()
if ctx == nil {
t.Fatal("Background() returned nil")
}
if ctx.count != 0 {
t.Errorf("expected count 0, got %d", ctx.count)
}
})
t.Run("NilReceiver", func(t *testing.T) {
var ctx *Context
if v := ctx.Value("key"); v != "" {
t.Errorf("expected empty string for nil context, got '%s'", v)
}
})
t.Run("WithValueAndGet", func(t *testing.T) {
ctx := Background()
var err error
ctx, err = WithValue(ctx, "user", "alice")
if err != nil {
t.Fatal(err)
}
ctx, err = WithValue(ctx, "role", "admin")
if err != nil {
t.Fatal(err)
}
if v := ctx.Value("user"); v != "alice" {
t.Errorf("expected 'alice', got '%s'", v)
}
if v := ctx.Value("role"); v != "admin" {
t.Errorf("expected 'admin', got '%s'", v)
}
if v := ctx.Value("missing"); v != "" {
t.Errorf("expected '', got '%s'", v)
}
})
t.Run("OverwriteValue", func(t *testing.T) {
ctx, err := WithValue(Background(), "key", "v1")
if err != nil {
t.Fatal(err)
}
ctx, err = WithValue(ctx, "key", "v2")
if err != nil {
t.Fatal(err)
}
if v := ctx.Value("key"); v != "v2" {
t.Errorf("expected 'v2', got '%s'", v)
}
})
t.Run("MaxCapacity", func(t *testing.T) {
ctx := Background()
var err error
for i := 0; i < 16; i++ {
ctx, err = WithValue(ctx, "k", "v")
if err != nil {
t.Fatalf("unexpected error at index %d: %v", i, err)
}
}
// 17th should fail
_, err = WithValue(ctx, "k", "v")
if err != errCapacityExceeded {
t.Errorf("expected errCapacityExceeded, got %v", err)
}
})
t.Run("MutableSet", func(t *testing.T) {
ctx := Background()
if err := ctx.Set("name", "bob"); err != nil {
t.Fatal(err)
}
if v := ctx.Value("name"); v != "bob" {
t.Errorf("expected 'bob', got '%s'", v)
}
// Verify it mutates the same pointer
if err := ctx.Set("job", "dev"); err != nil {
t.Fatal(err)
}
if v := ctx.Value("job"); v != "dev" {
t.Errorf("expected 'dev', got '%s'", v)
}
if v := ctx.Value("name"); v != "bob" {
t.Errorf("expected 'bob' still there, got '%s'", v)
}
// Test capacity with Set
ctx = Background()
for i := 0; i < 16; i++ {
if err := ctx.Set("k", "v"); err != nil {
t.Fatalf("unexpected error at index %d: %v", i, err)
}
}
if err := ctx.Set("k", "v"); err != errCapacityExceeded {
t.Errorf("expected errCapacityExceeded, got %v", err)
}
})
t.Run("Keys", func(t *testing.T) {
ctx := Background()
_ = ctx.Set("k1", "v1")
_ = ctx.Set("k2", "v2")
_ = ctx.Set("k1", "v3") // Duplicate key
keys := ctx.Keys()
if len(keys) != 3 {
t.Errorf("expected 3 keys, got %d", len(keys))
}
expected := []string{"k1", "k2", "k1"}
for i, k := range keys {
if k != expected[i] {
t.Errorf("expected key %d to be %s, got %s", i, expected[i], k)
}
}
})
}