-
Notifications
You must be signed in to change notification settings - Fork 2
/
safety_test.go
539 lines (507 loc) · 14.8 KB
/
safety_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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//go:build !race
package parallel
import (
"context"
"errors"
"runtime"
"sync"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
)
// The tests in this file can be detected as racy by the race condition checker
// because we are reaching under the hood to look at the group's channel, so we
// can see when the group's functions have started running. There's no good
// reason make those channels otherwise accessible, since they are completely
// owned by the group and making this work in a "non-racy" way would require
// extra complexity and overhead.
func TestLimitedGroupCleanup(t *testing.T) {
t.Parallel()
var counter int64
var leak contextLeak
opsQueue := func() chan func(context.Context) {
g := Limited(context.Background(), 10)
for i := 0; i < 100; i++ {
g.Go(func(ctx context.Context) {
atomic.AddInt64(&counter, 1)
leak.leak(ctx)
})
}
return g.(*limitedGroup).ops
// leak the un-awaited group
}()
assert.NotNil(t, opsQueue)
runtime.GC() // Trigger cleanups for leaked resources
runtime.GC() // Trigger cleanups for leaked resources
runtime.GC() // Trigger cleanups for leaked resources
// In the event that we need to drain the ops queue below, we need to have
// a context to leak that satisfies our test predicate.
fakeCtx, cancel := context.WithCancelCause(context.Background())
// We can cancel the context immediately since we don't really use it
cancel(errGroupAbandoned)
for op := range opsQueue {
op(fakeCtx) // have mercy and run those ops anyway, just so we get a full count
}
// The channel should get closed!
assert.Equal(t, int64(100), atomic.LoadInt64(&counter))
leak.assertAllCanceled(t, errGroupAbandoned)
}
func TestTrivialGroupCleanup(t *testing.T) {
t.Parallel()
var counter int64
var leak contextLeak
func() {
g := Limited(context.Background(), 0)
for i := 0; i < 100; i++ {
g.Go(func(ctx context.Context) {
counter++
leak.leak(ctx)
})
}
}()
runtime.GC() // Trigger cleanups for leaked resources
runtime.GC() // Trigger cleanups for leaked resources
runtime.GC() // Trigger cleanups for leaked resources
assert.Equal(t, int64(100), counter)
// The context should be canceled!
leak.assertAllCanceled(t, errGroupAbandoned)
}
func TestCollectorCleanup(t *testing.T) {
t.Parallel()
var leak contextLeak
valuePipe := func() chan int {
g := Collect[int](Unlimited(context.Background()))
g.Go(func(ctx context.Context) (int, error) {
leak.leak(ctx)
return 1, nil
})
return g.(collectingGroup[int]).pipe
// leak the un-awaited group
}()
assert.NotNil(t, valuePipe)
runtime.GC() // Trigger cleanup of the collector
runtime.GC() // Trigger cleanup of the executor it owned
runtime.GC() // One more for good measure
for range valuePipe {
// The channel should get closed!
}
leak.assertAllCanceled(t) // the cancelation error is inconsistent here,
// depending on whether the pipe group or the executor was reaped first
}
func TestFeederCleanup(t *testing.T) {
t.Parallel()
var leak contextLeak
valuePipe := func() chan int {
g := Feed[int](Unlimited(context.Background()), func(context.Context, int) error { return nil })
g.Go(func(ctx context.Context) (int, error) {
leak.leak(ctx)
return 1, nil
})
return g.(feedingGroup[int]).pipe
// leak the un-awaited group
}()
assert.NotNil(t, valuePipe)
runtime.GC() // Trigger cleanup of the feeder
runtime.GC() // Trigger cleanup of the executor it owned
runtime.GC() // One more for good measure
for range valuePipe {
// The channel should get closed!
}
leak.assertAllCanceled(t) // the cancelation error is inconsistent here,
// depending on whether the pipe group or the executor was reaped first
}
func TestGatherErrCleanup(t *testing.T) {
t.Parallel()
var leak contextLeak
valuePipe := func() chan error {
g := GatherErrs(Unlimited(context.Background()))
g.Go(func(ctx context.Context) error {
leak.leak(ctx)
return nil
})
return g.(multiErrGroup).pipe
// leak the un-awaited group
}()
assert.NotNil(t, valuePipe)
runtime.GC() // Trigger cleanup of the gatherer
runtime.GC() // Trigger cleanup of the executor it owned
runtime.GC() // One more for good measure
for range valuePipe {
// The channel should get closed!
}
leak.assertAllCanceled(t) // the cancelation error is inconsistent here,
// depending on whether the pipe group or the executor was reaped first
}
func TestCollectWithErrsCleanup(t *testing.T) {
t.Parallel()
var leak contextLeak
valuePipe := func() chan withErr[int] {
g := CollectWithErrs[int](Unlimited(context.Background()))
g.Go(func(ctx context.Context) (int, error) {
leak.leak(ctx)
return 1, nil
})
return g.(collectingMultiErrGroup[int]).pipe
// leak the un-awaited group
}()
assert.NotNil(t, valuePipe)
runtime.GC() // Trigger cleanup of the collector
runtime.GC() // Trigger cleanup of the executor it owned
runtime.GC() // One more for good measure
for range valuePipe {
// The channel should get closed!
}
leak.assertAllCanceled(t) // the cancelation error is inconsistent here,
// depending on whether the pipe group or the executor was reaped first
}
func TestFeedWithErrsCleanup(t *testing.T) {
t.Parallel()
var leak contextLeak
valuePipe := func() chan withErr[int] {
g := FeedWithErrs(Unlimited(context.Background()),
func(context.Context, int) error { return nil })
g.Go(func(ctx context.Context) (int, error) {
leak.leak(ctx)
return 1, nil
})
return g.(feedingMultiErrGroup[int]).pipe
// leak the un-awaited group
}()
assert.NotNil(t, valuePipe)
runtime.GC() // Trigger cleanup of the collector
runtime.GC() // Trigger cleanup of the executor it owned
runtime.GC() // One more for good measure
for range valuePipe {
// The channel should get closed!
}
leak.assertAllCanceled(t) // the cancelation error is inconsistent here,
// depending on whether the pipe group or the executor was reaped first
}
func TestPanicGroup(t *testing.T) {
t.Parallel()
var leak contextLeak
g := Unlimited(context.Background())
var blocker sync.WaitGroup
blocker.Add(1)
g.Go(func(ctx context.Context) {
leak.leak(ctx)
blocker.Wait()
panic("wow")
})
g.Go(func(context.Context) {
blocker.Done()
})
// Wait for the group to "die" when the panic hits
ctx, _ := g.getContext()
<-ctx.Done()
assertPanicsWithValue(t, "wow", func() {
g.Wait()
})
leak.assertAllCanceled(t, errPanicked)
}
func TestPanicGroupSecondPath(t *testing.T) {
t.Parallel()
var leak contextLeak
g := Unlimited(context.Background())
var blocker sync.WaitGroup
blocker.Add(1)
g.Go(func(ctx context.Context) {
leak.leak(ctx)
blocker.Wait()
panic("wow")
})
g.Go(func(context.Context) {
blocker.Done()
})
// Wait for the group to "die" when the panic hits
ctx, _ := g.getContext()
<-ctx.Done()
assertPanicsWithValue(t, "wow", func() {
g.Go(func(context.Context) {
t.Fatal("this op should never run")
})
})
leak.assertAllCanceled(t, errPanicked)
}
func TestPanicLimitedGroup(t *testing.T) {
t.Parallel()
var leak contextLeak
var waitForNonPanic, unblockInnocent, block sync.WaitGroup
waitForNonPanic.Add(1)
unblockInnocent.Add(1)
block.Add(1)
g := Limited(context.Background(), 10)
g.Go(func(ctx context.Context) { // Innocent function
leak.leak(ctx)
waitForNonPanic.Done()
unblockInnocent.Wait()
})
g.Go(func(context.Context) { // Panicking function
block.Wait()
unblockInnocent.Done()
panic("lol")
})
waitForNonPanic.Wait()
block.Done()
assertPanicsWithValue(t, "lol", func() {
g.Wait()
})
leak.assertAllCanceled(t, errPanicked)
}
func TestPanicLimitedGroupSecondPath(t *testing.T) {
t.Parallel()
var leak contextLeak
var waitForNonPanic, unblockInnocent, block sync.WaitGroup
waitForNonPanic.Add(1)
unblockInnocent.Add(1)
block.Add(1)
g := Limited(context.Background(), 10)
g.Go(func(ctx context.Context) { // Innocent function
leak.leak(ctx)
waitForNonPanic.Done()
unblockInnocent.Wait()
})
g.Go(func(context.Context) { // Panicking function
block.Wait()
unblockInnocent.Done()
panic("lol")
})
waitForNonPanic.Wait()
block.Done()
assertPanicsWithValue(t, "lol", func() {
// Eventually :)
for {
g.Go(func(context.Context) {})
}
})
leak.assertAllCanceled(t, errPanicked)
}
func TestPanicFeedFunction(t *testing.T) {
t.Parallel()
var leak contextLeak
g := Feed(Unlimited(context.Background()), func(ctx context.Context, _ int) error {
leak.leak(ctx)
panic("oh no!")
})
g.Go(func(context.Context) (int, error) {
return 1, nil
})
assertPanicsWithValue(t, "oh no!", func() { _ = g.Wait() })
leak.assertAllCanceled(t, errPanicked)
}
func TestPanicFeedWork(t *testing.T) {
t.Parallel()
var leak contextLeak
g := Feed(Unlimited(context.Background()), func(context.Context, int) error {
t.Fatal("should not get called")
return nil
})
g.Go(func(ctx context.Context) (int, error) {
leak.leak(ctx)
panic("oh no!")
})
assertPanicsWithValue(t, "oh no!", func() { _ = g.Wait() })
leak.assertAllCanceled(t, errPanicked)
}
func TestPanicFeedWorkSecondPath(t *testing.T) {
t.Parallel()
var leak contextLeak
g := Feed(Unlimited(context.Background()), func(context.Context, int) error {
t.Fatal("should not get a value")
return nil
})
g.Go(func(ctx context.Context) (int, error) {
leak.leak(ctx)
panic("oh no!")
})
ctx, _ := g.(feedingGroup[int]).g.getContext()
<-ctx.Done()
assertPanicsWithValue(t, "oh no!", func() {
g.Go(func(context.Context) (int, error) { return 2, nil })
})
leak.assertAllCanceled(t, errPanicked)
}
func TestPanicFeedFunctionNotCalled(t *testing.T) {
t.Parallel()
var leak contextLeak
g := Feed(Unlimited(context.Background()), func(context.Context, int) error {
t.Fatal("should not get a value")
return nil
})
fooError := errors.New("foo")
g.Go(func(ctx context.Context) (int, error) {
leak.leak(ctx)
return 0, fooError
})
assert.NotPanics(t, func() {
assert.ErrorIs(t, g.Wait(), fooError)
})
leak.assertAllCanceled(t, fooError)
}
func TestPanicFeedErrFunction(t *testing.T) {
t.Parallel()
var leak contextLeak
g := FeedWithErrs(Unlimited(context.Background()), func(context.Context, int) error {
panic("oh no!")
})
g.Go(func(ctx context.Context) (int, error) {
leak.leak(ctx)
return 1, nil
})
assertPanicsWithValue(t, "oh no!", func() { _ = g.Wait() })
leak.assertAllCanceled(t, errPanicked)
}
func TestPanicFeedErrWork(t *testing.T) {
t.Parallel()
var leak contextLeak
g := FeedWithErrs(Unlimited(context.Background()), func(context.Context, int) error {
t.Fatal("should not get a value")
return nil
})
g.Go(func(ctx context.Context) (int, error) {
leak.leak(ctx)
panic("oh no!")
})
assertPanicsWithValue(t, "oh no!", func() { _ = g.Wait() })
leak.assertAllCanceled(t, errPanicked)
}
func TestPanicFeedErrWorkSecondPath(t *testing.T) {
t.Parallel()
var leak contextLeak
g := FeedWithErrs(Unlimited(context.Background()), func(context.Context, int) error {
t.Fatal("should not get a value")
return nil
})
g.Go(func(ctx context.Context) (int, error) {
leak.leak(ctx)
panic("oh no!")
})
ctx, _ := g.(feedingMultiErrGroup[int]).g.getContext()
<-ctx.Done()
assertPanicsWithValue(t, "oh no!", func() {
g.Go(func(context.Context) (int, error) { return 2, nil })
})
leak.assertAllCanceled(t, errPanicked)
}
func TestPanicFeedErrFunctionNoValues(t *testing.T) {
t.Parallel()
var leak contextLeak
g := FeedWithErrs(Unlimited(context.Background()), func(context.Context, int) error {
t.Fatal("should not get a value")
return nil
})
g.Go(func(ctx context.Context) (int, error) {
leak.leak(ctx)
return 0, errors.New("regular error")
})
assert.Errorf(t, g.Wait(), "regular error")
leak.assertAllCanceled(t, errGroupDone)
}
func TestMisuseReuse(t *testing.T) {
t.Parallel()
limitedWithAllWorkers := Limited(context.Background(), 10)
for i := 0; i < 10; i++ {
limitedWithAllWorkers.Go(func(context.Context) {})
}
for _, testCase := range []struct {
name string
g Executor
}{
{"Unlimited", Unlimited(context.Background())},
{"Limited", Limited(context.Background(), 10)},
{"Serial", Limited(context.Background(), 0)},
{"Limited with all workers", limitedWithAllWorkers},
} {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
testCase.g.Wait()
assert.PanicsWithValue(
t,
"parallel executor misuse: don't reuse executors",
func() {
testCase.g.Go(func(context.Context) {
t.Fatal("this should never run")
})
},
)
})
}
}
func TestMisuseReuseCollector(t *testing.T) {
t.Parallel()
g := Collect[int](Unlimited(context.Background()))
res, err := g.Wait()
assert.NoError(t, err)
assert.Equal(t, []int(nil), res)
assert.PanicsWithValue(
t,
"parallel executor misuse: don't reuse executors",
func() {
g.Go(func(context.Context) (int, error) {
t.Fatal("this should never run")
return 1, nil
})
},
)
}
func TestGroupsPanicAgain(t *testing.T) {
t.Parallel()
for _, test := range []struct {
name string
g func() Executor
}{
{"Unlimited", func() Executor { return Unlimited(context.Background()) }},
{"Limited", func() Executor { return Limited(context.Background(), 10) }},
} {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
innerGroup := test.g()
outerGroup := test.g()
outerGroup.Go(func(context.Context) {
innerGroup.Go(func(context.Context) { panic("at the disco") })
innerGroup.Wait()
})
assertPanicsWithValue(t, "at the disco", outerGroup.Wait)
assertPanicsWithValue(t, "at the disco", innerGroup.Wait)
assertPanicsWithValue(t, "at the disco", outerGroup.Wait)
assertPanicsWithValue(t, "at the disco", innerGroup.Wait)
})
}
}
func TestPipeGroupPanicsAgain(t *testing.T) {
t.Parallel()
g := Feed(Unlimited(context.Background()), func(context.Context, int) error { return nil })
g.Go(func(context.Context) (int, error) { panic("at the disco") })
assertPanicsWithValue(t, "at the disco", func() { _ = g.Wait() })
assertPanicsWithValue(t, "at the disco", func() { _ = g.Wait() })
}
func TestForgottenPipeLegiblePanic(t *testing.T) {
t.Parallel()
exec := Unlimited(context.Background())
var blocker sync.WaitGroup
blocker.Add(1)
valuePipe := func() chan int {
g := Collect[int](exec)
g.Go(func(context.Context) (int, error) {
blocker.Wait()
return 1, nil
})
return g.(collectingGroup[int]).pipe
// leak the un-awaited group
}()
assert.NotNil(t, valuePipe)
runtime.GC() // Trigger cleanups for leaked resources
runtime.GC() // Trigger cleanups for leaked resources
runtime.GC() // Trigger cleanups for leaked resources
for range valuePipe {
}
// The collector's pipe is now closed. Unblock the task we submitted to the
// collector now, so its value will be sent to the closed pipe. When this
// happens the panic will be stored in the executor, so we re-panic that
// specific error with a more diagnostic message.
blocker.Done()
assertPanicsWithValue(t, "parallel executor pipe error: a "+
"collector using this same executor was probably not awaited", exec.Wait)
}