-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathctx_test.go
158 lines (128 loc) · 2.78 KB
/
ctx_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
package ctx
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestBindFunc(t *testing.T) {
f := BindFunc(func(d Doner) { panic("called") })
assert.Panics(t, func() { f.Bind(nil) }, "called")
}
func TestC(t *testing.T) {
ch := make(chan struct{})
close(ch)
select {
case <-C(ch).Done():
default:
t.Error("doner did not reflect closed state of channel")
}
}
func TestDefer(t *testing.T) {
ch := make(chan struct{})
close(ch)
chT := make(chan struct{})
Defer(C(ch), func() { close(chT) })
select {
case <-chT:
case <-time.After(time.Millisecond):
t.Error("deferred function was not called")
}
}
func TestLink(t *testing.T) {
ch := make(chan struct{})
close(ch)
select {
case <-Link(C(ch), C(nil)):
case <-time.After(time.Millisecond):
t.Error("link did not fire despite a Doner having fired")
}
}
func TestJoin(t *testing.T) {
ch := make(chan struct{})
close(ch)
c, cancel := context.WithCancel(context.Background())
d := Join(C(ch), c)
select {
case <-d.Done():
t.Error("premature firing of join-Doner")
default:
}
cancel()
select {
case <-d.Done():
case <-time.After(time.Millisecond):
t.Error("join-Doner did not fire despite all constituent Doners having fired")
}
}
func TestCtx(t *testing.T) {
ch := make(chan struct{})
c := AsContext(C(ch)) // should not panic
t.Run("Deadline", func(t *testing.T) {
d, ok := c.Deadline()
assert.Zero(t, d)
assert.False(t, ok)
})
t.Run("Value", func(t *testing.T) {
// should always be nil
assert.Nil(t, c.Value(struct{}{}))
})
t.Run("Err", func(t *testing.T) {
assert.NoError(t, c.Err())
close(ch)
assert.EqualError(t, c.Err(), context.Canceled.Error())
})
}
func TestWithCancel(t *testing.T) {
d, cancel := WithCancel(C(make(chan struct{})))
t.Run("NotCancelled", func(t *testing.T) {
select {
case <-d.Done():
t.Error("Doner expired by default")
default:
}
})
t.Run("Cancelled", func(t *testing.T) {
cancel()
select {
case <-d.Done():
default:
t.Error("not cancelled")
}
})
t.Run("IdempotentCancel", func(t *testing.T) {
cancel() // subsequent calls to cancel should not panic
})
t.Run("CloseUnderlyingDoner", func(t *testing.T) {
ch := make(chan struct{})
d, cancel := WithCancel(C(ch))
defer cancel() // should not panic
close(ch)
select {
case <-d.Done():
case <-time.After(time.Millisecond):
t.Error("not cancelled")
}
})
}
func TestTick(t *testing.T) {
ch := make(chan struct{})
tc := Tick(C(ch))
t.Run("RecvWhileOpen", func(t *testing.T) {
for i := 0; i < 10; i++ {
select {
case <-tc:
case <-time.After(time.Millisecond):
t.Error("no tick")
}
}
})
t.Run("BlockWhenClose", func(t *testing.T) {
close(ch)
select {
case <-tc:
t.Error("failed to stop ticking")
default:
}
})
}