forked from mqttjs/MQTT.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
542 lines (465 loc) · 14.3 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
'use strict'
var mqtt = require('..')
var should = require('should')
var fork = require('child_process').fork
var path = require('path')
var abstractClientTests = require('./abstract_client')
var net = require('net')
var eos = require('end-of-stream')
var mqttPacket = require('mqtt-packet')
var Buffer = require('safe-buffer').Buffer
var Duplex = require('readable-stream').Duplex
var Connection = require('mqtt-connection')
var Server = require('./server')
var port = 9876
var server
function connOnlyServer () {
return new Server(function (client) {
client.on('connect', function (packet) {
client.connack({returnCode: 0})
})
})
}
/**
* Test server
*/
function buildServer () {
return new Server(function (client) {
client.on('connect', function (packet) {
if (packet.clientId === 'invalid') {
client.connack({returnCode: 2})
} else {
client.connack({returnCode: 0})
}
})
client.on('publish', function (packet) {
setImmediate(function () {
switch (packet.qos) {
case 0:
break
case 1:
client.puback(packet)
break
case 2:
client.pubrec(packet)
break
}
})
})
client.on('pubrel', function (packet) {
client.pubcomp(packet)
})
client.on('pubrec', function (packet) {
client.pubrel(packet)
})
client.on('pubcomp', function () {
// Nothing to be done
})
client.on('subscribe', function (packet) {
client.suback({
messageId: packet.messageId,
granted: packet.subscriptions.map(function (e) {
return e.qos
})
})
})
client.on('unsubscribe', function (packet) {
client.unsuback(packet)
})
client.on('pingreq', function () {
client.pingresp()
})
})
}
server = buildServer().listen(port)
describe('MqttClient', function () {
describe('creating', function () {
it('should allow instantiation of MqttClient without the \'new\' operator', function (done) {
should(function () {
var client
try {
client = mqtt.MqttClient(function () {
throw Error('break')
}, {})
client.end()
} catch (err) {
if (err.message !== 'break') {
throw err
}
done()
}
}).not.throw('Object #<Object> has no method \'_setupStream\'')
})
})
var config = { protocol: 'mqtt', port: port }
abstractClientTests(server, config)
describe('message ids', function () {
it('should increment the message id', function () {
var client = mqtt.connect(config)
var currentId = client._nextId()
client._nextId().should.equal(currentId + 1)
client.end()
})
it('should return 1 once the internal counter reached limit', function () {
var client = mqtt.connect(config)
client.nextId = 65535
client._nextId().should.equal(65535)
client._nextId().should.equal(1)
client.end()
})
it('should return 65535 for last message id once the internal counter reached limit', function () {
var client = mqtt.connect(config)
client.nextId = 65535
client._nextId().should.equal(65535)
client.getLastMessageId().should.equal(65535)
client._nextId().should.equal(1)
client.getLastMessageId().should.equal(1)
client.end()
})
it('should not throw an error if packet\'s messageId is not found when receiving a pubrel packet', function (done) {
var server2 = new Server(function (c) {
c.on('connect', function (packet) {
c.connack({returnCode: 0})
c.pubrel({ messageId: Math.floor(Math.random() * 9000) + 1000 })
})
})
server2.listen(port + 49, function () {
var client = mqtt.connect({
port: port + 49,
host: 'localhost'
})
client.on('packetsend', function (packet) {
if (packet.cmd === 'pubcomp') {
client.end()
server2.close()
done()
}
})
})
})
it('should not go overflow if the TCP frame contains a lot of PUBLISH packets', function (done) {
var parser = mqttPacket.parser()
var count = 0
var max = 1000
var duplex = new Duplex({
read: function (n) {},
write: function (chunk, enc, cb) {
parser.parse(chunk)
cb() // nothing to do
}
})
var client = new mqtt.MqttClient(function () {
return duplex
}, {})
client.on('message', function (t, p, packet) {
if (++count === max) {
done()
}
})
parser.on('packet', function (packet) {
var packets = []
if (packet.cmd === 'connect') {
duplex.push(mqttPacket.generate({
cmd: 'connack',
sessionPresent: false,
returnCode: 0
}))
for (var i = 0; i < max; i++) {
packets.push(mqttPacket.generate({
cmd: 'publish',
topic: Buffer.from('hello'),
payload: Buffer.from('world'),
retain: false,
dup: false,
messageId: i + 1,
qos: 1
}))
}
duplex.push(Buffer.concat(packets))
}
})
})
})
describe('flushing', function () {
it('should attempt to complete pending unsub and send on ping timeout', function (done) {
this.timeout(10000)
var server3 = connOnlyServer().listen(port + 72)
var pubCallbackCalled = false
var unsubscribeCallbackCalled = false
var client = mqtt.connect({
port: port + 72,
host: 'localhost',
keepalive: 1,
connectTimeout: 350,
reconnectPeriod: 0
})
client.once('connect', () => {
client.publish('fakeTopic', 'fakeMessage', {qos: 1}, (err, result) => {
should.exist(err)
pubCallbackCalled = true
})
client.unsubscribe('fakeTopic', (err, result) => {
should.exist(err)
unsubscribeCallbackCalled = true
})
setTimeout(() => {
client.end(() => {
should.equal(pubCallbackCalled && unsubscribeCallbackCalled, true, 'callbacks not invoked')
server3.close()
done()
})
}, 5000)
})
})
})
describe('reconnecting', function () {
it('should attempt to reconnect once server is down', function (done) {
this.timeout(15000)
var innerServer = fork(path.join(__dirname, 'helpers', 'server_process.js'))
var client = mqtt.connect({ port: 3000, host: 'localhost', keepalive: 1 })
client.once('connect', function () {
innerServer.kill('SIGINT') // mocks server shutdown
client.once('close', function () {
should.exist(client.reconnectTimer)
client.end()
done()
})
})
})
it('should reconnect to multiple host-ports combination if servers is passed', function (done) {
this.timeout(15000)
var server2 = buildServer().listen(port + 42)
server2.on('client', function (c) {
c.stream.destroy()
server2.close()
})
server2.on('listening', function () {
var client = mqtt.connect({
servers: [
{ port: port + 42, host: 'localhost' },
{ port: port, host: 'localhost' }
],
keepalive: 50
})
server.once('client', function () {
client.end()
done()
})
client.once('connect', function () {
client.stream.destroy()
})
})
})
it('should reconnect if a connack is not received in an interval', function (done) {
this.timeout(2000)
var server2 = net.createServer().listen(port + 43)
server2.on('connection', function (c) {
eos(c, function () {
server2.close()
})
})
server2.on('listening', function () {
var client = mqtt.connect({
servers: [
{ port: port + 43, host: 'localhost_fake' },
{ port: port, host: 'localhost' }
],
connectTimeout: 500
})
server.once('client', function () {
client.end()
done()
})
client.once('connect', function () {
client.stream.destroy()
})
})
})
it('should not be cleared by the connack timer', function (done) {
this.timeout(4000)
var server2 = net.createServer().listen(port + 44)
server2.on('connection', function (c) {
c.destroy()
})
server2.once('listening', function () {
var reconnects = 0
var connectTimeout = 1000
var reconnectPeriod = 100
var expectedReconnects = Math.floor(connectTimeout / reconnectPeriod)
var client = mqtt.connect({
port: port + 44,
host: 'localhost',
connectTimeout: connectTimeout,
reconnectPeriod: reconnectPeriod
})
client.on('reconnect', function () {
reconnects++
if (reconnects >= expectedReconnects) {
client.end()
done()
}
})
})
})
it('should not keep requeueing the first message when offline', function (done) {
this.timeout(2500)
var server2 = buildServer().listen(port + 45)
var client = mqtt.connect({
port: port + 45,
host: 'localhost',
connectTimeout: 350,
reconnectPeriod: 300
})
server2.on('client', function (c) {
client.publish('hello', 'world', { qos: 1 }, function () {
c.destroy()
server2.close()
client.publish('hello', 'world', { qos: 1 })
})
})
setTimeout(function () {
if (client.queue.length === 0) {
client.end(true)
done()
} else {
client.end(true)
}
}, 2000)
})
it('should not send the same subscribe multiple times on a flaky connection', function (done) {
this.timeout(3500)
var KILL_COUNT = 4
var killedConnections = 0
var subIds = {}
var client = mqtt.connect({
port: port + 46,
host: 'localhost',
connectTimeout: 350,
reconnectPeriod: 300
})
var server2 = new Server(function (client) {
client.on('error', function () {})
client.on('connect', function (packet) {
if (packet.clientId === 'invalid') {
client.connack({returnCode: 2})
} else {
client.connack({returnCode: 0})
}
})
}).listen(port + 46)
server2.on('client', function (c) {
client.subscribe('topic', function () {
done()
client.end()
c.destroy()
server2.close()
})
c.on('subscribe', function (packet) {
if (killedConnections < KILL_COUNT) {
// Kill the first few sub attempts to simulate a flaky connection
killedConnections++
c.destroy()
} else {
// Keep track of acks
if (!subIds[packet.messageId]) {
subIds[packet.messageId] = 0
}
subIds[packet.messageId]++
if (subIds[packet.messageId] > 1) {
done(new Error('Multiple duplicate acked subscriptions received for messageId ' + packet.messageId))
client.end(true)
c.destroy()
server2.destroy()
}
c.suback({
messageId: packet.messageId,
granted: packet.subscriptions.map(function (e) {
return e.qos
})
})
}
})
})
})
it('should not fill the queue of subscribes if it cannot connect', function (done) {
this.timeout(2500)
var port2 = port + 48
var server2 = net.createServer(function (stream) {
var client = new Connection(stream)
client.on('error', function () {})
client.on('connect', function (packet) {
client.connack({returnCode: 0})
client.destroy()
})
})
server2.listen(port2, function () {
var client = mqtt.connect({
port: port2,
host: 'localhost',
connectTimeout: 350,
reconnectPeriod: 300
})
client.subscribe('hello')
setTimeout(function () {
client.queue.length.should.equal(1)
client.end()
done()
}, 1000)
})
})
it('should not send the same publish multiple times on a flaky connection', function (done) {
this.timeout(3500)
var KILL_COUNT = 4
var killedConnections = 0
var pubIds = {}
var client = mqtt.connect({
port: port + 47,
host: 'localhost',
connectTimeout: 350,
reconnectPeriod: 300
})
var server2 = net.createServer(function (stream) {
var client = new Connection(stream)
client.on('error', function () {})
client.on('connect', function (packet) {
if (packet.clientId === 'invalid') {
client.connack({returnCode: 2})
} else {
client.connack({returnCode: 0})
}
})
this.emit('client', client)
}).listen(port + 47)
server2.on('client', function (c) {
client.publish('topic', 'data', { qos: 1 }, function () {
done()
client.end()
c.destroy()
server2.destroy()
})
c.on('publish', function onPublish (packet) {
if (killedConnections < KILL_COUNT) {
// Kill the first few pub attempts to simulate a flaky connection
killedConnections++
c.destroy()
// to avoid receiving inflight messages
c.removeListener('publish', onPublish)
} else {
// Keep track of acks
if (!pubIds[packet.messageId]) {
pubIds[packet.messageId] = 0
}
pubIds[packet.messageId]++
if (pubIds[packet.messageId] > 1) {
done(new Error('Multiple duplicate acked publishes received for messageId ' + packet.messageId))
client.end(true)
c.destroy()
server2.destroy()
}
c.puback(packet)
}
})
})
})
})
})