UNPKG

2.67 kBJavaScriptView Raw
1'use strict'
2
3require('should')
4
5module.exports = function abstractStoreTest (build) {
6 var store
7
8 beforeEach(function (done) {
9 build(function (err, _store) {
10 store = _store
11 done(err)
12 })
13 })
14
15 afterEach(function (done) {
16 store.close(done)
17 })
18
19 it('should put and stream in-flight packets', function (done) {
20 var packet = {
21 topic: 'hello',
22 payload: 'world',
23 qos: 1,
24 messageId: 42
25 }
26
27 store.put(packet, function () {
28 store
29 .createStream()
30 .on('data', function (data) {
31 data.should.eql(packet)
32 done()
33 })
34 })
35 })
36
37 it('should support destroying the stream', function (done) {
38 var packet = {
39 topic: 'hello',
40 payload: 'world',
41 qos: 1,
42 messageId: 42
43 }
44
45 store.put(packet, function () {
46 var stream = store.createStream()
47 stream.on('close', done)
48 stream.destroy()
49 })
50 })
51
52 it('should add and del in-flight packets', function (done) {
53 var packet = {
54 topic: 'hello',
55 payload: 'world',
56 qos: 1,
57 messageId: 42
58 }
59
60 store.put(packet, function () {
61 store.del(packet, function () {
62 store
63 .createStream()
64 .on('data', function () {
65 done(new Error('this should never happen'))
66 })
67 .on('end', done)
68 })
69 })
70 })
71
72 it('should replace a packet when doing put with the same messageId', function (done) {
73 var packet1 = {
74 topic: 'hello',
75 payload: 'world',
76 qos: 2,
77 messageId: 42
78 }
79 var packet2 = {
80 qos: 2,
81 messageId: 42
82 }
83
84 store.put(packet1, function () {
85 store.put(packet2, function () {
86 store
87 .createStream()
88 .on('data', function (data) {
89 data.should.eql(packet2)
90 done()
91 })
92 })
93 })
94 })
95
96 it('should return the original packet on del', function (done) {
97 var packet = {
98 topic: 'hello',
99 payload: 'world',
100 qos: 1,
101 messageId: 42
102 }
103
104 store.put(packet, function () {
105 store.del({ messageId: 42 }, function (err, deleted) {
106 if (err) {
107 throw err
108 }
109 deleted.should.eql(packet)
110 done()
111 })
112 })
113 })
114
115 it('should get a packet with the same messageId', function (done) {
116 var packet = {
117 topic: 'hello',
118 payload: 'world',
119 qos: 1,
120 messageId: 42
121 }
122
123 store.put(packet, function () {
124 store.get({ messageId: 42 }, function (err, fromDb) {
125 if (err) {
126 throw err
127 }
128 fromDb.should.eql(packet)
129 done()
130 })
131 })
132 })
133}