Debian Codegraph @

server/store/inmemory_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 store
18
19 import (
20 "encoding/json"
21 "time"
22
23 . "gopkg.in/check.v1"
24
25 "gitlab.com/ubports/development/core/lomiri-push-service/protocol"
26 help "gitlab.com/ubports/development/core/lomiri-push-service/testing"
27 )
28
29 type inMemorySuite struct{}
30
31 var _ = Suite(&inMemorySuite{})
32
33 func (s *inMemorySuite) TestRegister(c *C) {
34 sto := NewInMemoryPendingStore()
35
36 tok1, err := sto.Register("DEV1", "app1")
37 c.Assert(err, IsNil)
38 tok2, err := sto.Register("DEV1", "app1")
39 c.Assert(err, IsNil)
40 c.Check(len(tok1), Not(Equals), 0)
41 c.Check(tok1, Equals, tok2)
42 }
43
44 func (s *inMemorySuite) TestUnregister(c *C) {
45 sto := NewInMemoryPendingStore()
46
47 err := sto.Unregister("DEV1", "app1")
48 c.Assert(err, IsNil)
49 }
50
51 func (s *inMemorySuite) TestGetInternalChannelIdFromToken(c *C) {
52 sto := NewInMemoryPendingStore()
53
54 tok1, err := sto.Register("DEV1", "app1")
55 c.Assert(err, IsNil)
56 chanId, err := sto.GetInternalChannelIdFromToken(tok1, "app1", "", "")
57 c.Assert(err, IsNil)
58 c.Check(chanId, Equals, UnicastInternalChannelId("DEV1", "DEV1"))
59 }
60
61 func (s *inMemorySuite) TestGetInternalChannelIdFromTokenFallback(c *C) {
62 sto := NewInMemoryPendingStore()
63
64 chanId, err := sto.GetInternalChannelIdFromToken("", "app1", "u1", "d1")
65 c.Assert(err, IsNil)
66 c.Check(chanId, Equals, UnicastInternalChannelId("u1", "d1"))
67 }
68
69 func (s *inMemorySuite) TestGetInternalChannelIdFromTokenErrors(c *C) {
70 sto := NewInMemoryPendingStore()
71 tok1, err := sto.Register("DEV1", "app1")
72 c.Assert(err, IsNil)
73
74 _, err = sto.GetInternalChannelIdFromToken(tok1, "app2", "", "")
75 c.Assert(err, Equals, ErrUnauthorized)
76
77 _, err = sto.GetInternalChannelIdFromToken("", "app2", "", "")
78 c.Assert(err, Equals, ErrUnknownToken)
79
80 _, err = sto.GetInternalChannelIdFromToken("****", "app2", "", "")
81 c.Assert(err, Equals, ErrUnknownToken)
82 }
83
84 func (s *inMemorySuite) TestGetInternalChannelId(c *C) {
85 sto := NewInMemoryPendingStore()
86
87 chanId, err := sto.GetInternalChannelId("system")
88 c.Check(err, IsNil)
89 c.Check(chanId, Equals, SystemInternalChannelId)
90
91 chanId, err = sto.GetInternalChannelId("blah")
92 c.Check(err, Equals, ErrUnknownChannel)
93 c.Check(chanId, Equals, InternalChannelId(""))
94 }
95
96 func (s *inMemorySuite) TestGetChannelSnapshotEmpty(c *C) {
97 sto := NewInMemoryPendingStore()
98
99 top, res, err := sto.GetChannelSnapshot(SystemInternalChannelId)
100 c.Assert(err, IsNil)
101 c.Check(top, Equals, int64(0))
102 c.Check(res, DeepEquals, []protocol.Notification(nil))
103 }
104
105 func (s *inMemorySuite) TestGetChannelUnfilteredEmpty(c *C) {
106 sto := NewInMemoryPendingStore()
107
108 top, res, meta, err := sto.GetChannelUnfiltered(SystemInternalChannelId)
109 c.Assert(err, IsNil)
110 c.Check(top, Equals, int64(0))
111 c.Check(res, DeepEquals, []protocol.Notification(nil))
112 c.Check(meta, DeepEquals, []Metadata(nil))
113 }
114
115 func (s *inMemorySuite) TestAppendToChannelAndGetChannelSnapshot(c *C) {
116 sto := NewInMemoryPendingStore()
117
118 notification1 := json.RawMessage(`{"a":1}`)
119 notification2 := json.RawMessage(`{"a":2}`)
120
121 muchLater := time.Now().Add(time.Minute)
122
123 sto.AppendToChannel(SystemInternalChannelId, notification1, muchLater)
124 sto.AppendToChannel(SystemInternalChannelId, notification2, muchLater)
125 top, res, err := sto.GetChannelSnapshot(SystemInternalChannelId)
126 c.Assert(err, IsNil)
127 c.Check(top, Equals, int64(2))
128 c.Check(res, DeepEquals, help.Ns(notification1, notification2))
129 }
130
131 func (s *inMemorySuite) TestAppendToUnicastChannelAndGetChannelSnapshot(c *C) {
132 sto := NewInMemoryPendingStore()
133
134 chanId := UnicastInternalChannelId("user", "dev1")
135 notification1 := json.RawMessage(`{"a":1}`)
136 notification2 := json.RawMessage(`{"b":2}`)
137
138 muchLater := Metadata{Expiration: time.Now().Add(time.Minute)}
139
140 err := sto.AppendToUnicastChannel(chanId, "app1", notification1, "m1", muchLater)
141 c.Assert(err, IsNil)
142 err = sto.AppendToUnicastChannel(chanId, "app2", notification2, "m2", muchLater)
143 c.Assert(err, IsNil)
144 top, res, err := sto.GetChannelSnapshot(chanId)
145 c.Assert(err, IsNil)
146 c.Check(res, DeepEquals, []protocol.Notification{
147 protocol.Notification{Payload: notification1, AppId: "app1", MsgId: "m1"},
148 protocol.Notification{Payload: notification2, AppId: "app2", MsgId: "m2"},
149 })
150 c.Check(top, Equals, int64(0))
151 }
152
153 func (s *inMemorySuite) TestAppendToChannelAndGetChannelUnfiltered(c *C) {
154 sto := NewInMemoryPendingStore()
155
156 notification1 := json.RawMessage(`{"a":1}`)
157 notification2 := json.RawMessage(`{"a":2}`)
158
159 gone := time.Now().Add(-1 * time.Minute)
160 muchLater := time.Now().Add(time.Minute)
161
162 sto.AppendToChannel(SystemInternalChannelId, notification1, muchLater)
163 sto.AppendToChannel(SystemInternalChannelId, notification2, gone)
164
165 top, res, meta, err := sto.GetChannelUnfiltered(SystemInternalChannelId)
166 c.Assert(err, IsNil)
167 c.Check(top, Equals, int64(2))
168 c.Check(res, DeepEquals, help.Ns(notification1, notification2))
169 c.Check(meta, DeepEquals, []Metadata{
170 Metadata{Expiration: muchLater},
171 Metadata{Expiration: gone},
172 })
173 }
174
175 func (s *inMemorySuite) TestAppendToUnicastChannelReplaceTagAndGetChannelUnfiltered(c *C) {
176 sto := NewInMemoryPendingStore()
177
178 chanId := UnicastInternalChannelId("user", "dev1")
179 notification1 := json.RawMessage(`{"a":1}`)
180 notification2 := json.RawMessage(`{"a":2}`)
181
182 meta1 := Metadata{Expiration: time.Now().Add(2 * time.Minute)}
183 meta2 := Metadata{
184 Expiration: time.Now().Add(3 * time.Minute),
185 ReplaceTag: "u1",
186 }
187
188 sto.AppendToUnicastChannel(chanId, "app1", notification1, "m1", meta1)
189 sto.AppendToUnicastChannel(chanId, "app2", notification2, "m2", meta2)
190
191 top, res, meta, err := sto.GetChannelUnfiltered(chanId)
192 c.Assert(err, IsNil)
193 c.Check(top, Equals, int64(0))
194 c.Check(res, DeepEquals, []protocol.Notification{
195 protocol.Notification{Payload: notification1, AppId: "app1", MsgId: "m1"},
196 protocol.Notification{Payload: notification2, AppId: "app2", MsgId: "m2"},
197 })
198 c.Check(meta, DeepEquals, []Metadata{meta1, meta2})
199 }
200
201 func (s *inMemorySuite) TestAppendToChannelAndGetChannelSnapshotWithExpiration(c *C) {
202 sto := NewInMemoryPendingStore()
203
204 notification1 := json.RawMessage(`{"a":1}`)
205 notification2 := json.RawMessage(`{"a":2}`)
206
207 gone := time.Now().Add(-1 * time.Minute)
208 muchLater := time.Now().Add(time.Minute)
209
210 sto.AppendToChannel(SystemInternalChannelId, notification1, muchLater)
211 sto.AppendToChannel(SystemInternalChannelId, notification2, gone)
212
213 top, res, err := sto.GetChannelSnapshot(SystemInternalChannelId)
214 c.Assert(err, IsNil)
215 c.Check(top, Equals, int64(2))
216 c.Check(res, DeepEquals, help.Ns(notification1))
217 }
218
219 func (s *inMemorySuite) TestAppendToUnicastChannelAndGetChannelSnapshotWithExpirationAndCoalescing(c *C) {
220 sto := NewInMemoryPendingStore()
221
222 chanId := UnicastInternalChannelId("user", "dev1")
223 notification1 := json.RawMessage(`{"a":1}`)
224 notification2 := json.RawMessage(`{"a":2}`)
225 notification3 := json.RawMessage(`{"a":4}`)
226 notification4 := json.RawMessage(`{"a":4}`)
227
228 meta1 := Metadata{
229 Expiration: time.Now().Add(1 * time.Minute),
230 ReplaceTag: "u1",
231 }
232 meta2 := Metadata{Expiration: time.Now().Add(-1 * time.Minute)}
233 meta3 := Metadata{
234 Expiration: time.Now().Add(1 * time.Minute),
235 ReplaceTag: "u1",
236 }
237 meta4 := Metadata{Expiration: time.Now().Add(1 * time.Minute)}
238
239 err := sto.AppendToUnicastChannel(chanId, "app1", notification1, "m1", meta1)
240 c.Assert(err, IsNil)
241 err = sto.AppendToUnicastChannel(chanId, "app1", notification2, "m2", meta2)
242 c.Assert(err, IsNil)
243 err = sto.AppendToUnicastChannel(chanId, "app1", notification3, "m3", meta3)
244 c.Assert(err, IsNil)
245 err = sto.AppendToUnicastChannel(chanId, "app1", notification4, "m4", meta4)
246 c.Assert(err, IsNil)
247
248 top, res, err := sto.GetChannelSnapshot(chanId)
249 c.Assert(err, IsNil)
250 c.Check(top, Equals, int64(0))
251 c.Check(res, DeepEquals, []protocol.Notification{
252 protocol.Notification{Payload: notification3, AppId: "app1", MsgId: "m3"},
253 protocol.Notification{Payload: notification4, AppId: "app1", MsgId: "m4"},
254 })
255 }
256
257 func (s *inMemorySuite) TestScrubNop(c *C) {
258 sto := NewInMemoryPendingStore()
259
260 chanId := UnicastInternalChannelId("user", "dev1")
261
262 err := sto.Scrub(chanId)
263 c.Assert(err, IsNil)
264 }
265
266 func (s *inMemorySuite) TestScrubMax2Criteria(c *C) {
267 sto := NewInMemoryPendingStore()
268
269 chanId := UnicastInternalChannelId("user", "dev1")
270
271 c.Check(func() { sto.Scrub(chanId, "a", "b", "c") }, PanicMatches, `Scrub\(\) expects only up to two criterias`)
272 }
273
274 func (s *inMemorySuite) TestScrubOnlyExpired(c *C) {
275 sto := NewInMemoryPendingStore()
276
277 chanId := UnicastInternalChannelId("user", "dev1")
278
279 notification1 := json.RawMessage(`{"a":1}`)
280 notification2 := json.RawMessage(`{"b":2}`)
281 notification3 := json.RawMessage(`{"c":3}`)
282 notification4 := json.RawMessage(`{"d":4}`)
283
284 gone := Metadata{Expiration: time.Now().Add(-1 * time.Minute)}
285 muchLater1 := Metadata{Expiration: time.Now().Add(4 * time.Minute)}
286 muchLater2 := Metadata{Expiration: time.Now().Add(5 * time.Minute)}
287
288 err := sto.AppendToUnicastChannel(chanId, "app1", notification1, "m1", muchLater1)
289 c.Assert(err, IsNil)
290 err = sto.AppendToUnicastChannel(chanId, "app2", notification2, "m2", gone)
291 c.Assert(err, IsNil)
292 err = sto.AppendToUnicastChannel(chanId, "app1", notification3, "m3", gone)
293 c.Assert(err, IsNil)
294 err = sto.AppendToUnicastChannel(chanId, "app2", notification4, "m4", muchLater2)
295 c.Assert(err, IsNil)
296
297 err = sto.Scrub(chanId)
298 c.Assert(err, IsNil)
299
300 top, res, meta, err := sto.GetChannelUnfiltered(chanId)
301 c.Assert(err, IsNil)
302 c.Check(top, Equals, int64(0))
303 c.Check(res, DeepEquals, []protocol.Notification{
304 protocol.Notification{Payload: notification1, AppId: "app1", MsgId: "m1"},
305 protocol.Notification{Payload: notification4, AppId: "app2", MsgId: "m4"},
306 })
307 c.Check(meta, DeepEquals, []Metadata{muchLater1, muchLater2})
308 }
309
310 func (s *inMemorySuite) TestScrubApp(c *C) {
311 sto := NewInMemoryPendingStore()
312
313 chanId := UnicastInternalChannelId("user", "dev1")
314
315 notification1 := json.RawMessage(`{"a":1}`)
316 notification2 := json.RawMessage(`{"b":2}`)
317 notification3 := json.RawMessage(`{"c":3}`)
318 notification4 := json.RawMessage(`{"d":4}`)
319
320 gone := Metadata{Expiration: time.Now().Add(-1 * time.Minute)}
321 muchLater := Metadata{Expiration: time.Now().Add(time.Minute)}
322
323 err := sto.AppendToUnicastChannel(chanId, "app1", notification1, "m1", muchLater)
324 c.Assert(err, IsNil)
325 err = sto.AppendToUnicastChannel(chanId, "app2", notification2, "m2", gone)
326 c.Assert(err, IsNil)
327 err = sto.AppendToUnicastChannel(chanId, "app1", notification3, "m3", muchLater)
328 c.Assert(err, IsNil)
329 err = sto.AppendToUnicastChannel(chanId, "app2", notification4, "m4", muchLater)
330 c.Assert(err, IsNil)
331
332 err = sto.Scrub(chanId, "app1")
333 c.Assert(err, IsNil)
334
335 top, res, meta, err := sto.GetChannelUnfiltered(chanId)
336 c.Assert(err, IsNil)
337 c.Check(top, Equals, int64(0))
338 c.Check(res, DeepEquals, []protocol.Notification{
339 protocol.Notification{Payload: notification4, AppId: "app2", MsgId: "m4"},
340 })
341 c.Check(meta, DeepEquals, []Metadata{muchLater})
342 }
343
344 func (s *inMemorySuite) TestScrubReplaceTag(c *C) {
345 sto := NewInMemoryPendingStore()
346
347 chanId := UnicastInternalChannelId("user", "dev1")
348 notification1 := json.RawMessage(`{"a":1}`)
349 notification2 := json.RawMessage(`{"a":2}`)
350 notification3 := json.RawMessage(`{"a":4}`)
351 notification4 := json.RawMessage(`{"a":4}`)
352
353 meta1 := Metadata{
354 Expiration: time.Now().Add(1 * time.Minute),
355 ReplaceTag: "u1",
356 }
357 meta2 := Metadata{Expiration: time.Now().Add(-1 * time.Minute)}
358 meta3 := Metadata{
359 Expiration: time.Now().Add(1 * time.Minute),
360 ReplaceTag: "u1",
361 }
362 meta4 := Metadata{
363 Expiration: time.Now().Add(1 * time.Minute),
364 ReplaceTag: "u2",
365 }
366
367 err := sto.AppendToUnicastChannel(chanId, "app1", notification1, "m1", meta1)
368 c.Assert(err, IsNil)
369 err = sto.AppendToUnicastChannel(chanId, "app1", notification2, "m2", meta2)
370 c.Assert(err, IsNil)
371 err = sto.AppendToUnicastChannel(chanId, "app1", notification3, "m3", meta3)
372 c.Assert(err, IsNil)
373 err = sto.AppendToUnicastChannel(chanId, "app1", notification4, "m4", meta4)
374 c.Assert(err, IsNil)
375
376 err = sto.Scrub(chanId, "app1", "u1")
377 c.Assert(err, IsNil)
378
379 top, res, meta, err := sto.GetChannelUnfiltered(chanId)
380 c.Assert(err, IsNil)
381 c.Check(top, Equals, int64(0))
382 c.Check(res, DeepEquals, []protocol.Notification{
383 protocol.Notification{Payload: notification4, AppId: "app1", MsgId: "m4"},
384 })
385 c.Check(meta, DeepEquals, []Metadata{meta4})
386 }
387
388 func (s *inMemorySuite) TestDropByMsgId(c *C) {
389 sto := NewInMemoryPendingStore()
390
391 chanId := UnicastInternalChannelId("user", "dev2")
392
393 // nothing to do is fine
394 err := sto.DropByMsgId(chanId, nil)
395 c.Assert(err, IsNil)
396
397 notification1 := json.RawMessage(`{"a":1}`)
398 notification2 := json.RawMessage(`{"b":2}`)
399 notification3 := json.RawMessage(`{"a":2}`)
400
401 muchLater := Metadata{Expiration: time.Now().Add(time.Minute)}
402
403 err = sto.AppendToUnicastChannel(chanId, "app1", notification1, "m1", muchLater)
404 c.Assert(err, IsNil)
405 err = sto.AppendToUnicastChannel(chanId, "app2", notification2, "m2", muchLater)
406 c.Assert(err, IsNil)
407 err = sto.AppendToUnicastChannel(chanId, "app1", notification3, "m3", muchLater)
408 c.Assert(err, IsNil)
409 _, res, err := sto.GetChannelSnapshot(chanId)
410 c.Assert(err, IsNil)
411
412 err = sto.DropByMsgId(chanId, res[:2])
413 c.Assert(err, IsNil)
414
415 _, res, err = sto.GetChannelSnapshot(chanId)
416 c.Assert(err, IsNil)
417 c.Check(res, HasLen, 1)
418 c.Check(res, DeepEquals, []protocol.Notification{
419 protocol.Notification{Payload: notification3, AppId: "app1", MsgId: "m3"},
420 })
421 }