-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_test.go
186 lines (138 loc) · 4.58 KB
/
local_test.go
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package context_test
import (
"fmt"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/wspowell/context"
)
type immutableContextKey struct{}
type localContextKey struct{}
type localValueContextKey struct{}
type duplicateContextKey struct{}
const (
immutableValue = "immutableValue"
localValue = "localValue"
duplicateValue = "myValue"
)
func checkContext(t *testing.T, ctx context.Context) {
t.Helper()
if ctx.Value(localContextKey{}) != nil {
assert.Fail(t, "expected 'localContextKey{}' to be nil")
}
if ctx.Value(duplicateContextKey{}).(string) != immutableValue {
assert.Fail(t, fmt.Sprintf("expected 'duplicatedKey' to be %v but was %v", immutableValue, ctx.Value(duplicateContextKey{})))
}
if ctx.Value(immutableContextKey{}).(string) != immutableValue {
assert.Fail(t, fmt.Sprintf("expected 'immutable' to be %v but was %v", immutableContextKey{}, ctx.Value(immutableContextKey{})))
}
}
func checkLocal(t *testing.T, ctx context.Context) {
t.Helper()
if ctx.Value(localContextKey{}).(string) != localValue {
assert.Fail(t, fmt.Sprintf("expected 'localContextKey{}' to be %v but was %v", localValue, ctx.Value(localContextKey{})))
}
if ctx.Value(duplicateContextKey{}).(string) != duplicateValue {
assert.Fail(t, fmt.Sprintf("expected 'duplicatedKey' to be %v but was %v", duplicateValue, ctx.Value(duplicateContextKey{})))
}
if ctx.Value(immutableContextKey{}).(string) != immutableValue {
assert.Fail(t, fmt.Sprintf("expected 'immutable' to be %v but was %v", immutableContextKey{}, ctx.Value(immutableContextKey{})))
}
}
func Test_Localize(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctx = context.WithValue(ctx, immutableContextKey{}, immutableValue)
ctx = context.WithValue(ctx, duplicateContextKey{}, immutableValue)
var localCtx context.Context
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
localCtx = context.Localize(ctx)
context.WithLocalValue(localCtx, localContextKey{}, localValue)
context.WithLocalValue(localCtx, duplicateContextKey{}, duplicateValue)
checkLocal(t, localCtx)
}()
wg.Wait()
checkContext(t, ctx)
}
func Test_WithLocalValue_Localize(t *testing.T) {
t.Parallel()
ctx := context.Background()
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
defer func() {
if err := recover(); err != nil {
t.Errorf("expected no panic, but did")
return
}
}()
ctx = context.Localize(ctx)
ctx = context.WithValue(ctx, immutableContextKey{}, immutableValue)
ctx = context.WithValue(ctx, duplicateContextKey{}, immutableValue)
context.WithLocalValue(ctx, localContextKey{}, localValue)
context.WithLocalValue(ctx, duplicateContextKey{}, duplicateValue)
}()
wg.Wait()
}
func Test_Context_ThreadSafety_Correct_Usage(t *testing.T) {
t.Parallel()
immutableValue := "immutableValue"
localValue := "localValue"
ctx := context.Background()
ctx = context.WithValue(ctx, immutableContextKey{}, immutableValue)
context.WithLocalValue(ctx, localContextKey{}, &localValue)
context.WithLocalValue(ctx, localValueContextKey{}, 15)
if ctx.Value(immutableContextKey{}) != immutableValue {
t.Errorf("expected immutableContextKey{} == immutableValue")
}
if ctx.Value(localContextKey{}) != &localValue {
t.Errorf("expected localContextKey{} == localValue")
}
if ctx.Value(localValueContextKey{}) != 15 {
t.Errorf("expected localValueContextKey{} == 15")
}
var paniced bool
var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
go func(boundaryCtx context.Context, wg *sync.WaitGroup) {
defer func() {
if err := recover(); err != nil {
paniced = true
}
}()
defer wg.Done()
localCtx := context.Localize(boundaryCtx)
if localCtx.Value(localContextKey{}) == &localValue {
t.Errorf("local value should not be copied")
}
if localCtx.Value(localValueContextKey{}) == 15 {
t.Errorf("local value should not be copied")
}
if localCtx.Value(localContextKey{}) == "goroutineValue" {
t.Errorf("local value should not be shared")
}
context.WithLocalValue(localCtx, localContextKey{}, "goroutineValue")
if localCtx.Value(localContextKey{}) != "goroutineValue" {
t.Errorf("local value should be set")
}
}(ctx, &wg)
}
wg.Wait()
if paniced {
t.Errorf("unexpected panic")
}
if ctx.Value(immutableContextKey{}) != immutableValue {
t.Errorf("expected immutableContextKey{} == immutableValue")
}
if ctx.Value(localContextKey{}) != &localValue {
t.Errorf("expected localContextKey{} == localValue")
}
if ctx.Value(localValueContextKey{}) != 15 {
t.Errorf("expected localValueContextKey{} == 15")
}
}