-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_test.go
314 lines (308 loc) · 8.76 KB
/
service_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
package npoint_test
import (
"fmt"
"net/http"
"strings"
"testing"
"github.com/muir/nject"
"github.com/muir/npoint"
"github.com/stretchr/testify/assert"
)
func TestStaticInitializerWaitsForStart(t *testing.T) {
t.Parallel()
var debugOutput string
var svcInitCount int
var svcInvokeCount int
serviceSequenceInitFunc := func(db *nject.Debugging) string {
t.Logf("service init chain")
debugOutput = strings.Join(db.Included, "\n")
svcInitCount++
return "foo"
}
serviceSequenceNonCacheable := func(s string, r *http.Request, db *nject.Debugging) {
t.Logf("service invoke chain")
debugOutput = strings.Join(db.Included, "\n")
svcInvokeCount++
}
var initCount int
var invokeCount int
endpointSequenceInitFunc := func(db *nject.Debugging) int {
t.Logf("endpoint init chain")
initCount++
debugOutput = strings.Join(db.Included, "\n")
return initCount
}
endpointSequenceFinalFunc := func(w http.ResponseWriter, i int, db *nject.Debugging) {
t.Logf("endpoint invoke chain")
w.WriteHeader(204)
debugOutput = strings.Join(db.Included, "\n")
invokeCount++
}
multiStartups(
t,
"test",
nject.Sequence("SERVICE", nject.Cacheable(serviceSequenceInitFunc), nject.Cacheable(serviceSequenceNonCacheable)),
nject.Sequence("ENDPOINT", nject.Cacheable(endpointSequenceInitFunc), nject.Cacheable(endpointSequenceFinalFunc)),
func(s string) {
// reset
t.Logf("reset for %s", s)
debugOutput = ""
initCount = 0
invokeCount = 0
svcInitCount = 10
svcInvokeCount = 10
},
func(s string) {
// after register
assert.Equal(t, 0, initCount, s+" after register endpoint init count\n"+debugOutput)
assert.Equal(t, 0, invokeCount, s+" after register endpoint invoke count\n"+debugOutput)
assert.Equal(t, 10, svcInitCount, s+" after register service init count\n"+debugOutput)
assert.Equal(t, 10, svcInvokeCount, s+" after register service invoke count\n"+debugOutput)
},
func(s string) {
// after start
assert.Equal(t, 1, initCount, s+" after start endpoint init count\n"+debugOutput)
assert.Equal(t, 0, invokeCount, s+" after start endpoint invoke count\n"+debugOutput)
assert.Equal(t, 11, svcInitCount, s+" after start service init count\n"+debugOutput)
assert.Equal(t, 10, svcInvokeCount, s+" after start service invoke count\n"+debugOutput)
},
func(s string) {
// after 1st call
assert.Equal(t, 1, initCount, s+" 1st call start init count\n"+debugOutput)
assert.Equal(t, 1, invokeCount, s+" 1st call start invoke count\n"+debugOutput)
assert.Equal(t, 11, svcInitCount, s+" 1st call service init count\n"+debugOutput)
assert.Equal(t, 11, svcInvokeCount, s+" 1st call service invoke count\n"+debugOutput)
},
func(s string) {
// after 2nd call
assert.Equal(t, 1, initCount, s+" 2nd call endpoint init count\n"+debugOutput)
assert.Equal(t, 2, invokeCount, s+" 2nd call endpoint invoke count\n"+debugOutput)
assert.Equal(t, 11, svcInitCount, s+" 2nd call service init count\n"+debugOutput)
assert.Equal(t, 12, svcInvokeCount, s+" 2nd call service invoke count\n"+debugOutput)
},
)
}
func TestFallibleInjectorFailing(t *testing.T) {
t.Parallel()
var initCount int
var invokeCount int
var errorsCount int
multiStartups(
t,
"test",
nil,
nject.Sequence("hc",
func(inner func() error, w http.ResponseWriter) {
t.Logf("wraper (before)")
err := inner()
t.Logf("wraper (after, err=%v)", err)
if err != nil {
assert.Equal(t, "bailing out", err.Error())
errorsCount++
w.WriteHeader(204)
}
},
func() (nject.TerminalError, int) {
t.Logf("endpoint init")
initCount++
return fmt.Errorf("bailing out"), initCount
},
func(w http.ResponseWriter, i int) error {
t.Logf("endpoint invoke")
w.WriteHeader(204)
invokeCount++
return nil
},
),
func(s string) {
// reset
t.Logf("reset for %s", s)
initCount = 0
invokeCount = 0
errorsCount = 0
},
func(s string) {
// after register
assert.Equal(t, 0, initCount, s+" after register endpoint init count")
assert.Equal(t, 0, invokeCount, s+" after register endpoint invoke count")
assert.Equal(t, 0, errorsCount, s+" after register endpoint invoke count")
},
func(s string) {
// after start
assert.Equal(t, 0, initCount, s+" after start endpoint init count")
assert.Equal(t, 0, invokeCount, s+" after start endpoint invoke count")
assert.Equal(t, 0, errorsCount, s+" after register endpoint invoke count")
},
func(s string) {
// after 1st call
assert.Equal(t, 1, initCount, s+" 1st call start init count")
assert.Equal(t, 0, invokeCount, s+" 1st call start invoke count")
assert.Equal(t, 1, errorsCount, s+" after register endpoint invoke count")
},
func(s string) {
// after 2nd call
assert.Equal(t, 2, initCount, s+" 2nd call endpoint init count")
assert.Equal(t, 0, invokeCount, s+" 2nd call endpoint invoke count")
assert.Equal(t, 2, errorsCount, s+" after register endpoint invoke count")
},
)
}
func TestFallibleInjectorNotFailing(t *testing.T) {
t.Parallel()
var initCount int
var invokeCount int
var errorsCount int
multiStartups(
t,
"testFallibleInjectorNotFailing",
nil,
nject.Sequence("hc",
func(inner func() error) {
t.Logf("wraper (before)")
err := inner()
t.Logf("wraper (after, err=%v)", err)
if err != nil {
errorsCount++
}
},
func() (nject.TerminalError, int) {
t.Logf("endpoint init")
initCount++
return nil, 17
},
func(w http.ResponseWriter, i int) error {
assert.Equal(t, 17, i)
t.Logf("endpoint invoke")
w.WriteHeader(204)
invokeCount++
return nil
},
),
func(s string) {
// reset
t.Logf("reset for %s", s)
initCount = 0
invokeCount = 0
errorsCount = 0
},
func(s string) {
// after register
assert.Equal(t, 0, initCount, s+" after register endpoint init count")
assert.Equal(t, 0, invokeCount, s+" after register endpoint invoke count")
assert.Equal(t, 0, errorsCount, s+" after register endpoint invoke count")
},
func(s string) {
// after start
assert.Equal(t, 0, initCount, s+" after start endpoint init count")
assert.Equal(t, 0, invokeCount, s+" after start endpoint invoke count")
assert.Equal(t, 0, errorsCount, s+" after register endpoint invoke count")
},
func(s string) {
// after 1st call
assert.Equal(t, 1, initCount, s+" 1st call start init count")
assert.Equal(t, 1, invokeCount, s+" 1st call start invoke count")
assert.Equal(t, 0, errorsCount, s+" after register endpoint invoke count")
},
func(s string) {
// after 2nd call
assert.Equal(t, 2, initCount, s+" 2nd call endpoint init count")
assert.Equal(t, 2, invokeCount, s+" 2nd call endpoint invoke count")
assert.Equal(t, 0, errorsCount, s+" after register endpoint invoke count")
},
)
}
func multiStartups(
t *testing.T,
name string,
shc *nject.Collection,
hc *nject.Collection,
reset func(string),
afterRegister func(string), // not called for CreateEnpoint
afterStart func(string),
afterCall1 func(string),
afterCall2 func(string),
) {
{
n := name + "-1PreregisterNoMuxRegisterEndpointBeforeStart"
reset(n)
ept := "/" + n
s := npoint.PreregisterService(n, shc)
s.RegisterEndpoint(ept, hc)
afterRegister(n)
b := NewBinder()
s.Start(b.Bind)
afterStart(n)
// nolint:bodyclose
b.Call(ept, "GET", "", nil)
afterCall1(n)
// nolint:bodyclose
b.Call(ept, "GET", "", nil)
afterCall2(n)
}
{
n := name + "-2PreregisterNoMuxRegisterEndpointAfterStartWithOriginalService"
reset(n)
ept := "/" + n
s := npoint.PreregisterService(n, shc)
b := NewBinder()
s.Start(b.Bind)
s.RegisterEndpoint(ept, hc)
// afterRegister(n)
afterStart(n)
// nolint:bodyclose
b.Call(ept, "GET", "", nil)
afterCall1(n)
// nolint:bodyclose
b.Call(ept, "GET", "", nil)
afterCall2(n)
}
{
n := name + "-3PreregisterNoMuxRegisterEndpointAfterStartWithStartedService"
reset(n)
ept := "/" + n
s := npoint.PreregisterService(n, shc)
b := NewBinder()
sr := s.Start(b.Bind)
sr.RegisterEndpoint(ept, hc)
// afterRegister(n)
afterStart(n)
// nolint:bodyclose
b.Call(ept, "GET", "", nil)
afterCall1(n)
// nolint:bodyclose
b.Call(ept, "GET", "", nil)
afterCall2(n)
}
{
n := name + "-7CreateEndpoint"
reset(n)
ept := "/" + n
b := NewBinder()
e := npoint.CreateEndpoint(shc, hc)
b.Bind(ept, e)
// afterRegister(n)
afterStart(n)
// nolint:bodyclose
b.Call(ept, "GET", "", nil)
afterCall1(n)
// nolint:bodyclose
b.Call(ept, "GET", "", nil)
afterCall2(n)
}
{
n := name + "-8RegisterServiceNoMux"
reset(n)
ept := "/" + n
b := NewBinder()
s := npoint.RegisterService(n, b.Bind, shc)
s.RegisterEndpoint(ept, hc)
// afterRegister(n)
afterStart(n)
// nolint:bodyclose
b.Call(ept, "GET", "", nil)
afterCall1(n)
// nolint:bodyclose
b.Call(ept, "GET", "", nil)
afterCall2(n)
}
}