-
Notifications
You must be signed in to change notification settings - Fork 0
/
errgroup_test.go
172 lines (138 loc) · 4.38 KB
/
errgroup_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
package errgroup
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/suite"
)
type errgroupSuite struct{ suite.Suite }
func (suite *errgroupSuite) TestPanicWithString() {
g, ctx := WithContext(context.Background())
g.Go(func() error { panic("oh noes") })
// this function ensures that the panic in fact cancels the context, by not
// returning until it's been cancelled; it should return context.Canceled
g.Go(func() error {
<-ctx.Done()
return ctx.Err()
})
// Wait() will finish only once all goroutines do, but returns the first
// error
err := g.Wait()
suite.Require().Error(err)
suite.Require().Contains(err.Error(), "oh noes")
// ctx should now be canceled.
suite.Require().Error(ctx.Err())
}
func (suite *errgroupSuite) TestPanicWithError() {
g, ctx := WithContext(context.Background())
panicErr := errors.New("oh noes")
g.Go(func() error { panic(panicErr) })
// this function ensures that the panic in fact cancels the context, by not
// returning until it's been cancelled; it should return context.Canceled
g.Go(func() error {
<-ctx.Done()
return ctx.Err()
})
// Wait() will finish only once all goroutines do, but returns the first
// error
err := g.Wait()
suite.Require().Error(err)
suite.Require().Contains(err.Error(), "oh noes")
suite.Require().True(errors.Is(err, panicErr))
// ctx should now be canceled.
suite.Require().Error(ctx.Err())
}
func (suite *errgroupSuite) TestPanicWithOtherValue() {
g, ctx := WithContext(context.Background())
panicVal := struct {
int
string
}{1234567890, "oh noes"}
g.Go(func() error { panic(panicVal) })
// this function ensures that the panic in fact cancels the context, by not
// returning until it's been cancelled; it should return context.Canceled
g.Go(func() error {
<-ctx.Done()
return ctx.Err()
})
// Wait() will finish only once all goroutines do, but returns the first
// error
err := g.Wait()
suite.Require().Error(err)
suite.Require().Contains(err.Error(), "oh noes")
suite.Require().Contains(err.Error(), "1234567890")
// ctx should now be canceled.
suite.Require().Error(ctx.Err())
}
func (suite *errgroupSuite) TestError() {
g, ctx := WithContext(context.Background())
goroutineErr := errors.New("oh noes")
g.Go(func() error { return goroutineErr })
// this function ensures that the panic in fact cancels the context, by not
// returning until it's been cancelled; it should return context.Canceled
g.Go(func() error {
<-ctx.Done()
return ctx.Err()
})
// Wait() will finish only once all goroutines do, but returns the first
// error
err := g.Wait()
suite.Require().Error(err)
suite.Require().Contains(err.Error(), "oh noes")
suite.Require().True(errors.Is(err, goroutineErr))
// ctx should now be canceled.
suite.Require().Error(ctx.Err())
}
func (suite *errgroupSuite) TestSuccess() {
g, ctx := WithContext(context.Background())
g.Go(func() error { return nil })
// since no goroutine errored, ctx.Err() should be nil
// (until all goroutines are done)
g.Go(ctx.Err)
err := g.Wait()
suite.Require().NoError(err)
// ctx should now still be canceled.
suite.Require().Error(ctx.Err())
}
func (suite *errgroupSuite) TestManyGoroutines() {
n := 100
g, ctx := WithContext(context.Background())
for i := 0; i < n; i++ {
// put in a bunch of goroutines that just return right away
g.Go(func() error { return nil })
// and also a bunch that wait for the error
g.Go(func() error {
<-ctx.Done()
return ctx.Err()
})
}
// finally, put in a panic
g.Go(func() error { panic("oh noes") })
// as before, Wait() will finish only once all goroutines do, but returns
// the first error (namely the panic)
err := g.Wait()
suite.Require().Error(err)
suite.Require().Contains(err.Error(), "oh noes")
// ctx should now be canceled.
suite.Require().Error(ctx.Err())
}
func (suite *errgroupSuite) TestZeroGroupPanic() {
var g Group
// either of these could happen first, since a zero group does not cancel
g.Go(func() error { panic("oh noes") })
g.Go(func() error { return nil })
// Wait() still returns the error.
err := g.Wait()
suite.Require().Error(err)
suite.Require().Contains(err.Error(), "oh noes")
}
func (suite *errgroupSuite) TestZeroGroupSuccess() {
var g Group
g.Go(func() error { return nil })
g.Go(func() error { return nil })
err := g.Wait()
suite.Require().NoError(err)
}
func TestErrgroup(t *testing.T) {
suite.Run(t, new(errgroupSuite))
}