forked from moscajs/aedes-persistence-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
323 lines (292 loc) · 7.41 KB
/
test.js
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
const test = require('node:test')
const persistence = require('./')
const Redis = require('ioredis')
const mqemitterRedis = require('mqemitter-redis')
const abs = require('aedes-cached-persistence/abstract')
function sleep (sec) {
return new Promise(resolve => setTimeout(resolve, sec * 1000))
}
function waitForEvent (obj, resolveEvt) {
return new Promise((resolve, reject) => {
obj.once(resolveEvt, () => {
resolve()
})
obj.once('error', reject)
})
}
function setUpPersistence (t, id, persistenceOpts) {
const emitter = mqemitterRedis()
const instance = persistence(persistenceOpts)
instance.broker = toBroker(id, emitter)
t.diagnostic(`instance ${id} created`)
return { instance, 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)
}
}
function unref () {
this.connector.stream.unref()
}
// testing starts here
const db = new Redis()
db.on('error', e => {
console.trace(e)
})
db.on('connect', unref)
test('external Redis conn', async t => {
t.plan(2)
const externalRedis = new Redis()
await waitForEvent(externalRedis, 'connect')
t.diagnostic('redis connected')
t.assert.ok(true, 'redis connected')
const p = setUpPersistence(t, '1', {
conn: externalRedis
})
await waitForEvent(p.instance, 'ready')
t.assert.ok(true, 'instance ready')
t.diagnostic('instance ready')
externalRedis.disconnect()
cleanUpPersistence(t, p)
})
abs({
test,
buildEmitter () {
const emitter = mqemitterRedis()
emitter.subConn.on('connect', unref)
emitter.pubConn.on('connect', unref)
return emitter
},
persistence: () => {
db.flushall()
return persistence()
},
waitForReady: true
})
test('packet ttl', async t => {
t.plan(3)
// the promise is required for the test to wait for the end event
const executeTest = new Promise((resolve, reject) => {
db.flushall()
const p = 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
}
instance.outgoingEnqueueCombi(subs, packet, async function enqueued (err, saved) {
t.assert.ifError(err)
t.assert.deepEqual(saved, packet)
await sleep(1)
const offlineStream = instance.outgoingStream({ id: 'ttlTest' })
for await (const offlinePacket of offlineStream) {
t.assert.ok(!offlinePacket)
}
cleanUpPersistence(t, p)
resolve()
})
})
await executeTest
})
test('outgoingUpdate doesn\'t clear packet ttl', async t => {
t.plan(3)
db.flushall()
const p = 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 new Promise((resolve, reject) => {
instance.outgoingEnqueueCombi(subs, packet, function enqueued (err, saved) {
t.assert.ifError(err)
t.assert.deepEqual(saved, packet)
instance.outgoingUpdate(client, packet, async function updated () {
await sleep(2)
db.exists('packet:1:42', (_, exists) => {
t.assert.ok(!exists, 'packet key should have expired')
cleanUpPersistence(t, p)
resolve()
})
})
})
})
})
test('multiple persistences', {
timeout: 60 * 1000
}, async t => {
t.plan(3)
const executeTest = new Promise((resolve, reject) => {
db.flushall()
const p1 = setUpPersistence(t, '1')
const p2 = 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
}]
let gotSubs = false
let addedSubs = false
function close () {
if (gotSubs && addedSubs) {
cleanUpPersistence(t, p1)
cleanUpPersistence(t, p2)
resolve()
}
}
instance2._waitFor(client, true, 'hello', () => {
instance2.subscriptionsByTopic('hello', (err, resubs) => {
t.assert.ok(!err, 'subs by topic no error')
t.assert.deepEqual(resubs, [{
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
}], 'received correct subs')
gotSubs = true
close()
})
})
let ready = false
let ready2 = false
function addSubs () {
if (ready && ready2) {
instance.addSubscriptions(client, subs, err => {
t.assert.ok(!err, 'add subs no error')
addedSubs = true
close()
})
}
}
instance.on('ready', () => {
ready = true
addSubs()
})
instance2.on('ready', () => {
ready2 = true
addSubs()
})
})
await executeTest
})
test('unknown cache key', async t => {
t.plan(2)
const executeTest = new Promise((resolve, reject) => {
db.flushall()
const p = 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
}
instance.on('ready', () => {
instance.outgoingUpdate(client, packet, (err, client, packet) => {
t.assert.ok(err, 'error received')
t.assert.equal(err.message, 'unknown key', 'Received unknown PUBREC')
cleanUpPersistence(t, p)
resolve()
})
})
})
await executeTest
})
test('wills table de-duplicate', async t => {
t.plan(3)
const executeTest = new Promise((resolve, reject) => {
db.flushall()
const p = 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
}
instance.putWill(client, packet, err => {
t.assert.ok(!err, 'putWill #1 no error')
instance.putWill(client, packet, err => {
t.assert.ok(!err, 'putWill #2 no error')
let willCount = 0
const wills = instance.streamWill()
wills.on('data', (chunk) => {
willCount++
})
wills.on('end', () => {
t.assert.equal(willCount, 1, 'should only be one will')
cleanUpPersistence(t, p)
resolve()
})
})
})
})
await executeTest
})
// clients will keep on running after the test
sleep(10).then(() => process.exit(0))