-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontext_test.go
149 lines (118 loc) · 2.58 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
package synx
import (
"context"
"testing"
"time"
)
const testDelay = 100 * time.Millisecond
func TestContextFromSignal(t *testing.T) {
ch := make(chan struct{})
ctx := ContextFromSignal(ch)
if val := ctx.Value("anything"); val != nil {
t.Fatal("must store nothing")
}
deadline, ok := ctx.Deadline()
if want := (time.Time{}); ok || !want.Equal(deadline) {
t.Fatalf("want (%v, %v) got (%v, %v)", want, false, deadline, ok)
}
go func() {
time.Sleep(testDelay)
close(ch)
}()
if err := ctx.Err(); err != nil {
t.Fatal(err)
}
select {
case <-ctx.Done():
t.Fatal("must not be done yet")
default:
// pass
}
<-ch
select {
case <-ctx.Done():
default:
t.Fatal("must be done already")
}
if err := ctx.Err(); err == nil {
t.Fatal("must be nil")
}
}
func TestWithCancel(t *testing.T) {
ctx, cancel := WithCancel()
if err := ctx.Err(); err != nil {
t.Fatal(err)
}
cancel()
select {
case <-ctx.Done():
default:
t.Fatal()
}
}
func TestWithDeadline(t *testing.T) {
ctx, cancel := WithDeadline(time.Now().Add(100 * time.Millisecond))
defer cancel()
if err := ctx.Err(); err != nil {
t.Fatal(err)
}
select {
case <-ctx.Done():
case <-time.After(150 * time.Millisecond):
t.Fatal()
}
}
func TestWithTimeout(t *testing.T) {
ctx, cancel := WithTimeout(100 * time.Millisecond)
defer cancel()
if err := ctx.Err(); err != nil {
t.Fatal(err)
}
select {
case <-ctx.Done():
case <-time.After(150 * time.Millisecond):
t.Fatal()
}
}
func TestContextWithoutValues(t *testing.T) {
ctx := context.WithValue(context.Background(), "foo", "bar")
ctx = ContextWithoutValues(ctx)
if got := ctx.Value("foo"); got != nil {
t.Fatalf("got %v, want %v", got, nil)
}
}
func TestContextCtxKey(t *testing.T) {
type UserID int64
type TeamID int64
if any(ctxKey[UserID]{}) == any(ctxKey[TeamID]{}) {
t.Fatal()
}
}
func TestContextGetValue(t *testing.T) {
type UserID int64
type UnknownID int64
ctx := context.Background()
ctx = WithValue(ctx, UserID(10))
if got := GetValue[UserID](ctx); got != 10 {
t.Fatalf("got %v, want %v", got, nil)
}
if got := GetValue[UnknownID](ctx); got != 0 {
t.Fatalf("got %v, want %v", got, nil)
}
}
func TestContextWithValues(t *testing.T) {
ctx := context.WithValue(context.Background(), "foo", "bar")
ctx = ContextWithValues(ctx, map[any]any{
"a": "b",
10: 20,
})
if got := ctx.Value("foo"); got != "bar" {
t.Fatalf("got %v, want %v", got, "bar")
}
if got := ctx.Value("a"); got != "b" {
t.Fatalf("got %v, want %v", got, nil)
}
if got := ctx.Value(10); got != 20 {
t.Fatalf("got %v, want %v", got, nil)
}
}