-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_test.go
More file actions
126 lines (95 loc) · 2.45 KB
/
context_test.go
File metadata and controls
126 lines (95 loc) · 2.45 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 core_test
import (
. "dappco.re/go"
)
func TestContext_Background_Good(t *T) {
ctx := Background()
AssertNil(t, ctx.Err())
AssertNil(t, ctx.Done())
}
func TestContext_Background_Bad(t *T) {
AssertNil(t, Background().Value("agent"))
}
func TestContext_Background_Ugly(t *T) {
AssertNotSameContext(t, Background(), TODO())
}
func TestContext_TODO_Good(t *T) {
ctx := TODO()
AssertNil(t, ctx.Err())
AssertNil(t, ctx.Done())
}
func TestContext_TODO_Bad(t *T) {
AssertNil(t, TODO().Value("agent"))
}
func TestContext_TODO_Ugly(t *T) {
AssertNotSameContext(t, TODO(), Background())
}
func TestContext_WithCancel_Good(t *T) {
ctx, cancel := WithCancel(Background())
cancel()
assertContextDone(t, ctx)
}
func TestContext_WithCancel_Bad(t *T) {
AssertPanics(t, func() { WithCancel(nil) })
}
func TestContext_WithCancel_Ugly(t *T) {
ctx, cancel := WithCancel(Background())
AssertNotPanics(t, func() {
cancel()
cancel()
})
assertContextDone(t, ctx)
}
func TestContext_WithDeadline_Good(t *T) {
ctx, cancel := WithDeadline(Background(), Now().Add(Second))
defer cancel()
deadline, ok := ctx.Deadline()
AssertTrue(t, ok)
AssertGreater(t, Until(deadline), Duration(0))
}
func TestContext_WithDeadline_Bad(t *T) {
AssertPanics(t, func() { WithDeadline(nil, Now()) })
}
func TestContext_WithDeadline_Ugly(t *T) {
ctx, cancel := WithDeadline(Background(), UnixTime(0))
defer cancel()
assertContextDone(t, ctx)
}
func TestContext_WithTimeout_Good(t *T) {
ctx, cancel := WithTimeout(Background(), Millisecond)
defer cancel()
Sleep(2 * Millisecond)
assertContextDone(t, ctx)
}
func TestContext_WithTimeout_Bad(t *T) {
AssertPanics(t, func() { WithTimeout(nil, Millisecond) })
}
func TestContext_WithTimeout_Ugly(t *T) {
ctx, cancel := WithTimeout(Background(), 0)
defer cancel()
assertContextDone(t, ctx)
}
func TestContext_WithValue_Good(t *T) {
ctx := WithValue(Background(), "request_id", "req-123")
AssertEqual(t, "req-123", ctx.Value("request_id"))
}
func TestContext_WithValue_Bad(t *T) {
AssertPanics(t, func() { WithValue(Background(), nil, "req-123") })
}
func TestContext_WithValue_Ugly(t *T) {
ctx := WithValue(Background(), "", "empty-key")
AssertEqual(t, "empty-key", ctx.Value(""))
}
func assertContextDone(t *T, ctx Context) {
t.Helper()
select {
case <-ctx.Done():
AssertNotNil(t, ctx.Err())
default:
t.Fatalf("context not done")
}
}
func AssertNotSameContext(t *T, a, b Context) {
t.Helper()
AssertFalse(t, a == b)
}