forked from uber-go/fx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_test.go
670 lines (590 loc) · 16.3 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// 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 fx_test
import (
"bytes"
"context"
"errors"
"fmt"
"testing"
"time"
. "go.uber.org/fx"
"go.uber.org/fx/fxtest"
"go.uber.org/multierr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type printerSpy struct {
*bytes.Buffer
}
func (ps printerSpy) Printf(format string, args ...interface{}) {
fmt.Fprintf(ps.Buffer, format, args...)
ps.Buffer.WriteRune('\n')
}
func NewForTest(t testing.TB, opts ...Option) *App {
testOpts := []Option{Logger(fxtest.NewTestPrinter(t))}
opts = append(testOpts, opts...)
return New(opts...)
}
func TestNewApp(t *testing.T) {
t.Run("ProvidesLifecycleAndShutdowner", func(t *testing.T) {
var (
l Lifecycle
s Shutdowner
)
fxtest.New(
t,
Populate(&l, &s),
)
assert.NotNil(t, l)
assert.NotNil(t, s)
})
t.Run("OptionsHappensBeforeProvides", func(t *testing.T) {
// Should be grouping all provides and pushing them into the container
// after applying other options. This prevents the app configuration
// (e.g., logging) from changing halfway through our provides.
spy := &printerSpy{&bytes.Buffer{}}
app := fxtest.New(t, Provide(func() struct{} { return struct{}{} }), Logger(spy))
defer app.RequireStart().RequireStop()
assert.Contains(t, spy.String(), "PROVIDE\tstruct {}")
})
t.Run("CircularGraphReturnsError", func(t *testing.T) {
type A struct{}
type B struct{}
app := NewForTest(t,
Provide(func(A) B { return B{} }),
Provide(func(B) A { return A{} }),
Invoke(func(B) {}),
)
err := app.Err()
require.Error(t, err, "fx.New should return an error")
errMsg := err.Error()
assert.Contains(t, errMsg, "cycle detected in dependency graph")
assert.Contains(t, errMsg, "depends on fx_test.A")
assert.Contains(t, errMsg, "depends on fx_test.B")
})
t.Run("ProvidesDotGraph", func(t *testing.T) {
type A struct{}
type B struct{}
type C struct{}
var g DotGraph
app := fxtest.New(t,
Provide(func() A { return A{} }),
Provide(func(A) B { return B{} }),
Provide(func(A, B) C { return C{} }),
Populate(&g),
)
defer app.RequireStart().RequireStop()
require.NoError(t, app.Err())
assert.Contains(t, g, `"fx.DotGraph" [label=<fx.DotGraph>];`)
})
t.Run("ProvidesWithAnnotate", func(t *testing.T) {
type A struct{}
type B struct {
In
Foo A `name:"foo"`
Bar A `name:"bar"`
Foos []A `group:"foo"`
}
app := fxtest.New(t,
Provide(
Annotated{
Target: func() A { return A{} },
Name: "foo",
},
Annotated{
Target: func() A { return A{} },
Name: "bar",
},
Annotated{
Target: func() A { return A{} },
Group: "foo",
},
),
Invoke(
func(b B) {
assert.NotNil(t, b.Foo)
assert.NotNil(t, b.Bar)
assert.Len(t, b.Foos, 1)
},
),
)
defer app.RequireStart().RequireStop()
require.NoError(t, app.Err())
})
t.Run("ProvidesWithEmptyAnnotate", func(t *testing.T) {
type A struct{}
type B struct {
In
Foo A
}
app := fxtest.New(t,
Provide(
Annotated{
Target: func() A { return A{} },
},
),
Invoke(
func(b B) {
assert.NotNil(t, b.Foo)
},
),
)
defer app.RequireStart().RequireStop()
require.NoError(t, app.Err())
})
t.Run("CannotNameAndGroup", func(t *testing.T) {
type A struct{}
app := NewForTest(t,
Provide(
Annotated{
Target: func() A { return A{} },
Name: "foo",
Group: "bar",
},
),
)
err := app.Err()
require.Error(t, err)
})
}
type errHandlerFunc func(error)
func (f errHandlerFunc) HandleError(err error) { f(err) }
func TestInvokes(t *testing.T) {
t.Run("ErrorsAreNotOverriden", func(t *testing.T) {
type A struct{}
type B struct{}
app := NewForTest(t,
Provide(func() B { return B{} }), // B inserted into the graph
Invoke(func(A) {}), // failed A invoke
Invoke(func(B) {}), // successful B invoke
)
err := app.Err()
require.Error(t, err)
assert.Contains(t, err.Error(), "fx_test.A is not in the container")
})
t.Run("ErrorHooksAreCalled", func(t *testing.T) {
type A struct{}
count := 0
h := errHandlerFunc(func(err error) {
count++
})
NewForTest(t,
Invoke(func(A) {}),
ErrorHook(h),
)
assert.Equal(t, 1, count)
})
}
func TestError(t *testing.T) {
t.Run("NilErrorOption", func(t *testing.T) {
var invoked bool
app := NewForTest(t,
Error(nil),
Invoke(func() { invoked = true }),
)
err := app.Err()
require.NoError(t, err)
assert.True(t, invoked)
})
t.Run("SingleErrorOption", func(t *testing.T) {
app := NewForTest(t,
Error(errors.New("module failure")),
Invoke(func() { t.Errorf("Invoke should not be called") }),
)
err := app.Err()
assert.EqualError(t, err, "module failure")
})
t.Run("MultipleErrorOption", func(t *testing.T) {
type A struct{}
app := NewForTest(t,
Provide(func() A {
t.Errorf("Provide should not be called")
return A{}
},
),
Invoke(func(A) { t.Errorf("Invoke should not be called") }),
Error(
errors.New("module A failure"),
errors.New("module B failure"),
),
)
err := app.Err()
require.Error(t, err)
assert.Contains(t, err.Error(), "module A failure")
assert.Contains(t, err.Error(), "module B failure")
assert.NotContains(t, err.Error(), "not in the container")
})
t.Run("ProvideAndInvokeErrorsAreIgnored", func(t *testing.T) {
type A struct{}
type B struct{}
app := NewForTest(t,
Provide(func(b B) A {
t.Errorf("B is missing from the container; Provide should not be called")
return A{}
},
),
Error(errors.New("module failure")),
Invoke(func(A) { t.Errorf("A was not provided; Invoke should not be called") }),
)
err := app.Err()
assert.EqualError(t, err, "module failure")
})
}
func TestOptions(t *testing.T) {
t.Run("OptionsComposition", func(t *testing.T) {
var n int
construct := func() struct{} {
n++
return struct{}{}
}
use := func(struct{}) {
n++
}
app := fxtest.New(t, Options(Provide(construct), Invoke(use)))
defer app.RequireStart().RequireStop()
assert.Equal(t, 2, n)
})
t.Run("ProvidesCalledInGraphOrder", func(t *testing.T) {
type type1 struct{}
type type2 struct{}
type type3 struct{}
initOrder := 0
new1 := func() type1 {
initOrder++
assert.Equal(t, 1, initOrder)
return type1{}
}
new2 := func(type1) type2 {
initOrder++
assert.Equal(t, 2, initOrder)
return type2{}
}
new3 := func(type1, type2) type3 {
initOrder++
assert.Equal(t, 3, initOrder)
return type3{}
}
biz := func(s1 type1, s2 type2, s3 type3) {
initOrder++
assert.Equal(t, 4, initOrder)
}
app := fxtest.New(t,
Provide(new1, new2, new3),
Invoke(biz),
)
defer app.RequireStart().RequireStop()
assert.Equal(t, 4, initOrder)
})
t.Run("ProvidesCalledLazily", func(t *testing.T) {
count := 0
newBuffer := func() *bytes.Buffer {
t.Error("this module should not init: no provided type relies on it")
return nil
}
newEmpty := func() struct{} {
count++
return struct{}{}
}
app := fxtest.New(t,
Provide(newBuffer, newEmpty),
Invoke(func(struct{}) { count++ }),
)
defer app.RequireStart().RequireStop()
assert.Equal(t, 2, count)
})
t.Run("Error", func(t *testing.T) {
spy := printerSpy{&bytes.Buffer{}}
New(
Provide(&bytes.Buffer{}), // error, not a constructor
Logger(spy),
)
assert.Contains(t, spy.String(), "must provide constructor function")
})
}
func TestTimeoutOptions(t *testing.T) {
const timeout = time.Minute
// Further assertions can't succeed unless the test timeout is greater than the default.
require.True(t, timeout > DefaultTimeout, "test assertions require timeout greater than default")
var started, stopped bool
assertCustomContext := func(ctx context.Context, phase string) {
deadline, ok := ctx.Deadline()
if assert.True(t, ok, "no %s deadline", phase) {
remaining := time.Until(deadline)
assert.True(t, remaining > DefaultTimeout, "didn't respect customized %s timeout", phase)
}
}
verify := func(lc Lifecycle) {
lc.Append(Hook{
OnStart: func(ctx context.Context) error {
assertCustomContext(ctx, "start")
started = true
return nil
},
OnStop: func(ctx context.Context) error {
assertCustomContext(ctx, "stop")
stopped = true
return nil
},
})
}
app := fxtest.New(
t,
Invoke(verify),
StartTimeout(timeout),
StopTimeout(timeout),
)
app.RequireStart().RequireStop()
assert.True(t, started, "app wasn't started")
assert.True(t, stopped, "app wasn't stopped")
}
func TestAppStart(t *testing.T) {
t.Run("Timeout", func(t *testing.T) {
type A struct{}
blocker := func(lc Lifecycle) *A {
lc.Append(
Hook{
OnStart: func(context.Context) error {
select {}
},
},
)
return &A{}
}
app := fxtest.New(
t,
Provide(blocker),
Invoke(func(*A) {}),
)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
err := app.Start(ctx)
require.Error(t, err)
assert.Contains(t, err.Error(), "context deadline exceeded")
})
t.Run("StartError", func(t *testing.T) {
failStart := func(lc Lifecycle) struct{} {
lc.Append(Hook{OnStart: func(context.Context) error {
return errors.New("OnStart fail")
}})
return struct{}{}
}
app := fxtest.New(t,
Provide(failStart),
Invoke(func(struct{}) {}),
)
err := app.Start(context.Background())
require.Error(t, err)
assert.Contains(t, err.Error(), "OnStart fail")
})
t.Run("StartAndStopErrors", func(t *testing.T) {
errStop1 := errors.New("OnStop fail 1")
errStart2 := errors.New("OnStart fail 2")
fail := func(lc Lifecycle) struct{} {
lc.Append(Hook{
OnStart: func(context.Context) error { return nil },
OnStop: func(context.Context) error { return errStop1 },
})
lc.Append(Hook{
OnStart: func(context.Context) error { return errStart2 },
OnStop: func(context.Context) error { assert.Fail(t, "should be never called"); return nil },
})
return struct{}{}
}
app := NewForTest(t,
Provide(fail),
Invoke(func(struct{}) {}),
)
err := app.Start(context.Background())
require.Error(t, err)
assert.Equal(t, []error{errStart2, errStop1}, multierr.Errors(err))
})
t.Run("InvokeNonFunction", func(t *testing.T) {
app := NewForTest(t, Invoke(struct{}{}))
err := app.Err()
require.Error(t, err, "expected start failure")
assert.Contains(t, err.Error(), "can't invoke non-function")
})
t.Run("ProvidingAProvideShouldFail", func(t *testing.T) {
type type1 struct{}
type type2 struct{}
type type3 struct{}
app := NewForTest(t,
Provide(
func() type1 { return type1{} },
Provide(
func() type2 { return type2{} },
func() type3 { return type3{} },
),
),
)
err := app.Err()
require.Error(t, err, "expected start failure")
assert.Contains(t, err.Error(), "fx.Option should be passed to fx.New directly, not to fx.Provide")
assert.Contains(t, err.Error(), "fx.Provide received fx.Provide(go.uber.org/fx_test.TestAppStart")
})
t.Run("InvokingAnInvokeShouldFail", func(t *testing.T) {
type type1 struct{}
app := NewForTest(t,
Provide(func() type1 { return type1{} }),
Invoke(Invoke(func(type1) {
})),
)
newErr := app.Err()
require.Error(t, newErr)
err := app.Start(context.Background())
require.Error(t, err, "expected start failure")
assert.Equal(t, err, newErr, "start should return the same error fx.New encountered")
assert.Contains(t, err.Error(), "fx.Option should be passed to fx.New directly, not to fx.Invoke")
assert.Contains(t, err.Error(), "fx.Invoke received fx.Invoke(go.uber.org/fx_test.TestAppStart")
})
t.Run("ProvidingOptionsShouldFail", func(t *testing.T) {
type type1 struct{}
type type2 struct{}
type type3 struct{}
module := Options(
Provide(
func() type1 { return type1{} },
func() type2 { return type2{} },
),
Invoke(func(type1) {
require.FailNow(t, "module Invoke must not be called")
}),
)
app := NewForTest(t,
Provide(
func() type3 { return type3{} },
module,
),
)
err := app.Err()
require.Error(t, err, "expected start failure")
assert.Contains(t, err.Error(), "fx.Option should be passed to fx.New directly, not to fx.Provide")
assert.Contains(t, err.Error(), "fx.Provide received fx.Options(fx.Provide(go.uber.org/fx_test.TestAppStart")
})
}
func TestAppStop(t *testing.T) {
t.Run("Timeout", func(t *testing.T) {
block := func(context.Context) error { select {} }
app := fxtest.New(t, Invoke(func(l Lifecycle) {
l.Append(Hook{OnStop: block})
}))
app.RequireStart()
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
err := app.Stop(ctx)
require.Error(t, err)
assert.Contains(t, err.Error(), "context deadline exceeded")
})
t.Run("StopError", func(t *testing.T) {
failStop := func(lc Lifecycle) struct{} {
lc.Append(Hook{OnStop: func(context.Context) error {
return errors.New("OnStop fail")
}})
return struct{}{}
}
app := fxtest.New(t,
Provide(failStop),
Invoke(func(struct{}) {}),
)
app.RequireStart()
err := app.Stop(context.Background())
require.Error(t, err)
assert.Contains(t, err.Error(), "OnStop fail")
})
}
func TestDone(t *testing.T) {
done := fxtest.New(t).Done()
require.NotNil(t, done, "Got a nil channel.")
select {
case sig := <-done:
t.Fatalf("Got unexpected signal %v from application's Done channel.", sig)
default:
}
}
func TestReplaceLogger(t *testing.T) {
spy := printerSpy{&bytes.Buffer{}}
app := fxtest.New(t, Logger(spy))
app.RequireStart().RequireStop()
assert.Contains(t, spy.String(), "RUNNING")
}
func TestNopLogger(t *testing.T) {
app := fxtest.New(t, NopLogger)
app.RequireStart().RequireStop()
}
type testErrorWithGraph struct {
graph string
}
func (we testErrorWithGraph) Graph() DotGraph {
return DotGraph(we.graph)
}
func (we testErrorWithGraph) Error() string {
return "great sadness"
}
func TestVisualizeError(t *testing.T) {
t.Run("NotWrappedError", func(t *testing.T) {
_, err := VisualizeError(errors.New("great sadness"))
require.Error(t, err)
})
t.Run("WrappedErrorWithEmptyGraph", func(t *testing.T) {
graph, err := VisualizeError(testErrorWithGraph{graph: ""})
assert.Empty(t, graph)
require.Error(t, err)
})
t.Run("WrappedError", func(t *testing.T) {
graph, err := VisualizeError(testErrorWithGraph{graph: "graph"})
assert.Equal(t, "graph", graph)
require.NoError(t, err)
})
}
func TestErrorHook(t *testing.T) {
t.Run("UnvisualizableError", func(t *testing.T) {
type A struct{}
var graphErr error
h := errHandlerFunc(func(err error) {
_, graphErr = VisualizeError(err)
})
NewForTest(t,
Provide(func() A { return A{} }),
Invoke(func(A) error { return errors.New("great sadness") }),
ErrorHook(h),
)
assert.Equal(t, errors.New("unable to visualize error"), graphErr)
})
t.Run("GraphWithError", func(t *testing.T) {
type A struct{}
type B struct{}
type C struct{}
var errStr, graphStr string
h := errHandlerFunc(func(err error) {
errStr = err.Error()
graphStr, _ = VisualizeError(err)
})
NewForTest(t,
Provide(func() (B, error) { return B{}, fmt.Errorf("great sadness") }),
Provide(func(B) A { return A{} }),
Invoke(func(A) {}),
ErrorHook(&h),
)
assert.Contains(t, errStr, "great sadness")
assert.Contains(t, graphStr, `"fx_test.B" [color=red];`)
assert.Contains(t, graphStr, `"fx_test.A" [color=orange];`)
})
}