-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
207 lines (168 loc) · 5.05 KB
/
context_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package ctxkey
import (
"context"
"fmt"
"sync"
"testing"
)
// ContextHandler is the interface for a "chunk" of work.
type ContextHandler interface {
Handle(context.Context)
}
// ContextHandlerFunc is a function type that implements ContextHandler
type ContextHandlerFunc func(ctx context.Context)
func (f ContextHandlerFunc) Handle(ctx context.Context) {
f(ctx)
}
// Handler wraps a ContextHandler and attaches an id to it.
type Handler struct {
ContextHandler
id string
}
// NewHandlerFromFunc creates a new Handler from a ContextHandlerFunc.
func NewHandlerFromFunc(h ContextHandlerFunc, id string) Handler {
return Handler{ContextHandler: h, id: id}
}
func ExampleNew() {
type ExpensiveComputation struct {
result string
}
CtxExpensiveObject := New[*ExpensiveComputation]()
useHandler := NewHandlerFromFunc(func(ctx context.Context) {
// fetch the computed value after the computation
fmt.Println(CtxExpensiveObject.MustValue(ctx).result)
}, "use")
// the compute handler performs some computation that we wish to re-use
computeHandler := NewHandlerFromFunc(func(ctx context.Context) {
myComputedExpensiveObject := ExpensiveComputation{result: "computed"}
ctx = CtxExpensiveObject.WithValue(ctx, &myComputedExpensiveObject)
useHandler.Handle(ctx)
}, "compute")
ctx := context.Background()
computeHandler.Handle(ctx)
// Output: computed
}
func ExampleNewWithDefault() {
type ExpensiveComputation struct {
result string
}
CtxExpensiveObject := NewWithDefault[*ExpensiveComputation](&ExpensiveComputation{result: "pending"})
useHandler := NewHandlerFromFunc(func(ctx context.Context) {
// fetch the computed value after the computation
fmt.Println(CtxExpensiveObject.MustNonEmptyValue(ctx).result)
}, "use")
// the compute handler performs some computation that we wish to re-use
computeHandler := NewHandlerFromFunc(func(ctx context.Context) {
// fetch the default value before the computation
fmt.Println(CtxExpensiveObject.MustNonEmptyValue(ctx).result)
myComputedExpensiveObject := ExpensiveComputation{result: "computed"}
ctx = CtxExpensiveObject.WithValue(ctx, &myComputedExpensiveObject)
useHandler.Handle(ctx)
}, "compute")
ctx := context.Background()
computeHandler.Handle(ctx)
// Output: pending
// computed
}
func ExampleNewBoxedWithDefault() {
type ExpensiveComputation struct {
result string
}
CtxExpensiveObject := NewBoxedWithDefault[*ExpensiveComputation](nil)
// the compute handler performs some computation that we wish to re-use
computeHandler := NewHandlerFromFunc(func(ctx context.Context) {
myComputedExpensiveObject := ExpensiveComputation{result: "computed"}
CtxExpensiveObject.WithValue(ctx, &myComputedExpensiveObject)
}, "compute")
decorateHandler := NewHandlerFromFunc(func(ctx context.Context) {
// adds an empty box
ctx = CtxExpensiveObject.WithBox(ctx)
// fills in the box with the value
computeHandler.Handle(ctx)
// returns the unboxed value
fmt.Println(CtxExpensiveObject.Value(ctx).result)
}, "decorated")
ctx := context.Background()
decorateHandler.Handle(ctx)
// Output: computed
}
func TestNew(t *testing.T) {
ctxKey := New[string]()
ctx := context.Background()
expectPanic(t, func() {
_ = ctxKey.MustValue(ctx)
})
ctx = ctxKey.WithValue(ctx, "value")
if ctxKey.MustValue(ctx) != "value" {
t.Fatal("expected value")
}
value, ok := ctxKey.Value(ctx)
if !ok {
t.Fatal("expected ok")
}
if value != "value" {
t.Fatal("expected value")
}
}
func TestNewWithDefault(t *testing.T) {
ctxKey := NewWithDefault[*string](nil)
ctx := context.Background()
expectPanic(t, func() {
_ = ctxKey.MustNonEmptyValue(ctx)
})
if value := ctxKey.Value(ctx); value != nil {
t.Fatal("expected default")
}
nonDefaultValue := "non-default"
ctx = ctxKey.WithValue(ctx, &nonDefaultValue)
if *ctxKey.MustNonEmptyValue(ctx) != "non-default" {
t.Fatal("expected value")
}
if value := ctxKey.Value(ctx); *value != "non-default" {
t.Fatal("expected value")
}
ctxKeyNonNilDefault := NewWithDefault[*string](&nonDefaultValue)
if value := ctxKeyNonNilDefault.Value(ctx); *value != "non-default" {
t.Fatal("expected value")
}
}
func TestNewBoxedWithDefault(t *testing.T) {
ctxKey := NewBoxedWithDefault[*string](nil)
ctx := context.Background()
if ctxKey.Value(ctx) != nil {
t.Fatal("expected nil")
}
ctx = ctxKey.WithBox(ctx)
if ctxKey.Value(ctx) != nil {
t.Fatal("expected nil")
}
value := "value"
ctx = ctxKey.WithValue(ctx, &value)
if *ctxKey.Value(ctx) != "value" {
t.Fatal("expected value")
}
ctxKeyNonNilDefault := NewBoxedWithDefault[*string](&value)
if *ctxKeyNonNilDefault.Value(ctx) != "value" {
t.Fatal("expected value")
}
ctxKeySetBoxAndValueTogether := NewBoxedWithDefault[*string](nil)
ctx = ctxKeySetBoxAndValueTogether.WithValue(ctx, &value)
if *ctxKeySetBoxAndValueTogether.Value(ctx) != "value" {
t.Fatal("expected value")
}
}
func expectPanic(t testing.TB, f func()) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
defer func() {
if recover() == nil {
t.Error("expected panic")
return
}
}()
f()
}()
wg.Wait()
}