forked from moscajs/aedes-persistence-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
131 lines (109 loc) · 3.23 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
'use strict'
var test = require('tape').test
var persistence = require('./')
var abs = require('aedes-cached-persistence/abstract')
var mqemitterMongo = require('mqemitter-mongodb')
var clean = require('mongo-clean')
var mongourl = 'mongodb://127.0.0.1/aedes-test'
var cleanopts = {
action: 'remove'
}
clean(mongourl, cleanopts, function (err, db) {
if (err) {
throw err
}
test.onFinish(function () {
db.unref()
db.close()
})
var dbopts = {
url: mongourl
}
abs({
test: test,
buildEmitter: function () {
var emitter = mqemitterMongo(dbopts)
return emitter
},
persistence: function build (cb) {
clean(db, cleanopts, function (err) {
if (err) {
return cb(err)
}
var instance = persistence(dbopts)
var oldDestroy = instance.destroy
instance.destroy = function (cb) {
instance.destroy = oldDestroy
instance.destroy(function () {
instance.broker.mq.close(function () {
cb()
})
})
}
cb(null, instance)
})
}
})
function toBroker (id, emitter) {
return {
id: id,
publish: emitter.emit.bind(emitter),
subscribe: emitter.on.bind(emitter),
unsubscribe: emitter.removeListener.bind(emitter)
}
}
test('multiple persistences', function (t) {
t.plan(12)
clean(db, cleanopts, function (err) {
t.error(err)
var emitter = mqemitterMongo(dbopts)
emitter.status.on('stream', function () {
t.pass('mqemitter 1 ready')
var emitter2 = mqemitterMongo(dbopts)
emitter2.status.on('stream', function () {
t.pass('mqemitter 2 ready')
var instance = persistence(dbopts)
instance.broker = toBroker('1', emitter)
instance.on('ready', function () {
t.pass('instance ready')
var instance2 = persistence(dbopts)
instance2.broker = toBroker('2', emitter2)
instance2.on('ready', function () {
t.pass('instance2 ready')
var client = { id: 'abcde' }
var subs = [{
topic: 'hello',
qos: 1
}, {
topic: 'hello/#',
qos: 1
}, {
topic: 'matteo',
qos: 1
}]
instance.addSubscriptions(client, subs, function (err) {
t.notOk(err, 'no error')
instance2.subscriptionsByTopic('hello', function (err, resubs) {
t.notOk(err, 'no error')
t.deepEqual(resubs, [{
clientId: client.id,
topic: 'hello/#',
qos: 1
}, {
clientId: client.id,
topic: 'hello',
qos: 1
}])
instance.destroy(t.pass.bind(t, 'first dies'))
instance2.destroy(t.pass.bind(t, 'second dies'))
emitter.close(t.pass.bind(t, 'first emitter dies'))
emitter2.close(t.pass.bind(t, 'second emitter dies'))
})
})
})
})
})
})
})
})
})