-
Notifications
You must be signed in to change notification settings - Fork 1
/
context_dump_test.go
120 lines (111 loc) · 2.39 KB
/
context_dump_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
package synx
import (
"context"
"reflect"
"testing"
"time"
)
func TestDumpContext(t *testing.T) {
t.Skip()
var cancel context.CancelFunc
withCancel := func(ctx context.Context) context.Context {
ctx, cancel = context.WithCancel(ctx)
defer cancel()
return ctx
}
withTimeout := func(ctx context.Context) context.Context {
ctx, cancel = context.WithTimeout(ctx, time.Second)
defer cancel()
return ctx
}
withDeadline := func(ctx context.Context) context.Context {
ctx, cancel = context.WithDeadline(ctx, time.Now())
defer cancel()
return ctx
}
withNop := func(ctx context.Context) context.Context {
type nopCtx struct {
context.Context
}
return nopCtx{ctx}
}
testCases := []struct {
ctx context.Context
wantValues map[any]any
}{
{
ctx: nil,
wantValues: nil,
},
{
ctx: context.Background(),
wantValues: map[any]any{},
},
{
ctx: withCancel(context.Background()),
wantValues: map[any]any{},
},
{
ctx: withTimeout(context.Background()),
wantValues: map[any]any{},
},
{
ctx: withDeadline(context.Background()),
wantValues: map[any]any{},
},
{
ctx: withNop(context.Background()),
wantValues: map[any]any{},
},
{
ctx: context.WithValue(context.Background(), "foo", "bar"),
wantValues: map[any]any{
"foo": "bar",
},
},
{
ctx: context.WithValue(context.WithValue(
context.WithValue(context.Background(), "foo1", "bar1"),
"foo2", "bar2"),
"foo3", "bar3"),
wantValues: map[any]any{
"foo1": "bar1",
"foo2": "bar2",
"foo3": "bar3",
},
},
{
ctx: withDeadline(context.WithValue(
withTimeout(context.WithValue(
withCancel(context.WithValue(
context.Background(), "foo", "bar"),
), "foo2", "bar2"),
), "foo3", "bar3"),
),
wantValues: map[any]any{
"foo": "bar",
"foo2": "bar2",
"foo3": "bar3",
},
},
{
ctx: withDeadline(context.WithValue(
withTimeout(context.WithValue(
withNop(withCancel(context.WithValue(
context.Background(), "foo", "bar")),
), "foo2", "bar2"),
), "foo3", "bar3"),
),
wantValues: map[any]any{
"foo2": "bar2",
"foo3": "bar3",
},
},
}
for i, test := range testCases {
values := DumpContext(test.ctx)
if !reflect.DeepEqual(values, test.wantValues) {
t.Fatalf("#%d: want %+v, got %+v", i+1, test.wantValues, values)
}
}
}