-
Notifications
You must be signed in to change notification settings - Fork 281
/
lifecycle_test.go
369 lines (318 loc) · 9.35 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
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
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package lifecycle
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/fx/fxevent"
"go.uber.org/fx/internal/fxclock"
"go.uber.org/fx/internal/fxlog"
"go.uber.org/fx/internal/fxreflect"
"go.uber.org/fx/internal/testutil"
"go.uber.org/goleak"
"go.uber.org/multierr"
)
func testLogger(t *testing.T) fxevent.Logger {
return fxlog.DefaultLogger(testutil.WriteSyncer{T: t})
}
func TestLifecycleStart(t *testing.T) {
t.Parallel()
t.Run("ExecutesInOrder", func(t *testing.T) {
t.Parallel()
l := New(testLogger(t), fxclock.System)
count := 0
l.Append(Hook{
OnStart: func(context.Context) error {
count++
assert.Equal(t, 1, count, "expected this starter to be executed first")
return nil
},
})
l.Append(Hook{
OnStart: func(context.Context) error {
count++
assert.Equal(t, 2, count, "expected this starter to be executed second")
return nil
},
})
assert.NoError(t, l.Start(context.Background()))
assert.Equal(t, 2, count)
})
t.Run("ErrHaltsChainAndRollsBack", func(t *testing.T) {
t.Parallel()
l := New(testLogger(t), fxclock.System)
err := errors.New("a starter error")
starterCount := 0
stopperCount := 0
// this event's starter succeeded, so no matter what the stopper should run
l.Append(Hook{
OnStart: func(context.Context) error {
starterCount++
return nil
},
OnStop: func(context.Context) error {
stopperCount++
return nil
},
})
// this event's starter fails, so the stopper shouldnt run
l.Append(Hook{
OnStart: func(context.Context) error {
starterCount++
return err
},
OnStop: func(context.Context) error {
t.Error("this stopper shouldnt run, since the starter in this event failed")
return nil
},
})
// this event is last in the chain, so it should never run since the previous failed
l.Append(Hook{
OnStart: func(context.Context) error {
t.Error("this starter should never run, since the previous event failed")
return nil
},
OnStop: func(context.Context) error {
t.Error("this stopper should never run, since the previous event failed")
return nil
},
})
assert.Error(t, err, l.Start(context.Background()))
assert.NoError(t, l.Stop(context.Background()))
assert.Equal(t, 2, starterCount, "expected the first and second starter to execute")
assert.Equal(t, 1, stopperCount, "expected the first stopper to execute since the second starter failed")
})
t.Run("DoNotRunStartHooksWithExpiredCtx", func(t *testing.T) {
t.Parallel()
l := New(testLogger(t), fxclock.System)
l.Append(Hook{
OnStart: func(context.Context) error {
assert.Fail(t, "this hook should not run")
return nil
},
OnStop: func(context.Context) error {
assert.Fail(t, "this hook should not run")
return nil
},
})
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := l.Start(ctx)
require.Error(t, err)
// Note: Stop does not return an error here because no hooks
// have been started, so we don't end up any of the corresponding
// stop hooks.
require.NoError(t, l.Stop(ctx))
})
}
func TestLifecycleStop(t *testing.T) {
t.Parallel()
t.Run("DoesNothingWithoutHooks", func(t *testing.T) {
t.Parallel()
l := New(testLogger(t), fxclock.System)
assert.Nil(t, l.Stop(context.Background()), "no lifecycle hooks should have resulted in stop returning nil")
})
t.Run("DoesNothingWhenNotStarted", func(t *testing.T) {
t.Parallel()
hook := Hook{
OnStop: func(context.Context) error {
assert.Fail(t, "OnStop should not be called if lifecycle was never started")
return nil
},
}
l := New(testLogger(t), fxclock.System)
l.Append(hook)
l.Stop(context.Background())
})
t.Run("ExecutesInReverseOrder", func(t *testing.T) {
t.Parallel()
l := New(testLogger(t), fxclock.System)
count := 2
l.Append(Hook{
OnStop: func(context.Context) error {
count--
assert.Equal(t, 0, count, "this stopper was added first, so should execute last")
return nil
},
})
l.Append(Hook{
OnStop: func(context.Context) error {
count--
assert.Equal(t, 1, count, "this stopper was added last, so should execute first")
return nil
},
})
assert.NoError(t, l.Start(context.Background()))
assert.NoError(t, l.Stop(context.Background()))
assert.Equal(t, 0, count)
})
t.Run("ErrDoesntHaltChain", func(t *testing.T) {
t.Parallel()
l := New(testLogger(t), fxclock.System)
count := 0
l.Append(Hook{
OnStop: func(context.Context) error {
count++
return nil
},
})
err := errors.New("some stop error")
l.Append(Hook{
OnStop: func(context.Context) error {
count++
return err
},
})
assert.NoError(t, l.Start(context.Background()))
assert.Equal(t, err, l.Stop(context.Background()))
assert.Equal(t, 2, count)
})
t.Run("GathersAllErrs", func(t *testing.T) {
t.Parallel()
l := New(testLogger(t), fxclock.System)
err := errors.New("some stop error")
err2 := errors.New("some other stop error")
l.Append(Hook{
OnStop: func(context.Context) error {
return err2
},
})
l.Append(Hook{
OnStop: func(context.Context) error {
return err
},
})
assert.NoError(t, l.Start(context.Background()))
assert.Equal(t, multierr.Combine(err, err2), l.Stop(context.Background()))
})
t.Run("AllowEmptyHooks", func(t *testing.T) {
t.Parallel()
l := New(testLogger(t), fxclock.System)
l.Append(Hook{})
l.Append(Hook{})
assert.NoError(t, l.Start(context.Background()))
assert.NoError(t, l.Stop(context.Background()))
})
t.Run("DoesNothingIfStartFailed", func(t *testing.T) {
t.Parallel()
l := New(testLogger(t), fxclock.System)
err := errors.New("some start error")
l.Append(Hook{
OnStart: func(context.Context) error {
return err
},
OnStop: func(context.Context) error {
assert.Fail(t, "OnStop should not be called if start failed")
return nil
},
})
assert.Equal(t, err, l.Start(context.Background()))
l.Stop(context.Background())
})
t.Run("DoNotRunStopHooksWithExpiredCtx", func(t *testing.T) {
t.Parallel()
l := New(testLogger(t), fxclock.System)
l.Append(Hook{
OnStart: func(context.Context) error {
return nil
},
OnStop: func(context.Context) error {
assert.Fail(t, "this hook should not run")
return nil
},
})
ctx, cancel := context.WithCancel(context.Background())
err := l.Start(ctx)
require.NoError(t, err)
cancel()
require.Error(t, l.Stop(ctx))
})
t.Run("nil ctx", func(t *testing.T) {
t.Parallel()
l := New(testLogger(t), fxclock.System)
l.Append(Hook{
OnStart: func(context.Context) error {
assert.Fail(t, "this hook should not run")
return nil
},
OnStop: func(context.Context) error {
assert.Fail(t, "this hook should not run")
return nil
},
})
//lint:ignore SA1012 this test specifically tests for the lint failure
err := l.Start(nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "called OnStart with nil context")
//lint:ignore SA1012 this test specifically tests for the lint failure
err = l.Stop(nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "called OnStop with nil context")
})
}
func TestHookRecordsFormat(t *testing.T) {
t.Parallel()
t.Run("SortRecords", func(t *testing.T) {
t.Parallel()
t1, err := time.ParseDuration("10ms")
require.NoError(t, err)
t2, err := time.ParseDuration("20ms")
require.NoError(t, err)
f := fxreflect.Frame{
Function: "someFunc",
File: "somefunc.go",
Line: 1,
}
r := HookRecords{
HookRecord{
CallerFrame: f,
Func: func(context.Context) error {
return nil
},
Runtime: t1,
},
HookRecord{
CallerFrame: f,
Func: func(context.Context) error {
return nil
},
Runtime: t2,
},
}
sort.Sort(r)
for _, format := range []string{"%v", "%+v", "%s"} {
s := fmt.Sprintf(format, r)
hook1Idx := strings.Index(s, "TestHookRecordsFormat.func1.1()")
hook2Idx := strings.Index(s, "TestHookRecordsFormat.func1.2()")
assert.Greater(t, hook1Idx, hook2Idx, "second hook must appear first in the formatted string")
assert.Contains(t, s, "somefunc.go:1", "file name and line should be reported")
}
})
}
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}