generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdms_suite_test.go
80 lines (66 loc) · 1.73 KB
/
dms_suite_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
package main
import (
"time"
"github.com/stretchr/testify/suite"
"github.com/xmidt-org/chronon"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
)
// DMSSuite hosts common infrastructure for dms unit test suites.
type DMSSuite struct {
suite.Suite
now time.Time
logger Logger
}
var _ suite.BeforeTest = (*DMSSuite)(nil)
var _ suite.SetupAllSuite = (*DMSSuite)(nil)
func (suite *DMSSuite) SetupSuite() {
suite.now = time.Now()
}
func (suite *DMSSuite) BeforeTest(suiteName, testName string) {
suite.logger = testLogger{
t: suite.T(),
suiteName: suiteName,
testName: testName,
}
}
func (suite *DMSSuite) provideLogger() fx.Option {
return fx.Provide(
func() Logger {
return suite.logger
},
)
}
func (suite *DMSSuite) clock() *chronon.FakeClock {
return chronon.NewFakeClock(suite.now)
}
// switchConfig returns a SwitchConfig built with this suite's current state.
// A fake clock is used to control any ticker loops started by tests.
func (suite *DMSSuite) switchConfig(ttl time.Duration, maxMisses int, actions ...Action) (SwitchConfig, *chronon.FakeClock) {
clock := suite.clock()
cfg := SwitchConfig{
Logger: suite.logger,
TTL: ttl,
MaxMisses: maxMisses,
Actions: actions,
Clock: clock,
}
return cfg, clock
}
// newSwitch uses NewSwitch to directly construct a Switch.
func (suite *DMSSuite) newSwitch(cfg SwitchConfig) *Switch {
s := NewSwitch(cfg)
suite.Require().NotNil(s)
return s
}
// provideSwitch uses an enclosing fx.App to create a Switch.
func (suite *DMSSuite) provideSwitch(cfg SwitchConfig, populate ...interface{}) *fxtest.App {
app := fxtest.New(
suite.T(),
fx.Supply(cfg),
provideSwitch(),
fx.Populate(populate...),
)
suite.Require().NoError(app.Err())
return app
}