mqtt 4.0.0 → 4.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -24
- package/bin/mqtt.js +27 -0
- package/bin/pub.js +0 -0
- package/bin/sub.js +0 -0
- package/dist/mqtt.js +2794 -2512
- package/dist/mqtt.min.js +1 -1
- package/lib/client.js +72 -59
- package/lib/connect/index.js +7 -2
- package/lib/connect/tcp.js +2 -0
- package/lib/connect/tls.js +3 -0
- package/lib/connect/ws.js +194 -34
- package/lib/connect/wx.js +3 -3
- package/lib/store.js +2 -2
- package/mqtt.js +0 -24
- package/package.json +9 -10
- package/types/lib/client-options.d.ts +14 -1
- package/types/lib/client.d.ts +4 -3
- package/examples/client/secure-client.js +0 -24
- package/examples/client/simple-both.js +0 -13
- package/examples/client/simple-publish.js +0 -7
- package/examples/client/simple-subscribe.js +0 -9
- package/examples/tls client/crt.ca.cg.pem +0 -34
- package/examples/tls client/mqttclient.js +0 -48
- package/examples/tls client/tls-cert.pem +0 -13
- package/examples/tls client/tls-key.pem +0 -15
- package/examples/wss/client.js +0 -49
- package/examples/wss/client_with_proxy.js +0 -58
- package/test/abstract_client.js +0 -3139
- package/test/abstract_store.js +0 -135
- package/test/browser/server.js +0 -132
- package/test/browser/test.js +0 -92
- package/test/client.js +0 -540
- package/test/client_mqtt5.js +0 -537
- package/test/helpers/port_list.js +0 -45
- package/test/helpers/private-csr.pem +0 -16
- package/test/helpers/private-key.pem +0 -27
- package/test/helpers/public-cert.pem +0 -19
- package/test/helpers/public-key.pem +0 -9
- package/test/helpers/server.js +0 -53
- package/test/helpers/server_process.js +0 -9
- package/test/helpers/tls-cert.pem +0 -14
- package/test/helpers/tls-csr.pem +0 -11
- package/test/helpers/tls-key.pem +0 -15
- package/test/helpers/wrong-cert.pem +0 -13
- package/test/helpers/wrong-csr.pem +0 -11
- package/test/helpers/wrong-key.pem +0 -15
- package/test/mocha.opts +0 -4
- package/test/mqtt.js +0 -230
- package/test/mqtt_store.js +0 -9
- package/test/secure_client.js +0 -188
- package/test/server.js +0 -94
- package/test/server_helpers_for_client_tests.js +0 -100
- package/test/store.js +0 -10
- package/test/typescript/broker-connect-subscribe-and-publish.js +0 -24
- package/test/typescript/broker-connect-subscribe-and-publish.js.map +0 -1
- package/test/typescript/broker-connect-subscribe-and-publish.ts +0 -25
- package/test/typescript/tsconfig.json +0 -14
- package/test/util.js +0 -13
- package/test/websocket_client.js +0 -146
package/test/abstract_client.js
DELETED
|
@@ -1,3139 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Testing dependencies
|
|
5
|
-
*/
|
|
6
|
-
var should = require('chai').should
|
|
7
|
-
var sinon = require('sinon')
|
|
8
|
-
var mqtt = require('../')
|
|
9
|
-
var xtend = require('xtend')
|
|
10
|
-
var MqttServer = require('./server').MqttServer
|
|
11
|
-
var Store = require('./../lib/store')
|
|
12
|
-
var assert = require('chai').assert
|
|
13
|
-
var ports = require('./helpers/port_list')
|
|
14
|
-
|
|
15
|
-
module.exports = function (server, config) {
|
|
16
|
-
var version = config.protocolVersion || 4
|
|
17
|
-
|
|
18
|
-
function connect (opts) {
|
|
19
|
-
opts = xtend(config, opts)
|
|
20
|
-
return mqtt.connect(opts)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
describe('closing', function () {
|
|
24
|
-
it('should emit close if stream closes', function (done) {
|
|
25
|
-
var client = connect()
|
|
26
|
-
|
|
27
|
-
client.once('connect', function () {
|
|
28
|
-
client.stream.end()
|
|
29
|
-
})
|
|
30
|
-
client.once('close', function () {
|
|
31
|
-
client.end()
|
|
32
|
-
done()
|
|
33
|
-
})
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
it('should mark the client as disconnected', function (done) {
|
|
37
|
-
var client = connect()
|
|
38
|
-
|
|
39
|
-
client.once('close', function () {
|
|
40
|
-
client.end()
|
|
41
|
-
if (!client.connected) {
|
|
42
|
-
done()
|
|
43
|
-
} else {
|
|
44
|
-
done(new Error('Not marked as disconnected'))
|
|
45
|
-
}
|
|
46
|
-
})
|
|
47
|
-
client.once('connect', function () {
|
|
48
|
-
client.stream.end()
|
|
49
|
-
})
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
it('should stop ping timer if stream closes', function (done) {
|
|
53
|
-
var client = connect()
|
|
54
|
-
|
|
55
|
-
client.once('close', function () {
|
|
56
|
-
assert.notExists(client.pingTimer)
|
|
57
|
-
client.end(true, done)
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
client.once('connect', function () {
|
|
61
|
-
assert.exists(client.pingTimer)
|
|
62
|
-
client.stream.end()
|
|
63
|
-
})
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
it('should emit close after end called', function (done) {
|
|
67
|
-
var client = connect()
|
|
68
|
-
|
|
69
|
-
client.once('close', function () {
|
|
70
|
-
done()
|
|
71
|
-
})
|
|
72
|
-
|
|
73
|
-
client.once('connect', function () {
|
|
74
|
-
client.end()
|
|
75
|
-
})
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
it('should emit end after end called and client must be disconnected', function (done) {
|
|
79
|
-
var client = connect()
|
|
80
|
-
|
|
81
|
-
client.once('end', function () {
|
|
82
|
-
if (client.disconnected) {
|
|
83
|
-
return done()
|
|
84
|
-
}
|
|
85
|
-
done(new Error('client must be disconnected'))
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
client.once('connect', function () {
|
|
89
|
-
client.end()
|
|
90
|
-
})
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
it('should pass store close error to end callback but not to end listeners', function (done) {
|
|
94
|
-
var store = new Store()
|
|
95
|
-
var client = connect({outgoingStore: store})
|
|
96
|
-
|
|
97
|
-
store.close = function (cb) {
|
|
98
|
-
cb(new Error('test'))
|
|
99
|
-
}
|
|
100
|
-
client.once('end', function () {
|
|
101
|
-
if (arguments.length === 0) {
|
|
102
|
-
return done()
|
|
103
|
-
}
|
|
104
|
-
throw new Error('no argument shoould be passed to event')
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
client.once('connect', function () {
|
|
108
|
-
client.end(function (test) {
|
|
109
|
-
if (test && test.message === 'test') {
|
|
110
|
-
return
|
|
111
|
-
}
|
|
112
|
-
throw new Error('bad argument passed to callback')
|
|
113
|
-
})
|
|
114
|
-
})
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
it('should return `this` if end called twice', function (done) {
|
|
118
|
-
var client = connect()
|
|
119
|
-
|
|
120
|
-
client.once('connect', function () {
|
|
121
|
-
client.end()
|
|
122
|
-
var value = client.end()
|
|
123
|
-
if (value === client) {
|
|
124
|
-
done()
|
|
125
|
-
} else {
|
|
126
|
-
done(new Error('Not returning client.'))
|
|
127
|
-
}
|
|
128
|
-
})
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
it('should emit end only on first client end', function (done) {
|
|
132
|
-
var client = connect()
|
|
133
|
-
|
|
134
|
-
client.once('end', function () {
|
|
135
|
-
var timeout = setTimeout(done.bind(null), 200)
|
|
136
|
-
client.once('end', function () {
|
|
137
|
-
clearTimeout(timeout)
|
|
138
|
-
done(new Error('end was emitted twice'))
|
|
139
|
-
})
|
|
140
|
-
client.end()
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
client.once('connect', client.end.bind(client))
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
it('should stop ping timer after end called', function (done) {
|
|
147
|
-
var client = connect()
|
|
148
|
-
|
|
149
|
-
client.once('connect', function () {
|
|
150
|
-
assert.exists(client.pingTimer)
|
|
151
|
-
client.end(() => {
|
|
152
|
-
assert.notExists(client.pingTimer)
|
|
153
|
-
done()
|
|
154
|
-
})
|
|
155
|
-
})
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
it('should be able to end even on a failed connection', function (done) {
|
|
159
|
-
var client = connect({host: 'this_hostname_should_not_exist'})
|
|
160
|
-
|
|
161
|
-
var timeout = setTimeout(function () {
|
|
162
|
-
done(new Error('Failed to end a disconnected client'))
|
|
163
|
-
}, 500)
|
|
164
|
-
|
|
165
|
-
setTimeout(function () {
|
|
166
|
-
client.end(function () {
|
|
167
|
-
clearTimeout(timeout)
|
|
168
|
-
done()
|
|
169
|
-
})
|
|
170
|
-
}, 200)
|
|
171
|
-
})
|
|
172
|
-
|
|
173
|
-
it('should emit end even on a failed connection', function (done) {
|
|
174
|
-
var client = connect({host: 'this_hostname_should_not_exist'})
|
|
175
|
-
|
|
176
|
-
var timeout = setTimeout(function () {
|
|
177
|
-
done(new Error('Disconnected client has failed to emit end'))
|
|
178
|
-
}, 500)
|
|
179
|
-
|
|
180
|
-
client.once('end', function () {
|
|
181
|
-
clearTimeout(timeout)
|
|
182
|
-
done()
|
|
183
|
-
})
|
|
184
|
-
|
|
185
|
-
// after 200ms manually invoke client.end
|
|
186
|
-
setTimeout(() => {
|
|
187
|
-
var boundEnd = client.end.bind(client)
|
|
188
|
-
boundEnd()
|
|
189
|
-
}, 200)
|
|
190
|
-
})
|
|
191
|
-
|
|
192
|
-
it.skip('should emit end only once for a reconnecting client', function (done) {
|
|
193
|
-
// I want to fix this test, but it will take signficant work, so I am marking it as a skipping test right now.
|
|
194
|
-
// Reason for it is that there are overlaps in the reconnectTimer and connectTimer. In the PR for this code
|
|
195
|
-
// there will be gists showing the difference between a successful test here and a failed test. For now we
|
|
196
|
-
// will add the retries syntax because of the flakiness.
|
|
197
|
-
var client = connect({host: 'this_hostname_should_not_exist', connectTimeout: 10, reconnectPeriod: 20})
|
|
198
|
-
setTimeout(done.bind(null), 1000)
|
|
199
|
-
var endCallback = function () {
|
|
200
|
-
assert.strictEqual(spy.callCount, 1, 'end was emitted more than once for reconnecting client')
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
var spy = sinon.spy(endCallback)
|
|
204
|
-
client.on('end', spy)
|
|
205
|
-
setTimeout(() => {
|
|
206
|
-
client.end.bind(client)
|
|
207
|
-
client.end()
|
|
208
|
-
}, 300)
|
|
209
|
-
})
|
|
210
|
-
})
|
|
211
|
-
|
|
212
|
-
describe('connecting', function () {
|
|
213
|
-
it('should connect to the broker', function (done) {
|
|
214
|
-
var client = connect()
|
|
215
|
-
client.on('error', done)
|
|
216
|
-
|
|
217
|
-
server.once('client', function () {
|
|
218
|
-
done()
|
|
219
|
-
client.end()
|
|
220
|
-
})
|
|
221
|
-
})
|
|
222
|
-
|
|
223
|
-
it('should send a default client id', function (done) {
|
|
224
|
-
var client = connect()
|
|
225
|
-
client.on('error', done)
|
|
226
|
-
|
|
227
|
-
server.once('client', function (serverClient) {
|
|
228
|
-
serverClient.once('connect', function (packet) {
|
|
229
|
-
assert.include(packet.clientId, 'mqttjs')
|
|
230
|
-
client.end(done)
|
|
231
|
-
serverClient.disconnect()
|
|
232
|
-
})
|
|
233
|
-
})
|
|
234
|
-
})
|
|
235
|
-
|
|
236
|
-
it('should send be clean by default', function (done) {
|
|
237
|
-
var client = connect()
|
|
238
|
-
client.on('error', done)
|
|
239
|
-
|
|
240
|
-
server.once('client', function (serverClient) {
|
|
241
|
-
serverClient.once('connect', function (packet) {
|
|
242
|
-
assert.strictEqual(packet.clean, true)
|
|
243
|
-
serverClient.disconnect()
|
|
244
|
-
done()
|
|
245
|
-
})
|
|
246
|
-
})
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
it('should connect with the given client id', function (done) {
|
|
250
|
-
var client = connect({clientId: 'testclient'})
|
|
251
|
-
client.on('error', function (err) {
|
|
252
|
-
throw err
|
|
253
|
-
})
|
|
254
|
-
|
|
255
|
-
server.once('client', function (serverClient) {
|
|
256
|
-
serverClient.once('connect', function (packet) {
|
|
257
|
-
assert.include(packet.clientId, 'testclient')
|
|
258
|
-
serverClient.disconnect()
|
|
259
|
-
client.end(function (err) {
|
|
260
|
-
done(err)
|
|
261
|
-
})
|
|
262
|
-
})
|
|
263
|
-
})
|
|
264
|
-
})
|
|
265
|
-
|
|
266
|
-
it('should connect with the client id and unclean state', function (done) {
|
|
267
|
-
var client = connect({clientId: 'testclient', clean: false})
|
|
268
|
-
client.on('error', function (err) {
|
|
269
|
-
throw err
|
|
270
|
-
})
|
|
271
|
-
|
|
272
|
-
server.once('client', function (serverClient) {
|
|
273
|
-
serverClient.once('connect', function (packet) {
|
|
274
|
-
assert.include(packet.clientId, 'testclient')
|
|
275
|
-
assert.isFalse(packet.clean)
|
|
276
|
-
serverClient.disconnect()
|
|
277
|
-
client.end(true, done)
|
|
278
|
-
})
|
|
279
|
-
})
|
|
280
|
-
})
|
|
281
|
-
|
|
282
|
-
it('should require a clientId with clean=false', function (done) {
|
|
283
|
-
try {
|
|
284
|
-
var client = connect({ clean: false })
|
|
285
|
-
client.on('error', function (err) {
|
|
286
|
-
done(err)
|
|
287
|
-
})
|
|
288
|
-
} catch (err) {
|
|
289
|
-
assert.strictEqual(err.message, 'Missing clientId for unclean clients')
|
|
290
|
-
done()
|
|
291
|
-
}
|
|
292
|
-
})
|
|
293
|
-
|
|
294
|
-
it('should default to localhost', function (done) {
|
|
295
|
-
var client = connect({clientId: 'testclient'})
|
|
296
|
-
client.on('error', function (err) {
|
|
297
|
-
throw err
|
|
298
|
-
})
|
|
299
|
-
|
|
300
|
-
server.once('client', function (serverClient) {
|
|
301
|
-
serverClient.once('connect', function (packet) {
|
|
302
|
-
assert.include(packet.clientId, 'testclient')
|
|
303
|
-
serverClient.disconnect()
|
|
304
|
-
done()
|
|
305
|
-
})
|
|
306
|
-
})
|
|
307
|
-
})
|
|
308
|
-
|
|
309
|
-
it('should emit connect', function (done) {
|
|
310
|
-
var client = connect()
|
|
311
|
-
client.once('connect', function () {
|
|
312
|
-
client.end(true, done)
|
|
313
|
-
})
|
|
314
|
-
client.once('error', done)
|
|
315
|
-
})
|
|
316
|
-
|
|
317
|
-
it('should provide connack packet with connect event', function (done) {
|
|
318
|
-
var connack = version === 5 ? {reasonCode: 0} : {returnCode: 0}
|
|
319
|
-
server.once('client', function (serverClient) {
|
|
320
|
-
connack.sessionPresent = true
|
|
321
|
-
serverClient.connack(connack)
|
|
322
|
-
server.once('client', function (serverClient) {
|
|
323
|
-
connack.sessionPresent = false
|
|
324
|
-
serverClient.connack(connack)
|
|
325
|
-
})
|
|
326
|
-
})
|
|
327
|
-
|
|
328
|
-
var client = connect()
|
|
329
|
-
client.once('connect', function (packet) {
|
|
330
|
-
assert.strictEqual(packet.sessionPresent, true)
|
|
331
|
-
client.once('connect', function (packet) {
|
|
332
|
-
assert.strictEqual(packet.sessionPresent, false)
|
|
333
|
-
client.end()
|
|
334
|
-
done()
|
|
335
|
-
})
|
|
336
|
-
})
|
|
337
|
-
})
|
|
338
|
-
|
|
339
|
-
it('should mark the client as connected', function (done) {
|
|
340
|
-
var client = connect()
|
|
341
|
-
client.once('connect', function () {
|
|
342
|
-
client.end()
|
|
343
|
-
if (client.connected) {
|
|
344
|
-
done()
|
|
345
|
-
} else {
|
|
346
|
-
done(new Error('Not marked as connected'))
|
|
347
|
-
}
|
|
348
|
-
})
|
|
349
|
-
})
|
|
350
|
-
|
|
351
|
-
it('should emit error on invalid clientId', function (done) {
|
|
352
|
-
var client = connect({clientId: 'invalid'})
|
|
353
|
-
client.once('connect', function () {
|
|
354
|
-
done(new Error('Should not emit connect'))
|
|
355
|
-
})
|
|
356
|
-
client.once('error', function (error) {
|
|
357
|
-
var value = version === 5 ? 128 : 2
|
|
358
|
-
assert.strictEqual(error.code, value) // code for clientID identifer rejected
|
|
359
|
-
client.end()
|
|
360
|
-
done()
|
|
361
|
-
})
|
|
362
|
-
})
|
|
363
|
-
|
|
364
|
-
it('should emit error event if the socket refuses the connection', function (done) {
|
|
365
|
-
// fake a port
|
|
366
|
-
var client = connect({ port: 4557 })
|
|
367
|
-
|
|
368
|
-
client.on('error', function (e) {
|
|
369
|
-
assert.equal(e.code, 'ECONNREFUSED')
|
|
370
|
-
client.end()
|
|
371
|
-
done()
|
|
372
|
-
})
|
|
373
|
-
})
|
|
374
|
-
|
|
375
|
-
it('should have different client ids', function (done) {
|
|
376
|
-
// bug identified in this test: the client.end callback is invoked twice, once when the `end`
|
|
377
|
-
// method completes closing the stores and invokes the callback, and another time when the
|
|
378
|
-
// stream is closed. When the stream is closed, for some reason the closeStores method is called
|
|
379
|
-
// a second time.
|
|
380
|
-
var client1 = connect()
|
|
381
|
-
var client2 = connect()
|
|
382
|
-
|
|
383
|
-
assert.notStrictEqual(client1.options.clientId, client2.options.clientId)
|
|
384
|
-
client1.end(true, () => {
|
|
385
|
-
client2.end(true, () => {
|
|
386
|
-
done()
|
|
387
|
-
})
|
|
388
|
-
})
|
|
389
|
-
})
|
|
390
|
-
})
|
|
391
|
-
|
|
392
|
-
describe('handling offline states', function () {
|
|
393
|
-
it('should emit offline event once when the client transitions from connected states to disconnected ones', function (done) {
|
|
394
|
-
var client = connect({reconnectPeriod: 20})
|
|
395
|
-
|
|
396
|
-
client.on('connect', function () {
|
|
397
|
-
this.stream.end()
|
|
398
|
-
})
|
|
399
|
-
|
|
400
|
-
client.on('offline', function () {
|
|
401
|
-
client.end(true, done)
|
|
402
|
-
})
|
|
403
|
-
})
|
|
404
|
-
|
|
405
|
-
it('should emit offline event once when the client (at first) can NOT connect to servers', function (done) {
|
|
406
|
-
// fake a port
|
|
407
|
-
var client = connect({ reconnectPeriod: 20, port: 4557 })
|
|
408
|
-
|
|
409
|
-
client.on('error', function () {})
|
|
410
|
-
|
|
411
|
-
client.on('offline', function () {
|
|
412
|
-
client.end(true, done)
|
|
413
|
-
})
|
|
414
|
-
})
|
|
415
|
-
})
|
|
416
|
-
|
|
417
|
-
describe('topic validations when subscribing', function () {
|
|
418
|
-
it('should be ok for well-formated topics', function (done) {
|
|
419
|
-
var client = connect()
|
|
420
|
-
client.subscribe(
|
|
421
|
-
[
|
|
422
|
-
'+', '+/event', 'event/+', '#', 'event/#', 'system/event/+',
|
|
423
|
-
'system/+/event', 'system/registry/event/#', 'system/+/event/#',
|
|
424
|
-
'system/registry/event/new_device', 'system/+/+/new_device'
|
|
425
|
-
],
|
|
426
|
-
function (err) {
|
|
427
|
-
client.end(function () {
|
|
428
|
-
if (err) {
|
|
429
|
-
return done(new Error(err))
|
|
430
|
-
}
|
|
431
|
-
done()
|
|
432
|
-
})
|
|
433
|
-
}
|
|
434
|
-
)
|
|
435
|
-
})
|
|
436
|
-
|
|
437
|
-
it('should return an error (via callbacks) for topic #/event', function (done) {
|
|
438
|
-
var client = connect()
|
|
439
|
-
client.subscribe(['#/event', 'event#', 'event+'], function (err) {
|
|
440
|
-
client.end(false, function () {
|
|
441
|
-
if (err) {
|
|
442
|
-
return done()
|
|
443
|
-
}
|
|
444
|
-
done(new Error('Validations do NOT work'))
|
|
445
|
-
})
|
|
446
|
-
})
|
|
447
|
-
})
|
|
448
|
-
|
|
449
|
-
it('should return an empty array for duplicate subs', function (done) {
|
|
450
|
-
var client = connect()
|
|
451
|
-
client.subscribe('event', function (err, granted1) {
|
|
452
|
-
if (err) {
|
|
453
|
-
return done(err)
|
|
454
|
-
}
|
|
455
|
-
client.subscribe('event', function (err, granted2) {
|
|
456
|
-
if (err) {
|
|
457
|
-
return done(err)
|
|
458
|
-
}
|
|
459
|
-
assert.isArray(granted2)
|
|
460
|
-
assert.isEmpty(granted2)
|
|
461
|
-
done()
|
|
462
|
-
})
|
|
463
|
-
})
|
|
464
|
-
})
|
|
465
|
-
|
|
466
|
-
it('should return an error (via callbacks) for topic #/event', function (done) {
|
|
467
|
-
var client = connect()
|
|
468
|
-
client.subscribe('#/event', function (err) {
|
|
469
|
-
client.end(function () {
|
|
470
|
-
if (err) {
|
|
471
|
-
return done()
|
|
472
|
-
}
|
|
473
|
-
done(new Error('Validations do NOT work'))
|
|
474
|
-
})
|
|
475
|
-
})
|
|
476
|
-
})
|
|
477
|
-
|
|
478
|
-
it('should return an error (via callbacks) for topic event#', function (done) {
|
|
479
|
-
var client = connect()
|
|
480
|
-
client.subscribe('event#', function (err) {
|
|
481
|
-
client.end(function () {
|
|
482
|
-
if (err) {
|
|
483
|
-
return done()
|
|
484
|
-
}
|
|
485
|
-
done(new Error('Validations do NOT work'))
|
|
486
|
-
})
|
|
487
|
-
})
|
|
488
|
-
})
|
|
489
|
-
|
|
490
|
-
it('should return an error (via callbacks) for topic system/#/event', function (done) {
|
|
491
|
-
var client = connect()
|
|
492
|
-
client.subscribe('system/#/event', function (err) {
|
|
493
|
-
client.end(function () {
|
|
494
|
-
if (err) {
|
|
495
|
-
return done()
|
|
496
|
-
}
|
|
497
|
-
done(new Error('Validations do NOT work'))
|
|
498
|
-
})
|
|
499
|
-
})
|
|
500
|
-
})
|
|
501
|
-
|
|
502
|
-
it('should return an error (via callbacks) for empty topic list', function (done) {
|
|
503
|
-
var client = connect()
|
|
504
|
-
client.subscribe([], function (err) {
|
|
505
|
-
client.end()
|
|
506
|
-
if (err) {
|
|
507
|
-
return done()
|
|
508
|
-
}
|
|
509
|
-
done(new Error('Validations do NOT work'))
|
|
510
|
-
})
|
|
511
|
-
})
|
|
512
|
-
|
|
513
|
-
it('should return an error (via callbacks) for topic system/+/#/event', function (done) {
|
|
514
|
-
var client = connect()
|
|
515
|
-
client.subscribe('system/+/#/event', function (err) {
|
|
516
|
-
client.end(true, function () {
|
|
517
|
-
if (err) {
|
|
518
|
-
return done()
|
|
519
|
-
}
|
|
520
|
-
done(new Error('Validations do NOT work'))
|
|
521
|
-
})
|
|
522
|
-
})
|
|
523
|
-
})
|
|
524
|
-
})
|
|
525
|
-
|
|
526
|
-
describe('offline messages', function () {
|
|
527
|
-
it('should queue message until connected', function (done) {
|
|
528
|
-
var client = connect()
|
|
529
|
-
|
|
530
|
-
client.publish('test', 'test')
|
|
531
|
-
client.subscribe('test')
|
|
532
|
-
client.unsubscribe('test')
|
|
533
|
-
assert.strictEqual(client.queue.length, 3)
|
|
534
|
-
|
|
535
|
-
client.once('connect', function () {
|
|
536
|
-
assert.strictEqual(client.queue.length, 0)
|
|
537
|
-
setTimeout(function () {
|
|
538
|
-
client.end(true, done)
|
|
539
|
-
}, 10)
|
|
540
|
-
})
|
|
541
|
-
})
|
|
542
|
-
|
|
543
|
-
it('should not queue qos 0 messages if queueQoSZero is false', function (done) {
|
|
544
|
-
var client = connect({queueQoSZero: false})
|
|
545
|
-
|
|
546
|
-
client.publish('test', 'test', {qos: 0})
|
|
547
|
-
assert.strictEqual(client.queue.length, 0)
|
|
548
|
-
client.on('connect', function () {
|
|
549
|
-
setTimeout(function () {
|
|
550
|
-
client.end(true, done)
|
|
551
|
-
}, 10)
|
|
552
|
-
})
|
|
553
|
-
})
|
|
554
|
-
|
|
555
|
-
it('should queue qos != 0 messages', function (done) {
|
|
556
|
-
var client = connect({queueQoSZero: false})
|
|
557
|
-
|
|
558
|
-
client.publish('test', 'test', {qos: 1})
|
|
559
|
-
client.publish('test', 'test', {qos: 2})
|
|
560
|
-
client.subscribe('test')
|
|
561
|
-
client.unsubscribe('test')
|
|
562
|
-
assert.strictEqual(client.queue.length, 2)
|
|
563
|
-
client.on('connect', function () {
|
|
564
|
-
setTimeout(function () {
|
|
565
|
-
client.end(true, done)
|
|
566
|
-
}, 10)
|
|
567
|
-
})
|
|
568
|
-
})
|
|
569
|
-
|
|
570
|
-
it('should not interrupt messages', function (done) {
|
|
571
|
-
var client = null
|
|
572
|
-
var incomingStore = new mqtt.Store({ clean: false })
|
|
573
|
-
var outgoingStore = new mqtt.Store({ clean: false })
|
|
574
|
-
var publishCount = 0
|
|
575
|
-
var server2 = new MqttServer(function (serverClient) {
|
|
576
|
-
serverClient.on('connect', function () {
|
|
577
|
-
serverClient.connack({returnCode: 0})
|
|
578
|
-
})
|
|
579
|
-
serverClient.on('publish', function (packet) {
|
|
580
|
-
if (packet.qos !== 0) {
|
|
581
|
-
serverClient.puback({messageId: packet.messageId})
|
|
582
|
-
}
|
|
583
|
-
switch (publishCount++) {
|
|
584
|
-
case 0:
|
|
585
|
-
assert.strictEqual(packet.payload.toString(), 'payload1')
|
|
586
|
-
break
|
|
587
|
-
case 1:
|
|
588
|
-
assert.strictEqual(packet.payload.toString(), 'payload2')
|
|
589
|
-
break
|
|
590
|
-
case 2:
|
|
591
|
-
assert.strictEqual(packet.payload.toString(), 'payload3')
|
|
592
|
-
break
|
|
593
|
-
case 3:
|
|
594
|
-
assert.strictEqual(packet.payload.toString(), 'payload4')
|
|
595
|
-
server2.close()
|
|
596
|
-
done()
|
|
597
|
-
break
|
|
598
|
-
}
|
|
599
|
-
})
|
|
600
|
-
})
|
|
601
|
-
|
|
602
|
-
server2.listen(ports.PORTAND50, function () {
|
|
603
|
-
client = mqtt.connect({
|
|
604
|
-
port: ports.PORTAND50,
|
|
605
|
-
host: 'localhost',
|
|
606
|
-
clean: false,
|
|
607
|
-
clientId: 'cid1',
|
|
608
|
-
reconnectPeriod: 0,
|
|
609
|
-
incomingStore: incomingStore,
|
|
610
|
-
outgoingStore: outgoingStore,
|
|
611
|
-
queueQoSZero: true
|
|
612
|
-
})
|
|
613
|
-
client.on('packetreceive', function (packet) {
|
|
614
|
-
if (packet.cmd === 'connack') {
|
|
615
|
-
setImmediate(
|
|
616
|
-
function () {
|
|
617
|
-
client.publish('test', 'payload3', {qos: 1})
|
|
618
|
-
client.publish('test', 'payload4', {qos: 0})
|
|
619
|
-
}
|
|
620
|
-
)
|
|
621
|
-
}
|
|
622
|
-
})
|
|
623
|
-
client.publish('test', 'payload1', {qos: 2})
|
|
624
|
-
client.publish('test', 'payload2', {qos: 2})
|
|
625
|
-
})
|
|
626
|
-
})
|
|
627
|
-
|
|
628
|
-
it('should call cb if an outgoing QoS 0 message is not sent', function (done) {
|
|
629
|
-
var client = connect({queueQoSZero: false})
|
|
630
|
-
var called = false
|
|
631
|
-
|
|
632
|
-
client.publish('test', 'test', {qos: 0}, function () {
|
|
633
|
-
called = true
|
|
634
|
-
})
|
|
635
|
-
|
|
636
|
-
client.on('connect', function () {
|
|
637
|
-
assert.isTrue(called)
|
|
638
|
-
setTimeout(function () {
|
|
639
|
-
client.end(true, done)
|
|
640
|
-
}, 10)
|
|
641
|
-
})
|
|
642
|
-
})
|
|
643
|
-
|
|
644
|
-
it('should delay ending up until all inflight messages are delivered', function (done) {
|
|
645
|
-
var client = connect()
|
|
646
|
-
var subscribeCalled = false
|
|
647
|
-
|
|
648
|
-
client.on('connect', function () {
|
|
649
|
-
client.subscribe('test', function () {
|
|
650
|
-
subscribeCalled = true
|
|
651
|
-
})
|
|
652
|
-
client.publish('test', 'test', function () {
|
|
653
|
-
client.end(false, function () {
|
|
654
|
-
assert.strictEqual(subscribeCalled, true)
|
|
655
|
-
done()
|
|
656
|
-
})
|
|
657
|
-
})
|
|
658
|
-
})
|
|
659
|
-
})
|
|
660
|
-
|
|
661
|
-
it('wait QoS 1 publish messages', function (done) {
|
|
662
|
-
var client = connect()
|
|
663
|
-
var messageReceived = false
|
|
664
|
-
|
|
665
|
-
client.on('connect', function () {
|
|
666
|
-
client.subscribe('test')
|
|
667
|
-
client.publish('test', 'test', { qos: 1 }, function () {
|
|
668
|
-
client.end(false, function () {
|
|
669
|
-
assert.strictEqual(messageReceived, true)
|
|
670
|
-
done()
|
|
671
|
-
})
|
|
672
|
-
})
|
|
673
|
-
client.on('message', function () {
|
|
674
|
-
messageReceived = true
|
|
675
|
-
})
|
|
676
|
-
})
|
|
677
|
-
|
|
678
|
-
server.once('client', function (serverClient) {
|
|
679
|
-
serverClient.on('subscribe', function () {
|
|
680
|
-
serverClient.on('publish', function (packet) {
|
|
681
|
-
serverClient.publish(packet)
|
|
682
|
-
})
|
|
683
|
-
})
|
|
684
|
-
})
|
|
685
|
-
})
|
|
686
|
-
|
|
687
|
-
it('does not wait acks when force-closing', function (done) {
|
|
688
|
-
// non-running broker
|
|
689
|
-
var client = connect('mqtt://localhost:8993')
|
|
690
|
-
client.publish('test', 'test', { qos: 1 })
|
|
691
|
-
client.end(true, done)
|
|
692
|
-
})
|
|
693
|
-
|
|
694
|
-
it('should call cb if store.put fails', function (done) {
|
|
695
|
-
const store = new Store()
|
|
696
|
-
store.put = function (packet, cb) {
|
|
697
|
-
process.nextTick(cb, new Error('oops there is an error'))
|
|
698
|
-
}
|
|
699
|
-
var client = connect({ incomingStore: store, outgoingStore: store })
|
|
700
|
-
client.publish('test', 'test', { qos: 2 }, function (err) {
|
|
701
|
-
if (err) {
|
|
702
|
-
client.end(true, done)
|
|
703
|
-
}
|
|
704
|
-
})
|
|
705
|
-
})
|
|
706
|
-
})
|
|
707
|
-
|
|
708
|
-
describe('publishing', function () {
|
|
709
|
-
it('should publish a message (offline)', function (done) {
|
|
710
|
-
var client = connect()
|
|
711
|
-
var payload = 'test'
|
|
712
|
-
var topic = 'test'
|
|
713
|
-
// don't wait on connect to send publish
|
|
714
|
-
client.publish(topic, payload)
|
|
715
|
-
|
|
716
|
-
server.on('client', onClient)
|
|
717
|
-
|
|
718
|
-
function onClient (serverClient) {
|
|
719
|
-
serverClient.once('connect', function () {
|
|
720
|
-
server.removeListener('client', onClient)
|
|
721
|
-
})
|
|
722
|
-
|
|
723
|
-
serverClient.once('publish', function (packet) {
|
|
724
|
-
assert.strictEqual(packet.topic, topic)
|
|
725
|
-
assert.strictEqual(packet.payload.toString(), payload)
|
|
726
|
-
assert.strictEqual(packet.qos, 0)
|
|
727
|
-
assert.strictEqual(packet.retain, false)
|
|
728
|
-
client.end(true, done)
|
|
729
|
-
})
|
|
730
|
-
}
|
|
731
|
-
})
|
|
732
|
-
|
|
733
|
-
it('should publish a message (online)', function (done) {
|
|
734
|
-
var client = connect()
|
|
735
|
-
var payload = 'test'
|
|
736
|
-
var topic = 'test'
|
|
737
|
-
// block on connect before sending publish
|
|
738
|
-
client.on('connect', function () {
|
|
739
|
-
client.publish(topic, payload)
|
|
740
|
-
})
|
|
741
|
-
|
|
742
|
-
server.on('client', onClient)
|
|
743
|
-
|
|
744
|
-
function onClient (serverClient) {
|
|
745
|
-
serverClient.once('connect', function () {
|
|
746
|
-
server.removeListener('client', onClient)
|
|
747
|
-
})
|
|
748
|
-
|
|
749
|
-
serverClient.once('publish', function (packet) {
|
|
750
|
-
assert.strictEqual(packet.topic, topic)
|
|
751
|
-
assert.strictEqual(packet.payload.toString(), payload)
|
|
752
|
-
assert.strictEqual(packet.qos, 0)
|
|
753
|
-
assert.strictEqual(packet.retain, false)
|
|
754
|
-
client.end(true, done)
|
|
755
|
-
})
|
|
756
|
-
}
|
|
757
|
-
})
|
|
758
|
-
|
|
759
|
-
it('should publish a message (retain, offline)', function (done) {
|
|
760
|
-
var client = connect({ queueQoSZero: true })
|
|
761
|
-
var payload = 'test'
|
|
762
|
-
var topic = 'test'
|
|
763
|
-
var called = false
|
|
764
|
-
|
|
765
|
-
client.publish(topic, payload, { retain: true }, function () {
|
|
766
|
-
called = true
|
|
767
|
-
})
|
|
768
|
-
|
|
769
|
-
server.once('client', function (serverClient) {
|
|
770
|
-
serverClient.once('publish', function (packet) {
|
|
771
|
-
assert.strictEqual(packet.topic, topic)
|
|
772
|
-
assert.strictEqual(packet.payload.toString(), payload)
|
|
773
|
-
assert.strictEqual(packet.qos, 0)
|
|
774
|
-
assert.strictEqual(packet.retain, true)
|
|
775
|
-
assert.strictEqual(called, true)
|
|
776
|
-
client.end(true, done)
|
|
777
|
-
})
|
|
778
|
-
})
|
|
779
|
-
})
|
|
780
|
-
|
|
781
|
-
it('should emit a packetsend event', function (done) {
|
|
782
|
-
var client = connect()
|
|
783
|
-
var payload = 'test_payload'
|
|
784
|
-
var topic = 'testTopic'
|
|
785
|
-
|
|
786
|
-
client.on('packetsend', function (packet) {
|
|
787
|
-
if (packet.cmd === 'publish') {
|
|
788
|
-
assert.strictEqual(packet.topic, topic)
|
|
789
|
-
assert.strictEqual(packet.payload.toString(), payload)
|
|
790
|
-
assert.strictEqual(packet.qos, 0)
|
|
791
|
-
assert.strictEqual(packet.retain, false)
|
|
792
|
-
client.end(true, done)
|
|
793
|
-
} else {
|
|
794
|
-
done(new Error('packet.cmd was not publish!'))
|
|
795
|
-
}
|
|
796
|
-
})
|
|
797
|
-
|
|
798
|
-
client.publish(topic, payload)
|
|
799
|
-
})
|
|
800
|
-
|
|
801
|
-
it('should accept options', function (done) {
|
|
802
|
-
var client = connect()
|
|
803
|
-
var payload = 'test'
|
|
804
|
-
var topic = 'test'
|
|
805
|
-
var opts = {
|
|
806
|
-
retain: true,
|
|
807
|
-
qos: 1
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
client.once('connect', function () {
|
|
811
|
-
client.publish(topic, payload, opts)
|
|
812
|
-
})
|
|
813
|
-
|
|
814
|
-
server.once('client', function (serverClient) {
|
|
815
|
-
serverClient.once('publish', function (packet) {
|
|
816
|
-
assert.strictEqual(packet.topic, topic)
|
|
817
|
-
assert.strictEqual(packet.payload.toString(), payload)
|
|
818
|
-
assert.strictEqual(packet.qos, opts.qos, 'incorrect qos')
|
|
819
|
-
assert.strictEqual(packet.retain, opts.retain, 'incorrect ret')
|
|
820
|
-
assert.strictEqual(packet.dup, false, 'incorrect dup')
|
|
821
|
-
client.end(done)
|
|
822
|
-
})
|
|
823
|
-
})
|
|
824
|
-
})
|
|
825
|
-
|
|
826
|
-
it('should publish with the default options for an empty parameter', function (done) {
|
|
827
|
-
var client = connect()
|
|
828
|
-
var payload = 'test'
|
|
829
|
-
var topic = 'test'
|
|
830
|
-
var defaultOpts = {qos: 0, retain: false, dup: false}
|
|
831
|
-
|
|
832
|
-
client.once('connect', function () {
|
|
833
|
-
client.publish(topic, payload, {})
|
|
834
|
-
})
|
|
835
|
-
|
|
836
|
-
server.once('client', function (serverClient) {
|
|
837
|
-
serverClient.once('publish', function (packet) {
|
|
838
|
-
assert.strictEqual(packet.topic, topic)
|
|
839
|
-
assert.strictEqual(packet.payload.toString(), payload)
|
|
840
|
-
assert.strictEqual(packet.qos, defaultOpts.qos, 'incorrect qos')
|
|
841
|
-
assert.strictEqual(packet.retain, defaultOpts.retain, 'incorrect ret')
|
|
842
|
-
assert.strictEqual(packet.dup, defaultOpts.dup, 'incorrect dup')
|
|
843
|
-
client.end(true, done)
|
|
844
|
-
})
|
|
845
|
-
})
|
|
846
|
-
})
|
|
847
|
-
|
|
848
|
-
it('should mark a message as duplicate when "dup" option is set', function (done) {
|
|
849
|
-
var client = connect()
|
|
850
|
-
var payload = 'duplicated-test'
|
|
851
|
-
var topic = 'test'
|
|
852
|
-
var opts = {
|
|
853
|
-
retain: true,
|
|
854
|
-
qos: 1,
|
|
855
|
-
dup: true
|
|
856
|
-
}
|
|
857
|
-
|
|
858
|
-
client.once('connect', function () {
|
|
859
|
-
client.publish(topic, payload, opts)
|
|
860
|
-
})
|
|
861
|
-
|
|
862
|
-
server.once('client', function (serverClient) {
|
|
863
|
-
serverClient.once('publish', function (packet) {
|
|
864
|
-
assert.strictEqual(packet.topic, topic)
|
|
865
|
-
assert.strictEqual(packet.payload.toString(), payload)
|
|
866
|
-
assert.strictEqual(packet.qos, opts.qos, 'incorrect qos')
|
|
867
|
-
assert.strictEqual(packet.retain, opts.retain, 'incorrect ret')
|
|
868
|
-
assert.strictEqual(packet.dup, opts.dup, 'incorrect dup')
|
|
869
|
-
client.end(done)
|
|
870
|
-
})
|
|
871
|
-
})
|
|
872
|
-
})
|
|
873
|
-
|
|
874
|
-
it('should fire a callback (qos 0)', function (done) {
|
|
875
|
-
var client = connect()
|
|
876
|
-
|
|
877
|
-
client.once('connect', function () {
|
|
878
|
-
client.publish('a', 'b', function () {
|
|
879
|
-
client.end()
|
|
880
|
-
done()
|
|
881
|
-
})
|
|
882
|
-
})
|
|
883
|
-
})
|
|
884
|
-
|
|
885
|
-
it('should fire a callback (qos 1)', function (done) {
|
|
886
|
-
var client = connect()
|
|
887
|
-
var opts = { qos: 1 }
|
|
888
|
-
|
|
889
|
-
client.once('connect', function () {
|
|
890
|
-
client.publish('a', 'b', opts, function () {
|
|
891
|
-
client.end()
|
|
892
|
-
done()
|
|
893
|
-
})
|
|
894
|
-
})
|
|
895
|
-
})
|
|
896
|
-
|
|
897
|
-
it('should fire a callback (qos 2)', function (done) {
|
|
898
|
-
var client = connect()
|
|
899
|
-
var opts = { qos: 2 }
|
|
900
|
-
|
|
901
|
-
client.once('connect', function () {
|
|
902
|
-
client.publish('a', 'b', opts, function () {
|
|
903
|
-
client.end()
|
|
904
|
-
done()
|
|
905
|
-
})
|
|
906
|
-
})
|
|
907
|
-
})
|
|
908
|
-
|
|
909
|
-
it('should support UTF-8 characters in topic', function (done) {
|
|
910
|
-
var client = connect()
|
|
911
|
-
|
|
912
|
-
client.once('connect', function () {
|
|
913
|
-
client.publish('中国', 'hello', function () {
|
|
914
|
-
client.end()
|
|
915
|
-
done()
|
|
916
|
-
})
|
|
917
|
-
})
|
|
918
|
-
})
|
|
919
|
-
|
|
920
|
-
it('should support UTF-8 characters in payload', function (done) {
|
|
921
|
-
var client = connect()
|
|
922
|
-
|
|
923
|
-
client.once('connect', function () {
|
|
924
|
-
client.publish('hello', '中国', function () {
|
|
925
|
-
client.end()
|
|
926
|
-
done()
|
|
927
|
-
})
|
|
928
|
-
})
|
|
929
|
-
})
|
|
930
|
-
|
|
931
|
-
it('should publish 10 QoS 2 and receive them', function (done) {
|
|
932
|
-
var client = connect()
|
|
933
|
-
var count = 0
|
|
934
|
-
|
|
935
|
-
client.on('connect', function () {
|
|
936
|
-
client.subscribe('test')
|
|
937
|
-
client.publish('test', 'test', { qos: 2 })
|
|
938
|
-
})
|
|
939
|
-
|
|
940
|
-
client.on('message', function () {
|
|
941
|
-
if (count >= 10) {
|
|
942
|
-
client.end()
|
|
943
|
-
done()
|
|
944
|
-
} else {
|
|
945
|
-
client.publish('test', 'test', { qos: 2 })
|
|
946
|
-
}
|
|
947
|
-
})
|
|
948
|
-
|
|
949
|
-
server.once('client', function (serverClient) {
|
|
950
|
-
serverClient.on('offline', function () {
|
|
951
|
-
client.end()
|
|
952
|
-
done('error went offline... didnt see this happen')
|
|
953
|
-
})
|
|
954
|
-
|
|
955
|
-
serverClient.on('subscribe', function () {
|
|
956
|
-
serverClient.on('publish', function (packet) {
|
|
957
|
-
serverClient.publish(packet)
|
|
958
|
-
})
|
|
959
|
-
})
|
|
960
|
-
|
|
961
|
-
serverClient.on('pubrel', function () {
|
|
962
|
-
count++
|
|
963
|
-
})
|
|
964
|
-
})
|
|
965
|
-
})
|
|
966
|
-
|
|
967
|
-
function testQosHandleMessage (qos, done) {
|
|
968
|
-
var client = connect()
|
|
969
|
-
|
|
970
|
-
var messageEventCount = 0
|
|
971
|
-
var handleMessageCount = 0
|
|
972
|
-
|
|
973
|
-
client.handleMessage = function (packet, callback) {
|
|
974
|
-
setTimeout(function () {
|
|
975
|
-
handleMessageCount++
|
|
976
|
-
// next message event should not emit until handleMessage completes
|
|
977
|
-
assert.strictEqual(handleMessageCount, messageEventCount)
|
|
978
|
-
if (handleMessageCount === 10) {
|
|
979
|
-
setTimeout(function () {
|
|
980
|
-
client.end(true, done)
|
|
981
|
-
})
|
|
982
|
-
}
|
|
983
|
-
callback()
|
|
984
|
-
}, 100)
|
|
985
|
-
}
|
|
986
|
-
|
|
987
|
-
client.on('message', function (topic, message, packet) {
|
|
988
|
-
messageEventCount++
|
|
989
|
-
})
|
|
990
|
-
|
|
991
|
-
client.on('connect', function () {
|
|
992
|
-
client.subscribe('test')
|
|
993
|
-
})
|
|
994
|
-
|
|
995
|
-
server.once('client', function (serverClient) {
|
|
996
|
-
serverClient.on('offline', function () {
|
|
997
|
-
client.end(true, function () {
|
|
998
|
-
done('error went offline... didnt see this happen')
|
|
999
|
-
})
|
|
1000
|
-
})
|
|
1001
|
-
|
|
1002
|
-
serverClient.on('subscribe', function () {
|
|
1003
|
-
for (var i = 0; i < 10; i++) {
|
|
1004
|
-
serverClient.publish({
|
|
1005
|
-
messageId: i,
|
|
1006
|
-
topic: 'test',
|
|
1007
|
-
payload: 'test' + i,
|
|
1008
|
-
qos: qos
|
|
1009
|
-
})
|
|
1010
|
-
}
|
|
1011
|
-
})
|
|
1012
|
-
})
|
|
1013
|
-
}
|
|
1014
|
-
|
|
1015
|
-
var qosTests = [ 0, 1, 2 ]
|
|
1016
|
-
qosTests.forEach(function (QoS) {
|
|
1017
|
-
it('should publish 10 QoS ' + QoS + 'and receive them only when `handleMessage` finishes', function (done) {
|
|
1018
|
-
testQosHandleMessage(QoS, done)
|
|
1019
|
-
})
|
|
1020
|
-
})
|
|
1021
|
-
|
|
1022
|
-
it('should not send a `puback` if the execution of `handleMessage` fails for messages with QoS `1`', function (done) {
|
|
1023
|
-
var client = connect()
|
|
1024
|
-
|
|
1025
|
-
client.handleMessage = function (packet, callback) {
|
|
1026
|
-
callback(new Error('Error thrown by the application'))
|
|
1027
|
-
}
|
|
1028
|
-
|
|
1029
|
-
client._sendPacket = sinon.spy()
|
|
1030
|
-
|
|
1031
|
-
client._handlePublish({
|
|
1032
|
-
messageId: Math.floor(65535 * Math.random()),
|
|
1033
|
-
topic: 'test',
|
|
1034
|
-
payload: 'test',
|
|
1035
|
-
qos: 1
|
|
1036
|
-
}, function (err) {
|
|
1037
|
-
assert.exists(err)
|
|
1038
|
-
})
|
|
1039
|
-
|
|
1040
|
-
assert.strictEqual(client._sendPacket.callCount, 0)
|
|
1041
|
-
client.end()
|
|
1042
|
-
client.on('connect', function () { done() })
|
|
1043
|
-
})
|
|
1044
|
-
|
|
1045
|
-
it('should silently ignore errors thrown by `handleMessage` and return when no callback is passed ' +
|
|
1046
|
-
'into `handlePublish` method', function (done) {
|
|
1047
|
-
var client = connect()
|
|
1048
|
-
|
|
1049
|
-
client.handleMessage = function (packet, callback) {
|
|
1050
|
-
callback(new Error('Error thrown by the application'))
|
|
1051
|
-
}
|
|
1052
|
-
|
|
1053
|
-
try {
|
|
1054
|
-
client._handlePublish({
|
|
1055
|
-
messageId: Math.floor(65535 * Math.random()),
|
|
1056
|
-
topic: 'test',
|
|
1057
|
-
payload: 'test',
|
|
1058
|
-
qos: 1
|
|
1059
|
-
})
|
|
1060
|
-
client.end(true, done)
|
|
1061
|
-
} catch (err) {
|
|
1062
|
-
client.end(true, () => { done(err) })
|
|
1063
|
-
}
|
|
1064
|
-
})
|
|
1065
|
-
|
|
1066
|
-
it('should handle error with async incoming store in QoS 1 `handlePublish` method', function (done) {
|
|
1067
|
-
class AsyncStore {
|
|
1068
|
-
put (packet, cb) {
|
|
1069
|
-
process.nextTick(function () {
|
|
1070
|
-
cb(null, 'Error')
|
|
1071
|
-
})
|
|
1072
|
-
}
|
|
1073
|
-
|
|
1074
|
-
close (cb) {
|
|
1075
|
-
cb()
|
|
1076
|
-
}
|
|
1077
|
-
}
|
|
1078
|
-
|
|
1079
|
-
var store = new AsyncStore()
|
|
1080
|
-
var client = connect({incomingStore: store})
|
|
1081
|
-
|
|
1082
|
-
client._handlePublish({
|
|
1083
|
-
messageId: 1,
|
|
1084
|
-
topic: 'test',
|
|
1085
|
-
payload: 'test',
|
|
1086
|
-
qos: 1
|
|
1087
|
-
}, function () {
|
|
1088
|
-
client.end()
|
|
1089
|
-
done()
|
|
1090
|
-
})
|
|
1091
|
-
})
|
|
1092
|
-
|
|
1093
|
-
it('should handle error with async incoming store in QoS 2 `handlePublish` method', function (done) {
|
|
1094
|
-
class AsyncStore {
|
|
1095
|
-
put (packet, cb) {
|
|
1096
|
-
process.nextTick(function () {
|
|
1097
|
-
cb(null, 'Error')
|
|
1098
|
-
})
|
|
1099
|
-
}
|
|
1100
|
-
|
|
1101
|
-
close (cb) {
|
|
1102
|
-
cb()
|
|
1103
|
-
}
|
|
1104
|
-
}
|
|
1105
|
-
|
|
1106
|
-
var store = new AsyncStore()
|
|
1107
|
-
var client = connect({incomingStore: store})
|
|
1108
|
-
|
|
1109
|
-
client._handlePublish({
|
|
1110
|
-
messageId: 1,
|
|
1111
|
-
topic: 'test',
|
|
1112
|
-
payload: 'test',
|
|
1113
|
-
qos: 2
|
|
1114
|
-
}, function () {
|
|
1115
|
-
client.end()
|
|
1116
|
-
done()
|
|
1117
|
-
})
|
|
1118
|
-
})
|
|
1119
|
-
|
|
1120
|
-
it('should handle error with async incoming store in QoS 2 `handlePubrel` method', function (done) {
|
|
1121
|
-
class AsyncStore {
|
|
1122
|
-
put (packet, cb) {
|
|
1123
|
-
process.nextTick(function () {
|
|
1124
|
-
cb(null, 'Error')
|
|
1125
|
-
})
|
|
1126
|
-
}
|
|
1127
|
-
|
|
1128
|
-
del (packet, cb) {
|
|
1129
|
-
process.nextTick(function () {
|
|
1130
|
-
cb(new Error('Error'))
|
|
1131
|
-
})
|
|
1132
|
-
}
|
|
1133
|
-
|
|
1134
|
-
get (packet, cb) {
|
|
1135
|
-
process.nextTick(function () {
|
|
1136
|
-
cb(null, {cmd: 'publish'})
|
|
1137
|
-
})
|
|
1138
|
-
}
|
|
1139
|
-
|
|
1140
|
-
close (cb) {
|
|
1141
|
-
cb()
|
|
1142
|
-
}
|
|
1143
|
-
}
|
|
1144
|
-
|
|
1145
|
-
var store = new AsyncStore()
|
|
1146
|
-
var client = connect({ incomingStore: store })
|
|
1147
|
-
|
|
1148
|
-
client._handlePubrel({
|
|
1149
|
-
messageId: 1,
|
|
1150
|
-
qos: 2
|
|
1151
|
-
}, function () {
|
|
1152
|
-
client.end(true, done)
|
|
1153
|
-
})
|
|
1154
|
-
})
|
|
1155
|
-
|
|
1156
|
-
it('should handle success with async incoming store in QoS 2 `handlePubrel` method', function (done) {
|
|
1157
|
-
var delComplete = false
|
|
1158
|
-
class AsyncStore {
|
|
1159
|
-
put (packet, cb) {
|
|
1160
|
-
process.nextTick(function () {
|
|
1161
|
-
cb(null, 'Error')
|
|
1162
|
-
})
|
|
1163
|
-
}
|
|
1164
|
-
|
|
1165
|
-
del (packet, cb) {
|
|
1166
|
-
process.nextTick(function () {
|
|
1167
|
-
delComplete = true
|
|
1168
|
-
cb(null)
|
|
1169
|
-
})
|
|
1170
|
-
}
|
|
1171
|
-
|
|
1172
|
-
get (packet, cb) {
|
|
1173
|
-
process.nextTick(function () {
|
|
1174
|
-
cb(null, {cmd: 'publish'})
|
|
1175
|
-
})
|
|
1176
|
-
}
|
|
1177
|
-
|
|
1178
|
-
close (cb) {
|
|
1179
|
-
cb()
|
|
1180
|
-
}
|
|
1181
|
-
}
|
|
1182
|
-
|
|
1183
|
-
var store = new AsyncStore()
|
|
1184
|
-
var client = connect({incomingStore: store})
|
|
1185
|
-
|
|
1186
|
-
client._handlePubrel({
|
|
1187
|
-
messageId: 1,
|
|
1188
|
-
qos: 2
|
|
1189
|
-
}, function () {
|
|
1190
|
-
assert.isTrue(delComplete)
|
|
1191
|
-
client.end(true, done)
|
|
1192
|
-
})
|
|
1193
|
-
})
|
|
1194
|
-
|
|
1195
|
-
it('should not send a `pubcomp` if the execution of `handleMessage` fails for messages with QoS `2`', function (done) {
|
|
1196
|
-
var store = new Store()
|
|
1197
|
-
var client = connect({incomingStore: store})
|
|
1198
|
-
|
|
1199
|
-
var messageId = Math.floor(65535 * Math.random())
|
|
1200
|
-
var topic = 'testTopic'
|
|
1201
|
-
var payload = 'testPayload'
|
|
1202
|
-
var qos = 2
|
|
1203
|
-
|
|
1204
|
-
client.handleMessage = function (packet, callback) {
|
|
1205
|
-
callback(new Error('Error thrown by the application'))
|
|
1206
|
-
}
|
|
1207
|
-
|
|
1208
|
-
client.once('connect', function () {
|
|
1209
|
-
client.subscribe(topic, {qos: 2})
|
|
1210
|
-
|
|
1211
|
-
store.put({
|
|
1212
|
-
messageId: messageId,
|
|
1213
|
-
topic: topic,
|
|
1214
|
-
payload: payload,
|
|
1215
|
-
qos: qos,
|
|
1216
|
-
cmd: 'publish'
|
|
1217
|
-
}, function () {
|
|
1218
|
-
// cleans up the client
|
|
1219
|
-
client._sendPacket = sinon.spy()
|
|
1220
|
-
client._handlePubrel({cmd: 'pubrel', messageId: messageId}, function (err) {
|
|
1221
|
-
assert.exists(err)
|
|
1222
|
-
assert.strictEqual(client._sendPacket.callCount, 0)
|
|
1223
|
-
client.end(true, done)
|
|
1224
|
-
})
|
|
1225
|
-
})
|
|
1226
|
-
})
|
|
1227
|
-
})
|
|
1228
|
-
|
|
1229
|
-
it('should silently ignore errors thrown by `handleMessage` and return when no callback is passed ' +
|
|
1230
|
-
'into `handlePubrel` method', function (done) {
|
|
1231
|
-
var store = new Store()
|
|
1232
|
-
var client = connect({incomingStore: store})
|
|
1233
|
-
|
|
1234
|
-
var messageId = Math.floor(65535 * Math.random())
|
|
1235
|
-
var topic = 'test'
|
|
1236
|
-
var payload = 'test'
|
|
1237
|
-
var qos = 2
|
|
1238
|
-
|
|
1239
|
-
client.handleMessage = function (packet, callback) {
|
|
1240
|
-
callback(new Error('Error thrown by the application'))
|
|
1241
|
-
}
|
|
1242
|
-
|
|
1243
|
-
client.once('connect', function () {
|
|
1244
|
-
client.subscribe(topic, {qos: 2})
|
|
1245
|
-
|
|
1246
|
-
store.put({
|
|
1247
|
-
messageId: messageId,
|
|
1248
|
-
topic: topic,
|
|
1249
|
-
payload: payload,
|
|
1250
|
-
qos: qos,
|
|
1251
|
-
cmd: 'publish'
|
|
1252
|
-
}, function () {
|
|
1253
|
-
try {
|
|
1254
|
-
client._handlePubrel({cmd: 'pubrel', messageId: messageId})
|
|
1255
|
-
client.end(true, done)
|
|
1256
|
-
} catch (err) {
|
|
1257
|
-
client.end(true, () => { done(err) })
|
|
1258
|
-
}
|
|
1259
|
-
})
|
|
1260
|
-
})
|
|
1261
|
-
})
|
|
1262
|
-
|
|
1263
|
-
it('should keep message order', function (done) {
|
|
1264
|
-
var publishCount = 0
|
|
1265
|
-
var reconnect = false
|
|
1266
|
-
var client = {}
|
|
1267
|
-
var incomingStore = new mqtt.Store({ clean: false })
|
|
1268
|
-
var outgoingStore = new mqtt.Store({ clean: false })
|
|
1269
|
-
var server2 = new MqttServer(function (serverClient) {
|
|
1270
|
-
// errors are not interesting for this test
|
|
1271
|
-
// but they might happen on some platforms
|
|
1272
|
-
serverClient.on('error', function () {})
|
|
1273
|
-
|
|
1274
|
-
serverClient.on('connect', function (packet) {
|
|
1275
|
-
serverClient.connack({returnCode: 0})
|
|
1276
|
-
})
|
|
1277
|
-
serverClient.on('publish', function (packet) {
|
|
1278
|
-
serverClient.puback({messageId: packet.messageId})
|
|
1279
|
-
if (reconnect) {
|
|
1280
|
-
switch (publishCount++) {
|
|
1281
|
-
case 0:
|
|
1282
|
-
assert.strictEqual(packet.payload.toString(), 'payload1')
|
|
1283
|
-
break
|
|
1284
|
-
case 1:
|
|
1285
|
-
assert.strictEqual(packet.payload.toString(), 'payload2')
|
|
1286
|
-
break
|
|
1287
|
-
case 2:
|
|
1288
|
-
assert.strictEqual(packet.payload.toString(), 'payload3')
|
|
1289
|
-
server2.close()
|
|
1290
|
-
done()
|
|
1291
|
-
break
|
|
1292
|
-
}
|
|
1293
|
-
}
|
|
1294
|
-
})
|
|
1295
|
-
})
|
|
1296
|
-
|
|
1297
|
-
server2.listen(ports.PORTAND50, function () {
|
|
1298
|
-
client = mqtt.connect({
|
|
1299
|
-
port: ports.PORTAND50,
|
|
1300
|
-
host: 'localhost',
|
|
1301
|
-
clean: false,
|
|
1302
|
-
clientId: 'cid1',
|
|
1303
|
-
reconnectPeriod: 0,
|
|
1304
|
-
incomingStore: incomingStore,
|
|
1305
|
-
outgoingStore: outgoingStore
|
|
1306
|
-
})
|
|
1307
|
-
|
|
1308
|
-
client.on('connect', function () {
|
|
1309
|
-
if (!reconnect) {
|
|
1310
|
-
client.publish('topic', 'payload1', {qos: 1})
|
|
1311
|
-
client.publish('topic', 'payload2', {qos: 1})
|
|
1312
|
-
client.end(true)
|
|
1313
|
-
} else {
|
|
1314
|
-
client.publish('topic', 'payload3', {qos: 1})
|
|
1315
|
-
}
|
|
1316
|
-
})
|
|
1317
|
-
client.on('close', function () {
|
|
1318
|
-
if (!reconnect) {
|
|
1319
|
-
client.reconnect({
|
|
1320
|
-
clean: false,
|
|
1321
|
-
incomingStore: incomingStore,
|
|
1322
|
-
outgoingStore: outgoingStore
|
|
1323
|
-
})
|
|
1324
|
-
reconnect = true
|
|
1325
|
-
}
|
|
1326
|
-
})
|
|
1327
|
-
})
|
|
1328
|
-
})
|
|
1329
|
-
|
|
1330
|
-
function testCallbackStorePutByQoS (qos, clean, expected, done) {
|
|
1331
|
-
var client = connect({
|
|
1332
|
-
clean: clean,
|
|
1333
|
-
clientId: 'testId'
|
|
1334
|
-
})
|
|
1335
|
-
|
|
1336
|
-
var callbacks = []
|
|
1337
|
-
|
|
1338
|
-
function cbStorePut () {
|
|
1339
|
-
callbacks.push('storeput')
|
|
1340
|
-
}
|
|
1341
|
-
|
|
1342
|
-
client.on('connect', function () {
|
|
1343
|
-
client.publish('test', 'test', {qos: qos, cbStorePut: cbStorePut}, function (err) {
|
|
1344
|
-
if (err) done(err)
|
|
1345
|
-
callbacks.push('publish')
|
|
1346
|
-
assert.deepEqual(callbacks, expected)
|
|
1347
|
-
client.end(true, done)
|
|
1348
|
-
})
|
|
1349
|
-
})
|
|
1350
|
-
}
|
|
1351
|
-
|
|
1352
|
-
var callbackStorePutByQoSParameters = [
|
|
1353
|
-
{args: [0, true], expected: ['publish']},
|
|
1354
|
-
{args: [0, false], expected: ['publish']},
|
|
1355
|
-
{args: [1, true], expected: ['storeput', 'publish']},
|
|
1356
|
-
{args: [1, false], expected: ['storeput', 'publish']},
|
|
1357
|
-
{args: [2, true], expected: ['storeput', 'publish']},
|
|
1358
|
-
{args: [2, false], expected: ['storeput', 'publish']}
|
|
1359
|
-
]
|
|
1360
|
-
|
|
1361
|
-
callbackStorePutByQoSParameters.forEach(function (test) {
|
|
1362
|
-
if (test.args[0] === 0) { // QoS 0
|
|
1363
|
-
it('should not call cbStorePut when publishing message with QoS `' + test.args[0] + '` and clean `' + test.args[1] + '`', function (done) {
|
|
1364
|
-
testCallbackStorePutByQoS(test.args[0], test.args[1], test.expected, done)
|
|
1365
|
-
})
|
|
1366
|
-
} else { // QoS 1 and 2
|
|
1367
|
-
it('should call cbStorePut before publish completes when publishing message with QoS `' + test.args[0] + '` and clean `' + test.args[1] + '`', function (done) {
|
|
1368
|
-
testCallbackStorePutByQoS(test.args[0], test.args[1], test.expected, done)
|
|
1369
|
-
})
|
|
1370
|
-
}
|
|
1371
|
-
})
|
|
1372
|
-
})
|
|
1373
|
-
|
|
1374
|
-
describe('unsubscribing', function () {
|
|
1375
|
-
it('should send an unsubscribe packet (offline)', function (done) {
|
|
1376
|
-
var client = connect()
|
|
1377
|
-
|
|
1378
|
-
client.unsubscribe('test')
|
|
1379
|
-
|
|
1380
|
-
server.once('client', function (serverClient) {
|
|
1381
|
-
serverClient.once('unsubscribe', function (packet) {
|
|
1382
|
-
assert.include(packet.unsubscriptions, 'test')
|
|
1383
|
-
client.end(done)
|
|
1384
|
-
})
|
|
1385
|
-
})
|
|
1386
|
-
})
|
|
1387
|
-
|
|
1388
|
-
it('should send an unsubscribe packet', function (done) {
|
|
1389
|
-
var client = connect()
|
|
1390
|
-
var topic = 'topic'
|
|
1391
|
-
|
|
1392
|
-
client.once('connect', function () {
|
|
1393
|
-
client.unsubscribe(topic)
|
|
1394
|
-
})
|
|
1395
|
-
|
|
1396
|
-
server.once('client', function (serverClient) {
|
|
1397
|
-
serverClient.once('unsubscribe', function (packet) {
|
|
1398
|
-
assert.include(packet.unsubscriptions, topic)
|
|
1399
|
-
client.end(done)
|
|
1400
|
-
})
|
|
1401
|
-
})
|
|
1402
|
-
})
|
|
1403
|
-
|
|
1404
|
-
it('should emit a packetsend event', function (done) {
|
|
1405
|
-
var client = connect()
|
|
1406
|
-
var testTopic = 'testTopic'
|
|
1407
|
-
|
|
1408
|
-
client.once('connect', function () {
|
|
1409
|
-
client.subscribe(testTopic)
|
|
1410
|
-
})
|
|
1411
|
-
|
|
1412
|
-
client.on('packetsend', function (packet) {
|
|
1413
|
-
if (packet.cmd === 'subscribe') {
|
|
1414
|
-
client.end(true, done)
|
|
1415
|
-
}
|
|
1416
|
-
})
|
|
1417
|
-
})
|
|
1418
|
-
|
|
1419
|
-
it('should emit a packetreceive event', function (done) {
|
|
1420
|
-
var client = connect()
|
|
1421
|
-
var testTopic = 'testTopic'
|
|
1422
|
-
|
|
1423
|
-
client.once('connect', function () {
|
|
1424
|
-
client.subscribe(testTopic)
|
|
1425
|
-
})
|
|
1426
|
-
|
|
1427
|
-
client.on('packetreceive', function (packet) {
|
|
1428
|
-
if (packet.cmd === 'suback') {
|
|
1429
|
-
client.end(true, done)
|
|
1430
|
-
}
|
|
1431
|
-
})
|
|
1432
|
-
})
|
|
1433
|
-
|
|
1434
|
-
it('should accept an array of unsubs', function (done) {
|
|
1435
|
-
var client = connect()
|
|
1436
|
-
var topics = ['topic1', 'topic2']
|
|
1437
|
-
|
|
1438
|
-
client.once('connect', function () {
|
|
1439
|
-
client.unsubscribe(topics)
|
|
1440
|
-
})
|
|
1441
|
-
|
|
1442
|
-
server.once('client', function (serverClient) {
|
|
1443
|
-
serverClient.once('unsubscribe', function (packet) {
|
|
1444
|
-
assert.deepStrictEqual(packet.unsubscriptions, topics)
|
|
1445
|
-
client.end(done)
|
|
1446
|
-
})
|
|
1447
|
-
})
|
|
1448
|
-
})
|
|
1449
|
-
|
|
1450
|
-
it('should fire a callback on unsuback', function (done) {
|
|
1451
|
-
var client = connect()
|
|
1452
|
-
var topic = 'topic'
|
|
1453
|
-
|
|
1454
|
-
client.once('connect', function () {
|
|
1455
|
-
client.unsubscribe(topic, () => {
|
|
1456
|
-
client.end(true, done)
|
|
1457
|
-
})
|
|
1458
|
-
})
|
|
1459
|
-
|
|
1460
|
-
server.once('client', function (serverClient) {
|
|
1461
|
-
serverClient.once('unsubscribe', function (packet) {
|
|
1462
|
-
serverClient.unsuback(packet)
|
|
1463
|
-
})
|
|
1464
|
-
})
|
|
1465
|
-
})
|
|
1466
|
-
|
|
1467
|
-
it('should unsubscribe from a chinese topic', function (done) {
|
|
1468
|
-
var client = connect()
|
|
1469
|
-
var topic = '中国'
|
|
1470
|
-
|
|
1471
|
-
client.once('connect', function () {
|
|
1472
|
-
client.unsubscribe(topic, () => {
|
|
1473
|
-
client.end(err => {
|
|
1474
|
-
done(err)
|
|
1475
|
-
})
|
|
1476
|
-
})
|
|
1477
|
-
})
|
|
1478
|
-
|
|
1479
|
-
server.once('client', function (serverClient) {
|
|
1480
|
-
serverClient.once('unsubscribe', function (packet) {
|
|
1481
|
-
assert.include(packet.unsubscriptions, topic)
|
|
1482
|
-
})
|
|
1483
|
-
})
|
|
1484
|
-
})
|
|
1485
|
-
})
|
|
1486
|
-
|
|
1487
|
-
describe('keepalive', function () {
|
|
1488
|
-
var clock
|
|
1489
|
-
|
|
1490
|
-
beforeEach(function () {
|
|
1491
|
-
clock = sinon.useFakeTimers()
|
|
1492
|
-
})
|
|
1493
|
-
|
|
1494
|
-
afterEach(function () {
|
|
1495
|
-
clock.restore()
|
|
1496
|
-
})
|
|
1497
|
-
|
|
1498
|
-
it('should checkPing at keepalive interval', function (done) {
|
|
1499
|
-
var interval = 3
|
|
1500
|
-
var client = connect({ keepalive: interval })
|
|
1501
|
-
|
|
1502
|
-
client._checkPing = sinon.spy()
|
|
1503
|
-
|
|
1504
|
-
client.once('connect', function () {
|
|
1505
|
-
clock.tick(interval * 1000)
|
|
1506
|
-
assert.strictEqual(client._checkPing.callCount, 1)
|
|
1507
|
-
|
|
1508
|
-
clock.tick(interval * 1000)
|
|
1509
|
-
assert.strictEqual(client._checkPing.callCount, 2)
|
|
1510
|
-
|
|
1511
|
-
clock.tick(interval * 1000)
|
|
1512
|
-
assert.strictEqual(client._checkPing.callCount, 3)
|
|
1513
|
-
|
|
1514
|
-
client.end(true, done)
|
|
1515
|
-
})
|
|
1516
|
-
})
|
|
1517
|
-
|
|
1518
|
-
it('should not checkPing if publishing at a higher rate than keepalive', function (done) {
|
|
1519
|
-
var intervalMs = 3000
|
|
1520
|
-
var client = connect({keepalive: intervalMs / 1000})
|
|
1521
|
-
|
|
1522
|
-
client._checkPing = sinon.spy()
|
|
1523
|
-
|
|
1524
|
-
client.once('connect', function () {
|
|
1525
|
-
client.publish('foo', 'bar')
|
|
1526
|
-
clock.tick(intervalMs - 1)
|
|
1527
|
-
client.publish('foo', 'bar')
|
|
1528
|
-
clock.tick(2)
|
|
1529
|
-
|
|
1530
|
-
assert.strictEqual(client._checkPing.callCount, 0)
|
|
1531
|
-
client.end(true, done)
|
|
1532
|
-
})
|
|
1533
|
-
})
|
|
1534
|
-
|
|
1535
|
-
it('should checkPing if publishing at a higher rate than keepalive and reschedulePings===false', function (done) {
|
|
1536
|
-
var intervalMs = 3000
|
|
1537
|
-
var client = connect({
|
|
1538
|
-
keepalive: intervalMs / 1000,
|
|
1539
|
-
reschedulePings: false
|
|
1540
|
-
})
|
|
1541
|
-
|
|
1542
|
-
client._checkPing = sinon.spy()
|
|
1543
|
-
|
|
1544
|
-
client.once('connect', function () {
|
|
1545
|
-
client.publish('foo', 'bar')
|
|
1546
|
-
clock.tick(intervalMs - 1)
|
|
1547
|
-
client.publish('foo', 'bar')
|
|
1548
|
-
clock.tick(2)
|
|
1549
|
-
|
|
1550
|
-
assert.strictEqual(client._checkPing.callCount, 1)
|
|
1551
|
-
client.end(true, done)
|
|
1552
|
-
})
|
|
1553
|
-
})
|
|
1554
|
-
})
|
|
1555
|
-
|
|
1556
|
-
describe('pinging', function () {
|
|
1557
|
-
it('should set a ping timer', function (done) {
|
|
1558
|
-
var client = connect({keepalive: 3})
|
|
1559
|
-
client.once('connect', function () {
|
|
1560
|
-
assert.exists(client.pingTimer)
|
|
1561
|
-
client.end(true, done)
|
|
1562
|
-
})
|
|
1563
|
-
})
|
|
1564
|
-
|
|
1565
|
-
it('should not set a ping timer keepalive=0', function (done) {
|
|
1566
|
-
var client = connect({keepalive: 0})
|
|
1567
|
-
client.on('connect', function () {
|
|
1568
|
-
assert.notExists(client.pingTimer)
|
|
1569
|
-
client.end(true, done)
|
|
1570
|
-
})
|
|
1571
|
-
})
|
|
1572
|
-
|
|
1573
|
-
it('should reconnect if pingresp is not sent', function (done) {
|
|
1574
|
-
var client = connect({keepalive: 1, reconnectPeriod: 100})
|
|
1575
|
-
|
|
1576
|
-
// Fake no pingresp being send by stubbing the _handlePingresp function
|
|
1577
|
-
client._handlePingresp = function () {}
|
|
1578
|
-
|
|
1579
|
-
client.once('connect', function () {
|
|
1580
|
-
client.once('connect', function () {
|
|
1581
|
-
client.end(true, done)
|
|
1582
|
-
})
|
|
1583
|
-
})
|
|
1584
|
-
})
|
|
1585
|
-
|
|
1586
|
-
it('should not reconnect if pingresp is successful', function (done) {
|
|
1587
|
-
var client = connect({keepalive: 100})
|
|
1588
|
-
client.once('close', function () {
|
|
1589
|
-
done(new Error('Client closed connection'))
|
|
1590
|
-
})
|
|
1591
|
-
setTimeout(done, 1000)
|
|
1592
|
-
})
|
|
1593
|
-
|
|
1594
|
-
it('should defer the next ping when sending a control packet', function (done) {
|
|
1595
|
-
var client = connect({keepalive: 1})
|
|
1596
|
-
|
|
1597
|
-
client.once('connect', function () {
|
|
1598
|
-
client._checkPing = sinon.spy()
|
|
1599
|
-
|
|
1600
|
-
client.publish('foo', 'bar')
|
|
1601
|
-
setTimeout(function () {
|
|
1602
|
-
assert.strictEqual(client._checkPing.callCount, 0)
|
|
1603
|
-
client.publish('foo', 'bar')
|
|
1604
|
-
|
|
1605
|
-
setTimeout(function () {
|
|
1606
|
-
assert.strictEqual(client._checkPing.callCount, 0)
|
|
1607
|
-
client.publish('foo', 'bar')
|
|
1608
|
-
|
|
1609
|
-
setTimeout(function () {
|
|
1610
|
-
assert.strictEqual(client._checkPing.callCount, 0)
|
|
1611
|
-
done()
|
|
1612
|
-
}, 75)
|
|
1613
|
-
}, 75)
|
|
1614
|
-
}, 75)
|
|
1615
|
-
})
|
|
1616
|
-
})
|
|
1617
|
-
})
|
|
1618
|
-
|
|
1619
|
-
describe('subscribing', function () {
|
|
1620
|
-
it('should send a subscribe message (offline)', function (done) {
|
|
1621
|
-
var client = connect()
|
|
1622
|
-
|
|
1623
|
-
client.subscribe('test')
|
|
1624
|
-
|
|
1625
|
-
server.once('client', function (serverClient) {
|
|
1626
|
-
serverClient.once('subscribe', function () {
|
|
1627
|
-
done()
|
|
1628
|
-
})
|
|
1629
|
-
})
|
|
1630
|
-
})
|
|
1631
|
-
|
|
1632
|
-
it('should send a subscribe message', function (done) {
|
|
1633
|
-
var client = connect()
|
|
1634
|
-
var topic = 'test'
|
|
1635
|
-
|
|
1636
|
-
client.once('connect', function () {
|
|
1637
|
-
client.subscribe(topic)
|
|
1638
|
-
})
|
|
1639
|
-
|
|
1640
|
-
server.once('client', function (serverClient) {
|
|
1641
|
-
serverClient.once('subscribe', function (packet) {
|
|
1642
|
-
var result = {
|
|
1643
|
-
topic: topic,
|
|
1644
|
-
qos: 0
|
|
1645
|
-
}
|
|
1646
|
-
if (version === 5) {
|
|
1647
|
-
result.nl = false
|
|
1648
|
-
result.rap = false
|
|
1649
|
-
result.rh = 0
|
|
1650
|
-
}
|
|
1651
|
-
assert.include(packet.subscriptions[0], result)
|
|
1652
|
-
done()
|
|
1653
|
-
})
|
|
1654
|
-
})
|
|
1655
|
-
})
|
|
1656
|
-
|
|
1657
|
-
it('should emit a packetsend event', function (done) {
|
|
1658
|
-
var client = connect()
|
|
1659
|
-
var testTopic = 'testTopic'
|
|
1660
|
-
|
|
1661
|
-
client.once('connect', function () {
|
|
1662
|
-
client.subscribe(testTopic)
|
|
1663
|
-
})
|
|
1664
|
-
|
|
1665
|
-
client.on('packetsend', function (packet) {
|
|
1666
|
-
if (packet.cmd === 'subscribe') {
|
|
1667
|
-
done()
|
|
1668
|
-
}
|
|
1669
|
-
})
|
|
1670
|
-
})
|
|
1671
|
-
|
|
1672
|
-
it('should emit a packetreceive event', function (done) {
|
|
1673
|
-
var client = connect()
|
|
1674
|
-
var testTopic = 'testTopic'
|
|
1675
|
-
|
|
1676
|
-
client.once('connect', function () {
|
|
1677
|
-
client.subscribe(testTopic)
|
|
1678
|
-
})
|
|
1679
|
-
|
|
1680
|
-
client.on('packetreceive', function (packet) {
|
|
1681
|
-
if (packet.cmd === 'suback') {
|
|
1682
|
-
done()
|
|
1683
|
-
}
|
|
1684
|
-
})
|
|
1685
|
-
})
|
|
1686
|
-
|
|
1687
|
-
it('should accept an array of subscriptions', function (done) {
|
|
1688
|
-
var client = connect()
|
|
1689
|
-
var subs = ['test1', 'test2']
|
|
1690
|
-
|
|
1691
|
-
client.once('connect', function () {
|
|
1692
|
-
client.subscribe(subs)
|
|
1693
|
-
})
|
|
1694
|
-
|
|
1695
|
-
server.once('client', function (serverClient) {
|
|
1696
|
-
serverClient.once('subscribe', function (packet) {
|
|
1697
|
-
// i.e. [{topic: 'a', qos: 0}, {topic: 'b', qos: 0}]
|
|
1698
|
-
var expected = subs.map(function (i) {
|
|
1699
|
-
var result = {topic: i, qos: 0}
|
|
1700
|
-
if (version === 5) {
|
|
1701
|
-
result.nl = false
|
|
1702
|
-
result.rap = false
|
|
1703
|
-
result.rh = 0
|
|
1704
|
-
}
|
|
1705
|
-
return result
|
|
1706
|
-
})
|
|
1707
|
-
|
|
1708
|
-
assert.deepStrictEqual(packet.subscriptions, expected)
|
|
1709
|
-
client.end(done)
|
|
1710
|
-
})
|
|
1711
|
-
})
|
|
1712
|
-
})
|
|
1713
|
-
|
|
1714
|
-
it('should accept a hash of subscriptions', function (done) {
|
|
1715
|
-
var client = connect()
|
|
1716
|
-
var topics = {
|
|
1717
|
-
test1: {qos: 0},
|
|
1718
|
-
test2: {qos: 1}
|
|
1719
|
-
}
|
|
1720
|
-
|
|
1721
|
-
client.once('connect', function () {
|
|
1722
|
-
client.subscribe(topics)
|
|
1723
|
-
})
|
|
1724
|
-
|
|
1725
|
-
server.once('client', function (serverClient) {
|
|
1726
|
-
serverClient.once('subscribe', function (packet) {
|
|
1727
|
-
var k
|
|
1728
|
-
var expected = []
|
|
1729
|
-
|
|
1730
|
-
for (k in topics) {
|
|
1731
|
-
if (topics.hasOwnProperty(k)) {
|
|
1732
|
-
var result = {
|
|
1733
|
-
topic: k,
|
|
1734
|
-
qos: topics[k].qos
|
|
1735
|
-
}
|
|
1736
|
-
if (version === 5) {
|
|
1737
|
-
result.nl = false
|
|
1738
|
-
result.rap = false
|
|
1739
|
-
result.rh = 0
|
|
1740
|
-
}
|
|
1741
|
-
expected.push(result)
|
|
1742
|
-
}
|
|
1743
|
-
}
|
|
1744
|
-
|
|
1745
|
-
assert.deepStrictEqual(packet.subscriptions, expected)
|
|
1746
|
-
client.end(done)
|
|
1747
|
-
})
|
|
1748
|
-
})
|
|
1749
|
-
})
|
|
1750
|
-
|
|
1751
|
-
it('should accept an options parameter', function (done) {
|
|
1752
|
-
var client = connect()
|
|
1753
|
-
var topic = 'test'
|
|
1754
|
-
var opts = {qos: 1}
|
|
1755
|
-
|
|
1756
|
-
client.once('connect', function () {
|
|
1757
|
-
client.subscribe(topic, opts)
|
|
1758
|
-
})
|
|
1759
|
-
|
|
1760
|
-
server.once('client', function (serverClient) {
|
|
1761
|
-
serverClient.once('subscribe', function (packet) {
|
|
1762
|
-
var expected = [{
|
|
1763
|
-
topic: topic,
|
|
1764
|
-
qos: 1
|
|
1765
|
-
}]
|
|
1766
|
-
|
|
1767
|
-
if (version === 5) {
|
|
1768
|
-
expected[0].nl = false
|
|
1769
|
-
expected[0].rap = false
|
|
1770
|
-
expected[0].rh = 0
|
|
1771
|
-
}
|
|
1772
|
-
|
|
1773
|
-
assert.deepStrictEqual(packet.subscriptions, expected)
|
|
1774
|
-
done()
|
|
1775
|
-
})
|
|
1776
|
-
})
|
|
1777
|
-
})
|
|
1778
|
-
|
|
1779
|
-
it('should subscribe with the default options for an empty options parameter', function (done) {
|
|
1780
|
-
var client = connect()
|
|
1781
|
-
var topic = 'test'
|
|
1782
|
-
var defaultOpts = {qos: 0}
|
|
1783
|
-
|
|
1784
|
-
client.once('connect', function () {
|
|
1785
|
-
client.subscribe(topic, {})
|
|
1786
|
-
})
|
|
1787
|
-
|
|
1788
|
-
server.once('client', function (serverClient) {
|
|
1789
|
-
serverClient.once('subscribe', function (packet) {
|
|
1790
|
-
var result = {
|
|
1791
|
-
topic: topic,
|
|
1792
|
-
qos: defaultOpts.qos
|
|
1793
|
-
}
|
|
1794
|
-
if (version === 5) {
|
|
1795
|
-
result.nl = false
|
|
1796
|
-
result.rap = false
|
|
1797
|
-
result.rh = 0
|
|
1798
|
-
}
|
|
1799
|
-
|
|
1800
|
-
assert.include(packet.subscriptions[0], result)
|
|
1801
|
-
client.end(err => done(err))
|
|
1802
|
-
})
|
|
1803
|
-
})
|
|
1804
|
-
})
|
|
1805
|
-
|
|
1806
|
-
it('should fire a callback on suback', function (done) {
|
|
1807
|
-
var client = connect()
|
|
1808
|
-
var topic = 'test'
|
|
1809
|
-
|
|
1810
|
-
client.once('connect', function () {
|
|
1811
|
-
client.subscribe(topic, { qos: 2 }, function (err, granted) {
|
|
1812
|
-
if (err) {
|
|
1813
|
-
done(err)
|
|
1814
|
-
} else {
|
|
1815
|
-
assert.exists(granted, 'granted not given')
|
|
1816
|
-
var expectedResult = {topic: 'test', qos: 2}
|
|
1817
|
-
if (version === 5) {
|
|
1818
|
-
expectedResult.nl = false
|
|
1819
|
-
expectedResult.rap = false
|
|
1820
|
-
expectedResult.rh = 0
|
|
1821
|
-
expectedResult.properties = undefined
|
|
1822
|
-
}
|
|
1823
|
-
assert.include(granted[0], expectedResult)
|
|
1824
|
-
client.end(err => done(err))
|
|
1825
|
-
}
|
|
1826
|
-
})
|
|
1827
|
-
})
|
|
1828
|
-
})
|
|
1829
|
-
|
|
1830
|
-
it('should fire a callback with error if disconnected (options provided)', function (done) {
|
|
1831
|
-
var client = connect()
|
|
1832
|
-
var topic = 'test'
|
|
1833
|
-
client.once('connect', function () {
|
|
1834
|
-
client.end(true, function () {
|
|
1835
|
-
client.subscribe(topic, {qos: 2}, function (err, granted) {
|
|
1836
|
-
assert.notExists(granted, 'granted given')
|
|
1837
|
-
assert.exists(err, 'no error given')
|
|
1838
|
-
done()
|
|
1839
|
-
})
|
|
1840
|
-
})
|
|
1841
|
-
})
|
|
1842
|
-
})
|
|
1843
|
-
|
|
1844
|
-
it('should fire a callback with error if disconnected (options not provided)', function (done) {
|
|
1845
|
-
var client = connect()
|
|
1846
|
-
var topic = 'test'
|
|
1847
|
-
|
|
1848
|
-
client.once('connect', function () {
|
|
1849
|
-
client.end(true, function () {
|
|
1850
|
-
client.subscribe(topic, function (err, granted) {
|
|
1851
|
-
assert.notExists(granted, 'granted given')
|
|
1852
|
-
assert.exists(err, 'no error given')
|
|
1853
|
-
done()
|
|
1854
|
-
})
|
|
1855
|
-
})
|
|
1856
|
-
})
|
|
1857
|
-
})
|
|
1858
|
-
|
|
1859
|
-
it('should subscribe with a chinese topic', function (done) {
|
|
1860
|
-
var client = connect()
|
|
1861
|
-
var topic = '中国'
|
|
1862
|
-
|
|
1863
|
-
client.once('connect', function () {
|
|
1864
|
-
client.subscribe(topic)
|
|
1865
|
-
})
|
|
1866
|
-
|
|
1867
|
-
server.once('client', function (serverClient) {
|
|
1868
|
-
serverClient.once('subscribe', function (packet) {
|
|
1869
|
-
var result = {
|
|
1870
|
-
topic: topic,
|
|
1871
|
-
qos: 0
|
|
1872
|
-
}
|
|
1873
|
-
if (version === 5) {
|
|
1874
|
-
result.nl = false
|
|
1875
|
-
result.rap = false
|
|
1876
|
-
result.rh = 0
|
|
1877
|
-
}
|
|
1878
|
-
assert.include(packet.subscriptions[0], result)
|
|
1879
|
-
client.end(done)
|
|
1880
|
-
})
|
|
1881
|
-
})
|
|
1882
|
-
})
|
|
1883
|
-
})
|
|
1884
|
-
|
|
1885
|
-
describe('receiving messages', function () {
|
|
1886
|
-
it('should fire the message event', function (done) {
|
|
1887
|
-
var client = connect()
|
|
1888
|
-
var testPacket = {
|
|
1889
|
-
topic: 'test',
|
|
1890
|
-
payload: 'message',
|
|
1891
|
-
retain: true,
|
|
1892
|
-
qos: 1,
|
|
1893
|
-
messageId: 5
|
|
1894
|
-
}
|
|
1895
|
-
|
|
1896
|
-
//
|
|
1897
|
-
client.subscribe(testPacket.topic)
|
|
1898
|
-
client.once('message', function (topic, message, packet) {
|
|
1899
|
-
assert.strictEqual(topic, testPacket.topic)
|
|
1900
|
-
assert.strictEqual(message.toString(), testPacket.payload)
|
|
1901
|
-
assert.strictEqual(packet.cmd, 'publish')
|
|
1902
|
-
client.end(true, done)
|
|
1903
|
-
})
|
|
1904
|
-
|
|
1905
|
-
server.once('client', function (serverClient) {
|
|
1906
|
-
serverClient.on('subscribe', function () {
|
|
1907
|
-
serverClient.publish(testPacket)
|
|
1908
|
-
})
|
|
1909
|
-
})
|
|
1910
|
-
})
|
|
1911
|
-
|
|
1912
|
-
it('should emit a packetreceive event', function (done) {
|
|
1913
|
-
var client = connect()
|
|
1914
|
-
var testPacket = {
|
|
1915
|
-
topic: 'test',
|
|
1916
|
-
payload: 'message',
|
|
1917
|
-
retain: true,
|
|
1918
|
-
qos: 1,
|
|
1919
|
-
messageId: 5
|
|
1920
|
-
}
|
|
1921
|
-
|
|
1922
|
-
client.subscribe(testPacket.topic)
|
|
1923
|
-
client.on('packetreceive', function (packet) {
|
|
1924
|
-
if (packet.cmd === 'publish') {
|
|
1925
|
-
assert.strictEqual(packet.qos, 1)
|
|
1926
|
-
assert.strictEqual(packet.topic, testPacket.topic)
|
|
1927
|
-
assert.strictEqual(packet.payload.toString(), testPacket.payload)
|
|
1928
|
-
assert.strictEqual(packet.retain, true)
|
|
1929
|
-
client.end(true, done)
|
|
1930
|
-
}
|
|
1931
|
-
})
|
|
1932
|
-
|
|
1933
|
-
server.once('client', function (serverClient) {
|
|
1934
|
-
serverClient.on('subscribe', function () {
|
|
1935
|
-
serverClient.publish(testPacket)
|
|
1936
|
-
})
|
|
1937
|
-
})
|
|
1938
|
-
})
|
|
1939
|
-
|
|
1940
|
-
it('should support binary data', function (done) {
|
|
1941
|
-
var client = connect({ encoding: 'binary' })
|
|
1942
|
-
var testPacket = {
|
|
1943
|
-
topic: 'test',
|
|
1944
|
-
payload: 'message',
|
|
1945
|
-
retain: true,
|
|
1946
|
-
qos: 1,
|
|
1947
|
-
messageId: 5
|
|
1948
|
-
}
|
|
1949
|
-
|
|
1950
|
-
client.subscribe(testPacket.topic)
|
|
1951
|
-
client.once('message', function (topic, message, packet) {
|
|
1952
|
-
assert.strictEqual(topic, testPacket.topic)
|
|
1953
|
-
assert.instanceOf(message, Buffer)
|
|
1954
|
-
assert.strictEqual(message.toString(), testPacket.payload)
|
|
1955
|
-
assert.strictEqual(packet.cmd, 'publish')
|
|
1956
|
-
client.end(true, done)
|
|
1957
|
-
})
|
|
1958
|
-
|
|
1959
|
-
server.once('client', function (serverClient) {
|
|
1960
|
-
serverClient.on('subscribe', function () {
|
|
1961
|
-
serverClient.publish(testPacket)
|
|
1962
|
-
})
|
|
1963
|
-
})
|
|
1964
|
-
})
|
|
1965
|
-
|
|
1966
|
-
it('should emit a message event (qos=2)', function (done) {
|
|
1967
|
-
var client = connect()
|
|
1968
|
-
var testPacket = {
|
|
1969
|
-
topic: 'test',
|
|
1970
|
-
payload: 'message',
|
|
1971
|
-
retain: true,
|
|
1972
|
-
qos: 2,
|
|
1973
|
-
messageId: 5
|
|
1974
|
-
}
|
|
1975
|
-
|
|
1976
|
-
server.testPublish = testPacket
|
|
1977
|
-
|
|
1978
|
-
client.subscribe(testPacket.topic)
|
|
1979
|
-
client.once('message', function (topic, message, packet) {
|
|
1980
|
-
assert.strictEqual(topic, testPacket.topic)
|
|
1981
|
-
assert.strictEqual(message.toString(), testPacket.payload)
|
|
1982
|
-
assert.strictEqual(packet.messageId, testPacket.messageId)
|
|
1983
|
-
assert.strictEqual(packet.qos, testPacket.qos)
|
|
1984
|
-
client.end(true, done)
|
|
1985
|
-
})
|
|
1986
|
-
|
|
1987
|
-
server.once('client', function (serverClient) {
|
|
1988
|
-
serverClient.on('subscribe', function () {
|
|
1989
|
-
serverClient.publish(testPacket)
|
|
1990
|
-
})
|
|
1991
|
-
})
|
|
1992
|
-
})
|
|
1993
|
-
|
|
1994
|
-
it('should emit a message event (qos=2) - repeated publish', function (done) {
|
|
1995
|
-
var client = connect()
|
|
1996
|
-
var testPacket = {
|
|
1997
|
-
topic: 'test',
|
|
1998
|
-
payload: 'message',
|
|
1999
|
-
retain: true,
|
|
2000
|
-
qos: 2,
|
|
2001
|
-
messageId: 5
|
|
2002
|
-
}
|
|
2003
|
-
|
|
2004
|
-
server.testPublish = testPacket
|
|
2005
|
-
|
|
2006
|
-
var messageHandler = function (topic, message, packet) {
|
|
2007
|
-
assert.strictEqual(topic, testPacket.topic)
|
|
2008
|
-
assert.strictEqual(message.toString(), testPacket.payload)
|
|
2009
|
-
assert.strictEqual(packet.messageId, testPacket.messageId)
|
|
2010
|
-
assert.strictEqual(packet.qos, testPacket.qos)
|
|
2011
|
-
|
|
2012
|
-
assert.strictEqual(spiedMessageHandler.callCount, 1)
|
|
2013
|
-
client.end(true, done)
|
|
2014
|
-
}
|
|
2015
|
-
|
|
2016
|
-
var spiedMessageHandler = sinon.spy(messageHandler)
|
|
2017
|
-
|
|
2018
|
-
client.subscribe(testPacket.topic)
|
|
2019
|
-
client.on('message', spiedMessageHandler)
|
|
2020
|
-
|
|
2021
|
-
server.once('client', function (serverClient) {
|
|
2022
|
-
serverClient.on('subscribe', function () {
|
|
2023
|
-
serverClient.publish(testPacket)
|
|
2024
|
-
// twice, should be ignored
|
|
2025
|
-
serverClient.publish(testPacket)
|
|
2026
|
-
})
|
|
2027
|
-
})
|
|
2028
|
-
})
|
|
2029
|
-
|
|
2030
|
-
it('should support a chinese topic', function (done) {
|
|
2031
|
-
var client = connect({ encoding: 'binary' })
|
|
2032
|
-
var testPacket = {
|
|
2033
|
-
topic: '国',
|
|
2034
|
-
payload: 'message',
|
|
2035
|
-
retain: true,
|
|
2036
|
-
qos: 1,
|
|
2037
|
-
messageId: 5
|
|
2038
|
-
}
|
|
2039
|
-
|
|
2040
|
-
client.subscribe(testPacket.topic)
|
|
2041
|
-
client.once('message', function (topic, message, packet) {
|
|
2042
|
-
assert.strictEqual(topic, testPacket.topic)
|
|
2043
|
-
assert.instanceOf(message, Buffer)
|
|
2044
|
-
assert.strictEqual(message.toString(), testPacket.payload)
|
|
2045
|
-
assert.strictEqual(packet.messageId, testPacket.messageId)
|
|
2046
|
-
assert.strictEqual(packet.qos, testPacket.qos)
|
|
2047
|
-
client.end(true, done)
|
|
2048
|
-
})
|
|
2049
|
-
|
|
2050
|
-
server.once('client', function (serverClient) {
|
|
2051
|
-
serverClient.on('subscribe', function () {
|
|
2052
|
-
serverClient.publish(testPacket)
|
|
2053
|
-
})
|
|
2054
|
-
})
|
|
2055
|
-
})
|
|
2056
|
-
})
|
|
2057
|
-
|
|
2058
|
-
describe('qos handling', function () {
|
|
2059
|
-
it('should follow qos 0 semantics (trivial)', function (done) {
|
|
2060
|
-
var client = connect()
|
|
2061
|
-
var testTopic = 'test'
|
|
2062
|
-
var testMessage = 'message'
|
|
2063
|
-
|
|
2064
|
-
client.once('connect', function () {
|
|
2065
|
-
client.subscribe(testTopic, {qos: 0}, () => {
|
|
2066
|
-
client.end(true, done)
|
|
2067
|
-
})
|
|
2068
|
-
})
|
|
2069
|
-
|
|
2070
|
-
server.once('client', function (serverClient) {
|
|
2071
|
-
serverClient.once('subscribe', function () {
|
|
2072
|
-
serverClient.publish({
|
|
2073
|
-
topic: testTopic,
|
|
2074
|
-
payload: testMessage,
|
|
2075
|
-
qos: 0,
|
|
2076
|
-
retain: false
|
|
2077
|
-
})
|
|
2078
|
-
})
|
|
2079
|
-
})
|
|
2080
|
-
})
|
|
2081
|
-
|
|
2082
|
-
it('should follow qos 1 semantics', function (done) {
|
|
2083
|
-
var client = connect()
|
|
2084
|
-
var testTopic = 'test'
|
|
2085
|
-
var testMessage = 'message'
|
|
2086
|
-
var mid = 50
|
|
2087
|
-
|
|
2088
|
-
client.once('connect', function () {
|
|
2089
|
-
client.subscribe(testTopic, {qos: 1})
|
|
2090
|
-
})
|
|
2091
|
-
|
|
2092
|
-
server.once('client', function (serverClient) {
|
|
2093
|
-
serverClient.once('subscribe', function () {
|
|
2094
|
-
serverClient.publish({
|
|
2095
|
-
topic: testTopic,
|
|
2096
|
-
payload: testMessage,
|
|
2097
|
-
messageId: mid,
|
|
2098
|
-
qos: 1
|
|
2099
|
-
})
|
|
2100
|
-
})
|
|
2101
|
-
|
|
2102
|
-
serverClient.once('puback', function (packet) {
|
|
2103
|
-
assert.strictEqual(packet.messageId, mid)
|
|
2104
|
-
client.end(done)
|
|
2105
|
-
})
|
|
2106
|
-
})
|
|
2107
|
-
})
|
|
2108
|
-
|
|
2109
|
-
it('should follow qos 2 semantics', function (done) {
|
|
2110
|
-
var client = connect()
|
|
2111
|
-
var testTopic = 'test'
|
|
2112
|
-
var testMessage = 'message'
|
|
2113
|
-
var mid = 253
|
|
2114
|
-
var publishReceived = 0
|
|
2115
|
-
var pubrecReceived = 0
|
|
2116
|
-
var pubrelReceived = 0
|
|
2117
|
-
|
|
2118
|
-
client.once('connect', function () {
|
|
2119
|
-
client.subscribe(testTopic, {qos: 2})
|
|
2120
|
-
})
|
|
2121
|
-
|
|
2122
|
-
client.on('packetreceive', (packet) => {
|
|
2123
|
-
switch (packet.cmd) {
|
|
2124
|
-
case 'connack':
|
|
2125
|
-
case 'suback':
|
|
2126
|
-
// expected, but not specifically part of QOS 2 semantics
|
|
2127
|
-
break
|
|
2128
|
-
case 'publish':
|
|
2129
|
-
assert.strictEqual(pubrecReceived, 0, 'server received pubrec before client sent')
|
|
2130
|
-
assert.strictEqual(pubrelReceived, 0, 'server received pubrec before client sent')
|
|
2131
|
-
publishReceived += 1
|
|
2132
|
-
break
|
|
2133
|
-
case 'pubrel':
|
|
2134
|
-
assert.strictEqual(publishReceived, 1, 'only 1 publish must be received before a pubrel')
|
|
2135
|
-
assert.strictEqual(pubrecReceived, 1, 'invalid number of PUBREC messages (not only 1)')
|
|
2136
|
-
pubrelReceived += 1
|
|
2137
|
-
break
|
|
2138
|
-
default:
|
|
2139
|
-
should.fail()
|
|
2140
|
-
}
|
|
2141
|
-
})
|
|
2142
|
-
|
|
2143
|
-
server.once('client', function (serverClient) {
|
|
2144
|
-
serverClient.once('subscribe', function () {
|
|
2145
|
-
serverClient.publish({
|
|
2146
|
-
topic: testTopic,
|
|
2147
|
-
payload: testMessage,
|
|
2148
|
-
qos: 2,
|
|
2149
|
-
messageId: mid
|
|
2150
|
-
})
|
|
2151
|
-
})
|
|
2152
|
-
|
|
2153
|
-
serverClient.on('pubrec', function () {
|
|
2154
|
-
assert.strictEqual(publishReceived, 1, 'invalid number of PUBLISH messages received')
|
|
2155
|
-
assert.strictEqual(pubrecReceived, 0, 'invalid number of PUBREC messages recevied')
|
|
2156
|
-
pubrecReceived += 1
|
|
2157
|
-
})
|
|
2158
|
-
|
|
2159
|
-
serverClient.once('pubcomp', function () {
|
|
2160
|
-
client.removeAllListeners()
|
|
2161
|
-
serverClient.removeAllListeners()
|
|
2162
|
-
assert.strictEqual(publishReceived, 1, 'invalid number of PUBLISH messages')
|
|
2163
|
-
assert.strictEqual(pubrecReceived, 1, 'invalid number of PUBREC messages')
|
|
2164
|
-
assert.strictEqual(pubrelReceived, 1, 'invalid nubmer of PUBREL messages')
|
|
2165
|
-
client.end(true, done)
|
|
2166
|
-
})
|
|
2167
|
-
})
|
|
2168
|
-
})
|
|
2169
|
-
|
|
2170
|
-
it('should should empty the incoming store after a qos 2 handshake is completed', function (done) {
|
|
2171
|
-
var client = connect()
|
|
2172
|
-
var testTopic = 'test'
|
|
2173
|
-
var testMessage = 'message'
|
|
2174
|
-
var mid = 253
|
|
2175
|
-
|
|
2176
|
-
client.once('connect', function () {
|
|
2177
|
-
client.subscribe(testTopic, {qos: 2})
|
|
2178
|
-
})
|
|
2179
|
-
|
|
2180
|
-
client.on('packetreceive', (packet) => {
|
|
2181
|
-
if (packet.cmd === 'pubrel') {
|
|
2182
|
-
assert.strictEqual(client.incomingStore._inflights.size, 1)
|
|
2183
|
-
}
|
|
2184
|
-
})
|
|
2185
|
-
|
|
2186
|
-
server.once('client', function (serverClient) {
|
|
2187
|
-
serverClient.once('subscribe', function () {
|
|
2188
|
-
serverClient.publish({
|
|
2189
|
-
topic: testTopic,
|
|
2190
|
-
payload: testMessage,
|
|
2191
|
-
qos: 2,
|
|
2192
|
-
messageId: mid
|
|
2193
|
-
})
|
|
2194
|
-
})
|
|
2195
|
-
|
|
2196
|
-
serverClient.once('pubcomp', function () {
|
|
2197
|
-
assert.strictEqual(client.incomingStore._inflights.size, 0)
|
|
2198
|
-
client.removeAllListeners()
|
|
2199
|
-
client.end(true, done)
|
|
2200
|
-
})
|
|
2201
|
-
})
|
|
2202
|
-
})
|
|
2203
|
-
|
|
2204
|
-
function testMultiplePubrel (shouldSendPubcompFail, done) {
|
|
2205
|
-
var client = connect()
|
|
2206
|
-
var testTopic = 'test'
|
|
2207
|
-
var testMessage = 'message'
|
|
2208
|
-
var mid = 253
|
|
2209
|
-
var pubcompCount = 0
|
|
2210
|
-
var pubrelCount = 0
|
|
2211
|
-
var handleMessageCount = 0
|
|
2212
|
-
var emitMessageCount = 0
|
|
2213
|
-
var origSendPacket = client._sendPacket
|
|
2214
|
-
var shouldSendFail
|
|
2215
|
-
|
|
2216
|
-
client.handleMessage = function (packet, callback) {
|
|
2217
|
-
handleMessageCount++
|
|
2218
|
-
callback()
|
|
2219
|
-
}
|
|
2220
|
-
|
|
2221
|
-
client.on('message', function () {
|
|
2222
|
-
emitMessageCount++
|
|
2223
|
-
})
|
|
2224
|
-
|
|
2225
|
-
client._sendPacket = function (packet, sendDone) {
|
|
2226
|
-
shouldSendFail = packet.cmd === 'pubcomp' && shouldSendPubcompFail
|
|
2227
|
-
if (sendDone) {
|
|
2228
|
-
sendDone(shouldSendFail ? new Error('testing pubcomp failure') : undefined)
|
|
2229
|
-
}
|
|
2230
|
-
|
|
2231
|
-
// send the mocked response
|
|
2232
|
-
switch (packet.cmd) {
|
|
2233
|
-
case 'subscribe':
|
|
2234
|
-
const suback = {cmd: 'suback', messageId: packet.messageId, granted: [2]}
|
|
2235
|
-
client._handlePacket(suback, function (err) {
|
|
2236
|
-
assert.isNotOk(err)
|
|
2237
|
-
})
|
|
2238
|
-
break
|
|
2239
|
-
case 'pubrec':
|
|
2240
|
-
case 'pubcomp':
|
|
2241
|
-
// for both pubrec and pubcomp, reply with pubrel, simulating the server not receiving the pubcomp
|
|
2242
|
-
if (packet.cmd === 'pubcomp') {
|
|
2243
|
-
pubcompCount++
|
|
2244
|
-
if (pubcompCount === 2) {
|
|
2245
|
-
// end the test once the client has gone through two rounds of replying to pubrel messages
|
|
2246
|
-
assert.strictEqual(pubrelCount, 2)
|
|
2247
|
-
assert.strictEqual(handleMessageCount, 1)
|
|
2248
|
-
assert.strictEqual(emitMessageCount, 1)
|
|
2249
|
-
client._sendPacket = origSendPacket
|
|
2250
|
-
client.end(true, done)
|
|
2251
|
-
break
|
|
2252
|
-
}
|
|
2253
|
-
}
|
|
2254
|
-
|
|
2255
|
-
// simulate the pubrel message, either in response to pubrec or to mock pubcomp failing to be received
|
|
2256
|
-
const pubrel = {cmd: 'pubrel', messageId: mid}
|
|
2257
|
-
pubrelCount++
|
|
2258
|
-
client._handlePacket(pubrel, function (err) {
|
|
2259
|
-
if (shouldSendFail) {
|
|
2260
|
-
assert.exists(err)
|
|
2261
|
-
assert.instanceOf(err, Error)
|
|
2262
|
-
} else {
|
|
2263
|
-
assert.notExists(err)
|
|
2264
|
-
}
|
|
2265
|
-
})
|
|
2266
|
-
break
|
|
2267
|
-
}
|
|
2268
|
-
}
|
|
2269
|
-
|
|
2270
|
-
client.once('connect', function () {
|
|
2271
|
-
client.subscribe(testTopic, {qos: 2})
|
|
2272
|
-
const publish = {cmd: 'publish', topic: testTopic, payload: testMessage, qos: 2, messageId: mid}
|
|
2273
|
-
client._handlePacket(publish, function (err) {
|
|
2274
|
-
assert.notExists(err)
|
|
2275
|
-
})
|
|
2276
|
-
})
|
|
2277
|
-
}
|
|
2278
|
-
|
|
2279
|
-
it('handle qos 2 messages exactly once when multiple pubrel received', function (done) {
|
|
2280
|
-
testMultiplePubrel(false, done)
|
|
2281
|
-
})
|
|
2282
|
-
|
|
2283
|
-
it('handle qos 2 messages exactly once when multiple pubrel received and sending pubcomp fails on client', function (done) {
|
|
2284
|
-
testMultiplePubrel(true, done)
|
|
2285
|
-
})
|
|
2286
|
-
})
|
|
2287
|
-
|
|
2288
|
-
describe('auto reconnect', function () {
|
|
2289
|
-
it('should mark the client disconnecting if #end called', function (done) {
|
|
2290
|
-
var client = connect()
|
|
2291
|
-
|
|
2292
|
-
client.end(true, err => {
|
|
2293
|
-
assert.isTrue(client.disconnecting)
|
|
2294
|
-
done(err)
|
|
2295
|
-
})
|
|
2296
|
-
})
|
|
2297
|
-
|
|
2298
|
-
it('should reconnect after stream disconnect', function (done) {
|
|
2299
|
-
var client = connect()
|
|
2300
|
-
|
|
2301
|
-
var tryReconnect = true
|
|
2302
|
-
|
|
2303
|
-
client.on('connect', function () {
|
|
2304
|
-
if (tryReconnect) {
|
|
2305
|
-
client.stream.end()
|
|
2306
|
-
tryReconnect = false
|
|
2307
|
-
} else {
|
|
2308
|
-
client.end(true, done)
|
|
2309
|
-
}
|
|
2310
|
-
})
|
|
2311
|
-
})
|
|
2312
|
-
|
|
2313
|
-
it('should emit \'reconnect\' when reconnecting', function (done) {
|
|
2314
|
-
var client = connect()
|
|
2315
|
-
var tryReconnect = true
|
|
2316
|
-
var reconnectEvent = false
|
|
2317
|
-
|
|
2318
|
-
client.on('reconnect', function () {
|
|
2319
|
-
reconnectEvent = true
|
|
2320
|
-
})
|
|
2321
|
-
|
|
2322
|
-
client.on('connect', function () {
|
|
2323
|
-
if (tryReconnect) {
|
|
2324
|
-
client.stream.end()
|
|
2325
|
-
tryReconnect = false
|
|
2326
|
-
} else {
|
|
2327
|
-
assert.isTrue(reconnectEvent)
|
|
2328
|
-
client.end(true, done)
|
|
2329
|
-
}
|
|
2330
|
-
})
|
|
2331
|
-
})
|
|
2332
|
-
|
|
2333
|
-
it('should emit \'offline\' after going offline', function (done) {
|
|
2334
|
-
var client = connect()
|
|
2335
|
-
|
|
2336
|
-
var tryReconnect = true
|
|
2337
|
-
var offlineEvent = false
|
|
2338
|
-
|
|
2339
|
-
client.on('offline', function () {
|
|
2340
|
-
offlineEvent = true
|
|
2341
|
-
})
|
|
2342
|
-
|
|
2343
|
-
client.on('connect', function () {
|
|
2344
|
-
if (tryReconnect) {
|
|
2345
|
-
client.stream.end()
|
|
2346
|
-
tryReconnect = false
|
|
2347
|
-
} else {
|
|
2348
|
-
assert.isTrue(offlineEvent)
|
|
2349
|
-
client.end(true, done)
|
|
2350
|
-
}
|
|
2351
|
-
})
|
|
2352
|
-
})
|
|
2353
|
-
|
|
2354
|
-
it('should not reconnect if it was ended by the user', function (done) {
|
|
2355
|
-
var client = connect()
|
|
2356
|
-
|
|
2357
|
-
client.on('connect', function () {
|
|
2358
|
-
client.end()
|
|
2359
|
-
done() // it will raise an exception if called two times
|
|
2360
|
-
})
|
|
2361
|
-
})
|
|
2362
|
-
|
|
2363
|
-
it('should setup a reconnect timer on disconnect', function (done) {
|
|
2364
|
-
var client = connect()
|
|
2365
|
-
|
|
2366
|
-
client.once('connect', function () {
|
|
2367
|
-
assert.notExists(client.reconnectTimer)
|
|
2368
|
-
client.stream.end()
|
|
2369
|
-
})
|
|
2370
|
-
|
|
2371
|
-
client.once('close', function () {
|
|
2372
|
-
assert.exists(client.reconnectTimer)
|
|
2373
|
-
client.end(true, done)
|
|
2374
|
-
})
|
|
2375
|
-
})
|
|
2376
|
-
|
|
2377
|
-
var reconnectPeriodTests = [ {period: 200}, {period: 2000}, {period: 4000} ]
|
|
2378
|
-
reconnectPeriodTests.forEach((test) => {
|
|
2379
|
-
it('should allow specification of a reconnect period', function (done) {
|
|
2380
|
-
var end
|
|
2381
|
-
var client = connect({reconnectPeriod: test.period})
|
|
2382
|
-
var reconnect = false
|
|
2383
|
-
var start = Date.now()
|
|
2384
|
-
|
|
2385
|
-
client.on('connect', function () {
|
|
2386
|
-
if (!reconnect) {
|
|
2387
|
-
client.stream.end()
|
|
2388
|
-
reconnect = true
|
|
2389
|
-
} else {
|
|
2390
|
-
end = Date.now()
|
|
2391
|
-
client.end(() => {
|
|
2392
|
-
if (end - start >= test.period - 200 && end - start <= test.period + 200) {
|
|
2393
|
-
// give the connection a 200 ms slush window
|
|
2394
|
-
done()
|
|
2395
|
-
} else {
|
|
2396
|
-
done(new Error('Strange reconnect period'))
|
|
2397
|
-
}
|
|
2398
|
-
})
|
|
2399
|
-
}
|
|
2400
|
-
})
|
|
2401
|
-
})
|
|
2402
|
-
})
|
|
2403
|
-
|
|
2404
|
-
it('should always cleanup successfully on reconnection', function (done) {
|
|
2405
|
-
var client = connect({host: 'this_hostname_should_not_exist', connectTimeout: 0, reconnectPeriod: 1})
|
|
2406
|
-
// bind client.end so that when it is called it is automatically passed in the done callback
|
|
2407
|
-
setTimeout(client.end.bind(client, done), 50)
|
|
2408
|
-
})
|
|
2409
|
-
|
|
2410
|
-
it('should resend in-flight QoS 1 publish messages from the client', function (done) {
|
|
2411
|
-
var client = connect({reconnectPeriod: 200})
|
|
2412
|
-
var serverPublished = false
|
|
2413
|
-
var clientCalledBack = false
|
|
2414
|
-
|
|
2415
|
-
server.once('client', function (serverClient) {
|
|
2416
|
-
serverClient.on('connect', function () {
|
|
2417
|
-
setImmediate(function () {
|
|
2418
|
-
serverClient.stream.destroy()
|
|
2419
|
-
})
|
|
2420
|
-
})
|
|
2421
|
-
|
|
2422
|
-
server.once('client', function (serverClientNew) {
|
|
2423
|
-
serverClientNew.on('publish', function () {
|
|
2424
|
-
serverPublished = true
|
|
2425
|
-
check()
|
|
2426
|
-
})
|
|
2427
|
-
})
|
|
2428
|
-
})
|
|
2429
|
-
|
|
2430
|
-
client.publish('hello', 'world', { qos: 1 }, function () {
|
|
2431
|
-
clientCalledBack = true
|
|
2432
|
-
check()
|
|
2433
|
-
})
|
|
2434
|
-
|
|
2435
|
-
function check () {
|
|
2436
|
-
if (serverPublished && clientCalledBack) {
|
|
2437
|
-
client.end(true, done)
|
|
2438
|
-
}
|
|
2439
|
-
}
|
|
2440
|
-
})
|
|
2441
|
-
|
|
2442
|
-
it('should not resend in-flight publish messages if disconnecting', function (done) {
|
|
2443
|
-
var client = connect({reconnectPeriod: 200})
|
|
2444
|
-
var serverPublished = false
|
|
2445
|
-
var clientCalledBack = false
|
|
2446
|
-
server.once('client', function (serverClient) {
|
|
2447
|
-
serverClient.on('connect', function () {
|
|
2448
|
-
setImmediate(function () {
|
|
2449
|
-
serverClient.stream.destroy()
|
|
2450
|
-
client.end(true, err => {
|
|
2451
|
-
assert.isFalse(serverPublished)
|
|
2452
|
-
assert.isFalse(clientCalledBack)
|
|
2453
|
-
done(err)
|
|
2454
|
-
})
|
|
2455
|
-
})
|
|
2456
|
-
})
|
|
2457
|
-
server.once('client', function (serverClientNew) {
|
|
2458
|
-
serverClientNew.on('publish', function () {
|
|
2459
|
-
serverPublished = true
|
|
2460
|
-
})
|
|
2461
|
-
})
|
|
2462
|
-
})
|
|
2463
|
-
client.publish('hello', 'world', { qos: 1 }, function () {
|
|
2464
|
-
clientCalledBack = true
|
|
2465
|
-
})
|
|
2466
|
-
})
|
|
2467
|
-
|
|
2468
|
-
it('should resend in-flight QoS 2 publish messages from the client', function (done) {
|
|
2469
|
-
var client = connect({reconnectPeriod: 200})
|
|
2470
|
-
var serverPublished = false
|
|
2471
|
-
var clientCalledBack = false
|
|
2472
|
-
|
|
2473
|
-
server.once('client', function (serverClient) {
|
|
2474
|
-
// ignore errors
|
|
2475
|
-
serverClient.on('error', function () {})
|
|
2476
|
-
serverClient.on('publish', function () {
|
|
2477
|
-
setImmediate(function () {
|
|
2478
|
-
serverClient.stream.destroy()
|
|
2479
|
-
})
|
|
2480
|
-
})
|
|
2481
|
-
|
|
2482
|
-
server.once('client', function (serverClientNew) {
|
|
2483
|
-
serverClientNew.on('pubrel', function () {
|
|
2484
|
-
serverPublished = true
|
|
2485
|
-
check()
|
|
2486
|
-
})
|
|
2487
|
-
})
|
|
2488
|
-
})
|
|
2489
|
-
|
|
2490
|
-
client.publish('hello', 'world', { qos: 2 }, function () {
|
|
2491
|
-
clientCalledBack = true
|
|
2492
|
-
check()
|
|
2493
|
-
})
|
|
2494
|
-
|
|
2495
|
-
function check () {
|
|
2496
|
-
if (serverPublished && clientCalledBack) {
|
|
2497
|
-
client.end(true, done)
|
|
2498
|
-
}
|
|
2499
|
-
}
|
|
2500
|
-
})
|
|
2501
|
-
|
|
2502
|
-
it('should not resend in-flight QoS 1 removed publish messages from the client', function (done) {
|
|
2503
|
-
var client = connect({reconnectPeriod: 200})
|
|
2504
|
-
var clientCalledBack = false
|
|
2505
|
-
|
|
2506
|
-
server.once('client', function (serverClient) {
|
|
2507
|
-
serverClient.on('connect', function () {
|
|
2508
|
-
setImmediate(function () {
|
|
2509
|
-
serverClient.stream.destroy()
|
|
2510
|
-
})
|
|
2511
|
-
})
|
|
2512
|
-
|
|
2513
|
-
server.once('client', function (serverClientNew) {
|
|
2514
|
-
serverClientNew.on('publish', function () {
|
|
2515
|
-
should.fail()
|
|
2516
|
-
done()
|
|
2517
|
-
})
|
|
2518
|
-
})
|
|
2519
|
-
})
|
|
2520
|
-
|
|
2521
|
-
client.publish('hello', 'world', { qos: 1 }, function (err) {
|
|
2522
|
-
clientCalledBack = true
|
|
2523
|
-
assert.exists(err, 'error should exist')
|
|
2524
|
-
assert.strictEqual(err.message, 'Message removed', 'error message is incorrect')
|
|
2525
|
-
})
|
|
2526
|
-
assert.strictEqual(Object.keys(client.outgoing).length, 1)
|
|
2527
|
-
assert.strictEqual(client.outgoingStore._inflights.size, 1)
|
|
2528
|
-
client.removeOutgoingMessage(client.getLastMessageId())
|
|
2529
|
-
assert.strictEqual(Object.keys(client.outgoing).length, 0)
|
|
2530
|
-
assert.strictEqual(client.outgoingStore._inflights.size, 0)
|
|
2531
|
-
assert.isTrue(clientCalledBack)
|
|
2532
|
-
client.end(true, (err) => {
|
|
2533
|
-
done(err)
|
|
2534
|
-
})
|
|
2535
|
-
})
|
|
2536
|
-
|
|
2537
|
-
it('should not resend in-flight QoS 2 removed publish messages from the client', function (done) {
|
|
2538
|
-
var client = connect({reconnectPeriod: 200})
|
|
2539
|
-
var clientCalledBack = false
|
|
2540
|
-
|
|
2541
|
-
server.once('client', function (serverClient) {
|
|
2542
|
-
serverClient.on('connect', function () {
|
|
2543
|
-
setImmediate(function () {
|
|
2544
|
-
serverClient.stream.destroy()
|
|
2545
|
-
})
|
|
2546
|
-
})
|
|
2547
|
-
|
|
2548
|
-
server.once('client', function (serverClientNew) {
|
|
2549
|
-
serverClientNew.on('publish', function () {
|
|
2550
|
-
should.fail()
|
|
2551
|
-
done()
|
|
2552
|
-
})
|
|
2553
|
-
})
|
|
2554
|
-
})
|
|
2555
|
-
|
|
2556
|
-
client.publish('hello', 'world', { qos: 2 }, function (err) {
|
|
2557
|
-
clientCalledBack = true
|
|
2558
|
-
assert.strictEqual(err.message, 'Message removed')
|
|
2559
|
-
})
|
|
2560
|
-
assert.strictEqual(Object.keys(client.outgoing).length, 1)
|
|
2561
|
-
assert.strictEqual(client.outgoingStore._inflights.size, 1)
|
|
2562
|
-
client.removeOutgoingMessage(client.getLastMessageId())
|
|
2563
|
-
assert.strictEqual(Object.keys(client.outgoing).length, 0)
|
|
2564
|
-
assert.strictEqual(client.outgoingStore._inflights.size, 0)
|
|
2565
|
-
assert.isTrue(clientCalledBack)
|
|
2566
|
-
client.end(true, done)
|
|
2567
|
-
})
|
|
2568
|
-
|
|
2569
|
-
it('should resubscribe when reconnecting', function (done) {
|
|
2570
|
-
var client = connect({ reconnectPeriod: 100 })
|
|
2571
|
-
var tryReconnect = true
|
|
2572
|
-
var reconnectEvent = false
|
|
2573
|
-
|
|
2574
|
-
client.on('reconnect', function () {
|
|
2575
|
-
reconnectEvent = true
|
|
2576
|
-
})
|
|
2577
|
-
|
|
2578
|
-
client.on('connect', function () {
|
|
2579
|
-
if (tryReconnect) {
|
|
2580
|
-
client.subscribe('hello', function () {
|
|
2581
|
-
client.stream.end()
|
|
2582
|
-
|
|
2583
|
-
server.once('client', function (serverClient) {
|
|
2584
|
-
serverClient.on('subscribe', function () {
|
|
2585
|
-
client.end(done)
|
|
2586
|
-
})
|
|
2587
|
-
})
|
|
2588
|
-
})
|
|
2589
|
-
|
|
2590
|
-
tryReconnect = false
|
|
2591
|
-
} else {
|
|
2592
|
-
assert.isTrue(reconnectEvent)
|
|
2593
|
-
}
|
|
2594
|
-
})
|
|
2595
|
-
})
|
|
2596
|
-
|
|
2597
|
-
it('should not resubscribe when reconnecting if resubscribe is disabled', function (done) {
|
|
2598
|
-
var client = connect({ reconnectPeriod: 100, resubscribe: false })
|
|
2599
|
-
var tryReconnect = true
|
|
2600
|
-
var reconnectEvent = false
|
|
2601
|
-
|
|
2602
|
-
client.on('reconnect', function () {
|
|
2603
|
-
reconnectEvent = true
|
|
2604
|
-
})
|
|
2605
|
-
|
|
2606
|
-
client.on('connect', function () {
|
|
2607
|
-
if (tryReconnect) {
|
|
2608
|
-
client.subscribe('hello', function () {
|
|
2609
|
-
client.stream.end()
|
|
2610
|
-
|
|
2611
|
-
server.once('client', function (serverClient) {
|
|
2612
|
-
serverClient.on('subscribe', function () {
|
|
2613
|
-
should.fail()
|
|
2614
|
-
})
|
|
2615
|
-
})
|
|
2616
|
-
})
|
|
2617
|
-
|
|
2618
|
-
tryReconnect = false
|
|
2619
|
-
} else {
|
|
2620
|
-
assert.isTrue(reconnectEvent)
|
|
2621
|
-
assert.strictEqual(Object.keys(client._resubscribeTopics).length, 0)
|
|
2622
|
-
client.end(true, done)
|
|
2623
|
-
}
|
|
2624
|
-
})
|
|
2625
|
-
})
|
|
2626
|
-
|
|
2627
|
-
it('should not resubscribe when reconnecting if suback is error', function (done) {
|
|
2628
|
-
var tryReconnect = true
|
|
2629
|
-
var reconnectEvent = false
|
|
2630
|
-
var server2 = new MqttServer(function (serverClient) {
|
|
2631
|
-
serverClient.on('connect', function (packet) {
|
|
2632
|
-
serverClient.connack({returnCode: 0})
|
|
2633
|
-
})
|
|
2634
|
-
serverClient.on('subscribe', function (packet) {
|
|
2635
|
-
serverClient.suback({
|
|
2636
|
-
messageId: packet.messageId,
|
|
2637
|
-
granted: packet.subscriptions.map(function (e) {
|
|
2638
|
-
return e.qos | 0x80
|
|
2639
|
-
})
|
|
2640
|
-
})
|
|
2641
|
-
serverClient.pubrel({ messageId: Math.floor(Math.random() * 9000) + 1000 })
|
|
2642
|
-
})
|
|
2643
|
-
})
|
|
2644
|
-
|
|
2645
|
-
server2.listen(ports.PORTAND49, function () {
|
|
2646
|
-
var client = mqtt.connect({
|
|
2647
|
-
port: ports.PORTAND49,
|
|
2648
|
-
host: 'localhost',
|
|
2649
|
-
reconnectPeriod: 100
|
|
2650
|
-
})
|
|
2651
|
-
|
|
2652
|
-
client.on('reconnect', function () {
|
|
2653
|
-
reconnectEvent = true
|
|
2654
|
-
})
|
|
2655
|
-
|
|
2656
|
-
client.on('connect', function () {
|
|
2657
|
-
if (tryReconnect) {
|
|
2658
|
-
client.subscribe('hello', function () {
|
|
2659
|
-
client.stream.end()
|
|
2660
|
-
|
|
2661
|
-
server.once('client', function (serverClient) {
|
|
2662
|
-
serverClient.on('subscribe', function () {
|
|
2663
|
-
should.fail()
|
|
2664
|
-
})
|
|
2665
|
-
})
|
|
2666
|
-
})
|
|
2667
|
-
tryReconnect = false
|
|
2668
|
-
} else {
|
|
2669
|
-
assert.isTrue(reconnectEvent)
|
|
2670
|
-
assert.strictEqual(Object.keys(client._resubscribeTopics).length, 0)
|
|
2671
|
-
server2.close()
|
|
2672
|
-
client.end(true, done)
|
|
2673
|
-
}
|
|
2674
|
-
})
|
|
2675
|
-
})
|
|
2676
|
-
})
|
|
2677
|
-
|
|
2678
|
-
it('should preserved incomingStore after disconnecting if clean is false', function (done) {
|
|
2679
|
-
var reconnect = false
|
|
2680
|
-
var client = {}
|
|
2681
|
-
var incomingStore = new mqtt.Store({ clean: false })
|
|
2682
|
-
var outgoingStore = new mqtt.Store({ clean: false })
|
|
2683
|
-
var server2 = new MqttServer(function (serverClient) {
|
|
2684
|
-
serverClient.on('connect', function (packet) {
|
|
2685
|
-
serverClient.connack({returnCode: 0})
|
|
2686
|
-
if (reconnect) {
|
|
2687
|
-
serverClient.pubrel({ messageId: 1 })
|
|
2688
|
-
}
|
|
2689
|
-
})
|
|
2690
|
-
serverClient.on('subscribe', function (packet) {
|
|
2691
|
-
serverClient.suback({
|
|
2692
|
-
messageId: packet.messageId,
|
|
2693
|
-
granted: packet.subscriptions.map(function (e) {
|
|
2694
|
-
return e.qos
|
|
2695
|
-
})
|
|
2696
|
-
})
|
|
2697
|
-
serverClient.publish({ topic: 'topic', payload: 'payload', qos: 2, messageId: 1, retain: false })
|
|
2698
|
-
})
|
|
2699
|
-
serverClient.on('pubrec', function (packet) {
|
|
2700
|
-
client.end(false, function () {
|
|
2701
|
-
client.reconnect({
|
|
2702
|
-
incomingStore: incomingStore,
|
|
2703
|
-
outgoingStore: outgoingStore
|
|
2704
|
-
})
|
|
2705
|
-
})
|
|
2706
|
-
})
|
|
2707
|
-
serverClient.on('pubcomp', function (packet) {
|
|
2708
|
-
client.end(true, () => {
|
|
2709
|
-
server2.close()
|
|
2710
|
-
done()
|
|
2711
|
-
})
|
|
2712
|
-
})
|
|
2713
|
-
})
|
|
2714
|
-
|
|
2715
|
-
server2.listen(ports.PORTAND50, function () {
|
|
2716
|
-
client = mqtt.connect({
|
|
2717
|
-
port: ports.PORTAND50,
|
|
2718
|
-
host: 'localhost',
|
|
2719
|
-
clean: false,
|
|
2720
|
-
clientId: 'cid1',
|
|
2721
|
-
reconnectPeriod: 0,
|
|
2722
|
-
incomingStore: incomingStore,
|
|
2723
|
-
outgoingStore: outgoingStore
|
|
2724
|
-
})
|
|
2725
|
-
|
|
2726
|
-
client.on('connect', function () {
|
|
2727
|
-
if (!reconnect) {
|
|
2728
|
-
client.subscribe('test', {qos: 2}, function () {
|
|
2729
|
-
})
|
|
2730
|
-
reconnect = true
|
|
2731
|
-
}
|
|
2732
|
-
})
|
|
2733
|
-
client.on('message', function (topic, message) {
|
|
2734
|
-
assert.strictEqual(topic, 'topic')
|
|
2735
|
-
assert.strictEqual(message.toString(), 'payload')
|
|
2736
|
-
})
|
|
2737
|
-
})
|
|
2738
|
-
})
|
|
2739
|
-
|
|
2740
|
-
it('should clear outgoing if close from server', function (done) {
|
|
2741
|
-
var reconnect = false
|
|
2742
|
-
var client = {}
|
|
2743
|
-
var server2 = new MqttServer(function (serverClient) {
|
|
2744
|
-
serverClient.on('connect', function (packet) {
|
|
2745
|
-
serverClient.connack({returnCode: 0})
|
|
2746
|
-
})
|
|
2747
|
-
serverClient.on('subscribe', function (packet) {
|
|
2748
|
-
if (reconnect) {
|
|
2749
|
-
serverClient.suback({
|
|
2750
|
-
messageId: packet.messageId,
|
|
2751
|
-
granted: packet.subscriptions.map(function (e) {
|
|
2752
|
-
return e.qos
|
|
2753
|
-
})
|
|
2754
|
-
})
|
|
2755
|
-
} else {
|
|
2756
|
-
serverClient.destroy()
|
|
2757
|
-
}
|
|
2758
|
-
})
|
|
2759
|
-
})
|
|
2760
|
-
|
|
2761
|
-
server2.listen(ports.PORTAND50, function () {
|
|
2762
|
-
client = mqtt.connect({
|
|
2763
|
-
port: ports.PORTAND50,
|
|
2764
|
-
host: 'localhost',
|
|
2765
|
-
clean: true,
|
|
2766
|
-
clientId: 'cid1',
|
|
2767
|
-
reconnectPeriod: 0
|
|
2768
|
-
})
|
|
2769
|
-
|
|
2770
|
-
client.on('connect', function () {
|
|
2771
|
-
client.subscribe('test', {qos: 2}, function (e) {
|
|
2772
|
-
if (!e) {
|
|
2773
|
-
client.end()
|
|
2774
|
-
}
|
|
2775
|
-
})
|
|
2776
|
-
})
|
|
2777
|
-
|
|
2778
|
-
client.on('close', function () {
|
|
2779
|
-
if (reconnect) {
|
|
2780
|
-
server2.close()
|
|
2781
|
-
done()
|
|
2782
|
-
} else {
|
|
2783
|
-
assert.strictEqual(Object.keys(client.outgoing).length, 0)
|
|
2784
|
-
reconnect = true
|
|
2785
|
-
client.reconnect()
|
|
2786
|
-
}
|
|
2787
|
-
})
|
|
2788
|
-
})
|
|
2789
|
-
})
|
|
2790
|
-
|
|
2791
|
-
it('should resend in-flight QoS 1 publish messages from the client if clean is false', function (done) {
|
|
2792
|
-
var reconnect = false
|
|
2793
|
-
var client = {}
|
|
2794
|
-
var incomingStore = new mqtt.Store({ clean: false })
|
|
2795
|
-
var outgoingStore = new mqtt.Store({ clean: false })
|
|
2796
|
-
var server2 = new MqttServer(function (serverClient) {
|
|
2797
|
-
serverClient.on('connect', function (packet) {
|
|
2798
|
-
serverClient.connack({returnCode: 0})
|
|
2799
|
-
})
|
|
2800
|
-
serverClient.on('publish', function (packet) {
|
|
2801
|
-
if (reconnect) {
|
|
2802
|
-
server2.close()
|
|
2803
|
-
client.end(true, done)
|
|
2804
|
-
} else {
|
|
2805
|
-
client.end(true, () => {
|
|
2806
|
-
client.reconnect({
|
|
2807
|
-
incomingStore: incomingStore,
|
|
2808
|
-
outgoingStore: outgoingStore
|
|
2809
|
-
})
|
|
2810
|
-
reconnect = true
|
|
2811
|
-
})
|
|
2812
|
-
}
|
|
2813
|
-
})
|
|
2814
|
-
})
|
|
2815
|
-
|
|
2816
|
-
server2.listen(ports.PORTAND50, function () {
|
|
2817
|
-
client = mqtt.connect({
|
|
2818
|
-
port: ports.PORTAND50,
|
|
2819
|
-
host: 'localhost',
|
|
2820
|
-
clean: false,
|
|
2821
|
-
clientId: 'cid1',
|
|
2822
|
-
reconnectPeriod: 0,
|
|
2823
|
-
incomingStore: incomingStore,
|
|
2824
|
-
outgoingStore: outgoingStore
|
|
2825
|
-
})
|
|
2826
|
-
|
|
2827
|
-
client.on('connect', function () {
|
|
2828
|
-
if (!reconnect) {
|
|
2829
|
-
client.publish('topic', 'payload', {qos: 1})
|
|
2830
|
-
}
|
|
2831
|
-
})
|
|
2832
|
-
client.on('error', function () {})
|
|
2833
|
-
})
|
|
2834
|
-
})
|
|
2835
|
-
|
|
2836
|
-
it('should resend in-flight QoS 2 publish messages from the client if clean is false', function (done) {
|
|
2837
|
-
var reconnect = false
|
|
2838
|
-
var client = {}
|
|
2839
|
-
var incomingStore = new mqtt.Store({ clean: false })
|
|
2840
|
-
var outgoingStore = new mqtt.Store({ clean: false })
|
|
2841
|
-
var server2 = new MqttServer(function (serverClient) {
|
|
2842
|
-
serverClient.on('connect', function (packet) {
|
|
2843
|
-
serverClient.connack({returnCode: 0})
|
|
2844
|
-
})
|
|
2845
|
-
serverClient.on('publish', function (packet) {
|
|
2846
|
-
if (reconnect) {
|
|
2847
|
-
server2.close()
|
|
2848
|
-
client.end(true, done)
|
|
2849
|
-
} else {
|
|
2850
|
-
client.end(true, function () {
|
|
2851
|
-
client.reconnect({
|
|
2852
|
-
incomingStore: incomingStore,
|
|
2853
|
-
outgoingStore: outgoingStore
|
|
2854
|
-
})
|
|
2855
|
-
reconnect = true
|
|
2856
|
-
})
|
|
2857
|
-
}
|
|
2858
|
-
})
|
|
2859
|
-
})
|
|
2860
|
-
|
|
2861
|
-
server2.listen(ports.PORTAND50, function () {
|
|
2862
|
-
client = mqtt.connect({
|
|
2863
|
-
port: ports.PORTAND50,
|
|
2864
|
-
host: 'localhost',
|
|
2865
|
-
clean: false,
|
|
2866
|
-
clientId: 'cid1',
|
|
2867
|
-
reconnectPeriod: 0,
|
|
2868
|
-
incomingStore: incomingStore,
|
|
2869
|
-
outgoingStore: outgoingStore
|
|
2870
|
-
})
|
|
2871
|
-
|
|
2872
|
-
client.on('connect', function () {
|
|
2873
|
-
if (!reconnect) {
|
|
2874
|
-
client.publish('topic', 'payload', {qos: 2})
|
|
2875
|
-
}
|
|
2876
|
-
})
|
|
2877
|
-
client.on('error', function () {})
|
|
2878
|
-
})
|
|
2879
|
-
})
|
|
2880
|
-
|
|
2881
|
-
it('should resend in-flight QoS 2 pubrel messages from the client if clean is false', function (done) {
|
|
2882
|
-
var reconnect = false
|
|
2883
|
-
var client = {}
|
|
2884
|
-
var incomingStore = new mqtt.Store({ clean: false })
|
|
2885
|
-
var outgoingStore = new mqtt.Store({ clean: false })
|
|
2886
|
-
var server2 = new MqttServer(function (serverClient) {
|
|
2887
|
-
serverClient.on('connect', function (packet) {
|
|
2888
|
-
serverClient.connack({returnCode: 0})
|
|
2889
|
-
})
|
|
2890
|
-
serverClient.on('publish', function (packet) {
|
|
2891
|
-
if (!reconnect) {
|
|
2892
|
-
serverClient.pubrec({messageId: packet.messageId})
|
|
2893
|
-
}
|
|
2894
|
-
})
|
|
2895
|
-
serverClient.on('pubrel', function () {
|
|
2896
|
-
if (reconnect) {
|
|
2897
|
-
server2.close()
|
|
2898
|
-
client.end(true, done)
|
|
2899
|
-
} else {
|
|
2900
|
-
client.end(true, function () {
|
|
2901
|
-
client.reconnect({
|
|
2902
|
-
incomingStore: incomingStore,
|
|
2903
|
-
outgoingStore: outgoingStore
|
|
2904
|
-
})
|
|
2905
|
-
reconnect = true
|
|
2906
|
-
})
|
|
2907
|
-
}
|
|
2908
|
-
})
|
|
2909
|
-
})
|
|
2910
|
-
|
|
2911
|
-
server2.listen(ports.PORTAND50, function () {
|
|
2912
|
-
client = mqtt.connect({
|
|
2913
|
-
port: ports.PORTAND50,
|
|
2914
|
-
host: 'localhost',
|
|
2915
|
-
clean: false,
|
|
2916
|
-
clientId: 'cid1',
|
|
2917
|
-
reconnectPeriod: 0,
|
|
2918
|
-
incomingStore: incomingStore,
|
|
2919
|
-
outgoingStore: outgoingStore
|
|
2920
|
-
})
|
|
2921
|
-
|
|
2922
|
-
client.on('connect', function () {
|
|
2923
|
-
if (!reconnect) {
|
|
2924
|
-
client.publish('topic', 'payload', {qos: 2})
|
|
2925
|
-
}
|
|
2926
|
-
})
|
|
2927
|
-
client.on('error', function () {})
|
|
2928
|
-
})
|
|
2929
|
-
})
|
|
2930
|
-
|
|
2931
|
-
it('should resend in-flight publish messages by published order', function (done) {
|
|
2932
|
-
var publishCount = 0
|
|
2933
|
-
var reconnect = false
|
|
2934
|
-
var disconnectOnce = true
|
|
2935
|
-
var client = {}
|
|
2936
|
-
var incomingStore = new mqtt.Store({ clean: false })
|
|
2937
|
-
var outgoingStore = new mqtt.Store({ clean: false })
|
|
2938
|
-
var server2 = new MqttServer(function (serverClient) {
|
|
2939
|
-
// errors are not interesting for this test
|
|
2940
|
-
// but they might happen on some platforms
|
|
2941
|
-
serverClient.on('error', function () {})
|
|
2942
|
-
|
|
2943
|
-
serverClient.on('connect', function (packet) {
|
|
2944
|
-
serverClient.connack({returnCode: 0})
|
|
2945
|
-
})
|
|
2946
|
-
serverClient.on('publish', function (packet) {
|
|
2947
|
-
serverClient.puback({messageId: packet.messageId})
|
|
2948
|
-
if (reconnect) {
|
|
2949
|
-
switch (publishCount++) {
|
|
2950
|
-
case 0:
|
|
2951
|
-
assert.strictEqual(packet.payload.toString(), 'payload1')
|
|
2952
|
-
break
|
|
2953
|
-
case 1:
|
|
2954
|
-
assert.strictEqual(packet.payload.toString(), 'payload2')
|
|
2955
|
-
break
|
|
2956
|
-
case 2:
|
|
2957
|
-
assert.strictEqual(packet.payload.toString(), 'payload3')
|
|
2958
|
-
server2.close()
|
|
2959
|
-
client.end(true, done)
|
|
2960
|
-
break
|
|
2961
|
-
}
|
|
2962
|
-
} else {
|
|
2963
|
-
if (disconnectOnce) {
|
|
2964
|
-
client.end(true, function () {
|
|
2965
|
-
reconnect = true
|
|
2966
|
-
client.reconnect({
|
|
2967
|
-
incomingStore: incomingStore,
|
|
2968
|
-
outgoingStore: outgoingStore
|
|
2969
|
-
})
|
|
2970
|
-
})
|
|
2971
|
-
disconnectOnce = false
|
|
2972
|
-
}
|
|
2973
|
-
}
|
|
2974
|
-
})
|
|
2975
|
-
})
|
|
2976
|
-
|
|
2977
|
-
server2.listen(ports.PORTAND50, function () {
|
|
2978
|
-
client = mqtt.connect({
|
|
2979
|
-
port: ports.PORTAND50,
|
|
2980
|
-
host: 'localhost',
|
|
2981
|
-
clean: false,
|
|
2982
|
-
clientId: 'cid1',
|
|
2983
|
-
reconnectPeriod: 0,
|
|
2984
|
-
incomingStore: incomingStore,
|
|
2985
|
-
outgoingStore: outgoingStore
|
|
2986
|
-
})
|
|
2987
|
-
|
|
2988
|
-
client.nextId = 65535
|
|
2989
|
-
|
|
2990
|
-
client.on('connect', function () {
|
|
2991
|
-
if (!reconnect) {
|
|
2992
|
-
client.publish('topic', 'payload1', {qos: 1})
|
|
2993
|
-
client.publish('topic', 'payload2', {qos: 1})
|
|
2994
|
-
client.publish('topic', 'payload3', {qos: 1})
|
|
2995
|
-
}
|
|
2996
|
-
})
|
|
2997
|
-
client.on('error', function () {})
|
|
2998
|
-
})
|
|
2999
|
-
})
|
|
3000
|
-
|
|
3001
|
-
it('should be able to pub/sub if reconnect() is called at close handler', function (done) {
|
|
3002
|
-
var client = connect({ reconnectPeriod: 0 })
|
|
3003
|
-
var tryReconnect = true
|
|
3004
|
-
var reconnectEvent = false
|
|
3005
|
-
|
|
3006
|
-
client.on('close', function () {
|
|
3007
|
-
if (tryReconnect) {
|
|
3008
|
-
tryReconnect = false
|
|
3009
|
-
client.reconnect()
|
|
3010
|
-
} else {
|
|
3011
|
-
assert.isTrue(reconnectEvent)
|
|
3012
|
-
done()
|
|
3013
|
-
}
|
|
3014
|
-
})
|
|
3015
|
-
|
|
3016
|
-
client.on('reconnect', function () {
|
|
3017
|
-
reconnectEvent = true
|
|
3018
|
-
})
|
|
3019
|
-
|
|
3020
|
-
client.on('connect', function () {
|
|
3021
|
-
if (tryReconnect) {
|
|
3022
|
-
client.end()
|
|
3023
|
-
} else {
|
|
3024
|
-
client.subscribe('hello', function () {
|
|
3025
|
-
client.end()
|
|
3026
|
-
})
|
|
3027
|
-
}
|
|
3028
|
-
})
|
|
3029
|
-
})
|
|
3030
|
-
|
|
3031
|
-
it('should be able to pub/sub if reconnect() is called at out of close handler', function (done) {
|
|
3032
|
-
var client = connect({ reconnectPeriod: 0 })
|
|
3033
|
-
var tryReconnect = true
|
|
3034
|
-
var reconnectEvent = false
|
|
3035
|
-
|
|
3036
|
-
client.on('close', function () {
|
|
3037
|
-
if (tryReconnect) {
|
|
3038
|
-
tryReconnect = false
|
|
3039
|
-
setTimeout(function () {
|
|
3040
|
-
client.reconnect()
|
|
3041
|
-
}, 100)
|
|
3042
|
-
} else {
|
|
3043
|
-
assert.isTrue(reconnectEvent)
|
|
3044
|
-
done()
|
|
3045
|
-
}
|
|
3046
|
-
})
|
|
3047
|
-
|
|
3048
|
-
client.on('reconnect', function () {
|
|
3049
|
-
reconnectEvent = true
|
|
3050
|
-
})
|
|
3051
|
-
|
|
3052
|
-
client.on('connect', function () {
|
|
3053
|
-
if (tryReconnect) {
|
|
3054
|
-
client.end()
|
|
3055
|
-
} else {
|
|
3056
|
-
client.subscribe('hello', function () {
|
|
3057
|
-
client.end()
|
|
3058
|
-
})
|
|
3059
|
-
}
|
|
3060
|
-
})
|
|
3061
|
-
})
|
|
3062
|
-
|
|
3063
|
-
context('with alternate server client', function () {
|
|
3064
|
-
var cachedClientListeners
|
|
3065
|
-
var connack = version === 5 ? { reasonCode: 0 } : { returnCode: 0 }
|
|
3066
|
-
|
|
3067
|
-
beforeEach(function () {
|
|
3068
|
-
cachedClientListeners = server.listeners('client')
|
|
3069
|
-
server.removeAllListeners('client')
|
|
3070
|
-
})
|
|
3071
|
-
|
|
3072
|
-
afterEach(function () {
|
|
3073
|
-
server.removeAllListeners('client')
|
|
3074
|
-
cachedClientListeners.forEach(function (listener) {
|
|
3075
|
-
server.on('client', listener)
|
|
3076
|
-
})
|
|
3077
|
-
})
|
|
3078
|
-
|
|
3079
|
-
it('should resubscribe even if disconnect is before suback', function (done) {
|
|
3080
|
-
var client = mqtt.connect(Object.assign({ reconnectPeriod: 100 }, config))
|
|
3081
|
-
var subscribeCount = 0
|
|
3082
|
-
var connectCount = 0
|
|
3083
|
-
|
|
3084
|
-
server.on('client', function (serverClient) {
|
|
3085
|
-
serverClient.on('connect', function () {
|
|
3086
|
-
connectCount++
|
|
3087
|
-
serverClient.connack(connack)
|
|
3088
|
-
})
|
|
3089
|
-
|
|
3090
|
-
serverClient.on('subscribe', function () {
|
|
3091
|
-
subscribeCount++
|
|
3092
|
-
|
|
3093
|
-
// disconnect before sending the suback on the first subscribe
|
|
3094
|
-
if (subscribeCount === 1) {
|
|
3095
|
-
client.stream.end()
|
|
3096
|
-
}
|
|
3097
|
-
|
|
3098
|
-
// after the second connection, confirm that the only two
|
|
3099
|
-
// subscribes have taken place, then cleanup and exit
|
|
3100
|
-
if (connectCount >= 2) {
|
|
3101
|
-
assert.strictEqual(subscribeCount, 2)
|
|
3102
|
-
client.end(true, done)
|
|
3103
|
-
}
|
|
3104
|
-
})
|
|
3105
|
-
})
|
|
3106
|
-
|
|
3107
|
-
client.subscribe('hello')
|
|
3108
|
-
})
|
|
3109
|
-
|
|
3110
|
-
it('should resubscribe exactly once', function (done) {
|
|
3111
|
-
var client = mqtt.connect(Object.assign({ reconnectPeriod: 100 }, config))
|
|
3112
|
-
var subscribeCount = 0
|
|
3113
|
-
|
|
3114
|
-
server.on('client', function (serverClient) {
|
|
3115
|
-
serverClient.on('connect', function () {
|
|
3116
|
-
serverClient.connack(connack)
|
|
3117
|
-
})
|
|
3118
|
-
|
|
3119
|
-
serverClient.on('subscribe', function () {
|
|
3120
|
-
subscribeCount++
|
|
3121
|
-
|
|
3122
|
-
// disconnect before sending the suback on the first subscribe
|
|
3123
|
-
if (subscribeCount === 1) {
|
|
3124
|
-
client.stream.end()
|
|
3125
|
-
}
|
|
3126
|
-
|
|
3127
|
-
// after the second connection, only two subs
|
|
3128
|
-
// subscribes have taken place, then cleanup and exit
|
|
3129
|
-
if (subscribeCount === 2) {
|
|
3130
|
-
client.end(true, done)
|
|
3131
|
-
}
|
|
3132
|
-
})
|
|
3133
|
-
})
|
|
3134
|
-
|
|
3135
|
-
client.subscribe('hello')
|
|
3136
|
-
})
|
|
3137
|
-
})
|
|
3138
|
-
})
|
|
3139
|
-
}
|