forked from wandb/parallel
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsafety_test.go
More file actions
451 lines (422 loc) · 11.9 KB
/
safety_test.go
File metadata and controls
451 lines (422 loc) · 11.9 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
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
//go:build !race
package parallel
import (
"context"
"errors"
"reflect"
"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
opsQueue := func() chan func(context.Context) {
g := Limited(context.Background(), 10)
for i := 0; i < 100; i++ {
g.Go(func(context.Context) {
atomic.AddInt64(&counter, 1)
})
}
return g.(*limitedGroup).ops
// leak the un-awaited group
}()
assert.NotNil(t, opsQueue)
runtime.GC() // Trigger cleanups for leaked resources
for op := range opsQueue {
op(nil) // have mercy and run those ops anyway, just so we get a full count
}
// The channel should get closed!
assert.Equal(t, int64(100), counter)
}
func TestCollectorCleanup(t *testing.T) {
t.Parallel()
valuePipe := func() chan int {
g := Collect[int](Unlimited(context.Background()))
g.Go(func(context.Context) (int, error) { return 1, nil })
return g.(collectingGroup[int]).pipe
// leak the un-awaited group
}()
assert.NotNil(t, valuePipe)
runtime.GC() // Trigger cleanups for leaked resources
for range valuePipe {
}
// The channel should get closed!
}
func TestFeederCleanup(t *testing.T) {
t.Parallel()
valuePipe := func() chan int {
g := Feed[int](Unlimited(context.Background()), func(context.Context, int) error { return nil })
g.Go(func(context.Context) (int, error) { return 1, nil })
return g.(feedingGroup[int]).pipe
// leak the un-awaited group
}()
assert.NotNil(t, valuePipe)
runtime.GC() // Trigger cleanups for leaked resources
for range valuePipe {
}
// The channel should get closed!
}
func TestGatherErrCleanup(t *testing.T) {
t.Parallel()
valuePipe := func() chan error {
g := GatherErrs(Unlimited(context.Background()))
g.Go(func(context.Context) error { return nil })
return g.(multiErrGroup).pipe
// leak the un-awaited group
}()
assert.NotNil(t, valuePipe)
runtime.GC() // Trigger cleanups for leaked resources
for range valuePipe {
}
// The channel should get closed!
}
func TestCollectWithErrsCleanup(t *testing.T) {
t.Parallel()
valuePipe := func() chan withErr[int] {
g := CollectWithErrs[int](Unlimited(context.Background()))
g.Go(func(context.Context) (int, error) { return 1, nil })
return g.(collectingMultiErrGroup[int]).pipe
// leak the un-awaited group
}()
assert.NotNil(t, valuePipe)
runtime.GC() // Trigger cleanups for leaked resources
for range valuePipe {
}
// The channel should get closed!
}
func TestFeedWithErrsCleanup(t *testing.T) {
t.Parallel()
valuePipe := func() chan withErr[int] {
g := FeedWithErrs(Unlimited(context.Background()),
func(context.Context, int) error { return nil })
g.Go(func(context.Context) (int, error) { return 1, nil })
return g.(feedingMultiErrGroup[int]).pipe
// leak the un-awaited group
}()
assert.NotNil(t, valuePipe)
runtime.GC() // Trigger cleanups for leaked resources
for range valuePipe {
}
// The channel should get closed!
}
func TestPanicGroup(t *testing.T) {
t.Parallel()
g := Unlimited(context.Background())
var blocker sync.WaitGroup
blocker.Add(1)
g.Go(func(context.Context) {
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()
assert.PanicsWithValue(t, "wow", func() {
g.Wait()
})
}
func TestPanicGroupSecondPath(t *testing.T) {
t.Parallel()
g := Unlimited(context.Background())
var blocker sync.WaitGroup
blocker.Add(1)
g.Go(func(context.Context) {
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()
assert.PanicsWithValue(t, "wow", func() {
g.Go(func(context.Context) {
t.Fatal("this op should never run")
})
})
}
func TestPanicLimitedGroup(t *testing.T) {
t.Parallel()
var waitForNonPanic, unblockInnocent, block sync.WaitGroup
waitForNonPanic.Add(1)
unblockInnocent.Add(1)
block.Add(1)
g := Limited(context.Background(), 10)
g.Go(func(context.Context) { // Innocent function
waitForNonPanic.Done()
unblockInnocent.Wait()
})
g.Go(func(context.Context) { // Panicking function
block.Wait()
unblockInnocent.Done()
panic("lol")
})
waitForNonPanic.Wait()
block.Done()
assert.PanicsWithValue(t, "lol", func() {
g.Wait()
})
}
func TestPanicLimitedGroupSecondPath(t *testing.T) {
t.Parallel()
var waitForNonPanic, unblockInnocent, block sync.WaitGroup
waitForNonPanic.Add(1)
unblockInnocent.Add(1)
block.Add(1)
g := Limited(context.Background(), 10)
g.Go(func(context.Context) { // Innocent function
waitForNonPanic.Done()
unblockInnocent.Wait()
})
g.Go(func(context.Context) { // Panicking function
block.Wait()
unblockInnocent.Done()
panic("lol")
})
waitForNonPanic.Wait()
block.Done()
assert.PanicsWithValue(t, "lol", func() {
// Eventually :)
for {
g.Go(func(context.Context) {})
}
})
}
func TestPanicFeedFunction(t *testing.T) {
t.Parallel()
g := Feed(Unlimited(context.Background()), func(context.Context, int) error {
panic("oh no!")
})
g.Go(func(context.Context) (int, error) {
return 1, nil
})
assert.PanicsWithValue(t, "oh no!", func() { _ = g.Wait() })
}
func TestPanicFeedWork(t *testing.T) {
t.Parallel()
g := Feed(Unlimited(context.Background()), func(context.Context, int) error {
t.Fatal("should not get called")
return nil
})
g.Go(func(context.Context) (int, error) {
panic("oh no!")
})
assert.PanicsWithValue(t, "oh no!", func() { _ = g.Wait() })
}
func TestPanicFeedWorkSecondPath(t *testing.T) {
t.Parallel()
g := Feed(Unlimited(context.Background()), func(context.Context, int) error {
t.Fatal("should not get called")
return nil
})
g.Go(func(context.Context) (int, error) {
panic("oh no!")
})
ctx, _ := g.(feedingGroup[int]).g.getContext()
<-ctx.Done()
assert.PanicsWithValue(t, "oh no!", func() {
g.Go(func(context.Context) (int, error) { return 2, nil })
})
}
func TestPanicFeedFunctionNotCalled(t *testing.T) {
t.Parallel()
g := Feed(Unlimited(context.Background()), func(context.Context, int) error {
panic("oh no!")
})
g.Go(func(context.Context) (int, error) {
return 0, errors.New("foo")
})
assert.NotPanics(t, func() {
assert.Errorf(t, g.Wait(), "foo")
})
}
func TestPanicFeedErrFunction(t *testing.T) {
t.Parallel()
g := FeedWithErrs(Unlimited(context.Background()), func(context.Context, int) error {
panic("oh no!")
})
g.Go(func(context.Context) (int, error) {
return 1, nil
})
assert.PanicsWithValue(t, "oh no!", func() { _ = g.Wait() })
}
func TestPanicFeedErrWork(t *testing.T) {
t.Parallel()
g := FeedWithErrs(Unlimited(context.Background()), func(context.Context, int) error {
t.Fatal("should not get a value")
return nil
})
g.Go(func(context.Context) (int, error) {
panic("oh no!")
})
assert.PanicsWithValue(t, "oh no!", func() { _ = g.Wait() })
}
func TestPanicFeedErrWorkSecondPath(t *testing.T) {
t.Parallel()
g := FeedWithErrs(Unlimited(context.Background()), func(context.Context, int) error {
t.Fatal("should not get a value")
return nil
})
g.Go(func(context.Context) (int, error) {
panic("oh no!")
})
ctx, _ := g.(feedingMultiErrGroup[int]).g.getContext()
<-ctx.Done()
assert.PanicsWithValue(t, "oh no!", func() {
g.Go(func(context.Context) (int, error) { return 2, nil })
})
}
func TestPanicFeedErrFunctionNoValues(t *testing.T) {
t.Parallel()
g := FeedWithErrs(Unlimited(context.Background()), func(context.Context, int) error {
panic("oh no!")
})
g.Go(func(context.Context) (int, error) {
return 0, errors.New("regular error")
})
assert.Errorf(t, g.Wait(), "regular error")
}
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()
})
assert.PanicsWithValue(t, "at the disco", outerGroup.Wait)
assert.PanicsWithValue(t, "at the disco", innerGroup.Wait)
assert.PanicsWithValue(t, "at the disco", outerGroup.Wait)
assert.PanicsWithValue(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") })
assert.PanicsWithValue(t, "at the disco", func() { _ = g.Wait() })
assert.PanicsWithValue(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
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()
assert.PanicsWithValue(t, "parallel executor pipe error: a "+
"collector using this same executor was probably not awaited", exec.Wait)
}
func TestPanicNil(t *testing.T) {
// Read what is actually thrown when we call panic(nil)
nilPanic := func() (p any) {
defer func() {
p = recover()
}()
panic(nil)
}()
if nilPanic != nil {
// We are probably on go1.21 or later, where panic(nil) is transformed
// into a runtime.PanicNilError.
assert.Equal(t,
"PanicNilError",
reflect.TypeOf(nilPanic).Elem().Name())
return
}
t.Parallel()
g := Unlimited(context.Background())
g.Go(func(context.Context) {
// Panics that are literally `nil` should also be caught, even though
// they aren't detectable without some trickery (prior to go1.21)
panic(nil)
})
assert.PanicsWithValue(t, nil, g.Wait)
}