-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_test.go
199 lines (152 loc) · 4.48 KB
/
container_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
package di
import (
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
type testQualifierA uint8
type testQualifierB uint8
type testServiceBase interface {
Name() string
Event(event string)
}
type testServiceA interface {
testServiceBase
IsA()
}
type testServiceB interface {
testServiceBase
IsB()
}
type testLoggger func(name, event string)
func newTestLogger() (logger testLoggger, logs func() []string) {
var entries []string
logger = func(name, event string) {
entries = append(entries, name+":"+event)
}
logs = func() []string {
return entries
}
return
}
type testServiceBaseImp struct {
name string
logger testLoggger
}
func (s *testServiceBaseImp) Name() string { return s.name }
func (s *testServiceBaseImp) Event(event string) {
if s.logger != nil {
s.logger(s.name, event)
}
}
func (s *testServiceBaseImp) Initialize() {
s.Event("Initialize")
}
func (s *testServiceBaseImp) Destroy() {
s.Event("Destroy")
}
type testServiceAImpl struct{ *testServiceBaseImp }
func (s *testServiceAImpl) IsA() {}
type testServiceBImpl struct{ *testServiceBaseImp }
func (s *testServiceBImpl) IsB() {}
func newTestServiceA(name string, logger testLoggger) testServiceA {
return &testServiceAImpl{&testServiceBaseImp{name: name, logger: logger}}
}
func newTestServiceB(name string, logger testLoggger) testServiceB {
return &testServiceBImpl{&testServiceBaseImp{name: name, logger: logger}}
}
func TestComponentKey(t *testing.T) {
ctn := New(nil)
logger, logs := newTestLogger()
// Component Type = Key = typeof testServiceA
ctn.Register(func() testServiceA {
return newTestServiceA("a", logger)
})
// Component Type = Key = typeof *testServiceBImpl
ctn.Register(func() *testServiceBImpl {
return newTestServiceB("b", logger).(*testServiceBImpl)
})
// a Key = typeof testServiceA, exact match
// b Key = typeof testServiceB, assignable match
// c Key = typeof *testServiceBImpl, exact match
ctn.Register(func(a testServiceA, b testServiceB, c *testServiceBImpl) {
require.True(t, b == c, "b testServiceB != d *testServiceBImpl")
}, Startup(100))
// missing dependencies
// func(di.testServiceA, di.testServiceB, *di.testServiceAImpl, *di.testServiceBImpl) depends on missing dependency
// missing dependencies: *di.testServiceAImpl
require.NoError(t, ctn.Initialize())
require.Equal(t, []string{
"a:Initialize",
"b:Initialize",
}, logs())
}
func TestConstructor(t *testing.T) {
ctn := New(nil)
var (
err error
called bool
result testServiceA
service = newTestServiceA("test", nil)
)
require.NoError(t, ctn.ShouldRegister(func() testServiceA {
require.False(t, called, "constructor must be called exactly once")
called = true
return service
}))
require.NoError(t, ctn.Initialize())
result, err = GetFrom[testServiceA](ctn)
require.NoError(t, err)
require.True(t, called, "constructor must be called first")
require.NotNil(t, result, "invoke got nil service")
require.Equal(t, service, result, "service must match constructor's return value")
}
func TestFullCycle(t *testing.T) {
ctn := New(nil)
logger, logs := newTestLogger()
count := 0
// qualified component
ctn.Register(
func() testServiceA {
count++
return newTestServiceA("srv-"+strconv.Itoa(count), logger)
},
Primary,
Qualify[testQualifierA](),
Initializer(func(s testServiceA) {
s.Event("Initializer()")
}),
Disposer(func(s testServiceA) {
s.Event("Disposer()")
}),
)
// Qualified
require.NoError(t, ctn.ShouldRegister(func(q Qualified[testServiceA, testQualifierA]) {
s := q.Get()
s.Event("Qualified") // expect "srv-1"
}, Startup(100)))
// Provider (same instance)
require.NoError(t, ctn.ShouldRegister(func(q Provider[testServiceA]) {
s, _ := q.Get()
s.Event("Provided") // expect "srv-1"
s, _ = q.Get()
s.Event("Provided") // expect "srv-1"
}, Startup(200)))
// Unmanaged (returns new instance)
require.NoError(t, ctn.ShouldRegister(func(u Unmanaged[testServiceA]) {
sa, da, _ := u.Get()
sa.Event("Unmanaged") // expect "srv-2"
sb, db, _ := u.Get()
sb.Event("Unmanaged") // expect "srv-3"
da.Dispose()
db.Dispose()
}, Startup(300)))
require.NoError(t, ctn.Initialize())
require.Equal(t, []string{
"srv-1:Initialize", "srv-1:Initializer()", "srv-1:Qualified", "srv-1:Provided", "srv-1:Provided",
"srv-2:Initialize", "srv-2:Initializer()", "srv-2:Unmanaged",
"srv-3:Initialize", "srv-3:Initializer()", "srv-3:Unmanaged",
"srv-2:Destroy", "srv-2:Disposer()",
"srv-3:Destroy", "srv-3:Disposer()",
}, logs())
}