forked from moscajs/aedes-persistence-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathown.js
More file actions
270 lines (240 loc) · 6.61 KB
/
Copy pathown.js
File metadata and controls
270 lines (240 loc) · 6.61 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const test = require('node:test')
const persistence = require('../persistence.js')
const Redis = require('ioredis')
const mqemitterRedis = require('mqemitter-redis')
const { PromisifiedPersistence } = require('aedes-persistence/promisified.js')
const { once } = require('node:events')
// helpers
function sleep (sec) {
return new Promise(resolve => setTimeout(resolve, sec * 1000))
}
async function setUpPersistence (t, id, persistenceOpts) {
const emitter = mqemitterRedis()
const instance = persistence(persistenceOpts)
instance.broker = toBroker(id, emitter)
if (!instance.ready) {
await once(instance, 'ready')
}
t.diagnostic(`instance ${id} created`)
const p = new PromisifiedPersistence(instance)
return { instance: p, emitter, id }
}
function cleanUpPersistence (t, { instance, emitter, id }) {
instance.destroy()
emitter.close()
t.diagnostic(`instance ${id} destroyed`)
}
function toBroker (id, emitter) {
return {
id,
publish: emitter.emit.bind(emitter),
subscribe: emitter.on.bind(emitter),
unsubscribe: emitter.removeListener.bind(emitter)
}
}
async function createDB () {
const db = new Redis()
await once(db, 'connect')
await db.flushall()
return db
}
async function cleanDB () {
const db = await createDB()
await db.disconnect()
}
// testing starts here
async function doTest () {
test('external Redis conn', async t => {
t.plan(1)
const conn = await createDB(t)
const p = await setUpPersistence(t, '1', {
conn
})
conn.disconnect()
t.assert.ok(true, 'redis disconnected')
t.diagnostic('redis disconnected')
cleanUpPersistence(t, p)
})
test('packet ttl', async t => {
t.plan(1)
await cleanDB()
const p = await setUpPersistence(t, '1', {
packetTTL () {
return 1
}
})
const instance = p.instance
const subs = [{
clientId: 'ttlTest',
topic: 'hello',
qos: 1
}]
const packet = {
cmd: 'publish',
topic: 'hello',
payload: 'ttl test',
qos: 1,
retain: false,
brokerId: instance.broker.id,
brokerCounter: 42
}
await instance.outgoingEnqueueCombi(subs, packet)
await sleep(2)
const packets = await instance.outgoingStream({ id: 'ttlTest' }).toArray()
const noPacket = (packets === undefined) || (packets[0] === undefined)
t.assert.ok(noPacket, 'packet is gone')
cleanUpPersistence(t, p)
})
test('outgoingUpdate doesn\'t clear packet ttl', async t => {
t.plan(1)
const db = await createDB()
const p = await setUpPersistence(t, '1', {
packetTTL () {
return 1
}
})
const instance = p.instance
const client = {
id: 'ttlTest'
}
const subs = [{
clientId: client.id,
topic: 'hello',
qos: 1
}]
const packet = {
cmd: 'publish',
topic: 'hello',
payload: 'ttl test',
qos: 1,
retain: false,
brokerId: instance.broker.id,
brokerCounter: 42,
messageId: 123
}
await instance.outgoingEnqueueCombi(subs, packet)
await instance.outgoingUpdate(client, packet)
await sleep(2)
const exists = await db.exists('packet:1:42')
t.assert.ok(!exists, 'packet key should have expired')
cleanUpPersistence(t, p)
db.disconnect()
})
test('multiple persistences', async t => {
t.plan(1)
await cleanDB()
const p1 = await setUpPersistence(t, '1')
const p2 = await setUpPersistence(t, '2')
const instance = p1.instance
const instance2 = p2.instance
const client = { id: 'multipleTest' }
const subs = [{
topic: 'hello',
qos: 1
}, {
topic: 'hello/#',
qos: 1
}, {
topic: 'matteo',
qos: 1
}]
const expected = [{
clientId: client.id,
topic: 'hello/#',
qos: 1,
rh: undefined,
rap: undefined,
nl: undefined
}, {
clientId: client.id,
topic: 'hello',
qos: 1,
rh: undefined,
rap: undefined,
nl: undefined
}]
await instance.addSubscriptions(client, subs)
await sleep(2)
const resubs = await instance2.subscriptionsByTopic('hello')
t.assert.deepEqual(resubs, expected, 'received correct subs')
cleanUpPersistence(t, p1)
cleanUpPersistence(t, p2)
})
test('unknown cache key', async t => {
t.plan(2)
await cleanDB()
const p = await setUpPersistence(t, '1')
const instance = p.instance
const client = { id: 'unknown_pubrec' }
// packet with no brokerId
const packet = {
cmd: 'pubrec',
topic: 'hello',
qos: 2,
retain: false
}
try {
await instance.outgoingUpdate(client, packet)
} catch (err) {
t.assert.ok(err, 'error received')
t.assert.equal(err.message, 'unknown key', 'Received unknown PUBREC')
}
cleanUpPersistence(t, p)
})
test('wills table de-duplicate', async t => {
t.plan(1)
await cleanDB()
const p = await setUpPersistence(t, '1')
const instance = p.instance
const client = { id: 'willsTest' }
const packet = {
cmd: 'publish',
topic: 'hello',
payload: 'willsTest',
qos: 1,
retain: false,
brokerId: instance.broker.id,
brokerCounter: 42,
messageId: 123
}
await instance.putWill(client, packet)
await instance.putWill(client, packet)
const wills = await instance.streamWill().toArray()
t.assert.equal(wills.length, 1, 'should only be one will')
cleanUpPersistence(t, p)
})
test('lrange delivers key with no value', async t => {
t.plan(1)
await cleanDB()
const db = await createDB()
const clientId = 'ghostClient'
const listKey = 'outgoing:' + encodeURIComponent(clientId)
// we just add a key to the list with no associated packet
await db.rpush(listKey, 'ghostKey')
const p = await setUpPersistence(t, 'ghost', { conn: db })
const instance = p.instance
const packets = []
for await (const pkt of instance.outgoingStream({ id: clientId })) {
packets.push(pkt)
}
t.assert.equal(packets.length, 0, 'no packets should be delivered')
await db.del(listKey)
cleanUpPersistence(t, p)
})
test('createWillStream delivers key with no value', async t => {
t.plan(1)
await cleanDB()
const db = await createDB()
await db.rpush('will', 'ghostWillKey')
const p = await setUpPersistence(t, 'ghostWill', { conn: db })
const instance = p.instance
const wills = []
for await (const will of instance.streamWill()) {
wills.push(will)
}
t.assert.equal(wills.length, 0, 'geen wills gevonden als value ontbreekt')
await db.del('will')
cleanUpPersistence(t, p)
})
}
doTest()