-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphase_test.go
More file actions
283 lines (218 loc) · 6.15 KB
/
phase_test.go
File metadata and controls
283 lines (218 loc) · 6.15 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package phase_test
import (
"context"
"testing"
"time"
"github.com/aelse/phase"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func assertContextAlive(t *testing.T, ctx context.Context) {
t.Helper()
select {
case <-ctx.Done():
t.Errorf("Expected context to be alive")
default:
}
}
func assertContextFinished(t *testing.T, ctx context.Context) {
t.Helper()
select {
case <-ctx.Done():
default:
t.Errorf("Expected context to be finished")
}
}
func TestPhaseCloseOne(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
p, _ := phase.Next(ctx)
cancel()
<-p.ChildrenDone()
p.Close()
assert.Error(t, p.Err(), "expect context to return error")
}
func TestDeadlineInParent(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(10*time.Millisecond))
defer cancel()
p0, _ := phase.Next(ctx)
defer p0.Close()
// Expect not to block since deadline cancels context chain.
<-p0.Done()
assert.Error(t, p0.Err())
}
func TestPhaseCancelChild(t *testing.T) {
t.Parallel()
p0, _ := phase.Next(context.Background())
defer p0.Close() // cleanup
ctx, cancel := context.WithCancel(p0)
p1, _ := phase.Next(ctx)
defer p1.Close()
// Cancel child context
cancel()
t.Logf("cancelled child")
// expect not to timeout due to blocking
<-p1.Done()
require.Error(t, p1.Err(), "expect child context returns error")
require.NoError(t, p0.Err(), "expect parent context is alive")
}
func TestPhaseCancelParent(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
p0, _ := phase.Next(ctx)
defer p0.Close()
p1, _ := phase.Next(p0)
// Child is responsible for calling Done when its context ends.
go func() {
<-p1.Done()
<-p1.ChildrenDone()
p1.Close()
}()
// Cancel top level context, which should propagate down.
cancel()
// Expect test not to timeout due to blocking on either context.
<-p0.Done()
<-p1.Done()
require.Error(t, p0.Err())
require.Error(t, p1.Err())
}
func TestPhaseCancelHeirarchy(t *testing.T) {
t.Skip()
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
p0, _ := phase.Next(ctx)
p00, _ := phase.Next(p0)
p01, _ := phase.Next(p0)
p010, _ := phase.Next(p01)
p011, _ := phase.Next(p01)
// At the start nothing has terminated, and set up a Cancel trigger upon context end.
for _, p := range []phase.Phaser{p0, p00, p01, p010, p011} {
assertContextAlive(t, p)
go func(p phase.Phaser) {
<-p.Done()
<-p.ChildrenDone()
p.Close()
}(p)
}
// Cancel p01 phaser, which should also cancel p010 and p011 but nothing else.
p01.Close()
// Allow time for goroutines to run.
time.Sleep(10 * time.Millisecond)
for _, ctx := range []context.Context{p0, p00} {
assertContextAlive(t, ctx)
}
for _, ctx := range []context.Context{p01, p010, p011} {
assertContextFinished(t, ctx)
}
// Cancel top level context and everything should end.
cancel()
time.Sleep(10 * time.Millisecond)
for _, p := range []context.Context{p0, p00, p01, p010, p011} {
assertContextFinished(t, p)
}
}
func TestPhaseChainedCancel(t *testing.T) {
t.Parallel()
ctx0, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
ctx := ctx0
for i := range 10 {
p, _ := phase.Next(ctx)
ctx = p // setup next iteration
go func(p phase.Phaser, i int) {
t.Logf("%d: waiting on context cancellation", i)
<-p.Done()
// If the context ends we wait on children
t.Logf("%d: waiting on children", i)
<-p.ChildrenDone()
// and then signal parent we're done
t.Logf("%d: signal parent", i)
p.Close()
}(p, i)
}
assertContextAlive(t, ctx0)
assertContextAlive(t, ctx)
// Allow time for goroutines to run after top level context times out.
time.Sleep(100 * time.Millisecond)
assertContextFinished(t, ctx0)
assertContextFinished(t, ctx)
}
func TestPhaseCancelCascade(t *testing.T) {
t.Parallel()
// When a Phaser's upstream context ends it should cancel downstream elements before its own context.
// Any listeners to the Phaser's Done() channel should not fire until after all children have done the same.
ctx, cancel := context.WithCancel(context.Background())
results := make(chan string, 2)
p0, _ := phase.Next(ctx)
go func() {
<-p0.Done()
<-p0.ChildrenDone()
results <- "p0"
p0.Close()
}()
p1, _ := phase.Next(p0)
go func() {
<-p1.Done()
// Parent Phaser's context should not end until we send a notification via Cancel()
time.Sleep(10 * time.Millisecond)
<-p1.ChildrenDone()
results <- "p1"
p1.Close()
}()
// Cancel the root context to trigger the cascade.
cancel()
// The first result should always come from p1 since p0 should block.
v := <-results
if v != "p1" {
t.Errorf("Expected child context (p1) termination but got %s", v)
}
v2 := <-results
if v2 != "p0" {
t.Errorf("Expected parent context (p0) termination but got %s", v2)
}
}
func TestPhaseValue(t *testing.T) {
t.Parallel()
ctx := context.WithValue(context.Background(), &t, "test")
p0, _ := phase.Next(ctx)
defer p0.Close()
assert.Equal(t, "test", p0.Value(&t), "Expected value to propagate through phase context")
}
func TestPhaserErrWhenParentCtxCancelled(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
p0, _ := phase.Next(ctx)
defer p0.Close()
cancel()
<-p0.Done()
assert.Error(t, p0.Err(), "Expected error after cancellation")
}
func TestPhaserWaitBlocksUntilDone(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
p0, _ := phase.Next(ctx)
defer p0.Close()
cancel()
select {
case <-p0.Done():
t.Log("Waited for done")
default:
t.Error("Expected Cancel() to block until context was done")
}
assert.Error(t, p0.Err(), "Expected non nil error after cancellation")
}
func TestPhaserContextCancelPropagatesToPhaser(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
p0, _ := phase.Next(ctx)
defer p0.Close()
cancel()
select {
case <-p0.Done():
t.Log("Waited for done and found it")
default:
t.Error("Expected Cancel to terminate phaser context")
}
assert.Error(t, p0.Err(), "Expected non nil error after cancellation")
}