| 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 testsuite contains a common test suite for brokers. |
| 18 |
package testsuite |
| 19 |
|
| 20 |
import ( |
| 21 |
"encoding/json" |
| 22 |
"errors" |
| 23 |
// "log" |
| 24 |
"time" |
| 25 |
|
| 26 |
. "gopkg.in/check.v1" |
| 27 |
|
| 28 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 29 |
"gitlab.com/ubports/development/core/lomiri-push-service/protocol" |
| 30 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/broker" |
| 31 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/broker/testing" |
| 32 |
"gitlab.com/ubports/development/core/lomiri-push-service/server/store" |
| 33 |
help "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 34 |
) |
| 35 |
|
| 36 |
// The expected interface for tested brokers. |
| 37 |
type FullBroker interface { |
| 38 |
broker.Broker |
| 39 |
broker.BrokerSending |
| 40 |
Start() |
| 41 |
Stop() |
| 42 |
Running() bool |
| 43 |
} |
| 44 |
|
| 45 |
// The common brokers' test suite. |
| 46 |
type CommonBrokerSuite struct { |
| 47 |
// Build the broker for testing. |
| 48 |
MakeBroker func(store.PendingStore, broker.BrokerConfig, logger.Logger) FullBroker |
| 49 |
// Build a session tracker for testing. |
| 50 |
MakeTracker func(sessionId string) broker.SessionTracker |
| 51 |
// Let us get to a session under the broker. |
| 52 |
RevealSession func(broker.Broker, string) broker.BrokerSession |
| 53 |
// Let us get to a broker.BroadcastExchange from an Exchange. |
| 54 |
RevealBroadcastExchange func(broker.Exchange) *broker.BroadcastExchange |
| 55 |
// Let us get to a broker.UnicastExchange from an Exchange. |
| 56 |
RevealUnicastExchange func(broker.Exchange) *broker.UnicastExchange |
| 57 |
// private |
| 58 |
testlog *help.TestLogger |
| 59 |
} |
| 60 |
|
| 61 |
func (s *CommonBrokerSuite) SetUpTest(c *C) { |
| 62 |
s.testlog = help.NewTestLogger(c, "error") |
| 63 |
} |
| 64 |
|
| 65 |
var testBrokerConfig = &testing.TestBrokerConfig{10, 5} |
| 66 |
|
| 67 |
func (s *CommonBrokerSuite) TestSanity(c *C) { |
| 68 |
sto := store.NewInMemoryPendingStore() |
| 69 |
b := s.MakeBroker(sto, testBrokerConfig, s.testlog) |
| 70 |
c.Check(s.RevealSession(b, "FOO"), IsNil) |
| 71 |
} |
| 72 |
|
| 73 |
func (s *CommonBrokerSuite) TestStartStop(c *C) { |
| 74 |
b := s.MakeBroker(nil, testBrokerConfig, s.testlog) |
| 75 |
b.Start() |
| 76 |
c.Check(b.Running(), Equals, true) |
| 77 |
b.Start() |
| 78 |
b.Stop() |
| 79 |
c.Check(b.Running(), Equals, false) |
| 80 |
b.Stop() |
| 81 |
} |
| 82 |
|
| 83 |
func (s *CommonBrokerSuite) TestRegistration(c *C) { |
| 84 |
sto := store.NewInMemoryPendingStore() |
| 85 |
b := s.MakeBroker(sto, testBrokerConfig, s.testlog) |
| 86 |
b.Start() |
| 87 |
defer b.Stop() |
| 88 |
sess, err := b.Register(&protocol.ConnectMsg{ |
| 89 |
Type: "connect", |
| 90 |
DeviceId: "dev-1", |
| 91 |
Levels: map[string]int64{"0": 5}, |
| 92 |
Info: map[string]interface{}{ |
| 93 |
"device": "model", |
| 94 |
"channel": "daily", |
| 95 |
}, |
| 96 |
}, s.MakeTracker("s1")) |
| 97 |
c.Assert(err, IsNil) |
| 98 |
c.Assert(s.RevealSession(b, "dev-1"), Equals, sess) |
| 99 |
c.Assert(sess.DeviceIdentifier(), Equals, "dev-1") |
| 100 |
c.Check(sess.DeviceImageModel(), Equals, "model") |
| 101 |
c.Check(sess.DeviceImageChannel(), Equals, "daily") |
| 102 |
c.Assert(sess.ExchangeScratchArea(), Not(IsNil)) |
| 103 |
c.Check(sess.Levels(), DeepEquals, broker.LevelsMap(map[store.InternalChannelId]int64{ |
| 104 |
store.SystemInternalChannelId: 5, |
| 105 |
})) |
| 106 |
b.Unregister(sess) |
| 107 |
// just to make sure the unregister was processed |
| 108 |
_, err = b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: ""}, s.MakeTracker("s2")) |
| 109 |
c.Assert(err, IsNil) |
| 110 |
c.Check(s.RevealSession(b, "dev-1"), IsNil) |
| 111 |
} |
| 112 |
|
| 113 |
func (s *CommonBrokerSuite) TestRegistrationBrokenLevels(c *C) { |
| 114 |
sto := store.NewInMemoryPendingStore() |
| 115 |
b := s.MakeBroker(sto, testBrokerConfig, s.testlog) |
| 116 |
b.Start() |
| 117 |
defer b.Stop() |
| 118 |
_, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-1", Levels: map[string]int64{"z": 5}}, s.MakeTracker("s1")) |
| 119 |
c.Check(err, FitsTypeOf, &broker.ErrAbort{}) |
| 120 |
} |
| 121 |
|
| 122 |
func (s *CommonBrokerSuite) TestRegistrationInfoErrors(c *C) { |
| 123 |
sto := store.NewInMemoryPendingStore() |
| 124 |
b := s.MakeBroker(sto, testBrokerConfig, s.testlog) |
| 125 |
b.Start() |
| 126 |
defer b.Stop() |
| 127 |
info := map[string]interface{}{ |
| 128 |
"device": -1, |
| 129 |
} |
| 130 |
_, err := b.Register(&protocol.ConnectMsg{Type: "connect", Info: info}, s.MakeTracker("s1")) |
| 131 |
c.Check(err, Equals, broker.ErrUnexpectedValue) |
| 132 |
info["device"] = "m" |
| 133 |
info["channel"] = -1 |
| 134 |
_, err = b.Register(&protocol.ConnectMsg{Type: "connect", Info: info}, s.MakeTracker("s2")) |
| 135 |
c.Check(err, Equals, broker.ErrUnexpectedValue) |
| 136 |
} |
| 137 |
|
| 138 |
func (s *CommonBrokerSuite) TestRegistrationFeedPending(c *C) { |
| 139 |
sto := store.NewInMemoryPendingStore() |
| 140 |
notification1 := json.RawMessage(`{"m": "M"}`) |
| 141 |
muchLater := time.Now().Add(10 * time.Minute) |
| 142 |
sto.AppendToChannel(store.SystemInternalChannelId, notification1, muchLater) |
| 143 |
b := s.MakeBroker(sto, testBrokerConfig, s.testlog) |
| 144 |
b.Start() |
| 145 |
defer b.Stop() |
| 146 |
sess, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-1"}, s.MakeTracker("s1")) |
| 147 |
c.Assert(err, IsNil) |
| 148 |
c.Check(len(sess.SessionChannel()), Equals, 2) |
| 149 |
} |
| 150 |
|
| 151 |
func (s *CommonBrokerSuite) TestRegistrationFeedPendingError(c *C) { |
| 152 |
sto := &testFailingStore{} |
| 153 |
b := s.MakeBroker(sto, testBrokerConfig, s.testlog) |
| 154 |
b.Start() |
| 155 |
defer b.Stop() |
| 156 |
_, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-1"}, s.MakeTracker("s1")) |
| 157 |
c.Assert(err, IsNil) |
| 158 |
// but |
| 159 |
c.Check(s.testlog.Captured(), Matches, "ERROR unsuccessful, get channel snapshot for 0 \\(cachedOk=true\\): get channel snapshot fail\n") |
| 160 |
} |
| 161 |
|
| 162 |
func clearOfPending(c *C, sess broker.BrokerSession) { |
| 163 |
c.Assert(len(sess.SessionChannel()) >= 1, Equals, true) |
| 164 |
<-sess.SessionChannel() |
| 165 |
} |
| 166 |
|
| 167 |
func (s *CommonBrokerSuite) TestRegistrationLastWins(c *C) { |
| 168 |
sto := store.NewInMemoryPendingStore() |
| 169 |
b := s.MakeBroker(sto, testBrokerConfig, s.testlog) |
| 170 |
b.Start() |
| 171 |
defer b.Stop() |
| 172 |
sess1, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-1"}, s.MakeTracker("s1")) |
| 173 |
c.Assert(err, IsNil) |
| 174 |
clearOfPending(c, sess1) |
| 175 |
sess2, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-1"}, s.MakeTracker("s2")) |
| 176 |
c.Assert(err, IsNil) |
| 177 |
// previous session got signaled by sending nil on its channel |
| 178 |
var sentinel broker.Exchange |
| 179 |
got := false |
| 180 |
select { |
| 181 |
case sentinel = <-sess1.SessionChannel(): |
| 182 |
got = true |
| 183 |
case <-time.After(5 * time.Second): |
| 184 |
c.Fatal("taking too long to get sentinel") |
| 185 |
} |
| 186 |
c.Check(got, Equals, true) |
| 187 |
c.Check(sentinel, IsNil) |
| 188 |
c.Assert(s.RevealSession(b, "dev-1"), Equals, sess2) |
| 189 |
b.Unregister(sess1) |
| 190 |
// just to make sure the unregister was processed |
| 191 |
_, err = b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: ""}, s.MakeTracker("s3")) |
| 192 |
c.Assert(err, IsNil) |
| 193 |
c.Check(s.RevealSession(b, "dev-1"), Equals, sess2) |
| 194 |
} |
| 195 |
|
| 196 |
func (s *CommonBrokerSuite) TestBroadcast(c *C) { |
| 197 |
sto := store.NewInMemoryPendingStore() |
| 198 |
notification1 := json.RawMessage(`{"m": "M"}`) |
| 199 |
decoded1 := map[string]interface{}{"m": "M"} |
| 200 |
b := s.MakeBroker(sto, testBrokerConfig, s.testlog) |
| 201 |
b.Start() |
| 202 |
defer b.Stop() |
| 203 |
sess1, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-1"}, s.MakeTracker("s1")) |
| 204 |
c.Assert(err, IsNil) |
| 205 |
clearOfPending(c, sess1) |
| 206 |
sess2, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-2"}, s.MakeTracker("s2")) |
| 207 |
c.Assert(err, IsNil) |
| 208 |
clearOfPending(c, sess2) |
| 209 |
// add notification to channel *after* the registrations |
| 210 |
muchLater := time.Now().Add(10 * time.Minute) |
| 211 |
sto.AppendToChannel(store.SystemInternalChannelId, notification1, muchLater) |
| 212 |
b.Broadcast(store.SystemInternalChannelId) |
| 213 |
select { |
| 214 |
case <-time.After(5 * time.Second): |
| 215 |
c.Fatal("taking too long to get broadcast exchange") |
| 216 |
case exchg1 := <-sess1.SessionChannel(): |
| 217 |
c.Check(s.RevealBroadcastExchange(exchg1), DeepEquals, &broker.BroadcastExchange{ |
| 218 |
ChanId: store.SystemInternalChannelId, |
| 219 |
TopLevel: 1, |
| 220 |
Notifications: help.Ns(notification1), |
| 221 |
Decoded: []map[string]interface{}{decoded1}, |
| 222 |
}) |
| 223 |
} |
| 224 |
select { |
| 225 |
case <-time.After(5 * time.Second): |
| 226 |
c.Fatal("taking too long to get broadcast exchange") |
| 227 |
case exchg2 := <-sess2.SessionChannel(): |
| 228 |
c.Check(s.RevealBroadcastExchange(exchg2), DeepEquals, &broker.BroadcastExchange{ |
| 229 |
ChanId: store.SystemInternalChannelId, |
| 230 |
TopLevel: 1, |
| 231 |
Notifications: help.Ns(notification1), |
| 232 |
Decoded: []map[string]interface{}{decoded1}, |
| 233 |
}) |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
type testFailingStore struct { |
| 238 |
store.InMemoryPendingStore |
| 239 |
countdownToFail int |
| 240 |
} |
| 241 |
|
| 242 |
func (sto *testFailingStore) GetChannelSnapshot(chanId store.InternalChannelId) (int64, []protocol.Notification, error) { |
| 243 |
if sto.countdownToFail == 0 { |
| 244 |
return 0, nil, errors.New("get channel snapshot fail") |
| 245 |
} |
| 246 |
sto.countdownToFail-- |
| 247 |
return 0, nil, nil |
| 248 |
} |
| 249 |
|
| 250 |
func (sto *testFailingStore) DropByMsgId(chanId store.InternalChannelId, targets []protocol.Notification) error { |
| 251 |
return errors.New("drop fail") |
| 252 |
} |
| 253 |
|
| 254 |
func (s *CommonBrokerSuite) TestBroadcastFail(c *C) { |
| 255 |
logged := make(chan bool, 1) |
| 256 |
s.testlog.SetLogEventCb(func(string) { |
| 257 |
logged <- true |
| 258 |
}) |
| 259 |
sto := &testFailingStore{countdownToFail: 1} |
| 260 |
b := s.MakeBroker(sto, testBrokerConfig, s.testlog) |
| 261 |
b.Start() |
| 262 |
defer b.Stop() |
| 263 |
sess, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-1"}, s.MakeTracker("s1")) |
| 264 |
c.Assert(err, IsNil) |
| 265 |
clearOfPending(c, sess) |
| 266 |
b.Broadcast(store.SystemInternalChannelId) |
| 267 |
select { |
| 268 |
case <-time.After(5 * time.Second): |
| 269 |
c.Fatal("taking too long to log error") |
| 270 |
case <-logged: |
| 271 |
} |
| 272 |
c.Check(s.testlog.Captured(), Matches, "ERROR.*: get channel snapshot fail\n") |
| 273 |
} |
| 274 |
|
| 275 |
func (s *CommonBrokerSuite) TestUnicast(c *C) { |
| 276 |
sto := store.NewInMemoryPendingStore() |
| 277 |
notification1 := json.RawMessage(`{"m": "M1"}`) |
| 278 |
notification2 := json.RawMessage(`{"m": "M2"}`) |
| 279 |
chanId1 := store.UnicastInternalChannelId("dev1", "dev1") |
| 280 |
chanId2 := store.UnicastInternalChannelId("dev2", "dev2") |
| 281 |
b := s.MakeBroker(sto, testBrokerConfig, s.testlog) |
| 282 |
b.Start() |
| 283 |
defer b.Stop() |
| 284 |
sess1, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev1"}, s.MakeTracker("s1")) |
| 285 |
c.Assert(err, IsNil) |
| 286 |
clearOfPending(c, sess1) |
| 287 |
sess2, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev2"}, s.MakeTracker("s2")) |
| 288 |
c.Assert(err, IsNil) |
| 289 |
clearOfPending(c, sess2) |
| 290 |
// add notification to channel *after* the registrations |
| 291 |
muchLater := store.Metadata{Expiration: time.Now().Add(10 * time.Minute)} |
| 292 |
sto.AppendToUnicastChannel(chanId1, "app1", notification1, "msg1", muchLater) |
| 293 |
sto.AppendToUnicastChannel(chanId2, "app2", notification2, "msg2", muchLater) |
| 294 |
b.Unicast(chanId2, chanId1) |
| 295 |
select { |
| 296 |
case <-time.After(5 * time.Second): |
| 297 |
c.Fatal("taking too long to get unicast exchange") |
| 298 |
case exchg1 := <-sess1.SessionChannel(): |
| 299 |
u1 := s.RevealUnicastExchange(exchg1) |
| 300 |
c.Check(u1.ChanId, Equals, chanId1) |
| 301 |
} |
| 302 |
select { |
| 303 |
case <-time.After(5 * time.Second): |
| 304 |
c.Fatal("taking too long to get unicast exchange") |
| 305 |
case exchg2 := <-sess2.SessionChannel(): |
| 306 |
u2 := s.RevealUnicastExchange(exchg2) |
| 307 |
c.Check(u2.ChanId, Equals, chanId2) |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
func (s *CommonBrokerSuite) TestGetAndDrop(c *C) { |
| 312 |
sto := store.NewInMemoryPendingStore() |
| 313 |
notification1 := json.RawMessage(`{"m": "M1"}`) |
| 314 |
chanId1 := store.UnicastInternalChannelId("dev3", "dev3") |
| 315 |
b := s.MakeBroker(sto, testBrokerConfig, s.testlog) |
| 316 |
b.Start() |
| 317 |
defer b.Stop() |
| 318 |
sess1, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev3"}, s.MakeTracker("s1")) |
| 319 |
c.Assert(err, IsNil) |
| 320 |
muchLater := store.Metadata{Expiration: time.Now().Add(10 * time.Minute)} |
| 321 |
sto.AppendToUnicastChannel(chanId1, "app1", notification1, "msg1", muchLater) |
| 322 |
_, expected, err := sto.GetChannelSnapshot(chanId1) |
| 323 |
c.Assert(err, IsNil) |
| 324 |
_, notifs, err := sess1.Get(chanId1, false) |
| 325 |
c.Check(notifs, HasLen, 1) |
| 326 |
c.Check(notifs, DeepEquals, expected) |
| 327 |
err = sess1.DropByMsgId(chanId1, notifs) |
| 328 |
c.Assert(err, IsNil) |
| 329 |
_, notifs, err = sess1.Get(chanId1, true) |
| 330 |
c.Check(notifs, HasLen, 0) |
| 331 |
_, expected, err = sto.GetChannelSnapshot(chanId1) |
| 332 |
c.Assert(err, IsNil) |
| 333 |
c.Check(expected, HasLen, 0) |
| 334 |
|
| 335 |
} |
| 336 |
|
| 337 |
func (s *CommonBrokerSuite) TestGetAndDropErrors(c *C) { |
| 338 |
chanId1 := store.UnicastInternalChannelId("dev3", "dev3") |
| 339 |
sto := &testFailingStore{countdownToFail: 1} |
| 340 |
b := s.MakeBroker(sto, testBrokerConfig, s.testlog) |
| 341 |
b.Start() |
| 342 |
defer b.Stop() |
| 343 |
sess1, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev3"}, s.MakeTracker("s1")) |
| 344 |
c.Assert(err, IsNil) |
| 345 |
_, _, err = sess1.Get(chanId1, false) |
| 346 |
c.Assert(err, ErrorMatches, "get channel snapshot fail") |
| 347 |
c.Check(s.testlog.Captured(), Matches, "ERROR unsuccessful, get channel snapshot for Udev3:dev3 \\(cachedOk=false\\): get channel snapshot fail\n") |
| 348 |
s.testlog.ResetCapture() |
| 349 |
|
| 350 |
err = sess1.DropByMsgId(chanId1, nil) |
| 351 |
c.Assert(err, ErrorMatches, "drop fail") |
| 352 |
c.Check(s.testlog.Captured(), Matches, "ERROR unsuccessful, drop from channel Udev3:dev3: drop fail\n") |
| 353 |
} |
| 354 |
|
| 355 |
func (s *CommonBrokerSuite) TestSessionFeed(c *C) { |
| 356 |
sto := store.NewInMemoryPendingStore() |
| 357 |
b := s.MakeBroker(sto, testBrokerConfig, s.testlog) |
| 358 |
b.Start() |
| 359 |
defer b.Stop() |
| 360 |
sess1, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev3"}, s.MakeTracker("s1")) |
| 361 |
c.Assert(err, IsNil) |
| 362 |
clearOfPending(c, sess1) |
| 363 |
bcast := &broker.BroadcastExchange{ChanId: store.SystemInternalChannelId, TopLevel: 99} |
| 364 |
sess1.Feed(bcast) |
| 365 |
c.Check(s.RevealBroadcastExchange(<-sess1.SessionChannel()), DeepEquals, bcast) |
| 366 |
|
| 367 |
ucast := &broker.UnicastExchange{ChanId: store.UnicastInternalChannelId("dev21", "dev21"), CachedOk: true} |
| 368 |
sess1.Feed(ucast) |
| 369 |
c.Check(s.RevealUnicastExchange(<-sess1.SessionChannel()), DeepEquals, ucast) |
| 370 |
} |