-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbridge_test.go
420 lines (353 loc) · 8.34 KB
/
bridge_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
package dingo
import (
"fmt"
"sync"
"github.com/stretchr/testify/suite"
)
type bridgeTestSuite struct {
suite.Suite
name string
bg bridge
trans *fnMgr
events []*Event
eventMux *mux
}
func (ts *bridgeTestSuite) SetupSuite() {
ts.eventMux = newMux()
ts.trans = newFnMgr("")
}
func (ts *bridgeTestSuite) TearDownSuite() {
}
func (ts *bridgeTestSuite) SetupTest() {
// prepare Bridge
ts.bg = newBridge(ts.name, ts.trans)
// bind event channel
es, err := ts.bg.Events()
ts.Nil(err)
for _, v := range es {
_, err := ts.eventMux.Register(v, 0)
ts.Nil(err)
}
// reset event array
ts.events = make([]*Event, 0, 10)
ts.eventMux.Handle(func(val interface{}, _ int) {
ts.events = append(ts.events, val.(*Event))
})
}
func (ts *bridgeTestSuite) TearDownTest() {
ts.Nil(ts.bg.Close())
for _, v := range ts.events {
if v.Level == EventLvl.Error {
ts.Nil(v)
}
}
ts.eventMux.Close()
}
func (ts *bridgeTestSuite) send(reports chan<- *Report, task *Task, s int16) {
r, err := task.composeReport(s, nil, nil)
ts.Nil(err)
reports <- r
}
func (ts *bridgeTestSuite) chk(expected *Task, got *Report, s int16) {
ts.Equal(expected.ID(), got.ID())
ts.Equal(expected.Name(), got.Name())
ts.Equal(s, got.Status())
}
func (ts *bridgeTestSuite) gen(reports chan<- *Report, task *Task, wait *sync.WaitGroup) {
defer wait.Done()
ts.Nil(ts.bg.(exHooks).ReporterHook(ReporterEvent.BeforeReport, task))
ts.send(reports, task, Status.Sent)
ts.send(reports, task, Status.Progress)
ts.send(reports, task, Status.Success)
}
func (ts *bridgeTestSuite) chks(task *Task, wait *sync.WaitGroup) {
defer wait.Done()
r, err := ts.bg.Poll(task)
ts.Nil(err)
ts.chk(task, <-r, Status.Sent)
ts.chk(task, <-r, Status.Progress)
ts.chk(task, <-r, Status.Success)
}
//
// test cases
//
func (ts *bridgeTestSuite) TestSendTask() {
ts.trans.Register(
"SendTask",
func() {},
)
// add listener
receipts := make(chan *TaskReceipt, 0)
tasks, err := ts.bg.AddListener(receipts)
ts.Nil(err)
// compose a task
t, err := ts.trans.ComposeTask("SendTask", nil, nil)
ts.Nil(err)
// send that task
ts.Nil(ts.bg.SendTask(t))
// make sure task is received through listeners
tReceived := <-tasks
ts.True(t.almostEqual(tReceived))
// send a receipt withoug blocked,
// which means someone is waiting
receipts <- &TaskReceipt{
ID: t.ID(),
Status: ReceiptStatus.OK,
}
}
func (ts *bridgeTestSuite) TestAddListener() {
ts.trans.Register(
"AddListener",
func() {},
)
// prepare listeners
r1 := make(chan *TaskReceipt, 0)
t1, err := ts.bg.AddListener(r1)
ts.Nil(err)
r2 := make(chan *TaskReceipt, 0)
t2, err := ts.bg.AddListener(r2)
ts.Nil(err)
r3 := make(chan *TaskReceipt, 0)
t3, err := ts.bg.AddListener(r3)
ts.Nil(err)
// compose a task, and send it
t, err := ts.trans.ComposeTask("AddListener", nil, nil)
ts.Nil(err)
ts.Nil(ts.bg.SendTask(t))
// get that task from one channel
pass := false
var (
tReceived *Task
receipt = &TaskReceipt{
ID: t.ID(),
Status: ReceiptStatus.OK,
}
)
select {
case tReceived = <-t1:
r1 <- receipt
pass = true
case tReceived = <-t2:
r2 <- receipt
pass = true
case tReceived = <-t3:
r3 <- receipt
pass = true
}
ts.True(pass)
ts.True(t.almostEqual(tReceived))
// close all listeners
ts.Nil(ts.bg.StopAllListeners())
// task channels are closed
_, ok := <-t1
ts.False(ok)
_, ok = <-t2
ts.False(ok)
_, ok = <-t3
ts.False(ok)
}
func (ts *bridgeTestSuite) TestMultipleClose() {
ts.Nil(ts.bg.Close())
ts.Nil(ts.bg.Close())
}
func (ts *bridgeTestSuite) TestReport() {
ts.Nil(ts.trans.Register(
"Report",
func() {},
))
// attach reporter channel
reports := make(chan *Report, 10)
ts.Nil(ts.bg.Report("Report", reports))
// a sample task
t, err := ts.trans.ComposeTask("Report", nil, nil)
ts.Nil(err)
outputs, err := ts.bg.Poll(t)
ts.Nil(err)
// normal report
{
input, err := t.composeReport(Status.Sent, nil, nil)
ts.Nil(err)
reports <- input
r, ok := <-outputs
ts.True(ok)
ts.Equal(input.ID(), r.ID())
}
// a report with 'Done' == true
// the output channel should be closed
{
input, err := t.composeReport(Status.Success, nil, nil)
ts.Nil(err)
reports <- input
r, ok := <-outputs
ts.True(ok)
ts.Equal(input.ID(), r.ID())
_, ok = <-outputs
ts.False(ok)
}
}
func (ts *bridgeTestSuite) TestPoll() {
ts.Nil(ts.trans.Register(
"Poll",
func() {},
))
count := 1
// multiple reports channel
rs := []chan *Report{}
for i := 0; i < count; i++ {
rs = append(rs, make(chan *Report, 10))
ts.Nil(ts.bg.Report("Poll", rs[len(rs)-1]))
}
// multiple tasks
// - t1, t2 -> r1
// - t3, t4 -> r2
// - t5 -> r3
tasks := []*Task{}
for i := 0; i < count; i++ {
t, err := ts.trans.ComposeTask("Poll", nil, []interface{}{fmt.Sprintf("t%d", 2*i)})
ts.Nil(err)
tasks = append(tasks, t)
t, err = ts.trans.ComposeTask("Poll", nil, []interface{}{fmt.Sprintf("t%d", 2*i+1)})
ts.Nil(err)
tasks = append(tasks, t)
}
// send reports through those channels,
// reports belongs to one task should be sent
// to the same reporter
for k, t := range tasks {
r, err := t.composeReport(Status.Sent, nil, nil)
ts.Nil(err)
rs[k/2] <- r
r, err = t.composeReport(Status.Progress, nil, nil)
ts.Nil(err)
rs[k/2] <- r
r, err = t.composeReport(Status.Success, nil, nil)
ts.Nil(err)
rs[k/2] <- r
}
// each poller should get expected reports,
// nothing more than that.
for _, t := range tasks {
out, err := ts.bg.Poll(t)
ts.Nil(err)
if err != nil {
break
}
r, ok := <-out
ts.True(ok)
ts.Equal(Status.Sent, r.Status())
r, ok = <-out
ts.True(ok)
ts.Equal(Status.Progress, r.Status())
r, ok = <-out
ts.True(ok)
ts.Equal(Status.Success, r.Status())
r, ok = <-out
ts.False(ok)
}
}
func (ts *bridgeTestSuite) TestExist() {
ts.True(ts.bg.Exists(ObjT.Reporter))
ts.True(ts.bg.Exists(ObjT.Consumer))
ts.True(ts.bg.Exists(ObjT.Producer))
ts.True(ts.bg.Exists(ObjT.Store))
// checking should be ==, not &=
ts.False(ts.bg.Exists(ObjT.Store | ObjT.Reporter))
ts.False(ts.bg.Exists(ObjT.All))
}
func (ts *bridgeTestSuite) TestFinalReportWhenShutdown() {
// when exiting, remaining polling should be closed,
// and a final report with
// register a marshaller
ts.Nil(ts.trans.Register(
"FinalReportWhenShutdown",
func() {},
))
// a report channel
reports := make(chan *Report, 1)
ts.Nil(ts.bg.Report("FinalReportWhenShutdown", reports))
// a sample task
task, err := ts.trans.ComposeTask("FinalReportWhenShutdown", nil, nil)
// poll that task
out, err := ts.bg.Poll(task)
ts.Nil(err)
// send reports: Sent, but not the final one
r, err := task.composeReport(Status.Sent, nil, nil)
ts.Nil(err)
reports <- r
o := <-out
ts.True(o.almostEqual(r))
// close bridge, a 'Shutdown' report should be received.
ts.Nil(ts.bg.Close())
o = <-out
ts.Equal(Status.Fail, o.Status())
ts.Equal(ErrCode.Shutdown, o.Error().Code())
}
func (ts *bridgeTestSuite) TestDifferentReportsWithSameID() {
// bridge should be ok when different reports have the same ID
var (
countOfTypes = 10
countOfTasks = 10
tasks []*Task
t *Task
wait sync.WaitGroup
err error
)
defer func() {
ts.Nil(err)
}()
// register idMaker, task
for i := 0; i < countOfTypes; i++ {
name := fmt.Sprintf("DifferentReportsWithSameID.%d", i)
err = ts.trans.AddIDMaker(100+i, &SeqIDMaker{})
if err != nil {
return
}
err = ts.trans.Register(name, func() {})
if err != nil {
return
}
err = ts.trans.SetIDMaker(name, 100+i)
if err != nil {
return
}
reports := make(chan *Report, 10)
err = ts.bg.Report(name, reports)
if err != nil {
return
}
for j := 0; j < countOfTasks; j++ {
t, err = ts.trans.ComposeTask(name, nil, nil)
if err != nil {
return
}
if t != nil {
wait.Add(1)
go ts.gen(reports, t, &wait)
tasks = append(tasks, t)
}
}
}
// wait for all routines finished
wait.Wait()
for _, v := range tasks {
wait.Add(1)
go ts.chks(v, &wait)
}
// wait for all chks routine
wait.Wait()
}
func (ts *bridgeTestSuite) TestAddNamedListener() {
var err error
defer func() {
ts.Nil(err)
}()
// register a task
err = ts.trans.Register("TestAddNamedListener", func() {})
if err != nil {
return
}
// add a named listener, should fail
tasks, err2 := ts.bg.AddNamedListener("TestAddNamedListener", make(chan *TaskReceipt, 10))
ts.Nil(tasks)
ts.NotNil(err2)
}