-
Notifications
You must be signed in to change notification settings - Fork 2
/
collect_test.go
426 lines (401 loc) · 11.5 KB
/
collect_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
package parallel
import (
"context"
"errors"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestErrGroup(t *testing.T) {
for _, test := range []struct {
name string
makeExec func(context.Context) Executor
}{
{"Unlimited", Unlimited},
{"Limited", func(ctx context.Context) Executor { return Limited(ctx, 10) }},
{"serial", func(ctx context.Context) Executor { return Limited(ctx, 0) }},
} {
test := test
t.Run(test.name, func(t *testing.T) {
testErrGroup(t, test.makeExec)
})
}
}
func testErrGroup(t *testing.T, makeExec func(context.Context) Executor) {
t.Parallel()
t.Run("nothing", func(t *testing.T) {
t.Parallel()
g := ErrGroup(makeExec(context.Background()))
assert.NoError(t, g.Wait())
})
t.Run("some", func(t *testing.T) {
t.Parallel()
g := ErrGroup(makeExec(context.Background()))
flag := 0
g.Go(func(context.Context) error {
flag = 1
return nil
})
assert.NoError(t, g.Wait())
assert.Equal(t, 1, flag)
})
t.Run("failing", func(t *testing.T) {
t.Parallel()
g := ErrGroup(makeExec(context.Background()))
g.Go(func(context.Context) error {
return errors.New("failed")
})
assert.Errorf(t, g.Wait(), "failed")
})
t.Run("canceled", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
g := ErrGroup(makeExec(ctx))
cancel()
g.Go(func(context.Context) error {
return errors.New("failed")
})
// context cancelation overrides other errors as long as the group isn't
// stopped by an error first
assert.ErrorIs(t, g.Wait(), context.Canceled)
})
}
func TestCollectNothing(t *testing.T) {
t.Parallel()
g := Collect[int](Unlimited(context.Background()))
res, err := g.Wait()
assert.NoError(t, err)
assert.Nil(t, res)
}
func TestCollectSome(t *testing.T) {
t.Parallel()
g := Collect[int](Unlimited(context.Background()))
g.Go(func(context.Context) (int, error) { return 1, nil })
g.Go(func(context.Context) (int, error) { return 1, nil })
g.Go(func(context.Context) (int, error) { return 1, nil })
res, err := g.Wait()
assert.NoError(t, err)
assert.Equal(t, []int{1, 1, 1}, res)
}
func TestCollectFailed(t *testing.T) {
t.Parallel()
g := Collect[int](Unlimited(context.Background()))
g.Go(func(context.Context) (int, error) { return 1, nil })
g.Go(func(context.Context) (int, error) { return 1, nil })
g.Go(func(context.Context) (int, error) { return 1, errors.New("nvm") })
res, err := g.Wait()
assert.Errorf(t, err, "nvm")
assert.Nil(t, res)
}
func TestCollectCanceled(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
g := Collect[int](Unlimited(ctx))
cancel()
g.Go(func(context.Context) (int, error) { return 1, errors.New("nvm") })
g.Go(func(context.Context) (int, error) { return 1, nil })
g.Go(func(context.Context) (int, error) { return 1, nil })
res, err := g.Wait()
// context cancellation overrides other errors as long as the group isn't
// stopped by an error first
assert.ErrorIs(t, err, context.Canceled)
assert.Nil(t, res)
}
func TestFeedNothing(t *testing.T) {
t.Parallel()
g := Feed[int](Unlimited(context.Background()), func(context.Context, int) error {
t.Fatal("never runs")
return nil
})
assert.NoError(t, g.Wait())
}
func TestFeedSome(t *testing.T) {
t.Parallel()
res := make(map[int]bool)
g := Feed(Unlimited(context.Background()), func(ctx context.Context, val int) error {
res[val] = true
return nil
})
g.Go(func(context.Context) (int, error) { return 1, nil })
g.Go(func(context.Context) (int, error) { return 2, nil })
g.Go(func(context.Context) (int, error) { return 3, nil })
assert.NoError(t, g.Wait())
assert.Equal(t, map[int]bool{1: true, 2: true, 3: true}, res)
}
func TestFeedErroring(t *testing.T) {
t.Parallel()
var res []int
g := Feed(Unlimited(context.Background()), func(ctx context.Context, val int) error {
res = append(res, val)
return nil
})
g.Go(func(context.Context) (int, error) { return 1, nil })
g.Go(func(context.Context) (int, error) { return 2, nil })
g.Go(func(context.Context) (int, error) { return 3, nil })
g.Go(func(context.Context) (int, error) { return 4, errors.New("oops") })
assert.Errorf(t, g.Wait(), "oops")
assert.Subset(t, []int{1, 2, 3}, res)
}
func TestFeedLastReceiverErrs(t *testing.T) {
t.Parallel()
// Even when the very very last item through the pipe group causes an error,
// the group's context shouldn't be canceled yet and it should still be able
// to set the error.
g := Feed(Limited(context.Background(), 0), func(ctx context.Context, val int) error {
if val == 10 {
return errors.New("boom")
} else {
return nil
}
})
for i := 1; i <= 10; i++ {
g.Go(func(ctx context.Context) (int, error) {
return i, nil
})
}
require.Error(t, g.Wait())
}
func TestFeedErroringInReceiver(t *testing.T) {
t.Parallel()
g := Feed(Unlimited(context.Background()), func(ctx context.Context, val int) error {
if val%2 == 1 {
return errors.New("odd numbers are unacceptable")
}
return nil
})
for i := 0; i < 100; i++ {
i := i
g.Go(func(context.Context) (int, error) { return i, nil })
}
assert.Errorf(t, g.Wait(), "odd numbers are unacceptable")
}
func TestFeedCanceled(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
g := Feed(Unlimited(ctx), func(ctx context.Context, val int) error {
return errors.New("error from receiver")
})
cancel()
g.Go(func(context.Context) (int, error) { return 1, errors.New("error from work") })
g.Go(func(context.Context) (int, error) { return 2, nil })
// context cancelation overrides other errors as long as the group isn't
// stopped by an error first.
assert.ErrorIs(t, g.Wait(), context.Canceled)
}
func TestGatherErrNothing(t *testing.T) {
t.Parallel()
g := GatherErrs(Unlimited(context.Background()))
assert.NoError(t, g.Wait())
}
func TestGatherNoErrs(t *testing.T) {
t.Parallel()
var res int64
g := GatherErrs(Unlimited(context.Background()))
g.Go(func(context.Context) error {
atomic.AddInt64(&res, 1)
return nil
})
g.Go(func(context.Context) error {
atomic.AddInt64(&res, 1)
return nil
})
g.Go(func(context.Context) error {
atomic.AddInt64(&res, 1)
return nil
})
assert.NoError(t, g.Wait())
assert.Equal(t, int64(3), res)
}
func TestGatherErrSome(t *testing.T) {
t.Parallel()
// Use a dummy executor so we ensure these run in order
g := GatherErrs(Limited(context.Background(), 0))
flag := 0
g.Go(func(context.Context) error {
return errors.New("oh no")
})
g.Go(func(context.Context) error {
flag = 1
return nil
})
g.Go(func(context.Context) error {
return NewMultiError(errors.New("another one"), errors.New("even more"))
})
err := g.Wait()
assert.Errorf(t, err, "oh no\nanother one\neven more")
assert.Equal(t, []error{
errors.New("oh no"),
errors.New("another one"),
errors.New("even more"),
}, err.Unwrap())
assert.Equal(t, 1, flag)
}
func TestCollectWithErrsNothing(t *testing.T) {
t.Parallel()
g := CollectWithErrs[int](Unlimited(context.Background()))
res, err := g.Wait()
assert.NoError(t, err)
assert.Nil(t, res)
}
func TestCollectWithErrsSome(t *testing.T) {
t.Parallel()
// Use a dummy executor so we ensure these run in order
g := CollectWithErrs[int](Limited(context.Background(), 0))
g.Go(func(context.Context) (int, error) {
return 0, errors.New("oh no")
})
g.Go(func(context.Context) (int, error) {
return 1, nil
})
g.Go(func(context.Context) (int, error) {
return 2, nil
})
g.Go(func(context.Context) (int, error) {
return 3, NewMultiError(errors.New("more"), errors.New("yet more"))
})
res, err := g.Wait()
assert.Equal(t, []int{1, 2}, res)
assert.Errorf(t, err, "oh no\nmore\nyet more")
// Multierrors get flattened :)
assert.Equal(t, []error{
errors.New("oh no"),
errors.New("more"),
errors.New("yet more"),
}, err.Unwrap())
}
func TestFeedWithErrsNothing(t *testing.T) {
t.Parallel()
g := FeedWithErrs(Unlimited(context.Background()), func(context.Context, int) error {
return nil
})
assert.NoError(t, g.Wait())
}
func TestFeedWithErrsSome(t *testing.T) {
t.Parallel()
res := make(map[int]bool)
// Use a dummy executor so we ensure these run in order
g := FeedWithErrs(Limited(context.Background(), 0), func(ctx context.Context, val int) error {
res[val] = true
return nil
})
g.Go(func(context.Context) (int, error) {
return 0, errors.New("oh no")
})
g.Go(func(context.Context) (int, error) {
return 1, nil
})
g.Go(func(context.Context) (int, error) {
return 2, nil
})
g.Go(func(context.Context) (int, error) {
return 3, NewMultiError(errors.New("more"), errors.New("yet more"))
})
err := g.Wait()
assert.Equal(t, map[int]bool{1: true, 2: true}, res)
assert.Errorf(t, err, "oh no\nmore\nyet more")
// Multierrors get flattened :)
assert.Equal(t, []error{
errors.New("oh no"),
errors.New("more"),
errors.New("yet more"),
}, err.Unwrap())
}
func TestFeedWithErrsInReceiver(t *testing.T) {
t.Parallel()
var res []int
// Use a dummy executor so we ensure these run in order
g := FeedWithErrs(Limited(context.Background(), 0), func(ctx context.Context, val int) error {
if val%5 == 0 {
return errors.New("buzz")
}
res = append(res, val)
return nil
})
for i := 1; i <= 10; i++ {
i := i
g.Go(func(context.Context) (int, error) {
if i%3 == 0 {
return 0, errors.New("fizz")
}
return i, nil
})
}
err := g.Wait()
assert.Equal(t, []int{1, 2, 4, 7, 8}, res)
assert.Error(t, err)
assert.Equal(t, []error{
errors.New("fizz"),
errors.New("buzz"),
errors.New("fizz"),
errors.New("fizz"),
errors.New("buzz"),
}, err.Unwrap())
}
func TestMultipleUsageOfExecutor(t *testing.T) {
t.Parallel()
for _, testCase := range []struct {
name string
executor Executor
}{
{"group", Unlimited(context.Background())},
{"limited", Limited(context.Background(), 10)},
{"serial", Limited(context.Background(), 0)},
} {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
collector := Collect[int](testCase.executor)
feedResult := make(map[string]bool)
feeder := Feed(testCase.executor, func(ctx context.Context, val string) error {
feedResult[val] = true
return nil
})
errorer := GatherErrs(testCase.executor)
sender := Unlimited(context.Background())
sender.Go(func(context.Context) {
collector.Go(func(context.Context) (int, error) {
return 1, nil
})
collector.Go(func(context.Context) (int, error) {
return 1, nil
})
collector.Go(func(context.Context) (int, error) {
return 1, nil
})
})
sender.Go(func(context.Context) {
feeder.Go(func(context.Context) (string, error) {
return "abc", nil
})
feeder.Go(func(context.Context) (string, error) {
return "foo", nil
})
feeder.Go(func(context.Context) (string, error) {
return "bar", nil
})
})
sender.Go(func(context.Context) {
errorer.Go(func(context.Context) error {
return nil
})
errorer.Go(func(context.Context) error {
return errors.New("kaboom")
})
})
sender.Wait()
collected, err := collector.Wait()
assert.NoError(t, err)
assert.Equal(t, []int{1, 1, 1}, collected)
assert.NoError(t, feeder.Wait())
assert.Equal(t, map[string]bool{"abc": true, "foo": true, "bar": true}, feedResult)
assert.Errorf(t, errorer.Wait(), "kaboom")
})
}
}
func TestWaitPipeGroupMultipleTimes(t *testing.T) {
t.Parallel()
g := Feed(Unlimited(context.Background()), func(context.Context, int) error { return nil })
assert.NotPanics(t, func() { assert.NoError(t, g.Wait()) })
assert.NotPanics(t, func() { assert.NoError(t, g.Wait()) })
}