-
-
Notifications
You must be signed in to change notification settings - Fork 228
/
will.js
204 lines (170 loc) · 5.32 KB
/
will.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
'use strict'
var Buffer = require('safe-buffer').Buffer
var test = require('tape').test
var memory = require('aedes-persistence')
var helper = require('./helper')
var setup = helper.setup
var connect = helper.connect
var aedes = require('../')
function willConnect (s, opts, connected) {
opts = opts || {}
opts.will = {
topic: 'mywill',
payload: Buffer.from('last will'),
qos: 0,
retain: false
}
return connect(s, opts, connected)
}
test('delivers a will', function (t) {
var opts = {}
// willConnect populates opts with a will
var s = willConnect(setup(), opts)
s.broker.mq.on('mywill', function (packet, cb) {
t.equal(packet.topic, opts.will.topic, 'topic matches')
t.deepEqual(packet.payload, opts.will.payload, 'payload matches')
t.equal(packet.qos, opts.will.qos, 'qos matches')
t.equal(packet.retain, opts.will.retain, 'retain matches')
cb()
t.end()
})
s.conn.destroy()
})
test('calling close two times should not deliver two wills', function (t) {
t.plan(4)
var opts = {}
var broker = aedes()
broker.on('client', function (client) {
client.close()
client.close()
})
broker.mq.on('mywill', onWill)
// willConnect populates opts with a will
willConnect(setup(broker), opts)
function onWill (packet, cb) {
broker.mq.removeListener('mywill', onWill)
broker.mq.on('mywill', t.fail.bind(t))
t.equal(packet.topic, opts.will.topic, 'topic matches')
t.deepEqual(packet.payload, opts.will.payload, 'payload matches')
t.equal(packet.qos, opts.will.qos, 'qos matches')
t.equal(packet.retain, opts.will.retain, 'retain matches')
cb()
}
})
test('delivers old will in case of a crash', function (t) {
t.plan(7)
var persistence = memory()
var will = {
topic: 'mywill',
payload: Buffer.from('last will'),
qos: 0,
retain: false
}
persistence.broker = {
id: 'anotherBroker'
}
persistence.putWill({
id: 'myClientId42'
}, will, function (err) {
t.error(err, 'no error')
var interval = 10 // ms, so that the will check happens fast!
var broker = aedes({
persistence: persistence,
heartbeatInterval: interval
})
var start = Date.now()
broker.mq.on('mywill', check)
function check (packet, cb) {
broker.mq.removeListener('mywill', check)
t.ok(Date.now() - start >= 3 * interval, 'the will needs to be emitted after 3 heartbeats')
t.equal(packet.topic, will.topic, 'topic matches')
t.deepEqual(packet.payload, will.payload, 'payload matches')
t.equal(packet.qos, will.qos, 'qos matches')
t.equal(packet.retain, will.retain, 'retain matches')
broker.mq.on('mywill', function (packet) {
t.fail('the will must be delivered only once')
})
setTimeout(function () {
broker.close(t.pass.bind(t, 'server closes'))
}, 15)
cb()
}
})
})
test('store the will in the persistence', function (t) {
var opts = {
clientId: 'abcde'
}
// willConnect populates opts with a will
var s = willConnect(setup(), opts)
s.broker.persistence.getWill({
id: opts.clientId
}, function (err, packet) {
t.error(err, 'no error')
t.deepEqual(packet.topic, opts.will.topic, 'will topic matches')
t.deepEqual(packet.payload, opts.will.payload, 'will payload matches')
t.deepEqual(packet.qos, opts.will.qos, 'will qos matches')
t.deepEqual(packet.retain, opts.will.retain, 'will retain matches')
t.end()
})
})
test('delete the will in the persistence after publish', function (t) {
var opts = {
clientId: 'abcde'
}
var broker = aedes()
broker.on('client', function (client) {
setImmediate(function () {
client.close()
})
})
broker.mq.on('mywill', check)
// willConnect populates opts with a will
willConnect(setup(broker), opts)
function check (packet, cb) {
broker.mq.removeListener('mywill', check)
setImmediate(function () {
broker.persistence.getWill({
id: opts.clientId
}, function (err, p) {
t.error(err, 'no error')
t.notOk(p, 'packet is empty')
t.end()
})
})
cb()
}
})
test('delivers a will with authorization', function (t) {
let authorized = false
var opts = {}
// willConnect populates opts with a will
var s = willConnect(setup(aedes({ authorizePublish: (_1, _2, callback) => { authorized = true; callback(null) } })), opts)
s.broker.on('clientDisconnect', function () {
t.end()
})
s.broker.mq.on('mywill', function (packet, cb) {
t.equal(packet.topic, opts.will.topic, 'topic matches')
t.deepEqual(packet.payload, opts.will.payload, 'payload matches')
t.equal(packet.qos, opts.will.qos, 'qos matches')
t.equal(packet.retain, opts.will.retain, 'retain matches')
t.equal(authorized, true, 'authorization called')
cb()
})
s.conn.destroy()
})
test('does not deliver a will without authorization', function (t) {
let authorized = false
var opts = {}
// willConnect populates opts with a will
var s = willConnect(setup(aedes({ authorizePublish: (_1, _2, callback) => { authorized = true; callback(new Error()) } })), opts)
s.broker.on('clientDisconnect', function () {
t.equal(authorized, true, 'authorization called')
t.end()
})
s.broker.mq.on('mywill', function (packet, cb) {
t.fail('received will without authorization')
cb()
})
s.conn.destroy()
})