-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgroup_test.go
More file actions
310 lines (244 loc) · 8.41 KB
/
Copy pathgroup_test.go
File metadata and controls
310 lines (244 loc) · 8.41 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
package coda_test
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/marnixbouhuis/coda"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGroup(t *testing.T) {
t.Parallel()
t.Run("it should stop all goroutines and wait for them to shutdown when the shutdown manager is stopped", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown()
g, err := s.NewGroup("group", nil)
require.NoError(t, err)
var wg sync.WaitGroup
f := func(ctx context.Context, ready func()) error {
ready()
<-ctx.Done()
wg.Done()
return nil
}
wg.Add(3)
g.Go(f)
g.Go(f)
g.Go(f)
s.Stop()
// Make sure all goroutines stop
stopped := make(chan struct{})
go func() {
wg.Wait()
close(stopped)
}()
select {
case <-stopped:
// Done! all goroutines stopped
case <-time.After(time.Second):
assert.Fail(t, "timed out while waiting for goroutines to stop, did all routines get the shutdown signal?")
}
})
t.Run("it should not block when starting go routines if disabled", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown()
g, err := s.NewGroup("group", nil)
require.NoError(t, err)
f := func(ctx context.Context, ready func()) error {
<-time.After(2 * time.Second) // Add a delay before calling ready to make sure we are not blocking
ready()
<-ctx.Done()
return nil
}
now := time.Now()
g.Go(f, coda.WithBlock(false))
g.Go(f, coda.WithBlock(false))
g.Go(f, coda.WithBlock(false))
// Make sure we started in less than 1 second.
// The go function waits for 2 seconds on startup, we should not wait for this when blocking is disabled.
assert.Less(t, time.Since(now), time.Second, "Should start without blocking")
})
t.Run("it should block when starting go routines if enabled", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown()
g, err := s.NewGroup("group", nil)
require.NoError(t, err)
f := func(ctx context.Context, ready func()) error {
<-time.After(2 * time.Second) // Add a delay before calling ready to make sure we are not blocking
ready()
<-ctx.Done()
return nil
}
now := time.Now()
g.Go(f, coda.WithBlock(true))
g.Go(f, coda.WithBlock(true))
g.Go(f, coda.WithBlock(true))
// Make sure we started in less than 1 second.
// The go function waits for 2 seconds on startup, we should wait for this when blocking is enabled.
assert.Greater(t, time.Since(now), 2*3*time.Second, "Should start with blocking")
})
t.Run("it should max block until the ready timeout has been hit", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown()
g, err := s.NewGroup("group", nil)
require.NoError(t, err)
f := func(ctx context.Context, ready func()) error {
<-time.After(30 * time.Second) // Extremely high delay to make sure we don't fully wait for this
ready()
<-ctx.Done()
return nil
}
now := time.Now()
g.Go(f, coda.WithBlock(true), coda.WithReadyTimeout(time.Second), coda.WithCrashOnReadyTimeoutHit(false))
g.Go(f, coda.WithBlock(true), coda.WithReadyTimeout(time.Second), coda.WithCrashOnReadyTimeoutHit(false))
g.Go(f, coda.WithBlock(true), coda.WithReadyTimeout(time.Second), coda.WithCrashOnReadyTimeoutHit(false))
// Make sure we started in around 3 seconds.
// The go function waits for 30 seconds on startup, but the ready timeout is 1 second.
assert.WithinDuration(t, now.Add(3*time.Second), time.Now(), 500*time.Millisecond)
})
t.Run("it should not crash the shutdown handler on ready timeout hit if this is disabled", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown()
g, err := s.NewGroup("group", nil)
require.NoError(t, err)
f := func(ctx context.Context, ready func()) error {
<-time.After(5 * time.Second) // Extremely high delay to make sure we don't fully wait for this
ready()
<-ctx.Done()
return nil
}
g.Go(f,
coda.WithBlock(true),
coda.WithReadyTimeout(time.Second),
coda.WithCrashOnReadyTimeoutHit(false),
)
s.Stop()
assert.NoError(t, s.Wait())
})
t.Run("it should crash the shutdown handler on ready timeout hit if this is enabled", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown()
g, err := s.NewGroup("group", nil)
require.NoError(t, err)
f := func(ctx context.Context, ready func()) error {
<-time.After(5 * time.Second) // Extremely high delay to make sure we don't fully wait for this
ready()
<-ctx.Done()
return nil
}
g.Go(f,
coda.WithBlock(true),
coda.WithReadyTimeout(time.Second),
coda.WithCrashOnReadyTimeoutHit(true),
)
// No need to call s.Stop() since a crash should auto stop the handler
var actualErr *coda.GroupNotReadyError
require.ErrorAs(t, s.Wait(), &actualErr)
assert.Equal(t, "group", actualErr.GroupName)
})
t.Run("it should crash the shutdown handler on ready timeout hit if this is enabled even when blocking is disabled", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown()
g, err := s.NewGroup("group", nil)
require.NoError(t, err)
f := func(ctx context.Context, ready func()) error {
<-time.After(5 * time.Second) // Extremely high delay to make sure we don't fully wait for this
ready()
<-ctx.Done()
return nil
}
g.Go(f,
coda.WithBlock(false),
coda.WithReadyTimeout(time.Second),
coda.WithCrashOnReadyTimeoutHit(true),
)
// No need to call s.Stop() since a crash should auto stop the handler.
// We still don't block so g.Go should continue immediately.
var actualErr *coda.GroupNotReadyError
require.ErrorAs(t, s.Wait(), &actualErr)
assert.Equal(t, "group", actualErr.GroupName)
})
t.Run("it should not crash the shutdown handler if the goroutine errors and crashing on this is disabled", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown()
g, err := s.NewGroup("group", nil)
require.NoError(t, err)
testErr := errors.New("some error")
f := func(_ context.Context, ready func()) error {
ready()
return testErr
}
g.Go(f, coda.WithCrashOnError(false), coda.WithBlock(true))
// Wait a bit to make sure that the shutdown cloud have been processed in the background
<-time.After(time.Second)
s.Stop()
assert.NoError(t, s.Wait())
})
t.Run("it should crash the shutdown handler if the goroutine errors and this is enabled", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown()
g, err := s.NewGroup("group", nil)
require.NoError(t, err)
testErr := errors.New("some error")
f := func(_ context.Context, _ func()) error {
return testErr
}
g.Go(f, coda.WithCrashOnError(true))
// No need to call s.Stop() since a crash should auto stop the handler.
// We still don't block so g.Go should continue immediately.
err = s.Wait()
var actualErr *coda.GroupCrashError
require.ErrorAs(t, s.Wait(), &actualErr)
assert.Equal(t, "group", actualErr.GroupName)
// Make sure the original error is still in the chain
require.ErrorIs(t, err, testErr)
})
t.Run("starting a new goroutine on a shutdown handler that is stopped should not call the goroutine", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown()
g, err := s.NewGroup("group", nil)
require.NoError(t, err)
s.Stop()
require.NoError(t, s.Wait())
var called bool
g.Go(func(_ context.Context, _ func()) error {
called = true
return nil
})
<-time.After(time.Second)
assert.False(t, called)
})
t.Run("the shutdown handler should crash if a goroutine stops early and this is configured to do so", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown()
g, err := s.NewGroup("group", nil)
require.NoError(t, err)
f := func(_ context.Context, ready func()) error {
ready()
<-time.After(time.Second) // Stop after 1 second, even though the context is not canceled
return nil
}
g.Go(f, coda.WithCrashOnEarlyStop(true))
var actualErr *coda.GroupStoppedEarlyError
require.ErrorAs(t, s.Wait(), &actualErr)
assert.Equal(t, "group", actualErr.GroupName)
})
t.Run("the shutdown handler should not crash if a goroutine stops early and crashing on this is disabled", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown()
g, err := s.NewGroup("group", nil)
require.NoError(t, err)
f := func(_ context.Context, ready func()) error {
ready()
<-time.After(time.Second) // Stop after 1 second, even though the context is not canceled
return nil
}
g.Go(f, coda.WithCrashOnEarlyStop(false))
// Wait a bit in order for the goroutine to exit already
<-time.After(3 * time.Second)
s.Stop()
assert.NoError(t, s.Wait())
})
}