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