forked from neilotoole/errgroup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errgroupn_test.go
337 lines (283 loc) · 7.31 KB
/
errgroupn_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
package errgroup_test
import (
"context"
"errors"
"sync"
"testing"
"time"
"golang.org/x/sync/errgroup"
errgroupn "github.com/neilotoole/errgroup"
)
// grouper is an abstraction of errgroup.Group's exported methodset.
type grouper interface {
Go(f func() error)
Wait() error
}
var (
_ grouper = &errgroup.Group{}
_ grouper = &errgroupn.Group{}
)
func newErrgroupZero() (grouper, context.Context) {
return &errgroup.Group{}, context.Background()
}
func newErrgroupnZero() (grouper, context.Context) {
return &errgroupn.Group{}, context.Background()
}
func newErrgroupWithContext() (grouper, context.Context) {
return errgroup.WithContext(context.Background())
}
func newErrgroupnWithContext() (grouper, context.Context) {
return errgroupn.WithContext(context.Background())
}
func newErrgroupnWithContextN(numG, qSize int) func() (grouper, context.Context) {
return func() (grouper, context.Context) {
return errgroupn.WithContextN(context.Background(), numG, qSize)
}
}
func TestGroup(t *testing.T) {
testCases := []struct {
name string
newG func() (grouper, context.Context)
}{
{name: "errgroup_zero", newG: newErrgroupZero},
{name: "errgroup_wctx", newG: newErrgroupWithContext},
{name: "errgroupn_zero", newG: newErrgroupnZero},
{name: "errgroupn_wctx", newG: newErrgroupnWithContext},
{name: "errgroupn_wctx_0_0", newG: newErrgroupnWithContextN(0, 0)},
{name: "errgroupn_wctx_1_0", newG: newErrgroupnWithContextN(1, 0)},
{name: "errgroupn_wctx_1_1", newG: newErrgroupnWithContextN(1, 1)},
{name: "errgroupn_wctx_4_16", newG: newErrgroupnWithContextN(4, 16)},
{name: "errgroupn_wctx_16_4", newG: newErrgroupnWithContextN(16, 4)},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
g, _ := tc.newG()
vals := make([]int, fibMax)
for i := 0; i < fibMax; i++ {
i := i
g.Go(func() error {
vals[i] = fib(i)
return nil
})
}
err := g.Wait()
if err != nil {
t.Error(err)
}
if !equalInts(vals, fibVals[fibMax-1]) {
t.Errorf("vals (%d) incorrect: %v | %v", fibMax, vals, fibVals[fibMax])
}
// Let's do this a second time to verify that g.Go continues
// to work after the first call to g.Wait
vals = make([]int, fibMax)
for i := 0; i < fibMax; i++ {
i := i
g.Go(func() error {
vals[i] = fib(i)
return nil
})
}
err = g.Wait()
if err != nil {
t.Error(err)
}
if !equalInts(vals, fibVals[fibMax-1]) {
t.Errorf("vals (%d) incorrect: %v | %v", fibMax, vals, fibVals[fibMax])
}
})
}
}
func TestGroupWithErrors(t *testing.T) {
testCases := []struct {
name string
newG func() (grouper, context.Context)
}{
{name: "errgroup_zero", newG: newErrgroupZero},
{name: "errgroup_wctx", newG: newErrgroupWithContext},
{name: "errgroupn_zero", newG: newErrgroupnZero},
{name: "errgroupn_wctx", newG: newErrgroupnWithContext},
{name: "errgroupn_wctx_0_0", newG: newErrgroupnWithContextN(0, 0)},
{name: "errgroupn_wctx_1_0", newG: newErrgroupnWithContextN(1, 0)},
{name: "errgroupn_wctx_1_1", newG: newErrgroupnWithContextN(1, 1)},
{name: "errgroupn_wctx_4_16", newG: newErrgroupnWithContextN(4, 16)},
{name: "errgroupn_wctx_16_4", newG: newErrgroupnWithContextN(16, 4)},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
f := func() {
g, _ := tc.newG()
for i := 0; i < 1000; i++ {
i := i
g.Go(func() error {
// return an error for every 3
if i%3 == 0 {
return errors.New("sample error")
}
return nil
})
}
if g.Wait() == nil {
t.Error("Wait should return an error but did not")
}
}
// this may cause a deadlock, so running test with a timeout
testTimeout := 10 * time.Second
if err := mustRunInTime(testTimeout, f); err != nil {
t.Errorf("mustRunInTime failed with error: %v", err)
}
})
}
}
func TestEquivalence_GoWaitThenGoAgain(t *testing.T) {
testCases := []struct {
name string
newG func() (grouper, context.Context)
}{
{name: "errgroup_zero", newG: newErrgroupZero},
{name: "errgroup_wctx", newG: newErrgroupWithContext},
{name: "errgroupn_zero", newG: newErrgroupnZero},
{name: "errgroupn_wctx", newG: newErrgroupnWithContext},
{name: "errgroupn_wctx_16_4", newG: newErrgroupnWithContextN(16, 4)},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
g, gctx := tc.newG()
actionCh := make(chan struct{}, 1)
actionMu := &sync.Mutex{}
actionFn := func() error {
actionMu.Lock()
defer actionMu.Unlock()
_ = doWork(gctx, 10)
actionCh <- struct{}{}
return nil
}
g.Go(actionFn)
err := g.Wait()
if err != nil {
t.Error(err)
}
if len(actionCh) != 1 {
t.Errorf("actionCh should have one item")
}
// drain actionCh
<-actionCh
g.Go(actionFn)
err = g.Wait()
if err != nil {
t.Error(err)
}
if len(actionCh) != 1 {
t.Errorf("actionCh should have one item")
}
})
}
}
func TestEquivalence_WaitThenGo(t *testing.T) {
testCases := []struct {
name string
newG func() (grouper, context.Context)
}{
{name: "errgroup_zero", newG: newErrgroupZero},
{name: "errgroup_wctx", newG: newErrgroupWithContext},
{name: "errgroupn_zero", newG: newErrgroupnZero},
{name: "errgroupn_wctx", newG: newErrgroupnWithContext},
{name: "errgroupn_wctx_16_4", newG: newErrgroupnWithContextN(16, 4)},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
g, gctx := tc.newG()
actionCh := make(chan struct{}, 1)
actionMu := &sync.Mutex{}
actionFn := func() error {
actionMu.Lock()
defer actionMu.Unlock()
_ = doWork(gctx, 10)
actionCh <- struct{}{}
return nil
}
time.Sleep(time.Second)
err := g.Wait()
if err != nil {
t.Error(err)
}
if len(actionCh) != 0 {
t.Errorf("actionCh should have zero items")
}
g.Go(actionFn)
time.Sleep(time.Second)
err = g.Wait()
if err != nil {
t.Error(err)
}
if len(actionCh) != 1 {
t.Errorf("actionCh should have one item")
}
})
}
}
// fibVals holds computed values of the fibonacci sequence.
// Each row holds the fib sequence for that row's index. That is,
// the first few rows look like:
//
// [0]
// [0 1]
// [0 1 1]
// [0 1 1 2]
// [0 1 1 2 3]
var fibVals [][]int
const fibMax = 50
func init() {
fibVals = make([][]int, fibMax)
for i := 0; i < fibMax; i++ {
fibVals[i] = make([]int, i+1)
if i == 0 {
fibVals[0][0] = 0
continue
}
copy(fibVals[i], fibVals[i-1])
fibVals[i][i] = fib(i)
}
}
// fib returns the fibonacci sequence of n.
func fib(n int) int {
a, b, temp := 0, 1, 0
for i := 0; i < n; i++ {
temp = a
a = b
b = temp + a
}
return a
}
func equalInts(a, b []int) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// mustRunInTime returns an error if execution of a function
// takes more than the timeout set
func mustRunInTime(d time.Duration, f func()) error {
c := make(chan struct{}, 1)
// Run your long running function in it's own goroutine and pass back it's
// response into our channel.
go func() {
f()
c <- struct{}{}
}()
// Listen on our channel AND a timeout channel - which ever happens first.
select {
case <-c:
return nil
case <-time.After(d):
return errors.New("timeout")
}
}