-
Notifications
You must be signed in to change notification settings - Fork 1
/
suite.go
212 lines (192 loc) · 7.08 KB
/
suite.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
/*
Copyright 2013-2014 Canonical Ltd.
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Package testsuite contains a common test suite for brokers.
package testsuite
import (
"encoding/json"
"errors"
. "launchpad.net/gocheck"
"launchpad.net/ubuntu-push/logger"
"launchpad.net/ubuntu-push/protocol"
"launchpad.net/ubuntu-push/server/broker"
"launchpad.net/ubuntu-push/server/broker/testing"
"launchpad.net/ubuntu-push/server/store"
helpers "launchpad.net/ubuntu-push/testing"
// "log"
"time"
)
// The expected interface for tested brokers.
type FullBroker interface {
broker.Broker
broker.BrokerSending
Start()
Stop()
Running() bool
}
// The common brokers' test suite.
type CommonBrokerSuite struct {
// Build the broker for testing.
MakeBroker func(store.PendingStore, broker.BrokerConfig, logger.Logger) FullBroker
// Let us get to a session under the broker.
RevealSession func(broker.Broker, string) broker.BrokerSession
// Let us get to a broker.BroadcastExchange from an Exchange.
RevealBroadcastExchange func(broker.Exchange) *broker.BroadcastExchange
// private
testlog *helpers.TestLogger
}
func (s *CommonBrokerSuite) SetUpTest(c *C) {
s.testlog = helpers.NewTestLogger(c, "error")
}
var testBrokerConfig = &testing.TestBrokerConfig{10, 5}
func (s *CommonBrokerSuite) TestSanity(c *C) {
sto := store.NewInMemoryPendingStore()
b := s.MakeBroker(sto, testBrokerConfig, nil)
c.Check(s.RevealSession(b, "FOO"), IsNil)
}
func (s *CommonBrokerSuite) TestStartStop(c *C) {
b := s.MakeBroker(nil, testBrokerConfig, nil)
b.Start()
c.Check(b.Running(), Equals, true)
b.Start()
b.Stop()
c.Check(b.Running(), Equals, false)
b.Stop()
}
func (s *CommonBrokerSuite) TestRegistration(c *C) {
sto := store.NewInMemoryPendingStore()
b := s.MakeBroker(sto, testBrokerConfig, nil)
b.Start()
defer b.Stop()
sess, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-1", Levels: map[string]int64{"0": 5}})
c.Assert(err, IsNil)
c.Assert(s.RevealSession(b, "dev-1"), Equals, sess)
c.Assert(sess.DeviceIdentifier(), Equals, "dev-1")
c.Assert(sess.ExchangeScratchArea(), Not(IsNil))
c.Check(sess.Levels(), DeepEquals, broker.LevelsMap(map[store.InternalChannelId]int64{
store.SystemInternalChannelId: 5,
}))
b.Unregister(sess)
// just to make sure the unregister was processed
_, err = b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: ""})
c.Assert(err, IsNil)
c.Check(s.RevealSession(b, "dev-1"), IsNil)
}
func (s *CommonBrokerSuite) TestRegistrationBrokenLevels(c *C) {
sto := store.NewInMemoryPendingStore()
b := s.MakeBroker(sto, testBrokerConfig, nil)
b.Start()
defer b.Stop()
_, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-1", Levels: map[string]int64{"z": 5}})
c.Check(err, FitsTypeOf, &broker.ErrAbort{})
}
func (s *CommonBrokerSuite) TestRegistrationFeedPending(c *C) {
sto := store.NewInMemoryPendingStore()
notification1 := json.RawMessage(`{"m": "M"}`)
sto.AppendToChannel(store.SystemInternalChannelId, notification1)
b := s.MakeBroker(sto, testBrokerConfig, nil)
b.Start()
defer b.Stop()
sess, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-1"})
c.Assert(err, IsNil)
c.Check(len(sess.SessionChannel()), Equals, 1)
}
func (s *CommonBrokerSuite) TestRegistrationFeedPendingError(c *C) {
sto := &testFailingStore{}
b := s.MakeBroker(sto, testBrokerConfig, s.testlog)
b.Start()
defer b.Stop()
_, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-1"})
c.Assert(err, IsNil)
// but
c.Check(s.testlog.Captured(), Matches, "ERROR unsuccessful feed pending, get channel snapshot for 0: get channel snapshot fail\n")
}
func (s *CommonBrokerSuite) TestRegistrationLastWins(c *C) {
sto := store.NewInMemoryPendingStore()
b := s.MakeBroker(sto, testBrokerConfig, nil)
b.Start()
defer b.Stop()
sess1, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-1"})
c.Assert(err, IsNil)
sess2, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-1"})
c.Assert(err, IsNil)
c.Assert(s.RevealSession(b, "dev-1"), Equals, sess2)
b.Unregister(sess1)
// just to make sure the unregister was processed
_, err = b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: ""})
c.Assert(err, IsNil)
c.Check(s.RevealSession(b, "dev-1"), Equals, sess2)
}
func (s *CommonBrokerSuite) TestBroadcast(c *C) {
sto := store.NewInMemoryPendingStore()
notification1 := json.RawMessage(`{"m": "M"}`)
b := s.MakeBroker(sto, testBrokerConfig, nil)
b.Start()
defer b.Stop()
sess1, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-1"})
c.Assert(err, IsNil)
sess2, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-2"})
c.Assert(err, IsNil)
// add notification to channel *after* the registrations
sto.AppendToChannel(store.SystemInternalChannelId, notification1)
b.Broadcast(store.SystemInternalChannelId)
select {
case <-time.After(5 * time.Second):
c.Fatal("taking too long to get broadcast exchange")
case exchg1 := <-sess1.SessionChannel():
c.Check(s.RevealBroadcastExchange(exchg1), DeepEquals, &broker.BroadcastExchange{
ChanId: store.SystemInternalChannelId,
TopLevel: 1,
NotificationPayloads: []json.RawMessage{notification1},
})
}
select {
case <-time.After(5 * time.Second):
c.Fatal("taking too long to get broadcast exchange")
case exchg2 := <-sess2.SessionChannel():
c.Check(s.RevealBroadcastExchange(exchg2), DeepEquals, &broker.BroadcastExchange{
ChanId: store.SystemInternalChannelId,
TopLevel: 1,
NotificationPayloads: []json.RawMessage{notification1},
})
}
}
type testFailingStore struct {
store.InMemoryPendingStore
countdownToFail int
}
func (sto *testFailingStore) GetChannelSnapshot(chanId store.InternalChannelId) (int64, []json.RawMessage, error) {
if sto.countdownToFail == 0 {
return 0, nil, errors.New("get channel snapshot fail")
}
sto.countdownToFail--
return 0, nil, nil
}
func (s *CommonBrokerSuite) TestBroadcastFail(c *C) {
logged := make(chan bool, 1)
s.testlog.SetLogEventCb(func(string) {
logged <- true
})
sto := &testFailingStore{countdownToFail: 1}
b := s.MakeBroker(sto, testBrokerConfig, s.testlog)
b.Start()
defer b.Stop()
_, err := b.Register(&protocol.ConnectMsg{Type: "connect", DeviceId: "dev-1"})
c.Assert(err, IsNil)
b.Broadcast(store.SystemInternalChannelId)
select {
case <-time.After(5 * time.Second):
c.Fatal("taking too long to log error")
case <-logged:
}
c.Check(s.testlog.Captured(), Matches, "ERROR unsuccessful broadcast, get channel snapshot for 0: get channel snapshot fail\n")
}