-
Notifications
You must be signed in to change notification settings - Fork 1
/
app_test.go
376 lines (348 loc) · 8.48 KB
/
app_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
package lu_test
import (
"context"
"io"
"os"
"testing"
"time"
"github.com/luno/jettison/jtest"
"github.com/luno/jettison/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/luno/lu"
"github.com/luno/lu/process"
"github.com/luno/lu/test"
)
func TestLifecycle(t *testing.T) {
ev := make(test.EventLog, 100)
a := &lu.App{OnEvent: ev.Append}
a.OnStartUp(func(ctx context.Context) error {
log.Info(ctx, "starting up")
return nil
}, lu.WithHookName("basic start hook"))
a.OnShutdown(func(ctx context.Context) error {
log.Info(ctx, "stopping")
return nil
}, lu.WithHookName("basic stop hook"))
a.AddProcess(
lu.Process{
Name: "one",
Run: func(ctx context.Context) error {
log.Info(ctx, "one")
<-ctx.Done()
return context.Cause(ctx)
},
},
lu.Process{
Name: "two",
Run: func(ctx context.Context) error {
log.Info(ctx, "two")
<-ctx.Done()
return context.Cause(ctx)
},
},
lu.Process{
Name: "three",
Run: func(ctx context.Context) error {
log.Info(ctx, "three")
<-ctx.Done()
return context.Cause(ctx)
},
},
process.ContextLoop(
func(ctx context.Context) (context.Context, context.CancelFunc, error) { return ctx, func() {}, nil },
func(ctx context.Context) error { return process.ErrBreakContextLoop },
process.WithName("break loop"),
process.WithBreakableLoop()),
)
err := a.Launch(context.Background())
jtest.AssertNil(t, err)
time.Sleep(250 * time.Millisecond)
err = a.Shutdown()
jtest.AssertNil(t, err)
close(ev)
test.AssertEvents(t, ev,
test.Event{Type: lu.AppStartup},
test.Event{Type: lu.PreHookStart, Name: "basic start hook"},
test.Event{Type: lu.PostHookStart, Name: "basic start hook"},
test.AnyOrder(
test.Event{Type: lu.ProcessStart, Name: "one"},
test.Event{Type: lu.ProcessStart, Name: "two"},
test.Event{Type: lu.ProcessStart, Name: "three"},
test.Event{Type: lu.ProcessStart, Name: "break loop"},
),
test.AnyOrder(
test.Event{Type: lu.AppRunning},
test.Event{Type: lu.ProcessEnd, Name: "break loop"},
),
test.Event{Type: lu.AppTerminating},
test.AnyOrder(
test.Event{Type: lu.ProcessEnd, Name: "one"},
test.Event{Type: lu.ProcessEnd, Name: "two"},
test.Event{Type: lu.ProcessEnd, Name: "three"},
),
test.Event{Type: lu.PreHookStop, Name: "basic stop hook"},
test.Event{Type: lu.PostHookStop, Name: "basic stop hook"},
test.Event{Type: lu.AppTerminated},
)
}
func TestShutdownWithParentContext(t *testing.T) {
var a lu.App
a.AddProcess(lu.Process{
Run: func(ctx context.Context) error {
<-ctx.Done()
return context.Cause(ctx)
},
})
ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)
defer cancel()
err := a.Launch(ctx)
jtest.AssertNil(t, err)
require.Eventually(t, func() bool {
select {
case <-a.WaitForShutdown():
return true
default:
return false
}
}, 2*time.Second, 100*time.Millisecond)
err = a.Shutdown()
jtest.Assert(t, context.DeadlineExceeded, err)
}
func TestProcessShutdown(t *testing.T) {
testCases := []struct {
name string
setupApp func(a *lu.App)
expErr error
}{
{name: "empty"},
{
name: "cancellable",
setupApp: func(a *lu.App) {
a.ShutdownTimeout = 100 * time.Millisecond
a.AddProcess(lu.Process{Shutdown: func(ctx context.Context) error {
<-ctx.Done()
return context.Cause(ctx)
}})
},
expErr: context.DeadlineExceeded,
},
{
name: "dependents",
setupApp: func(a *lu.App) {
ch := make(chan struct{})
p1 := lu.Process{Shutdown: func(ctx context.Context) error { <-ch; return nil }}
p2 := lu.Process{Shutdown: func(ctx context.Context) error { close(ch); return nil }}
p3 := lu.Process{Shutdown: func(ctx context.Context) error { <-ch; return nil }}
a.AddProcess(p1, p2, p3)
},
},
{
name: "blocked",
setupApp: func(a *lu.App) {
a.ShutdownTimeout = 100 * time.Millisecond
ch := make(chan struct{})
a.AddProcess(lu.Process{Shutdown: func(ctx context.Context) error { <-ch; return nil }})
},
expErr: context.DeadlineExceeded,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var a lu.App
if tc.setupApp != nil {
tc.setupApp(&a)
}
err := a.Launch(context.Background())
jtest.RequireNil(t, err)
err = a.Shutdown()
jtest.Assert(t, tc.expErr, err)
})
}
}
func TestRunningProcesses(t *testing.T) {
testCases := []struct {
name string
processes []lu.Process
expShutdownError error
expRunning []string
}{
{name: "nil"},
{
name: "blocker",
processes: []lu.Process{
{Name: "blocker", Run: func(ctx context.Context) error {
var c chan struct{}
<-c
return nil
}},
},
expShutdownError: context.DeadlineExceeded,
expRunning: []string{"blocker"},
},
{
name: "non-blocker",
processes: []lu.Process{
{Name: "gogo", Run: func(ctx context.Context) error {
<-ctx.Done()
return nil
}},
},
},
{
name: "one blocker among others",
processes: []lu.Process{
{Name: "gogo", Run: func(ctx context.Context) error {
<-ctx.Done()
return nil
}},
{Name: "blocker", Run: func(ctx context.Context) error {
var c chan struct{}
<-c
return nil
}},
},
expShutdownError: context.DeadlineExceeded,
expRunning: []string{"blocker"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
a := lu.App{ShutdownTimeout: 100 * time.Millisecond}
a.AddProcess(tc.processes...)
jtest.RequireNil(t, a.Launch(context.Background()))
jtest.Assert(t, tc.expShutdownError, a.Shutdown())
require.Equal(t, tc.expRunning, a.RunningProcesses())
})
}
}
func TestPIDRemoved(t *testing.T) {
tests := []struct {
name string
app func(t *testing.T) *lu.App
expExit int
}{
{
name: "not using pid",
app: func(t *testing.T) *lu.App {
var a lu.App
a.AddProcess(process.NoOp())
return &a
},
expExit: 1,
},
{
name: "normal app",
app: func(t *testing.T) *lu.App {
a := lu.App{UseProcessFile: true}
a.AddProcess(process.NoOp())
return &a
},
expExit: 1,
},
{
name: "app fails to start",
app: func(t *testing.T) *lu.App {
a := lu.App{UseProcessFile: true}
a.OnStartUp(func(ctx context.Context) error {
return io.ErrUnexpectedEOF
})
a.AddProcess(process.NoOp())
return &a
},
expExit: 1,
},
{
name: "app fails to shutdown",
app: func(t *testing.T) *lu.App {
a := lu.App{UseProcessFile: true}
a.OnShutdown(func(ctx context.Context) error {
return io.ErrUnexpectedEOF
})
a.AddProcess(process.NoOp())
return &a
},
expExit: 1,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
t.Cleanup(cancel)
lu.SetBackgroundContextForTesting(t, ctx)
exit := tc.app(t).Run()
assert.Equal(t, tc.expExit, exit)
_, err := os.Open("/tmp/lu.pid")
assert.True(t, os.IsNotExist(err))
})
}
}
func TestWaitFor(t *testing.T) {
tests := []struct {
name string
ctx func(t *testing.T) context.Context
ch func(t *testing.T) chan int
exp int
expErr error
}{
{
name: "canceled with io error",
ctx: func(t *testing.T) context.Context {
ctx, cancel := context.WithCancelCause(context.Background())
cancel(io.ErrUnexpectedEOF)
return ctx
},
ch: func(t *testing.T) chan int {
return nil
},
expErr: io.ErrUnexpectedEOF,
},
{
name: "canceled",
ctx: func(t *testing.T) context.Context {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx
},
ch: func(t *testing.T) chan int {
return nil
},
expErr: context.Canceled,
},
{
name: "time out",
ctx: func(t *testing.T) context.Context {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
t.Cleanup(cancel)
return ctx
},
ch: func(t *testing.T) chan int {
return nil
},
expErr: context.DeadlineExceeded,
},
{
name: "success",
ctx: func(t *testing.T) context.Context {
return context.Background()
},
ch: func(t *testing.T) chan int {
ch := make(chan int, 1)
ch <- 1
close(ch)
return ch
},
exp: 1,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
i, err := lu.WaitFor(tc.ctx(t), tc.ch(t))
jtest.Require(t, tc.expErr, err)
assert.Equal(t, tc.exp, i)
})
}
}
func TestNoApp(t *testing.T) {
require.Nil(t, lu.NoApp())
}