Debian Codegraph @

server/broker/exchanges_test.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 package broker_test // use a package test to avoid cyclic imports
18
19 import (
20 "encoding/json"
21 "errors"
22 "fmt"
23 "strings"
24 stdtesting "testing"
25
26 . "gopkg.in/check.v1"
27
28 "gitlab.com/ubports/development/core/lomiri-push-service/protocol"
29 "gitlab.com/ubports/development/core/lomiri-push-service/server/broker"
30 "gitlab.com/ubports/development/core/lomiri-push-service/server/broker/testing"
31 "gitlab.com/ubports/development/core/lomiri-push-service/server/store"
32 help "gitlab.com/ubports/development/core/lomiri-push-service/testing"
33 )
34
35 func TestBroker(t *stdtesting.T) { TestingT(t) }
36
37 type exchangesSuite struct{}
38
39 var _ = Suite(&exchangesSuite{})
40
41 func (s *exchangesSuite) TestBroadcastExchangeInit(c *C) {
42 exchg := &broker.BroadcastExchange{
43 ChanId: store.SystemInternalChannelId,
44 TopLevel: 3,
45 Notifications: help.Ns(
46 json.RawMessage(`{"a":"x"}`),
47 json.RawMessage(`[]`),
48 json.RawMessage(`{"a":"y"}`),
49 ),
50 }
51 exchg.Init()
52 c.Check(exchg.Decoded, DeepEquals, []map[string]interface{}{
53 map[string]interface{}{"a": "x"},
54 nil,
55 map[string]interface{}{"a": "y"},
56 })
57 }
58
59 func (s *exchangesSuite) TestBroadcastExchange(c *C) {
60 sess := &testing.TestBrokerSession{
61 LevelsMap: broker.LevelsMap(map[store.InternalChannelId]int64{}),
62 Model: "m1",
63 ImageChannel: "img1",
64 }
65 exchg := &broker.BroadcastExchange{
66 ChanId: store.SystemInternalChannelId,
67 TopLevel: 3,
68 Notifications: help.Ns(
69 json.RawMessage(`{"img1/m1":100}`),
70 json.RawMessage(`{"img2/m2":200}`),
71 ),
72 }
73 exchg.Init()
74 outMsg, inMsg, err := exchg.Prepare(sess)
75 c.Assert(err, IsNil)
76 // check
77 marshalled, err := json.Marshal(outMsg)
78 c.Assert(err, IsNil)
79 c.Check(string(marshalled), Equals, `{"T":"broadcast","ChanId":"0","TopLevel":3,"Payloads":[{"img1/m1":100}]}`)
80 err = json.Unmarshal([]byte(`{"T":"ack"}`), inMsg)
81 c.Assert(err, IsNil)
82 err = exchg.Acked(sess, true)
83 c.Assert(err, IsNil)
84 c.Check(sess.LevelsMap[store.SystemInternalChannelId], Equals, int64(3))
85 }
86
87 func (s *exchangesSuite) TestBroadcastExchangeEmpty(c *C) {
88 sess := &testing.TestBrokerSession{
89 LevelsMap: broker.LevelsMap(map[store.InternalChannelId]int64{}),
90 Model: "m1",
91 ImageChannel: "img1",
92 }
93 exchg := &broker.BroadcastExchange{
94 ChanId: store.SystemInternalChannelId,
95 TopLevel: 3,
96 Notifications: []protocol.Notification{},
97 }
98 exchg.Init()
99 outMsg, inMsg, err := exchg.Prepare(sess)
100 c.Assert(err, Equals, broker.ErrNop)
101 c.Check(outMsg, IsNil)
102 c.Check(inMsg, IsNil)
103 }
104
105 func (s *exchangesSuite) TestBroadcastExchangeEmptyButAhead(c *C) {
106 sess := &testing.TestBrokerSession{
107 LevelsMap: broker.LevelsMap(map[store.InternalChannelId]int64{
108 store.SystemInternalChannelId: 10,
109 }),
110 Model: "m1",
111 ImageChannel: "img1",
112 }
113 exchg := &broker.BroadcastExchange{
114 ChanId: store.SystemInternalChannelId,
115 TopLevel: 3,
116 Notifications: []protocol.Notification{},
117 }
118 exchg.Init()
119 outMsg, inMsg, err := exchg.Prepare(sess)
120 c.Assert(err, IsNil)
121 c.Check(outMsg, NotNil)
122 c.Check(inMsg, NotNil)
123 }
124
125 func (s *exchangesSuite) TestBroadcastExchangeReuseVsSplit(c *C) {
126 sess := &testing.TestBrokerSession{
127 LevelsMap: broker.LevelsMap(map[store.InternalChannelId]int64{}),
128 Model: "m1",
129 ImageChannel: "img1",
130 }
131 payloadFmt := fmt.Sprintf(`{"img1/m1":%%d,"bloat":"%s"}`, strings.Repeat("x", 1024*2))
132 needsSplitting := make([]json.RawMessage, 32)
133 for i := 0; i < 32; i++ {
134 needsSplitting[i] = json.RawMessage(fmt.Sprintf(payloadFmt, i))
135 }
136
137 topLevel := int64(len(needsSplitting))
138 exchg := &broker.BroadcastExchange{
139 ChanId: store.SystemInternalChannelId,
140 TopLevel: topLevel,
141 Notifications: help.Ns(needsSplitting...),
142 }
143 exchg.Init()
144 outMsg, _, err := exchg.Prepare(sess)
145 c.Assert(err, IsNil)
146 parts := 0
147 for {
148 done := outMsg.Split()
149 parts++
150 if done {
151 break
152 }
153 }
154 c.Assert(parts, Equals, 2)
155 exchg = &broker.BroadcastExchange{
156 ChanId: store.SystemInternalChannelId,
157 TopLevel: topLevel + 2,
158 Notifications: help.Ns(
159 json.RawMessage(`{"img1/m1":"x"}`),
160 json.RawMessage(`{"img1/m1":"y"}`),
161 ),
162 }
163 exchg.Init()
164 outMsg, _, err = exchg.Prepare(sess)
165 c.Assert(err, IsNil)
166 done := outMsg.Split() // shouldn't panic
167 c.Check(done, Equals, true)
168 }
169
170 func (s *exchangesSuite) TestBroadcastExchangeAckMismatch(c *C) {
171 sess := &testing.TestBrokerSession{
172 LevelsMap: broker.LevelsMap(map[store.InternalChannelId]int64{}),
173 Model: "m1",
174 ImageChannel: "img2",
175 }
176 exchg := &broker.BroadcastExchange{
177 ChanId: store.SystemInternalChannelId,
178 TopLevel: 3,
179 Notifications: help.Ns(
180 json.RawMessage(`{"img2/m1":1}`),
181 ),
182 }
183 exchg.Init()
184 outMsg, inMsg, err := exchg.Prepare(sess)
185 c.Assert(err, IsNil)
186 // check
187 marshalled, err := json.Marshal(outMsg)
188 c.Assert(err, IsNil)
189 c.Check(string(marshalled), Equals, `{"T":"broadcast","ChanId":"0","TopLevel":3,"Payloads":[{"img2/m1":1}]}`)
190 err = json.Unmarshal([]byte(`{}`), inMsg)
191 c.Assert(err, IsNil)
192 err = exchg.Acked(sess, true)
193 c.Assert(err, Not(IsNil))
194 c.Check(sess.LevelsMap[store.SystemInternalChannelId], Equals, int64(0))
195 }
196
197 func (s *exchangesSuite) TestBroadcastExchangeFilterByLevel(c *C) {
198 sess := &testing.TestBrokerSession{
199 LevelsMap: broker.LevelsMap(map[store.InternalChannelId]int64{
200 store.SystemInternalChannelId: 2,
201 }),
202 Model: "m1",
203 ImageChannel: "img1",
204 }
205 exchg := &broker.BroadcastExchange{
206 ChanId: store.SystemInternalChannelId,
207 TopLevel: 3,
208 Notifications: help.Ns(
209 json.RawMessage(`{"img1/m1":100}`),
210 json.RawMessage(`{"img1/m1":101}`),
211 ),
212 }
213 exchg.Init()
214 outMsg, inMsg, err := exchg.Prepare(sess)
215 c.Assert(err, IsNil)
216 // check
217 marshalled, err := json.Marshal(outMsg)
218 c.Assert(err, IsNil)
219 c.Check(string(marshalled), Equals, `{"T":"broadcast","ChanId":"0","TopLevel":3,"Payloads":[{"img1/m1":101}]}`)
220 err = json.Unmarshal([]byte(`{"T":"ack"}`), inMsg)
221 c.Assert(err, IsNil)
222 err = exchg.Acked(sess, true)
223 c.Assert(err, IsNil)
224 }
225
226 func (s *exchangesSuite) TestBroadcastExchangeChannelFilter(c *C) {
227 sess := &testing.TestBrokerSession{
228 LevelsMap: broker.LevelsMap(map[store.InternalChannelId]int64{}),
229 Model: "m1",
230 ImageChannel: "img1",
231 }
232 exchg := &broker.BroadcastExchange{
233 ChanId: store.SystemInternalChannelId,
234 TopLevel: 5,
235 Notifications: help.Ns(
236 json.RawMessage(`{"img1/m1":100}`),
237 json.RawMessage(`{"img2/m2":200}`),
238 json.RawMessage(`{"img1/m1":101}`),
239 ),
240 }
241 exchg.Init()
242 outMsg, inMsg, err := exchg.Prepare(sess)
243 c.Assert(err, IsNil)
244 // check
245 marshalled, err := json.Marshal(outMsg)
246 c.Assert(err, IsNil)
247 c.Check(string(marshalled), Equals, `{"T":"broadcast","ChanId":"0","TopLevel":5,"Payloads":[{"img1/m1":100},{"img1/m1":101}]}`)
248 err = json.Unmarshal([]byte(`{"T":"ack"}`), inMsg)
249 c.Assert(err, IsNil)
250 err = exchg.Acked(sess, true)
251 c.Assert(err, IsNil)
252 c.Check(sess.LevelsMap[store.SystemInternalChannelId], Equals, int64(5))
253 }
254
255 func (s *exchangesSuite) TestConnMetaExchange(c *C) {
256 sess := &testing.TestBrokerSession{}
257 var msg protocol.OnewayMsg = &protocol.ConnWarnMsg{"connwarn", "REASON"}
258 cbe := &broker.ConnMetaExchange{msg}
259 outMsg, inMsg, err := cbe.Prepare(sess)
260 c.Assert(err, IsNil)
261 c.Check(msg, Equals, outMsg)
262 c.Check(inMsg, IsNil) // no answer is expected
263 // check
264 marshalled, err := json.Marshal(outMsg)
265 c.Assert(err, IsNil)
266 c.Check(string(marshalled), Equals, `{"T":"connwarn","Reason":"REASON"}`)
267
268 c.Check(func() { cbe.Acked(nil, true) }, PanicMatches, "Acked should not get invoked on ConnMetaExchange")
269 }
270
271 func (s *exchangesSuite) TestUnicastExchange(c *C) {
272 chanId1 := store.UnicastInternalChannelId("u1", "d1")
273 notifs := []protocol.Notification{
274 protocol.Notification{
275 MsgId: "msg1",
276 AppId: "app1",
277 Payload: json.RawMessage(`{"m": 1}`),
278 },
279 protocol.Notification{
280 MsgId: "msg2",
281 AppId: "app2",
282 Payload: json.RawMessage(`{"m": 2}`),
283 },
284 }
285 dropped := make(chan []protocol.Notification, 2)
286 sess := &testing.TestBrokerSession{
287 DoGet: func(chanId store.InternalChannelId, cachedOk bool) (int64, []protocol.Notification, error) {
288 c.Check(chanId, Equals, chanId1)
289 c.Check(cachedOk, Equals, false)
290 return 0, notifs, nil
291 },
292 DoDropByMsgId: func(chanId store.InternalChannelId, targets []protocol.Notification) error {
293 c.Check(chanId, Equals, chanId1)
294 dropped <- targets
295 return nil
296 },
297 }
298 exchg := &broker.UnicastExchange{ChanId: chanId1, CachedOk: false}
299 outMsg, inMsg, err := exchg.Prepare(sess)
300 c.Assert(err, IsNil)
301 // check
302 marshalled, err := json.Marshal(outMsg)
303 c.Assert(err, IsNil)
304 c.Check(string(marshalled), Equals, `{"T":"notifications","Notifications":[{"A":"app1","M":"msg1","P":{"m":1}},{"A":"app2","M":"msg2","P":{"m":2}}]}`)
305 err = json.Unmarshal([]byte(`{"T":"ack"}`), inMsg)
306 c.Assert(err, IsNil)
307 err = exchg.Acked(sess, true)
308 c.Assert(err, IsNil)
309 c.Check(dropped, HasLen, 1)
310 c.Check(<-dropped, DeepEquals, notifs)
311 }
312
313 func (s *exchangesSuite) TestUnicastExchangeAckMismatch(c *C) {
314 notifs := []protocol.Notification{protocol.Notification{}}
315 dropped := make(chan []protocol.Notification, 2)
316 sess := &testing.TestBrokerSession{
317 DoGet: func(chanId store.InternalChannelId, cachedOk bool) (int64, []protocol.Notification, error) {
318 return 0, notifs, nil
319 },
320 DoDropByMsgId: func(chanId store.InternalChannelId, targets []protocol.Notification) error {
321 dropped <- targets
322 return nil
323 },
324 }
325 exchg := &broker.UnicastExchange{}
326 _, inMsg, err := exchg.Prepare(sess)
327 c.Assert(err, IsNil)
328 err = json.Unmarshal([]byte(`{}`), inMsg)
329 c.Assert(err, IsNil)
330 err = exchg.Acked(sess, true)
331 c.Assert(err, Not(IsNil))
332 c.Check(dropped, HasLen, 0)
333 }
334
335 func (s *exchangesSuite) TestUnicastExchangeErrorOnPrepare(c *C) {
336 fail := errors.New("fail")
337 sess := &testing.TestBrokerSession{
338 DoGet: func(chanId store.InternalChannelId, cachedOk bool) (int64, []protocol.Notification, error) {
339 return 0, nil, fail
340 },
341 }
342 exchg := &broker.UnicastExchange{}
343 _, _, err := exchg.Prepare(sess)
344 c.Assert(err, Equals, fail)
345 }
346
347 func (s *exchangesSuite) TestUnicastExchangeCachedOkNop(c *C) {
348 chanId1 := store.UnicastInternalChannelId("u1", "d1")
349 sess := &testing.TestBrokerSession{
350 DoGet: func(chanId store.InternalChannelId, cachedOk bool) (int64, []protocol.Notification, error) {
351 c.Check(chanId, Equals, chanId1)
352 c.Check(cachedOk, Equals, true)
353 return 0, nil, nil
354 },
355 }
356 exchg := &broker.UnicastExchange{ChanId: chanId1, CachedOk: true}
357 _, _, err := exchg.Prepare(sess)
358 c.Assert(err, Equals, broker.ErrNop)
359 }
360
361 func (s *exchangesSuite) TestUnicastExchangeErrorOnAcked(c *C) {
362 notifs := []protocol.Notification{protocol.Notification{}}
363 fail := errors.New("fail")
364 sess := &testing.TestBrokerSession{
365 DoGet: func(chanId store.InternalChannelId, cachedOk bool) (int64, []protocol.Notification, error) {
366 return 0, notifs, nil
367 },
368 DoDropByMsgId: func(chanId store.InternalChannelId, targets []protocol.Notification) error {
369 return fail
370 },
371 }
372 exchg := &broker.UnicastExchange{}
373 _, inMsg, err := exchg.Prepare(sess)
374 c.Assert(err, IsNil)
375 err = json.Unmarshal([]byte(`{"T":"ack"}`), inMsg)
376 c.Assert(err, IsNil)
377 err = exchg.Acked(sess, true)
378 c.Assert(err, Equals, fail)
379 }
380
381 func (s *exchangesSuite) TestFeedPending(c *C) {
382 bcast1 := json.RawMessage(`{"m": "M"}`)
383 decoded1 := map[string]interface{}{"m": "M"}
384 sess := &testing.TestBrokerSession{
385 Exchanges: make(chan broker.Exchange, 5),
386 DoGet: func(chanId store.InternalChannelId, cachedOk bool) (int64, []protocol.Notification, error) {
387 switch chanId {
388 case store.SystemInternalChannelId:
389 return 1, help.Ns(bcast1), nil
390 default:
391 return 0, nil, nil
392 }
393 },
394 }
395 err := broker.FeedPending(sess)
396 c.Assert(err, IsNil)
397 c.Assert(len(sess.Exchanges), Equals, 2)
398 exchg1 := <-sess.Exchanges
399 c.Check(exchg1, DeepEquals, &broker.BroadcastExchange{
400 ChanId: store.SystemInternalChannelId,
401 TopLevel: 1,
402 Notifications: help.Ns(bcast1),
403 Decoded: []map[string]interface{}{decoded1},
404 })
405 exchg2 := <-sess.Exchanges
406 c.Check(exchg2, DeepEquals, &broker.UnicastExchange{
407 ChanId: sess.InternalChannelId(),
408 CachedOk: true,
409 })
410 }
411
412 func (s *exchangesSuite) TestFeedPendingSystemChanNop(c *C) {
413 bcast1 := json.RawMessage(`{"m": "M"}`)
414 sess := &testing.TestBrokerSession{
415 LevelsMap: map[store.InternalChannelId]int64{
416 store.SystemInternalChannelId: 1,
417 },
418 Exchanges: make(chan broker.Exchange, 5),
419 DoGet: func(chanId store.InternalChannelId, cachedOk bool) (int64, []protocol.Notification, error) {
420 switch chanId {
421 case store.SystemInternalChannelId:
422 return 1, help.Ns(bcast1), nil
423 default:
424 return 0, nil, nil
425 }
426 },
427 }
428 err := broker.FeedPending(sess)
429 c.Assert(err, IsNil)
430 c.Check(len(sess.Exchanges), Equals, 1)
431 exchg1 := <-sess.Exchanges
432 c.Check(exchg1, FitsTypeOf, &broker.UnicastExchange{})
433 }
434
435 func (s *exchangesSuite) TestFeedPendingSystemChanFail(c *C) {
436 sess := &testing.TestBrokerSession{
437 LevelsMap: map[store.InternalChannelId]int64{
438 store.SystemInternalChannelId: 1,
439 },
440 Exchanges: make(chan broker.Exchange, 5),
441 DoGet: func(chanId store.InternalChannelId, cachedOk bool) (int64, []protocol.Notification, error) {
442 switch chanId {
443 case store.SystemInternalChannelId:
444 return 0, nil, errors.New("fail")
445 default:
446 return 0, nil, nil
447 }
448 },
449 }
450 err := broker.FeedPending(sess)
451 c.Assert(err, IsNil)
452 c.Check(len(sess.Exchanges), Equals, 1)
453 exchg1 := <-sess.Exchanges
454 c.Check(exchg1, FitsTypeOf, &broker.UnicastExchange{})
455 }