-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
abstract.js
1599 lines (1443 loc) · 43.9 KB
/
abstract.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const { Readable } = require('stream')
const Packet = require('aedes-packet')
function abstractPersistence (opts) {
const test = opts.test
let _persistence = opts.persistence
const waitForReady = opts.waitForReady
// requiring it here so it will not error for modules
// not using the default emitter
const buildEmitter = opts.buildEmitter || require('mqemitter')
if (_persistence.length === 0) {
_persistence = function asyncify (cb) {
cb(null, opts.persistence())
}
}
function persistence (cb) {
const mq = buildEmitter()
const broker = {
id: 'broker-42',
mq,
publish: mq.emit.bind(mq),
subscribe: mq.on.bind(mq),
unsubscribe: mq.removeListener.bind(mq),
counter: 0
}
_persistence((err, instance) => {
if (instance) {
// Wait for ready event, if applicable, to ensure the persistence isn't
// destroyed while it's still being set up.
// https://github.com/mcollina/aedes-persistence-redis/issues/41
if (waitForReady) {
// We have to listen to 'ready' before setting broker because that
// can result in 'ready' being emitted.
instance.on('ready', () => {
instance.removeListener('error', cb)
cb(null, instance)
})
instance.on('error', cb)
}
instance.broker = broker
if (waitForReady) {
// 'ready' event will call back.
return
}
}
cb(err, instance)
})
}
// legacy third party streams are typically not iterable
function iterableStream (stream) {
if (typeof stream[Symbol.asyncIterator] !== 'function') {
return new Readable({ objectMode: true }).wrap(stream)
}
return stream
}
// end of legacy third party streams support
async function getArrayFromStream (stream) {
const list = []
for await (const item of iterableStream(stream)) {
list.push(item)
}
return list
}
async function streamForEach (stream, fn) {
for await (const item of iterableStream(stream)) {
await fn(item)
}
}
function storeRetained (instance, opts, cb) {
opts = opts || {}
const packet = {
cmd: 'publish',
id: instance.broker.id,
topic: opts.topic || 'hello/world',
payload: opts.payload || Buffer.from('muahah'),
qos: 0,
retain: true
}
instance.storeRetained(packet, err => {
cb(err, packet)
})
}
function matchRetainedWithPattern (t, pattern, opts) {
persistence((err, instance) => {
if (err) { throw err }
storeRetained(instance, opts, (err, packet) => {
t.notOk(err, 'no error')
let stream
if (Array.isArray(pattern)) {
stream = instance.createRetainedStreamCombi(pattern)
} else {
stream = instance.createRetainedStream(pattern)
}
getArrayFromStream(stream).then(list => {
t.deepEqual(list, [packet], 'must return the packet')
instance.destroy(t.end.bind(t))
})
})
})
}
function testInstance (title, cb) {
test(title, t => {
persistence((err, instance) => {
if (err) { throw err }
cb(t, instance)
})
})
}
function testPacket (t, packet, expected) {
if (packet.messageId === null) packet.messageId = undefined
t.equal(packet.messageId, undefined, 'should have an unassigned messageId in queue')
t.deepLooseEqual(packet, expected, 'must return the packet')
}
function deClassed (obj) {
return Object.assign({}, obj)
}
test('store and look up retained messages', t => {
matchRetainedWithPattern(t, 'hello/world')
})
test('look up retained messages with a # pattern', t => {
matchRetainedWithPattern(t, '#')
})
test('look up retained messages with a hello/world/# pattern', t => {
matchRetainedWithPattern(t, 'hello/world/#')
})
test('look up retained messages with a + pattern', t => {
matchRetainedWithPattern(t, 'hello/+')
})
test('look up retained messages with multiple patterns', t => {
matchRetainedWithPattern(t, ['hello/+', 'other/hello'])
})
testInstance('store multiple retained messages in order', (t, instance) => {
const totalMessages = 1000
let done = 0
const retained = {
cmd: 'publish',
topic: 'hello',
payload: Buffer.from('world'),
qos: 1,
retain: true
}
function checkIndex (index) {
const packet = new Packet(retained, instance.broker)
instance.storeRetained(packet, err => {
t.notOk(err, 'no error')
t.equal(packet.brokerCounter, index + 1, 'packet stored in order')
if (++done === totalMessages) {
instance.destroy(t.end.bind(t))
}
})
}
for (let i = 0; i < totalMessages; i++) {
checkIndex(i)
}
})
testInstance('remove retained message', (t, instance) => {
storeRetained(instance, {}, (err, packet) => {
t.notOk(err, 'no error')
storeRetained(instance, {
payload: Buffer.alloc(0)
}, err => {
t.notOk(err, 'no error')
const stream = instance.createRetainedStream('#')
getArrayFromStream(stream).then(list => {
t.deepEqual(list, [], 'must return an empty list')
instance.destroy(t.end.bind(t))
})
})
})
})
testInstance('storing twice a retained message should keep only the last', (t, instance) => {
storeRetained(instance, {}, (err, packet) => {
t.notOk(err, 'no error')
storeRetained(instance, {
payload: Buffer.from('ahah')
}, (err, packet) => {
t.notOk(err, 'no error')
const stream = instance.createRetainedStream('#')
getArrayFromStream(stream).then(list => {
t.deepEqual(list, [packet], 'must return the last packet')
instance.destroy(t.end.bind(t))
})
})
})
})
testInstance('Create a new packet while storing a retained message', (t, instance) => {
const packet = {
cmd: 'publish',
id: instance.broker.id,
topic: opts.topic || 'hello/world',
payload: opts.payload || Buffer.from('muahah'),
qos: 0,
retain: true
}
const newPacket = Object.assign({}, packet)
instance.storeRetained(packet, err => {
t.notOk(err, 'no error')
// packet reference change to check if a new packet is stored always
packet.retain = false
const stream = instance.createRetainedStream('#')
getArrayFromStream(stream).then(list => {
t.deepEqual(list, [newPacket], 'must return the last packet')
instance.destroy(t.end.bind(t))
})
})
})
testInstance('store and look up subscriptions by client', (t, instance) => {
const client = { id: 'abcde' }
const subs = [{
topic: 'hello',
qos: 1,
rh: 0,
rap: true,
nl: false
}, {
topic: 'matteo',
qos: 1,
rh: 0,
rap: true,
nl: false
}, {
topic: 'noqos',
qos: 0,
rh: 0,
rap: true,
nl: false
}]
instance.addSubscriptions(client, subs, (err, reClient) => {
t.equal(reClient, client, 'client must be the same')
t.notOk(err, 'no error')
instance.subscriptionsByClient(client, (err, resubs, reReClient) => {
t.equal(reReClient, client, 'client must be the same')
t.notOk(err, 'no error')
t.deepEqual(resubs, subs)
instance.destroy(t.end.bind(t))
})
})
})
testInstance('remove subscriptions by client', (t, instance) => {
const client = { id: 'abcde' }
const subs = [{
topic: 'hello',
qos: 1,
rh: 0,
rap: true,
nl: false
}, {
topic: 'matteo',
qos: 1,
rh: 0,
rap: true,
nl: false
}]
instance.addSubscriptions(client, subs, (err, reClient) => {
t.notOk(err, 'no error')
instance.removeSubscriptions(client, ['hello'], (err, reClient) => {
t.notOk(err, 'no error')
t.equal(reClient, client, 'client must be the same')
instance.subscriptionsByClient(client, (err, resubs, reClient) => {
t.equal(reClient, client, 'client must be the same')
t.notOk(err, 'no error')
t.deepEqual(resubs, [{
topic: 'matteo',
qos: 1,
rh: 0,
rap: true,
nl: false
}])
instance.destroy(t.end.bind(t))
})
})
})
})
testInstance('store and look up subscriptions by topic', (t, instance) => {
const client = { id: 'abcde' }
const subs = [{
topic: 'hello',
qos: 1,
rh: 0,
rap: true,
nl: false
}, {
topic: 'hello/#',
qos: 1,
rh: 0,
rap: true,
nl: false
}, {
topic: 'matteo',
qos: 1,
rh: 0,
rap: true,
nl: false
}]
instance.addSubscriptions(client, subs, err => {
t.notOk(err, 'no error')
instance.subscriptionsByTopic('hello', (err, resubs) => {
t.notOk(err, 'no error')
t.deepEqual(resubs, [{
clientId: client.id,
topic: 'hello/#',
qos: 1,
rh: 0,
rap: true,
nl: false
}, {
clientId: client.id,
topic: 'hello',
qos: 1,
rh: 0,
rap: true,
nl: false
}])
instance.destroy(t.end.bind(t))
})
})
})
testInstance('get client list after subscriptions', (t, instance) => {
const client1 = { id: 'abcde' }
const client2 = { id: 'efghi' }
const subs = [{
topic: 'helloagain',
qos: 1
}]
instance.addSubscriptions(client1, subs, err => {
t.notOk(err, 'no error for client 1')
instance.addSubscriptions(client2, subs, err => {
t.notOk(err, 'no error for client 2')
const stream = instance.getClientList(subs[0].topic)
getArrayFromStream(stream).then(out => {
t.deepEqual(out, [client1.id, client2.id])
instance.destroy(t.end.bind(t))
})
})
})
})
testInstance('get client list after an unsubscribe', (t, instance) => {
const client1 = { id: 'abcde' }
const client2 = { id: 'efghi' }
const subs = [{
topic: 'helloagain',
qos: 1
}]
instance.addSubscriptions(client1, subs, err => {
t.notOk(err, 'no error for client 1')
instance.addSubscriptions(client2, subs, err => {
t.notOk(err, 'no error for client 2')
instance.removeSubscriptions(client2, [subs[0].topic], (err, reClient) => {
t.notOk(err, 'no error for removeSubscriptions')
const stream = instance.getClientList(subs[0].topic)
getArrayFromStream(stream).then(out => {
t.deepEqual(out, [client1.id])
instance.destroy(t.end.bind(t))
})
})
})
})
})
testInstance('get subscriptions list after an unsubscribe', (t, instance) => {
const client1 = { id: 'abcde' }
const client2 = { id: 'efghi' }
const subs = [{
topic: 'helloagain',
qos: 1
}]
instance.addSubscriptions(client1, subs, err => {
t.notOk(err, 'no error for client 1')
instance.addSubscriptions(client2, subs, err => {
t.notOk(err, 'no error for client 2')
instance.removeSubscriptions(client2, [subs[0].topic], (err, reClient) => {
t.notOk(err, 'no error for removeSubscriptions')
instance.subscriptionsByTopic(subs[0].topic, (err, clients) => {
t.notOk(err, 'no error getting subscriptions by topic')
t.deepEqual(clients[0].clientId, client1.id)
instance.destroy(t.end.bind(t))
})
})
})
})
})
testInstance('QoS 0 subscriptions, restored but not matched', (t, instance) => {
const client = { id: 'abcde' }
const subs = [{
topic: 'hello',
qos: 0,
rh: 0,
rap: true,
nl: false
}, {
topic: 'hello/#',
qos: 1,
rh: 0,
rap: true,
nl: false
}, {
topic: 'matteo',
qos: 1,
rh: 0,
rap: true,
nl: false
}]
instance.addSubscriptions(client, subs, err => {
t.notOk(err, 'no error')
instance.subscriptionsByClient(client, (err, resubs) => {
t.notOk(err, 'no error')
t.deepEqual(resubs, subs)
instance.subscriptionsByTopic('hello', (err, resubs2) => {
t.notOk(err, 'no error')
t.deepEqual(resubs2, [{
clientId: client.id,
topic: 'hello/#',
qos: 1,
rh: 0,
rap: true,
nl: false
}])
instance.destroy(t.end.bind(t))
})
})
})
})
testInstance('clean subscriptions', (t, instance) => {
const client = { id: 'abcde' }
const subs = [{
topic: 'hello',
qos: 1
}, {
topic: 'matteo',
qos: 1
}]
instance.addSubscriptions(client, subs, err => {
t.notOk(err, 'no error')
instance.cleanSubscriptions(client, err => {
t.notOk(err, 'no error')
instance.subscriptionsByTopic('hello', (err, resubs) => {
t.notOk(err, 'no error')
t.deepEqual(resubs, [], 'no subscriptions')
instance.subscriptionsByClient(client, (err, resubs) => {
t.error(err)
t.deepEqual(resubs, null, 'no subscriptions')
instance.countOffline((err, subsCount, clientsCount) => {
t.error(err, 'no error')
t.equal(subsCount, 0, 'no subscriptions added')
t.equal(clientsCount, 0, 'no clients added')
instance.destroy(t.end.bind(t))
})
})
})
})
})
})
testInstance('clean subscriptions with no active subscriptions', (t, instance) => {
const client = { id: 'abcde' }
instance.cleanSubscriptions(client, err => {
t.notOk(err, 'no error')
instance.subscriptionsByTopic('hello', (err, resubs) => {
t.notOk(err, 'no error')
t.deepEqual(resubs, [], 'no subscriptions')
instance.subscriptionsByClient(client, (err, resubs) => {
t.error(err)
t.deepEqual(resubs, null, 'no subscriptions')
instance.countOffline((err, subsCount, clientsCount) => {
t.error(err, 'no error')
t.equal(subsCount, 0, 'no subscriptions added')
t.equal(clientsCount, 0, 'no clients added')
instance.destroy(t.end.bind(t))
})
})
})
})
})
testInstance('same topic, different QoS', (t, instance) => {
const client = { id: 'abcde' }
const subs = [{
topic: 'hello',
qos: 0,
rh: 0,
rap: true,
nl: false
}, {
topic: 'hello',
qos: 1,
rh: 0,
rap: true,
nl: false
}]
instance.addSubscriptions(client, subs, (err, reClient) => {
t.equal(reClient, client, 'client must be the same')
t.error(err, 'no error')
instance.subscriptionsByClient(client, (err, subsForClient, client) => {
t.error(err, 'no error')
t.deepEqual(subsForClient, [{
topic: 'hello',
qos: 1,
rh: 0,
rap: true,
nl: false
}])
instance.subscriptionsByTopic('hello', (err, subsForTopic) => {
t.error(err, 'no error')
t.deepEqual(subsForTopic, [{
clientId: 'abcde',
topic: 'hello',
qos: 1,
rh: 0,
rap: true,
nl: false
}])
instance.countOffline((err, subsCount, clientsCount) => {
t.error(err, 'no error')
t.equal(subsCount, 1, 'one subscription added')
t.equal(clientsCount, 1, 'one client added')
instance.destroy(t.end.bind(t))
})
})
})
})
})
testInstance('replace subscriptions', (t, instance) => {
const client = { id: 'abcde' }
const topic = 'hello'
const sub = { topic, rh: 0, rap: true, nl: false }
const subByTopic = { clientId: client.id, topic, rh: 0, rap: true, nl: false }
function check (qos, cb) {
sub.qos = subByTopic.qos = qos
instance.addSubscriptions(client, [sub], (err, reClient) => {
t.equal(reClient, client, 'client must be the same')
t.error(err, 'no error')
instance.subscriptionsByClient(client, (err, subsForClient, client) => {
t.error(err, 'no error')
t.deepEqual(subsForClient, [sub])
instance.subscriptionsByTopic(topic, (err, subsForTopic) => {
t.error(err, 'no error')
t.deepEqual(subsForTopic, qos === 0 ? [] : [subByTopic])
instance.countOffline((err, subsCount, clientsCount) => {
t.error(err, 'no error')
if (qos === 0) {
t.equal(subsCount, 0, 'no subscriptions added')
} else {
t.equal(subsCount, 1, 'one subscription added')
}
t.equal(clientsCount, 1, 'one client added')
cb()
})
})
})
})
}
check(0, () => {
check(1, () => {
check(2, () => {
check(1, () => {
check(0, () => {
instance.destroy(t.end.bind(t))
})
})
})
})
})
})
testInstance('replace subscriptions in same call', (t, instance) => {
const client = { id: 'abcde' }
const topic = 'hello'
const subs = [
{ topic, qos: 0, rh: 0, rap: true, nl: false },
{ topic, qos: 1, rh: 0, rap: true, nl: false },
{ topic, qos: 2, rh: 0, rap: true, nl: false },
{ topic, qos: 1, rh: 0, rap: true, nl: false },
{ topic, qos: 0, rh: 0, rap: true, nl: false }
]
instance.addSubscriptions(client, subs, (err, reClient) => {
t.equal(reClient, client, 'client must be the same')
t.error(err, 'no error')
instance.subscriptionsByClient(client, (err, subsForClient, client) => {
t.error(err, 'no error')
t.deepEqual(subsForClient, [{ topic, qos: 0, rh: 0, rap: true, nl: false }])
instance.subscriptionsByTopic(topic, (err, subsForTopic) => {
t.error(err, 'no error')
t.deepEqual(subsForTopic, [])
instance.countOffline((err, subsCount, clientsCount) => {
t.error(err, 'no error')
t.equal(subsCount, 0, 'no subscriptions added')
t.equal(clientsCount, 1, 'one client added')
instance.destroy(t.end.bind(t))
})
})
})
})
})
testInstance('store and count subscriptions', (t, instance) => {
const client = { id: 'abcde' }
const subs = [{
topic: 'hello',
qos: 1
}, {
topic: 'matteo',
qos: 1
}, {
topic: 'noqos',
qos: 0
}]
instance.addSubscriptions(client, subs, (err, reClient) => {
t.equal(reClient, client, 'client must be the same')
t.error(err, 'no error')
instance.countOffline((err, subsCount, clientsCount) => {
t.error(err, 'no error')
t.equal(subsCount, 2, 'two subscriptions added')
t.equal(clientsCount, 1, 'one client added')
instance.removeSubscriptions(client, ['hello'], (err, reClient) => {
t.error(err, 'no error')
instance.countOffline((err, subsCount, clientsCount) => {
t.error(err, 'no error')
t.equal(subsCount, 1, 'one subscription added')
t.equal(clientsCount, 1, 'one client added')
instance.removeSubscriptions(client, ['matteo'], (err, reClient) => {
t.error(err, 'no error')
instance.countOffline((err, subsCount, clientsCount) => {
t.error(err, 'no error')
t.equal(subsCount, 0, 'zero subscriptions added')
t.equal(clientsCount, 1, 'one client added')
instance.removeSubscriptions(client, ['noqos'], (err, reClient) => {
t.error(err, 'no error')
instance.countOffline((err, subsCount, clientsCount) => {
t.error(err, 'no error')
t.equal(subsCount, 0, 'zero subscriptions added')
t.equal(clientsCount, 0, 'zero clients added')
instance.removeSubscriptions(client, ['noqos'], (err, reClient) => {
t.error(err, 'no error')
instance.countOffline((err, subsCount, clientsCount) => {
t.error(err, 'no error')
t.equal(subsCount, 0, 'zero subscriptions added')
t.equal(clientsCount, 0, 'zero clients added')
instance.destroy(t.end.bind(t))
})
})
})
})
})
})
})
})
})
})
})
testInstance('count subscriptions with two clients', (t, instance) => {
const client1 = { id: 'abcde' }
const client2 = { id: 'fghij' }
const subs = [{
topic: 'hello',
qos: 1
}, {
topic: 'matteo',
qos: 1
}, {
topic: 'noqos',
qos: 0
}]
function remove (client, subs, expectedSubs, expectedClients, cb) {
instance.removeSubscriptions(client, subs, (err, reClient) => {
t.error(err, 'no error')
t.equal(reClient, client, 'client must be the same')
instance.countOffline((err, subsCount, clientsCount) => {
t.error(err, 'no error')
t.equal(subsCount, expectedSubs, 'subscriptions added')
t.equal(clientsCount, expectedClients, 'clients added')
cb()
})
})
}
instance.addSubscriptions(client1, subs, (err, reClient) => {
t.equal(reClient, client1, 'client must be the same')
t.error(err, 'no error')
instance.addSubscriptions(client2, subs, (err, reClient) => {
t.equal(reClient, client2, 'client must be the same')
t.error(err, 'no error')
remove(client1, ['foobar'], 4, 2, () => {
remove(client1, ['hello'], 3, 2, () => {
remove(client1, ['hello'], 3, 2, () => {
remove(client1, ['matteo'], 2, 2, () => {
remove(client1, ['noqos'], 2, 1, () => {
remove(client2, ['hello'], 1, 1, () => {
remove(client2, ['matteo'], 0, 1, () => {
remove(client2, ['noqos'], 0, 0, () => {
instance.destroy(t.end.bind(t))
})
})
})
})
})
})
})
})
})
})
})
testInstance('add duplicate subs to persistence for qos > 0', (t, instance) => {
const client = { id: 'abcde' }
const topic = 'hello'
const subs = [{
topic,
qos: 1,
rh: 0,
rap: true,
nl: false
}]
instance.addSubscriptions(client, subs, (err, reClient) => {
t.equal(reClient, client, 'client must be the same')
t.error(err, 'no error')
instance.addSubscriptions(client, subs, (err, resCLient) => {
t.equal(resCLient, client, 'client must be the same')
t.error(err, 'no error')
subs[0].clientId = client.id
instance.subscriptionsByTopic(topic, (err, subsForTopic) => {
t.error(err, 'no error')
t.deepEqual(subsForTopic, subs)
instance.destroy(t.end.bind(t))
})
})
})
})
testInstance('add duplicate subs to persistence for qos 0', (t, instance) => {
const client = { id: 'abcde' }
const topic = 'hello'
const subs = [{
topic,
qos: 0,
rh: 0,
rap: true,
nl: false
}]
instance.addSubscriptions(client, subs, (err, reClient) => {
t.equal(reClient, client, 'client must be the same')
t.error(err, 'no error')
instance.addSubscriptions(client, subs, (err, resCLient) => {
t.equal(resCLient, client, 'client must be the same')
t.error(err, 'no error')
instance.subscriptionsByClient(client, (err, subsForClient, client) => {
t.error(err, 'no error')
t.deepEqual(subsForClient, subs)
instance.destroy(t.end.bind(t))
})
})
})
})
testInstance('get topic list after concurrent subscriptions of a client', (t, instance) => {
const client = { id: 'abcde' }
const subs1 = [{
topic: 'hello1',
qos: 1,
rh: 0,
rap: true,
nl: false
}]
const subs2 = [{
topic: 'hello2',
qos: 1,
rh: 0,
rap: true,
nl: false
}]
let calls = 2
function done () {
if (!--calls) {
instance.subscriptionsByClient(client, (err, resubs) => {
t.notOk(err, 'no error')
resubs.sort((a, b) => b.topic.localeCompare(b.topic, 'en'))
t.deepEqual(resubs, [subs1[0], subs2[0]])
instance.destroy(t.end.bind(t))
})
}
}
instance.addSubscriptions(client, subs1, err => {
t.notOk(err, 'no error for hello1')
done()
})
instance.addSubscriptions(client, subs2, err => {
t.notOk(err, 'no error for hello2')
done()
})
})
testInstance('add outgoing packet and stream it', (t, instance) => {
const sub = {
clientId: 'abcde',
topic: 'hello',
qos: 1
}
const client = {
id: sub.clientId
}
const packet = {
cmd: 'publish',
topic: 'hello',
payload: Buffer.from('world'),
qos: 1,
dup: false,
length: 14,
retain: false,
brokerId: instance.broker.id,
brokerCounter: 42
}
const expected = {
cmd: 'publish',
topic: 'hello',
payload: Buffer.from('world'),
qos: 1,
retain: false,
dup: false,
brokerId: instance.broker.id,
brokerCounter: 42,
messageId: undefined
}
instance.outgoingEnqueue(sub, packet, err => {
t.error(err)
const stream = instance.outgoingStream(client)
getArrayFromStream(stream).then(list => {
const packet = list[0]
testPacket(t, packet, expected)
instance.destroy(t.end.bind(t))
})
})
})
testInstance('add outgoing packet for multiple subs and stream to all', (t, instance) => {
const sub = {
clientId: 'abcde',
topic: 'hello',
qos: 1
}
const sub2 = {
clientId: 'fghih',
topic: 'hello',
qos: 1
}
const subs = [sub, sub2]
const client = {
id: sub.clientId
}
const client2 = {
id: sub2.clientId
}
const packet = {
cmd: 'publish',
topic: 'hello',
payload: Buffer.from('world'),
qos: 1,
dup: false,
length: 14,
retain: false,
brokerId: instance.broker.id,
brokerCounter: 42
}
const expected = {
cmd: 'publish',
topic: 'hello',
payload: Buffer.from('world'),
qos: 1,
retain: false,
dup: false,
brokerId: instance.broker.id,
brokerCounter: 42,
messageId: undefined
}
instance.outgoingEnqueueCombi(subs, packet, err => {
t.error(err)
const stream = instance.outgoingStream(client)
getArrayFromStream(stream).then(list => {
const packet = list[0]
testPacket(t, packet, expected)
const stream2 = instance.outgoingStream(client2)
getArrayFromStream(stream2).then(list2 => {
const packet = list2[0]
testPacket(t, packet, expected)
instance.destroy(t.end.bind(t))
})
})
})
})
testInstance('add outgoing packet as a string and pump', (t, instance) => {
const sub = {
clientId: 'abcde',
topic: 'hello',
qos: 1
}
const client = {
id: sub.clientId
}
const packet1 = {
cmd: 'publish',
topic: 'hello',
payload: Buffer.from('world'),
qos: 1,
retain: false,
brokerId: instance.broker.id,
brokerCounter: 10
}
const packet2 = {
cmd: 'publish',
topic: 'hello',