-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinject_test.go
248 lines (192 loc) · 5.03 KB
/
inject_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
package inject
import "testing"
import "errors"
import "github.com/jrivets/gorivets"
var initC []interface{} = make([]interface{}, 0)
var deinit []interface{} = make([]interface{}, 0)
type c1 struct {
C3Ref *c3 `inject:""`
err bool
panic bool
phase int
}
type c2 struct {
C1Ref *c1 `inject:""`
panic bool
postConstruct bool
}
type c3 struct {
C2Ref *c2 `inject:"c2"`
err bool
panic bool
}
func (c *c1) DiPhase() int {
return c.phase
}
func (c *c1) DiInit() error {
if c.err {
return errors.New("c1 error")
}
if c.panic {
panic("c1 panic")
}
initC = append(initC, c)
return nil
}
func (c *c1) DiShutdown() {
deinit = append(deinit, c)
}
func (c *c2) DiPostConstruct() {
c.postConstruct = true
if c.panic {
panic("c2 panics")
}
}
func (*c3) DiPhase() int {
return -1
}
func (*c3) String() string {
return "c3"
}
func (c *c3) DiInit() error {
if c.err {
return errors.New("c3 error")
}
if c.panic {
panic("c3 panic")
}
initC = append(initC, c)
return nil
}
func (c *c3) DiShutdown() {
deinit = append(deinit, c)
}
func TestInjectStraight(t *testing.T) {
inj := NewInjector(gorivets.NewStubLogger("inj"), gorivets.NewStubLogger("inj.fb"))
c1Inst := &c1{}
c2Inst := &c2{}
c3Inst := &c3{}
inj.Register(&Component{Component: c1Inst})
inj.Register(&Component{Component: c2Inst, Name: "c2"}, &Component{Component: c3Inst})
initC = make([]interface{}, 0)
deinit = make([]interface{}, 0)
inj.Construct()
if c1Inst.C3Ref != c3Inst {
t.Fatal("c1 doesn't refer to c3")
}
if c2Inst.C1Ref != c1Inst {
t.Fatal("c2 doesn't refer to c1")
}
if c3Inst.C2Ref != c2Inst {
t.Fatal("c3 doesn't refer to c2")
}
if len(initC) != 2 || initC[0] != c3Inst || initC[1] != c1Inst {
t.Fatal("2 inits should happen")
}
if len(deinit) != 0 {
t.Fatal("0 deinits should happen")
}
if !c2Inst.postConstruct {
t.Fatal("c2.DiPostConstruct() is not called")
}
inj.Shutdown()
if len(deinit) != 2 || deinit[0] != c1Inst || deinit[1] != c3Inst {
t.Fatal("2 de-inits should happen")
}
}
func TestInjectNew(t *testing.T) {
inj := NewInjector(gorivets.NewStubLogger("inj"), gorivets.NewStubLogger("inj.fb"))
c1Inst := &c1{}
c2Inst := &c2{}
c22Inst := &c2{}
inj.Register(&Component{Component: c1Inst, Name: "aaaa"})
inj.Register(&Component{Component: c2Inst, Name: "bbbb"})
inj.Register(&Component{Component: c22Inst, Name: "c2"})
initC = make([]interface{}, 0)
deinit = make([]interface{}, 0)
inj.Construct()
if c1Inst.C3Ref == nil {
t.Fatal("c1 is not initialized")
}
if c2Inst.C1Ref == nil {
t.Fatal("c2 is not initialized")
}
if c1Inst.C3Ref.C2Ref != c22Inst {
t.Fatal("c3 doesn't refer to c2")
}
// here is the trick we registered c1Inst, but c2Inst expects injection with different (default)
// name, which is not registered, so it will be created by default
if len(initC) != 3 || initC[0] != c1Inst.C3Ref || (initC[1] != c1Inst && initC[2] != c1Inst) {
t.Fatal("2 inits should happen!")
}
inj.Shutdown()
if len(deinit) != 3 || deinit[2] != c1Inst.C3Ref {
t.Fatal("3 de-inits should happen")
}
}
func TestRollbackInit(t *testing.T) {
inj := NewInjector(gorivets.NewStubLogger("inj"), gorivets.NewStubLogger("inj.fb"))
c1Inst := &c1{err: true}
c2Inst := &c2{}
c3Inst := &c3{}
inj.Register(&Component{Component: c1Inst}, &Component{Component: c2Inst, Name: "c2"}, &Component{Component: c3Inst})
initC = make([]interface{}, 0)
deinit = make([]interface{}, 0)
err := gorivets.CheckPanic(func() {
inj.Construct()
})
if err == nil {
t.Fatal("Panic is expected")
}
if len(initC) != 1 || initC[0] != c3Inst {
t.Fatal("1 inits should happen!")
}
if len(deinit) != 1 || deinit[0] != c3Inst {
t.Fatal("1 de-inits should happen")
}
}
func TestRollbackInit2(t *testing.T) {
inj := NewInjector(gorivets.NewStubLogger("inj"), gorivets.NewStubLogger("inj.fb"))
c1Inst := &c1{phase: -2}
c2Inst := &c2{}
c3Inst := &c3{panic: true}
inj.Register(&Component{Component: c1Inst}, &Component{Component: c2Inst, Name: "c2"}, &Component{Component: c3Inst})
initC = make([]interface{}, 0)
deinit = make([]interface{}, 0)
err := gorivets.CheckPanic(func() {
inj.Construct()
})
if err == nil {
t.Fatal("Panic is expected")
}
if len(initC) != 1 || initC[0] != c1Inst {
t.Fatal("1 inits should happen!")
}
if len(deinit) != 1 || deinit[0] != c1Inst {
t.Fatal("1 de-inits should happen")
}
}
func TestPostConstructPanics(t *testing.T) {
inj := NewInjector(gorivets.NewStubLogger("inj"), gorivets.NewStubLogger("inj.fb"))
c1Inst := &c1{phase: -2}
c2Inst := &c2{panic: true}
c3Inst := &c3{}
inj.Register(&Component{Component: c1Inst}, &Component{Component: c2Inst, Name: "c2"}, &Component{Component: c3Inst})
initC = make([]interface{}, 0)
deinit = make([]interface{}, 0)
err := gorivets.CheckPanic(func() {
inj.Construct()
})
if err == nil {
t.Fatal("Panic is expected")
}
if len(initC) != 0 {
t.Fatal("0 inits should happen!")
}
if len(deinit) != 0 {
t.Fatal("0 de-inits should happen")
}
if !c2Inst.postConstruct {
t.Fatal("c2.DiPostConstruct() is not called")
}
}