Debian Codegraph @

server/acceptance/suites/broadcast.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 suites
18
19 import (
20 "encoding/json"
21 "fmt"
22 "strings"
23 "time"
24
25 . "gopkg.in/check.v1"
26
27 "gitlab.com/ubports/development/core/lomiri-push-service/client/gethosts"
28 "gitlab.com/ubports/development/core/lomiri-push-service/protocol"
29 "gitlab.com/ubports/development/core/lomiri-push-service/server/api"
30 )
31
32 // BroadcastAcceptanceSuite has tests about broadcast.
33 type BroadcastAcceptanceSuite struct {
34 AcceptanceSuite
35 }
36
37 func (s *BroadcastAcceptanceSuite) TestBroadcastToConnected(c *C) {
38 events, errCh, stop := s.StartClient(c, "DEVB", nil)
39 got, err := s.PostRequest("/broadcast", &api.Broadcast{
40 Channel: "system",
41 ExpireOn: future,
42 Data: json.RawMessage(`{"img1/m1": 42}`),
43 })
44 c.Assert(err, IsNil, Commentf("%v", got))
45 c.Check(NextEvent(events, errCh), Equals, `broadcast chan:0 app: topLevel:1 payloads:[{"img1/m1":42}]`)
46 stop()
47 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
48 c.Check(len(errCh), Equals, 0)
49 }
50
51 func (s *BroadcastAcceptanceSuite) TestBroadcastToConnectedChannelFilter(c *C) {
52 events, errCh, stop := s.StartClient(c, "DEVB", nil)
53 got, err := s.PostRequest("/broadcast", &api.Broadcast{
54 Channel: "system",
55 ExpireOn: future,
56 Data: json.RawMessage(`{"img1/m2": 10}`),
57 })
58 c.Assert(err, IsNil, Commentf("%v", got))
59 got, err = s.PostRequest("/broadcast", &api.Broadcast{
60 Channel: "system",
61 ExpireOn: future,
62 Data: json.RawMessage(`{"img1/m1": 20}`),
63 })
64 c.Assert(err, IsNil, Commentf("%v", got))
65 c.Check(NextEvent(events, errCh), Equals, `broadcast chan:0 app: topLevel:2 payloads:[{"img1/m1":20}]`)
66 stop()
67 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
68 c.Check(len(errCh), Equals, 0)
69 }
70
71 func (s *BroadcastAcceptanceSuite) TestBroadcastPending(c *C) {
72 // send broadcast that will be pending
73 got, err := s.PostRequest("/broadcast", &api.Broadcast{
74 Channel: "system",
75 ExpireOn: future,
76 Data: json.RawMessage(`{"img1/m1": 1}`),
77 })
78 c.Assert(err, IsNil, Commentf("%v", got))
79
80 events, errCh, stop := s.StartClient(c, "DEVB", nil)
81 // gettting pending on connect
82 c.Check(NextEvent(events, errCh), Equals, `broadcast chan:0 app: topLevel:1 payloads:[{"img1/m1":1}]`)
83 stop()
84 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
85 c.Check(len(errCh), Equals, 0)
86 }
87
88 func (s *BroadcastAcceptanceSuite) TestBroadcastLargeNeedsSplitting(c *C) {
89 // send bunch of broadcasts that will be pending
90 payloadFmt := fmt.Sprintf(`{"img1/m1":%%d,"bloat":"%s"}`, strings.Repeat("x", 1024*2))
91 for i := 0; i < 32; i++ {
92 got, err := s.PostRequest("/broadcast", &api.Broadcast{
93 Channel: "system",
94 ExpireOn: future,
95 Data: json.RawMessage(fmt.Sprintf(payloadFmt, i)),
96 })
97 c.Assert(err, IsNil, Commentf("%v", got))
98 }
99
100 events, errCh, stop := s.StartClient(c, "DEVC", nil)
101 // gettting pending on connect
102 n := 0
103 for {
104 evt := NextEvent(events, errCh)
105 c.Check(evt, Matches, "broadcast chan:0 .*")
106 n += 1
107 if strings.Contains(evt, "topLevel:32") {
108 break
109 }
110 }
111 // was split
112 c.Check(n > 1, Equals, true)
113 stop()
114 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
115 c.Check(len(errCh), Equals, 0)
116 }
117
118 func (s *BroadcastAcceptanceSuite) TestBroadcastDistribution2(c *C) {
119 // start 1st client
120 events1, errCh1, stop1 := s.StartClient(c, "DEV1", nil)
121 // start 2nd client
122 events2, errCh2, stop2 := s.StartClient(c, "DEV2", nil)
123 // broadcast
124 got, err := s.PostRequest("/broadcast", &api.Broadcast{
125 Channel: "system",
126 ExpireOn: future,
127 Data: json.RawMessage(`{"img1/m1": 42}`),
128 })
129 c.Assert(err, IsNil, Commentf("%v", got))
130 c.Check(NextEvent(events1, errCh1), Equals, `broadcast chan:0 app: topLevel:1 payloads:[{"img1/m1":42}]`)
131 c.Check(NextEvent(events2, errCh2), Equals, `broadcast chan:0 app: topLevel:1 payloads:[{"img1/m1":42}]`)
132 stop1()
133 stop2()
134 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
135 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
136 c.Check(len(errCh1), Equals, 0)
137 c.Check(len(errCh2), Equals, 0)
138 }
139
140 func (s *BroadcastAcceptanceSuite) TestBroadcastFilterByLevel(c *C) {
141 events, errCh, stop := s.StartClient(c, "DEVD", nil)
142 got, err := s.PostRequest("/broadcast", &api.Broadcast{
143 Channel: "system",
144 ExpireOn: future,
145 Data: json.RawMessage(`{"img1/m1": 1}`),
146 })
147 c.Assert(err, IsNil, Commentf("%v", got))
148 c.Check(NextEvent(events, errCh), Equals, `broadcast chan:0 app: topLevel:1 payloads:[{"img1/m1":1}]`)
149 stop()
150 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
151 c.Check(len(errCh), Equals, 0)
152 // another broadcast
153 got, err = s.PostRequest("/broadcast", &api.Broadcast{
154 Channel: "system",
155 ExpireOn: future,
156 Data: json.RawMessage(`{"img1/m1": 2}`),
157 })
158 c.Assert(err, IsNil, Commentf("%v", got))
159 // reconnect, provide levels, get only later notification
160 events, errCh, stop = s.StartClient(c, "DEVD", map[string]int64{
161 protocol.SystemChannelId: 1,
162 })
163 c.Check(NextEvent(events, errCh), Equals, `broadcast chan:0 app: topLevel:2 payloads:[{"img1/m1":2}]`)
164 stop()
165 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
166 c.Check(len(errCh), Equals, 0)
167 }
168
169 func (s *BroadcastAcceptanceSuite) TestBroadcastTooAhead(c *C) {
170 // send broadcasts that will be pending
171 got, err := s.PostRequest("/broadcast", &api.Broadcast{
172 Channel: "system",
173 ExpireOn: future,
174 Data: json.RawMessage(`{"img1/m1": 1}`),
175 })
176 c.Assert(err, IsNil, Commentf("%v", got))
177 got, err = s.PostRequest("/broadcast", &api.Broadcast{
178 Channel: "system",
179 ExpireOn: future,
180 Data: json.RawMessage(`{"img1/m1": 2}`),
181 })
182 c.Assert(err, IsNil, Commentf("%v", got))
183
184 events, errCh, stop := s.StartClient(c, "DEVB", map[string]int64{
185 protocol.SystemChannelId: 10,
186 })
187 // gettting last one pending on connect
188 c.Check(NextEvent(events, errCh), Equals, `broadcast chan:0 app: topLevel:2 payloads:[{"img1/m1":2}]`)
189 stop()
190 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
191 c.Check(len(errCh), Equals, 0)
192 }
193
194 func (s *BroadcastAcceptanceSuite) TestBroadcastTooAheadOnEmpty(c *C) {
195 // nothing there
196 events, errCh, stop := s.StartClient(c, "DEVB", map[string]int64{
197 protocol.SystemChannelId: 10,
198 })
199 // gettting empty pending on connect
200 c.Check(NextEvent(events, errCh), Equals, `broadcast chan:0 app: topLevel:0 payloads:null`)
201 stop()
202 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
203 c.Check(len(errCh), Equals, 0)
204 }
205
206 func (s *BroadcastAcceptanceSuite) TestBroadcastWayBehind(c *C) {
207 // send broadcasts that will be pending
208 got, err := s.PostRequest("/broadcast", &api.Broadcast{
209 Channel: "system",
210 ExpireOn: future,
211 Data: json.RawMessage(`{"img1/m1": 1}`),
212 })
213 c.Assert(err, IsNil, Commentf("%v", got))
214 got, err = s.PostRequest("/broadcast", &api.Broadcast{
215 Channel: "system",
216 ExpireOn: future,
217 Data: json.RawMessage(`{"img1/m1": 2}`),
218 })
219 c.Assert(err, IsNil, Commentf("%v", got))
220
221 events, errCh, stop := s.StartClient(c, "DEVB", map[string]int64{
222 protocol.SystemChannelId: -10,
223 })
224 // gettting pending on connect
225 c.Check(NextEvent(events, errCh), Equals, `broadcast chan:0 app: topLevel:2 payloads:[{"img1/m1":1},{"img1/m1":2}]`)
226 stop()
227 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
228 c.Check(len(errCh), Equals, 0)
229 }
230
231 func (s *BroadcastAcceptanceSuite) TestBroadcastExpiration(c *C) {
232 // send broadcast that will be pending, and one that will expire
233 got, err := s.PostRequest("/broadcast", &api.Broadcast{
234 Channel: "system",
235 ExpireOn: future,
236 Data: json.RawMessage(`{"img1/m1": 1}`),
237 })
238 c.Assert(err, IsNil, Commentf("%v", got))
239 got, err = s.PostRequest("/broadcast", &api.Broadcast{
240 Channel: "system",
241 ExpireOn: time.Now().Add(1 * time.Second).Format(time.RFC3339),
242 Data: json.RawMessage(`{"img1/m1": 2}`),
243 })
244 c.Assert(err, IsNil, Commentf("%v", got))
245
246 time.Sleep(2 * time.Second)
247 // second broadcast is expired
248
249 events, errCh, stop := s.StartClient(c, "DEVB", nil)
250 // gettting pending on connect
251 c.Check(NextEvent(events, errCh), Equals, `broadcast chan:0 app: topLevel:2 payloads:[{"img1/m1":1}]`)
252 stop()
253 c.Assert(NextEvent(s.ServerEvents, nil), Matches, `.* ended with:.*EOF`)
254 c.Check(len(errCh), Equals, 0)
255 }
256
257 // test /delivery-hosts
258
259 func (s *BroadcastAcceptanceSuite) TestGetHosts(c *C) {
260 gh := gethosts.New("", s.ServerAPIURL+"/delivery-hosts", 2*time.Second)
261 host, err := gh.Get()
262 c.Assert(err, IsNil)
263 expected := &gethosts.Host{
264 Domain: "push-delivery",
265 Hosts: []string{s.ServerAddr},
266 }
267 c.Check(host, DeepEquals, expected)
268 }