-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifecycle_test.go
283 lines (249 loc) · 8.09 KB
/
lifecycle_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
package lifecycle_test
import (
"context"
"errors"
"os"
"sync/atomic"
"syscall"
"testing"
"time"
"zvelo.io/lifecycle"
)
func TestEmptyLifecycle(t *testing.T) {
ctx := lifecycle.New(context.Background())
// A lifecycle manager with no configuration should immediately return
// on manager with no error and not block.
err := lifecycle.Wait(ctx)
if err != nil {
t.Fatalf("empty lifecycle error: %v", err)
}
}
func TestSingleRoutine(t *testing.T) {
ctx := lifecycle.New(context.Background())
// A lifecycle manager with a single registered routine should immediately execute
// the routine without needing to call Wait.
var ran int64
lifecycle.Go(ctx, func() { atomic.StoreInt64(&ran, 1) })
time.Sleep(100 * time.Millisecond)
if atomic.LoadInt64(&ran) != 1 {
t.Error("lifecycle manager did not immediately run registered routine.")
}
}
func TestPrimaryError(t *testing.T) {
ctx := lifecycle.New(context.Background())
// A manager with a single erroring registered routine should return that
// error on Wait
lifecycle.GoErr(ctx, func() error { return errors.New("errored") })
err := lifecycle.Wait(ctx)
if err == nil {
t.Fatal("error expected but not received.")
}
if err.Error() != "errored" {
t.Fatalf("expected error of value \"errored\", but received: %v", err)
}
}
func TestMultiplePrimaryErrors(t *testing.T) {
ctx := lifecycle.New(context.Background())
// when multiple routines will error, the first error should be returned
// without waiting for the second routine to finish.
lifecycle.GoErr(ctx, func() error { return errors.New("error1") })
err := lifecycle.Wait(ctx)
if err == nil {
t.Fatal("error expected but none received.")
}
if err.Error() != "error1" {
t.Fatalf("expected error of value \"error1\", but received: %v", err)
}
}
func TestSingleDeferred(t *testing.T) {
ctx := lifecycle.New(context.Background())
// A manager with no primary routines and one deferred routine should
// execute the deferred routine on Wait. Deferred routines do not
// run immediately, requiring that Managebe explicitly invoked.
ran := false
lifecycle.Defer(ctx, func() { ran = true })
err := lifecycle.Wait(ctx)
if err != nil {
t.Fatalf("unexpected error on Wait: %v", err)
}
time.Sleep(100 * time.Millisecond)
if !ran {
t.Error("lifecycle manager did not run deferred routine upon Wait.")
}
}
func TestSingleDeferredError(t *testing.T) {
ctx := lifecycle.New(context.Background())
// A manager with no primary routines and one deferred routine should
// execute the deferred routine on Wait and return its error.
lifecycle.DeferErr(ctx, func() error { return errors.New("deferred error") })
err := lifecycle.Wait(ctx)
if err == nil {
t.Fatal("Manager with an erroring deferred expected error, but received none.")
}
if err.Error() != "deferred error" {
t.Fatalf("expected \"deferred error\" but got: %v", err)
}
}
func TestMultipleDeferredErrors(t *testing.T) {
ctx := lifecycle.New(context.Background())
// A manager with no primary routines and multiple deferred routines should
// execute the deferred routines, and return the first deferred error, not the last.
lifecycle.DeferErr(ctx, func() error { return errors.New("deferred error1") })
lifecycle.DeferErr(ctx, func() error {
time.Sleep(500 * time.Millisecond)
return errors.New("deferred error2")
})
err := lifecycle.Wait(ctx)
if err == nil {
t.Fatal("Manager with an erroring deferred expected error, but received none.")
}
if err.Error() != "deferred error1" {
t.Fatalf("expected \"deferred error1\" but got: %v", err)
}
}
func TestPrimaryAndSecondary(t *testing.T) {
ctx := lifecycle.New(context.Background())
// A manager with both a primary and deferred routine should execute both.
var primaryRan, deferredRan bool
lifecycle.Go(ctx, func() { primaryRan = true })
lifecycle.Defer(ctx, func() { deferredRan = true })
err := lifecycle.Wait(ctx)
if err != nil {
t.Fatalf("unexpected wait error: %v", err)
}
if !primaryRan {
t.Fatalf("primary routine did not run.")
}
if !deferredRan {
t.Fatalf("deferred routine did not run.")
}
}
func TestDeferredOnPrimaryError(t *testing.T) {
ctx := lifecycle.New(context.Background())
// a manager with a primary error should still run deferred routines.
var deferredRan bool
lifecycle.GoErr(ctx, func() error { return errors.New("primary error") })
lifecycle.Defer(ctx, func() { deferredRan = true })
err := lifecycle.Wait(ctx)
if err == nil {
t.Fatal("manager did not return primary routine error.")
}
if !deferredRan {
t.Fatal("deferred manager did not run on primary manager error.")
}
}
func TestDeferredTimeout(t *testing.T) {
ctx := lifecycle.New(
context.Background(),
lifecycle.WithTimeout(10*time.Millisecond),
)
// a manager with a deferred function that takes longer than the configured
// lifecycle timeout should return with a timeout error.
lifecycle.Defer(ctx, func() { time.Sleep(30 * time.Second) })
err := lifecycle.Wait(ctx)
if err == nil {
t.Fatal("deferred timeout expected a timeout error at 10ms.")
}
if err != context.DeadlineExceeded {
t.Fatalf("expected 'deadline exceeded' error but got: %v", err)
}
}
func TestContextCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
ctx = lifecycle.New(ctx,
lifecycle.WithTimeout(6*time.Second),
)
// a manger whose context is canceled should return before subroutines
// complete with a context cancelation error.
lifecycle.Go(ctx, func() { time.Sleep(10 * time.Second) })
lifecycle.Defer(ctx, func() { time.Sleep(10 * time.Second) })
go func() {
time.Sleep(1 * time.Second)
cancel()
}()
err := lifecycle.Wait(ctx)
if err == nil {
t.Fatal("canceled context expected canceled error.")
}
if err != context.Canceled {
t.Fatalf("expected 'context canceled' error but got: %v", err)
}
}
func TestSignalCancels(t *testing.T) {
ctx := lifecycle.New(context.Background(),
lifecycle.WithTimeout(1*time.Second),
lifecycle.WithSignals(syscall.SIGUSR1), // SIGUSR1 plays nicely with tests
)
// A long-running goroutine, when signaled, should invoke the deferred
// functions and wait up to timeout before interrupting the laggard.
deferredRan := int64(0)
lifecycle.Go(ctx, func() { time.Sleep(1 * time.Minute) })
lifecycle.Defer(ctx, func() { atomic.StoreInt64(&deferredRan, 1) })
go func() {
time.Sleep(100 * time.Millisecond)
process, _ := os.FindProcess(syscall.Getpid())
_ = process.Signal(syscall.SIGUSR1)
}()
err := lifecycle.Wait(ctx)
if err != context.DeadlineExceeded {
t.Fatalf("unexpected error on signal interrupt: %v", err)
}
if atomic.LoadInt64(&deferredRan) != 1 {
t.Fatal("signaled process did not run deferred func")
}
}
func TestIgnoreSignals(t *testing.T) {
ctx := lifecycle.New(
context.Background(),
lifecycle.WithTimeout(1*time.Second),
lifecycle.WithSignals(),
)
lifecycle.Defer(ctx, func() { time.Sleep(1 * time.Minute) })
go func() {
time.Sleep(100 * time.Millisecond)
process, _ := os.FindProcess(syscall.Getpid())
_ = process.Signal(syscall.SIGUSR1)
}()
err := lifecycle.Wait(ctx)
if err != context.DeadlineExceeded {
t.Fatalf("expected deadline exceeded, got: %v", err)
}
}
func TestDoneFunc(t *testing.T) {
ctx := lifecycle.New(context.Background())
// Given a function that returns a channel signaling when it is done:
var asyncCompleted bool
doAsyncThings := func() <-chan struct{} {
done := make(chan struct{})
go func() {
// ... do some things asynchronously
time.Sleep(100 * time.Millisecond)
// ... signaling done by closing the channel
asyncCompleted = true
close(done)
}()
return done
}
done := doAsyncThings()
// Use DoneFunc to conver the signal to
lifecycle.Go(ctx, lifecycle.DoneFunc(done))
err := lifecycle.Wait(ctx)
if err != nil {
t.Fatalf("Unexpected error from wait: %v", err)
}
if !asyncCompleted {
t.Fatalf("async job using DoneFunc did not complete.")
}
}
func TestErrors(t *testing.T) {
ctx := context.Background()
if err := lifecycle.Go(ctx); err == nil {
t.Error("expected an error")
}
if err := lifecycle.Defer(ctx); err == nil {
t.Error("expected an error")
}
if err := lifecycle.Wait(ctx); err == nil {
t.Error("expected an error")
}
}