-
Notifications
You must be signed in to change notification settings - Fork 15
/
dingo_api_test.go
336 lines (287 loc) · 7.68 KB
/
dingo_api_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
package dingo_test
import (
"testing"
"github.com/mission-liao/dingo"
"github.com/stretchr/testify/assert"
)
type testFakeProducer struct {
events chan *dingo.Event
}
func (pdc *testFakeProducer) Expect(int) (err error) { return }
func (pdc *testFakeProducer) Events() ([]<-chan *dingo.Event, error) {
return []<-chan *dingo.Event{
pdc.events,
}, nil
}
func (pdc *testFakeProducer) Close() (err error) { return }
func (pdc *testFakeProducer) ProducerHook(id int, p interface{}) (err error) { return }
func (pdc *testFakeProducer) Send(id dingo.Meta, body []byte) (err error) {
pdc.events <- dingo.NewEvent(
dingo.ObjT.Producer,
dingo.EventLvl.Info,
dingo.EventCode.Generic,
"Send",
)
return
}
type testFakeStore struct {
events chan *dingo.Event
}
func (st *testFakeStore) Expect(int) (err error) { return }
func (st *testFakeStore) Events() ([]<-chan *dingo.Event, error) {
return []<-chan *dingo.Event{
st.events,
}, nil
}
func (st *testFakeStore) Close() (err error) { return }
func (st *testFakeStore) StoreHook(id int, p interface{}) (err error) { return }
func (st *testFakeStore) Poll(meta dingo.Meta) (reports <-chan []byte, err error) {
st.events <- dingo.NewEvent(
dingo.ObjT.Store,
dingo.EventLvl.Info,
dingo.EventCode.Generic,
"Poll",
)
return make(chan []byte, 1), nil
}
func (st *testFakeStore) Done(meta dingo.Meta) (err error) { return }
func TestDingoEventFromBackendAndBroker(t *testing.T) {
// make sure events from backend/broker are published
ass := assert.New(t)
app, err := dingo.NewApp("remote", nil)
ass.Nil(err)
if err != nil {
return
}
// prepare a caller
_, _, err = app.Use(&testFakeProducer{make(chan *dingo.Event, 10)}, dingo.ObjT.Producer)
ass.Nil(err)
if err != nil {
return
}
_, _, err = app.Use(&testFakeStore{make(chan *dingo.Event, 10)}, dingo.ObjT.Store)
ass.Nil(err)
if err != nil {
return
}
// register a task
err = app.Register("TestDingoEvent", func() {})
ass.Nil(err)
if err != nil {
return
}
// there should be 2 events
_, events, err := app.Listen(dingo.ObjT.All, dingo.EventLvl.Info, 0)
ass.Nil(err)
if err != nil {
return
}
// send a task
_, err = app.Call("TestDingoEvent", nil)
ass.Nil(err)
if err != nil {
return
}
// exactly two event should be received.
e1 := <-events
e2 := <-events
ass.True(e1.Origin|e2.Origin == dingo.ObjT.Producer|dingo.ObjT.Store)
ass.True(e1.Level == dingo.EventLvl.Info)
ass.True(e2.Level == dingo.EventLvl.Info)
ass.True(e1.Payload.(string) == "Send" || e2.Payload.(string) == "Send")
ass.True(e1.Payload.(string) == "Poll" || e2.Payload.(string) == "Poll")
// release resource
ass.Nil(app.Close())
}
type testFakeObject struct {
events chan *dingo.Event
}
func (obj *testFakeObject) Expect(int) (err error) { return }
func (obj *testFakeObject) Events() ([]<-chan *dingo.Event, error) {
return []<-chan *dingo.Event{
obj.events,
}, nil
}
func (obj *testFakeObject) Close() (err error) { return }
func TestDingoEventLevel(t *testing.T) {
var (
err error
ass = assert.New(t)
)
defer func() {
ass.Nil(err)
}()
// attach a fake object
app, err := dingo.NewApp("local", nil)
if err != nil {
return
}
defer func() {
ass.Nil(app.Close())
}()
// allocate channels slice
chs := make([]<-chan *dingo.Event, 4)
Debug, Info, Warning, Error := 0, 1, 2, 3
idDebug, idInfo, idWarning, idError := 0, 1, 2, 3
recv := func(which, lvl int) {
e := <-chs[which]
ass.Equal(lvl, e.Level)
}
// new a fake object, whose event channel is accessible
obj := &testFakeObject{events: make(chan *dingo.Event, 1)}
_, _, err = app.Use(obj, 0)
if err != nil {
return
}
// subscribe a Debug channel
idDebug, chs[Debug], err = app.Listen(dingo.ObjT.All, dingo.EventLvl.Debug, 0)
if err != nil {
return
}
// subscribe an Info channel
idInfo, chs[Info], err = app.Listen(dingo.ObjT.All, dingo.EventLvl.Info, 0)
if err != nil {
return
}
// subscribe a Warning channel
idWarning, chs[Warning], err = app.Listen(dingo.ObjT.All, dingo.EventLvl.Warning, 0)
if err != nil {
return
}
// subscribe an Error channel
idError, chs[Error], err = app.Listen(dingo.ObjT.All, dingo.EventLvl.Error, 0)
if err != nil {
return
}
// send an error event
obj.events <- dingo.NewEvent(dingo.ObjT.Bridge, dingo.EventLvl.Error, 0, "error")
recv(Error, dingo.EventLvl.Error)
recv(Warning, dingo.EventLvl.Error)
recv(Info, dingo.EventLvl.Error)
recv(Debug, dingo.EventLvl.Error)
// send a warning event
obj.events <- dingo.NewEvent(dingo.ObjT.Bridge, dingo.EventLvl.Warning, 0, "warning")
recv(Warning, dingo.EventLvl.Warning)
recv(Info, dingo.EventLvl.Warning)
recv(Debug, dingo.EventLvl.Warning)
// send an info event
obj.events <- dingo.NewEvent(dingo.ObjT.Bridge, dingo.EventLvl.Info, 0, "info")
recv(Info, dingo.EventLvl.Info)
recv(Debug, dingo.EventLvl.Info)
// send a debug event
obj.events <- dingo.NewEvent(dingo.ObjT.Bridge, dingo.EventLvl.Debug, 0, "debug")
recv(Debug, dingo.EventLvl.Debug)
// stop debug channel, and send an debug event
err = app.StopListen(idDebug)
if err != nil {
return
}
// stop info channel, and send an info event
err = app.StopListen(idInfo)
if err != nil {
return
}
// stop warning channel, and send an warning event
err = app.StopListen(idWarning)
if err != nil {
return
}
// stop error channel, and send an error event
err = app.StopListen(idError)
if err != nil {
return
}
}
func TestDingoEventOrigin(t *testing.T) {
var (
err error
ass = assert.New(t)
)
defer func() {
ass.Nil(err)
}()
app, err := dingo.NewApp("remote", nil)
if err != nil {
return
}
defer func() {
ass.Nil(app.Close())
}()
// new a fake object, whose event channel is accessible
obj := &testFakeObject{events: make(chan *dingo.Event, 1)}
_, _, err = app.Use(obj, 0)
if err != nil {
return
}
chk := func(ch <-chan *dingo.Event, origin int) {
e := <-ch
ass.Equal(origin, e.Origin)
}
// subscribe bridge event
idBridge, chBridge, err := app.Listen(dingo.ObjT.Bridge, dingo.EventLvl.Debug, 0)
if err != nil {
return
}
// subscribe all event
idAll, chAll, err := app.Listen(dingo.ObjT.All, dingo.EventLvl.Debug, 0)
if err != nil {
return
}
// send a mapper event
obj.events <- dingo.NewEvent(dingo.ObjT.Mapper, dingo.EventLvl.Error, 0, "mapper")
chk(chAll, dingo.ObjT.Mapper)
// send a bridge event
obj.events <- dingo.NewEvent(dingo.ObjT.Bridge, dingo.EventLvl.Error, 0, "bridge")
chk(chAll, dingo.ObjT.Bridge)
chk(chBridge, dingo.ObjT.Bridge)
// send a mapper | bridge event
obj.events <- dingo.NewEvent(dingo.ObjT.Bridge|dingo.ObjT.Mapper, dingo.EventLvl.Error, 0, "bridge")
chk(chAll, dingo.ObjT.Bridge|dingo.ObjT.Mapper)
chk(chBridge, dingo.ObjT.Bridge|dingo.ObjT.Mapper)
err = app.StopListen(idBridge)
if err != nil {
return
}
err = app.StopListen(idAll)
if err != nil {
return
}
}
func TestDingoUse(t *testing.T) {
var (
err error
ass = assert.New(t)
)
defer func() {
ass.Nil(err)
}()
app, err := dingo.NewApp("remote", nil)
if err != nil {
return
}
defer func() {
ass.Nil(app.Close())
}()
// new a fake object, whose event channel is accessible
obj := &testFakeObject{}
chk := func(id int, used int, err error) {
ass.Equal(0, used)
ass.NotNil(err)
}
// attach it with different types
chk(app.Use(obj, dingo.ObjT.Producer))
chk(app.Use(obj, dingo.ObjT.Consumer))
chk(app.Use(obj, dingo.ObjT.NamedConsumer))
chk(app.Use(obj, dingo.ObjT.Reporter))
chk(app.Use(obj, dingo.ObjT.Store))
err = app.Register("TestDingoUse", func() {})
if err != nil {
return
}
remain, err2 := app.Allocate("TestDingoUse", 10, 1)
ass.Equal(10, remain)
ass.NotNil(err2)
reports, err2 := app.Call("TestDingoUse", nil)
ass.Nil(reports)
ass.NotNil(err2)
}