mqtt 2.18.6 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -7
- package/dist/mqtt.js +4438 -3110
- package/dist/mqtt.min.js +1 -1
- package/lib/client.js +513 -173
- package/lib/connect/ali.js +130 -0
- package/lib/connect/index.js +10 -2
- package/lib/connect/wx.js +96 -72
- package/package.json +13 -12
- package/test/abstract_client.js +545 -19
- package/test/abstract_store.js +2 -26
- package/test/client.js +594 -9
- package/test/server.js +30 -2
- package/test/typescript/broker-connect-subscribe-and-publish.ts +1 -1
- package/types/lib/client-options.d.ts +44 -3
- package/types/lib/client.d.ts +39 -4
- package/test/browser/wx.js +0 -93
package/test/abstract_client.js
CHANGED
|
@@ -12,6 +12,7 @@ var Store = require('./../lib/store')
|
|
|
12
12
|
var port = 9876
|
|
13
13
|
|
|
14
14
|
module.exports = function (server, config) {
|
|
15
|
+
var version = config.protocolVersion || 4
|
|
15
16
|
function connect (opts) {
|
|
16
17
|
opts = xtend(config, opts)
|
|
17
18
|
return mqtt.connect(opts)
|
|
@@ -302,11 +303,13 @@ module.exports = function (server, config) {
|
|
|
302
303
|
})
|
|
303
304
|
|
|
304
305
|
it('should provide connack packet with connect event', function (done) {
|
|
306
|
+
var connack = version === 5 ? {reasonCode: 0} : {returnCode: 0}
|
|
305
307
|
server.once('client', function (serverClient) {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
+
connack.sessionPresent = true
|
|
309
|
+
serverClient.connack(connack)
|
|
308
310
|
server.once('client', function (serverClient) {
|
|
309
|
-
|
|
311
|
+
connack.sessionPresent = false
|
|
312
|
+
serverClient.connack(connack)
|
|
310
313
|
})
|
|
311
314
|
})
|
|
312
315
|
|
|
@@ -339,7 +342,8 @@ module.exports = function (server, config) {
|
|
|
339
342
|
done(new Error('Should not emit connect'))
|
|
340
343
|
})
|
|
341
344
|
client.once('error', function (error) {
|
|
342
|
-
|
|
345
|
+
var value = version === 5 ? 128 : 2
|
|
346
|
+
should(error.code).be.equal(value) // code for clientID identifer rejected
|
|
343
347
|
client.end()
|
|
344
348
|
done()
|
|
345
349
|
})
|
|
@@ -532,6 +536,64 @@ module.exports = function (server, config) {
|
|
|
532
536
|
})
|
|
533
537
|
})
|
|
534
538
|
|
|
539
|
+
it('should not interrupt messages', function (done) {
|
|
540
|
+
var client = null
|
|
541
|
+
var incomingStore = new mqtt.Store({ clean: false })
|
|
542
|
+
var outgoingStore = new mqtt.Store({ clean: false })
|
|
543
|
+
var publishCount = 0
|
|
544
|
+
var server2 = new Server(function (c) {
|
|
545
|
+
c.on('connect', function () {
|
|
546
|
+
c.connack({returnCode: 0})
|
|
547
|
+
})
|
|
548
|
+
c.on('publish', function (packet) {
|
|
549
|
+
if (packet.qos !== 0) {
|
|
550
|
+
c.puback({messageId: packet.messageId})
|
|
551
|
+
}
|
|
552
|
+
switch (publishCount++) {
|
|
553
|
+
case 0:
|
|
554
|
+
packet.payload.toString().should.equal('payload1')
|
|
555
|
+
break
|
|
556
|
+
case 1:
|
|
557
|
+
packet.payload.toString().should.equal('payload2')
|
|
558
|
+
break
|
|
559
|
+
case 2:
|
|
560
|
+
packet.payload.toString().should.equal('payload3')
|
|
561
|
+
break
|
|
562
|
+
case 3:
|
|
563
|
+
packet.payload.toString().should.equal('payload4')
|
|
564
|
+
server2.close()
|
|
565
|
+
done()
|
|
566
|
+
break
|
|
567
|
+
}
|
|
568
|
+
})
|
|
569
|
+
})
|
|
570
|
+
|
|
571
|
+
server2.listen(port + 50, function () {
|
|
572
|
+
client = mqtt.connect({
|
|
573
|
+
port: port + 50,
|
|
574
|
+
host: 'localhost',
|
|
575
|
+
clean: false,
|
|
576
|
+
clientId: 'cid1',
|
|
577
|
+
reconnectPeriod: 0,
|
|
578
|
+
incomingStore: incomingStore,
|
|
579
|
+
outgoingStore: outgoingStore,
|
|
580
|
+
queueQoSZero: true
|
|
581
|
+
})
|
|
582
|
+
client.on('packetreceive', function (packet) {
|
|
583
|
+
if (packet.cmd === 'connack') {
|
|
584
|
+
setImmediate(
|
|
585
|
+
function () {
|
|
586
|
+
client.publish('test', 'payload3', {qos: 1})
|
|
587
|
+
client.publish('test', 'payload4', {qos: 0})
|
|
588
|
+
}
|
|
589
|
+
)
|
|
590
|
+
}
|
|
591
|
+
})
|
|
592
|
+
client.publish('test', 'payload1', {qos: 2})
|
|
593
|
+
client.publish('test', 'payload2', {qos: 2})
|
|
594
|
+
})
|
|
595
|
+
})
|
|
596
|
+
|
|
535
597
|
it('should call cb if an outgoing QoS 0 message is not sent', function (done) {
|
|
536
598
|
var client = connect({queueQoSZero: false})
|
|
537
599
|
var called = false
|
|
@@ -973,6 +1035,115 @@ module.exports = function (server, config) {
|
|
|
973
1035
|
}
|
|
974
1036
|
})
|
|
975
1037
|
|
|
1038
|
+
it('should handle error with async incoming store in QoS 2 `handlePublish` method', function (done) {
|
|
1039
|
+
function AsyncStore () {
|
|
1040
|
+
if (!(this instanceof AsyncStore)) {
|
|
1041
|
+
return new AsyncStore()
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
AsyncStore.prototype.put = function (packet, cb) {
|
|
1045
|
+
process.nextTick(function () {
|
|
1046
|
+
cb(new Error('Error'))
|
|
1047
|
+
})
|
|
1048
|
+
}
|
|
1049
|
+
var store = new AsyncStore()
|
|
1050
|
+
var client = connect({incomingStore: store})
|
|
1051
|
+
|
|
1052
|
+
client._handlePublish({
|
|
1053
|
+
messageId: 1,
|
|
1054
|
+
topic: 'test',
|
|
1055
|
+
payload: 'test',
|
|
1056
|
+
qos: 2
|
|
1057
|
+
}, function () {
|
|
1058
|
+
done()
|
|
1059
|
+
client.end()
|
|
1060
|
+
})
|
|
1061
|
+
})
|
|
1062
|
+
|
|
1063
|
+
it('should handle error with async incoming store in QoS 2 `handlePubrel` method', function (done) {
|
|
1064
|
+
function AsyncStore () {
|
|
1065
|
+
if (!(this instanceof AsyncStore)) {
|
|
1066
|
+
return new AsyncStore()
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
AsyncStore.prototype.del = function (packet, cb) {
|
|
1070
|
+
process.nextTick(function () {
|
|
1071
|
+
cb(new Error('Error'))
|
|
1072
|
+
})
|
|
1073
|
+
}
|
|
1074
|
+
AsyncStore.prototype.get = function (packet, cb) {
|
|
1075
|
+
process.nextTick(function () {
|
|
1076
|
+
cb(null, {cmd: 'publish'})
|
|
1077
|
+
})
|
|
1078
|
+
}
|
|
1079
|
+
var store = new AsyncStore()
|
|
1080
|
+
var client = connect({incomingStore: store})
|
|
1081
|
+
|
|
1082
|
+
client._handlePubrel({
|
|
1083
|
+
messageId: 1,
|
|
1084
|
+
qos: 2
|
|
1085
|
+
}, function () {
|
|
1086
|
+
done()
|
|
1087
|
+
client.end()
|
|
1088
|
+
})
|
|
1089
|
+
})
|
|
1090
|
+
|
|
1091
|
+
it('should handle success with async incoming store in QoS 2 `handlePubrel` method', function (done) {
|
|
1092
|
+
var delComplete = false
|
|
1093
|
+
function AsyncStore () {
|
|
1094
|
+
if (!(this instanceof AsyncStore)) {
|
|
1095
|
+
return new AsyncStore()
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
AsyncStore.prototype.del = function (packet, cb) {
|
|
1099
|
+
process.nextTick(function () {
|
|
1100
|
+
delComplete = true
|
|
1101
|
+
cb(null)
|
|
1102
|
+
})
|
|
1103
|
+
}
|
|
1104
|
+
AsyncStore.prototype.get = function (packet, cb) {
|
|
1105
|
+
process.nextTick(function () {
|
|
1106
|
+
cb(null, {cmd: 'publish'})
|
|
1107
|
+
})
|
|
1108
|
+
}
|
|
1109
|
+
var store = new AsyncStore()
|
|
1110
|
+
var client = connect({incomingStore: store})
|
|
1111
|
+
|
|
1112
|
+
client._handlePubrel({
|
|
1113
|
+
messageId: 1,
|
|
1114
|
+
qos: 2
|
|
1115
|
+
}, function () {
|
|
1116
|
+
delComplete.should.equal(true)
|
|
1117
|
+
done()
|
|
1118
|
+
client.end()
|
|
1119
|
+
})
|
|
1120
|
+
})
|
|
1121
|
+
|
|
1122
|
+
it('should handle error with async incoming store in QoS 1 `handlePublish` method', function (done) {
|
|
1123
|
+
function AsyncStore () {
|
|
1124
|
+
if (!(this instanceof AsyncStore)) {
|
|
1125
|
+
return new AsyncStore()
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
AsyncStore.prototype.put = function (packet, cb) {
|
|
1129
|
+
process.nextTick(function () {
|
|
1130
|
+
cb(null, 'Error')
|
|
1131
|
+
})
|
|
1132
|
+
}
|
|
1133
|
+
var store = new AsyncStore()
|
|
1134
|
+
var client = connect({incomingStore: store})
|
|
1135
|
+
|
|
1136
|
+
client._handlePublish({
|
|
1137
|
+
messageId: 1,
|
|
1138
|
+
topic: 'test',
|
|
1139
|
+
payload: 'test',
|
|
1140
|
+
qos: 1
|
|
1141
|
+
}, function () {
|
|
1142
|
+
done()
|
|
1143
|
+
client.end()
|
|
1144
|
+
})
|
|
1145
|
+
})
|
|
1146
|
+
|
|
976
1147
|
it('should not send a `pubcomp` if the execution of `handleMessage` fails for messages with QoS `2`', function (done) {
|
|
977
1148
|
var store = new Store()
|
|
978
1149
|
var client = connect({incomingStore: store})
|
|
@@ -1044,6 +1215,115 @@ module.exports = function (server, config) {
|
|
|
1044
1215
|
})
|
|
1045
1216
|
})
|
|
1046
1217
|
})
|
|
1218
|
+
|
|
1219
|
+
it('should keep message order', function (done) {
|
|
1220
|
+
var publishCount = 0
|
|
1221
|
+
var reconnect = false
|
|
1222
|
+
var client = {}
|
|
1223
|
+
var incomingStore = new mqtt.Store({ clean: false })
|
|
1224
|
+
var outgoingStore = new mqtt.Store({ clean: false })
|
|
1225
|
+
var server2 = new Server(function (c) {
|
|
1226
|
+
// errors are not interesting for this test
|
|
1227
|
+
// but they might happen on some platforms
|
|
1228
|
+
c.on('error', function () {})
|
|
1229
|
+
|
|
1230
|
+
c.on('connect', function (packet) {
|
|
1231
|
+
c.connack({returnCode: 0})
|
|
1232
|
+
})
|
|
1233
|
+
c.on('publish', function (packet) {
|
|
1234
|
+
c.puback({messageId: packet.messageId})
|
|
1235
|
+
if (reconnect) {
|
|
1236
|
+
switch (publishCount++) {
|
|
1237
|
+
case 0:
|
|
1238
|
+
packet.payload.toString().should.equal('payload1')
|
|
1239
|
+
break
|
|
1240
|
+
case 1:
|
|
1241
|
+
packet.payload.toString().should.equal('payload2')
|
|
1242
|
+
break
|
|
1243
|
+
case 2:
|
|
1244
|
+
packet.payload.toString().should.equal('payload3')
|
|
1245
|
+
server2.close()
|
|
1246
|
+
done()
|
|
1247
|
+
break
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
})
|
|
1251
|
+
})
|
|
1252
|
+
|
|
1253
|
+
server2.listen(port + 50, function () {
|
|
1254
|
+
client = mqtt.connect({
|
|
1255
|
+
port: port + 50,
|
|
1256
|
+
host: 'localhost',
|
|
1257
|
+
clean: false,
|
|
1258
|
+
clientId: 'cid1',
|
|
1259
|
+
reconnectPeriod: 0,
|
|
1260
|
+
incomingStore: incomingStore,
|
|
1261
|
+
outgoingStore: outgoingStore
|
|
1262
|
+
})
|
|
1263
|
+
|
|
1264
|
+
client.on('connect', function () {
|
|
1265
|
+
if (!reconnect) {
|
|
1266
|
+
client.publish('topic', 'payload1', {qos: 1})
|
|
1267
|
+
client.publish('topic', 'payload2', {qos: 1})
|
|
1268
|
+
client.end(true)
|
|
1269
|
+
} else {
|
|
1270
|
+
client.publish('topic', 'payload3', {qos: 1})
|
|
1271
|
+
}
|
|
1272
|
+
})
|
|
1273
|
+
client.on('close', function () {
|
|
1274
|
+
if (!reconnect) {
|
|
1275
|
+
client.reconnect({
|
|
1276
|
+
clean: false,
|
|
1277
|
+
incomingStore: incomingStore,
|
|
1278
|
+
outgoingStore: outgoingStore
|
|
1279
|
+
})
|
|
1280
|
+
reconnect = true
|
|
1281
|
+
}
|
|
1282
|
+
})
|
|
1283
|
+
})
|
|
1284
|
+
})
|
|
1285
|
+
|
|
1286
|
+
function testCallbackStorePutByQoS (qos, clean, expected, done) {
|
|
1287
|
+
var client = connect({
|
|
1288
|
+
clean: clean,
|
|
1289
|
+
clientId: 'testId'
|
|
1290
|
+
})
|
|
1291
|
+
|
|
1292
|
+
var callbacks = []
|
|
1293
|
+
|
|
1294
|
+
function cbStorePut () {
|
|
1295
|
+
callbacks.push('storeput')
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
client.on('connect', function () {
|
|
1299
|
+
client.publish('test', 'test', {qos: qos, cbStorePut: cbStorePut}, function (err) {
|
|
1300
|
+
if (err) done(err)
|
|
1301
|
+
callbacks.push('publish')
|
|
1302
|
+
should.deepEqual(callbacks, expected)
|
|
1303
|
+
done()
|
|
1304
|
+
})
|
|
1305
|
+
client.end()
|
|
1306
|
+
})
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
it('should not call cbStorePut when publishing message with QoS `0` and clean `true`', function (done) {
|
|
1310
|
+
testCallbackStorePutByQoS(0, true, ['publish'], done)
|
|
1311
|
+
})
|
|
1312
|
+
it('should not call cbStorePut when publishing message with QoS `0` and clean `false`', function (done) {
|
|
1313
|
+
testCallbackStorePutByQoS(0, false, ['publish'], done)
|
|
1314
|
+
})
|
|
1315
|
+
it('should call cbStorePut before publish completes when publishing message with QoS `1` and clean `true`', function (done) {
|
|
1316
|
+
testCallbackStorePutByQoS(1, true, ['storeput', 'publish'], done)
|
|
1317
|
+
})
|
|
1318
|
+
it('should call cbStorePut before publish completes when publishing message with QoS `1` and clean `false`', function (done) {
|
|
1319
|
+
testCallbackStorePutByQoS(1, false, ['storeput', 'publish'], done)
|
|
1320
|
+
})
|
|
1321
|
+
it('should call cbStorePut before publish completes when publishing message with QoS `2` and clean `true`', function (done) {
|
|
1322
|
+
testCallbackStorePutByQoS(2, true, ['storeput', 'publish'], done)
|
|
1323
|
+
})
|
|
1324
|
+
it('should call cbStorePut before publish completes when publishing message with QoS `2` and clean `false`', function (done) {
|
|
1325
|
+
testCallbackStorePutByQoS(2, false, ['storeput', 'publish'], done)
|
|
1326
|
+
})
|
|
1047
1327
|
})
|
|
1048
1328
|
|
|
1049
1329
|
describe('unsubscribing', function () {
|
|
@@ -1319,10 +1599,16 @@ module.exports = function (server, config) {
|
|
|
1319
1599
|
|
|
1320
1600
|
server.once('client', function (serverClient) {
|
|
1321
1601
|
serverClient.once('subscribe', function (packet) {
|
|
1322
|
-
|
|
1602
|
+
var result = {
|
|
1323
1603
|
topic: topic,
|
|
1324
1604
|
qos: 0
|
|
1325
|
-
}
|
|
1605
|
+
}
|
|
1606
|
+
if (version === 5) {
|
|
1607
|
+
result.nl = false
|
|
1608
|
+
result.rap = false
|
|
1609
|
+
result.rh = 0
|
|
1610
|
+
}
|
|
1611
|
+
packet.subscriptions.should.containEql(result)
|
|
1326
1612
|
done()
|
|
1327
1613
|
})
|
|
1328
1614
|
})
|
|
@@ -1370,7 +1656,13 @@ module.exports = function (server, config) {
|
|
|
1370
1656
|
serverClient.once('subscribe', function (packet) {
|
|
1371
1657
|
// i.e. [{topic: 'a', qos: 0}, {topic: 'b', qos: 0}]
|
|
1372
1658
|
var expected = subs.map(function (i) {
|
|
1373
|
-
|
|
1659
|
+
var result = {topic: i, qos: 0}
|
|
1660
|
+
if (version === 5) {
|
|
1661
|
+
result.nl = false
|
|
1662
|
+
result.rap = false
|
|
1663
|
+
result.rh = 0
|
|
1664
|
+
}
|
|
1665
|
+
return result
|
|
1374
1666
|
})
|
|
1375
1667
|
|
|
1376
1668
|
packet.subscriptions.should.eql(expected)
|
|
@@ -1382,8 +1674,8 @@ module.exports = function (server, config) {
|
|
|
1382
1674
|
it('should accept an hash of subscriptions', function (done) {
|
|
1383
1675
|
var client = connect()
|
|
1384
1676
|
var topics = {
|
|
1385
|
-
test1: 0,
|
|
1386
|
-
test2: 1
|
|
1677
|
+
test1: {qos: 0},
|
|
1678
|
+
test2: {qos: 1}
|
|
1387
1679
|
}
|
|
1388
1680
|
|
|
1389
1681
|
client.once('connect', function () {
|
|
@@ -1397,10 +1689,16 @@ module.exports = function (server, config) {
|
|
|
1397
1689
|
|
|
1398
1690
|
for (k in topics) {
|
|
1399
1691
|
if (topics.hasOwnProperty(k)) {
|
|
1400
|
-
|
|
1692
|
+
var result = {
|
|
1401
1693
|
topic: k,
|
|
1402
|
-
qos: topics[k]
|
|
1403
|
-
}
|
|
1694
|
+
qos: topics[k].qos
|
|
1695
|
+
}
|
|
1696
|
+
if (version === 5) {
|
|
1697
|
+
result.nl = false
|
|
1698
|
+
result.rap = false
|
|
1699
|
+
result.rh = 0
|
|
1700
|
+
}
|
|
1701
|
+
expected.push(result)
|
|
1404
1702
|
}
|
|
1405
1703
|
}
|
|
1406
1704
|
|
|
@@ -1426,6 +1724,12 @@ module.exports = function (server, config) {
|
|
|
1426
1724
|
qos: 1
|
|
1427
1725
|
}]
|
|
1428
1726
|
|
|
1727
|
+
if (version === 5) {
|
|
1728
|
+
expected[0].nl = false
|
|
1729
|
+
expected[0].rap = false
|
|
1730
|
+
expected[0].rh = 0
|
|
1731
|
+
}
|
|
1732
|
+
|
|
1429
1733
|
packet.subscriptions.should.eql(expected)
|
|
1430
1734
|
done()
|
|
1431
1735
|
})
|
|
@@ -1443,10 +1747,16 @@ module.exports = function (server, config) {
|
|
|
1443
1747
|
|
|
1444
1748
|
server.once('client', function (serverClient) {
|
|
1445
1749
|
serverClient.once('subscribe', function (packet) {
|
|
1446
|
-
|
|
1750
|
+
var result = {
|
|
1447
1751
|
topic: topic,
|
|
1448
1752
|
qos: defaultOpts.qos
|
|
1449
|
-
}
|
|
1753
|
+
}
|
|
1754
|
+
if (version === 5) {
|
|
1755
|
+
result.nl = false
|
|
1756
|
+
result.rap = false
|
|
1757
|
+
result.rh = 0
|
|
1758
|
+
}
|
|
1759
|
+
packet.subscriptions.should.containEql(result)
|
|
1450
1760
|
done()
|
|
1451
1761
|
})
|
|
1452
1762
|
})
|
|
@@ -1462,7 +1772,14 @@ module.exports = function (server, config) {
|
|
|
1462
1772
|
done(err)
|
|
1463
1773
|
} else {
|
|
1464
1774
|
should.exist(granted, 'granted not given')
|
|
1465
|
-
|
|
1775
|
+
var result = {topic: 'test', qos: 2}
|
|
1776
|
+
if (version === 5) {
|
|
1777
|
+
result.nl = false
|
|
1778
|
+
result.rap = false
|
|
1779
|
+
result.rh = 0
|
|
1780
|
+
result.properties = undefined
|
|
1781
|
+
}
|
|
1782
|
+
granted.should.containEql(result)
|
|
1466
1783
|
done()
|
|
1467
1784
|
}
|
|
1468
1785
|
})
|
|
@@ -1508,10 +1825,16 @@ module.exports = function (server, config) {
|
|
|
1508
1825
|
|
|
1509
1826
|
server.once('client', function (serverClient) {
|
|
1510
1827
|
serverClient.once('subscribe', function (packet) {
|
|
1511
|
-
|
|
1828
|
+
var result = {
|
|
1512
1829
|
topic: topic,
|
|
1513
1830
|
qos: 0
|
|
1514
|
-
}
|
|
1831
|
+
}
|
|
1832
|
+
if (version === 5) {
|
|
1833
|
+
result.nl = false
|
|
1834
|
+
result.rap = false
|
|
1835
|
+
result.rh = 0
|
|
1836
|
+
}
|
|
1837
|
+
packet.subscriptions.should.containEql(result)
|
|
1515
1838
|
done()
|
|
1516
1839
|
})
|
|
1517
1840
|
})
|
|
@@ -1738,11 +2061,35 @@ module.exports = function (server, config) {
|
|
|
1738
2061
|
var testTopic = 'test'
|
|
1739
2062
|
var testMessage = 'message'
|
|
1740
2063
|
var mid = 253
|
|
2064
|
+
var publishReceived = false
|
|
2065
|
+
var pubrecReceived = false
|
|
2066
|
+
var pubrelReceived = false
|
|
1741
2067
|
|
|
1742
2068
|
client.once('connect', function () {
|
|
1743
2069
|
client.subscribe(testTopic, {qos: 2})
|
|
1744
2070
|
})
|
|
1745
2071
|
|
|
2072
|
+
client.on('packetreceive', (packet) => {
|
|
2073
|
+
switch (packet.cmd) {
|
|
2074
|
+
case 'connack':
|
|
2075
|
+
case 'suback':
|
|
2076
|
+
// expected, but not specifically part of QOS 2 semantics
|
|
2077
|
+
break
|
|
2078
|
+
case 'publish':
|
|
2079
|
+
pubrecReceived.should.be.false()
|
|
2080
|
+
pubrelReceived.should.be.false()
|
|
2081
|
+
publishReceived = true
|
|
2082
|
+
break
|
|
2083
|
+
case 'pubrel':
|
|
2084
|
+
publishReceived.should.be.true()
|
|
2085
|
+
pubrecReceived.should.be.true()
|
|
2086
|
+
pubrelReceived = true
|
|
2087
|
+
break
|
|
2088
|
+
default:
|
|
2089
|
+
should.fail()
|
|
2090
|
+
}
|
|
2091
|
+
})
|
|
2092
|
+
|
|
1746
2093
|
server.once('client', function (serverClient) {
|
|
1747
2094
|
serverClient.once('subscribe', function () {
|
|
1748
2095
|
serverClient.publish({
|
|
@@ -1753,11 +2100,138 @@ module.exports = function (server, config) {
|
|
|
1753
2100
|
})
|
|
1754
2101
|
})
|
|
1755
2102
|
|
|
2103
|
+
serverClient.on('pubrec', function () {
|
|
2104
|
+
publishReceived.should.be.true()
|
|
2105
|
+
pubrelReceived.should.be.false()
|
|
2106
|
+
pubrecReceived = true
|
|
2107
|
+
})
|
|
2108
|
+
|
|
1756
2109
|
serverClient.once('pubcomp', function () {
|
|
2110
|
+
client.removeAllListeners()
|
|
2111
|
+
serverClient.removeAllListeners()
|
|
2112
|
+
publishReceived.should.be.true()
|
|
2113
|
+
pubrecReceived.should.be.true()
|
|
2114
|
+
pubrelReceived.should.be.true()
|
|
1757
2115
|
done()
|
|
1758
2116
|
})
|
|
1759
2117
|
})
|
|
1760
2118
|
})
|
|
2119
|
+
|
|
2120
|
+
it('should should empty the incoming store after a qos 2 handshake is completed', function (done) {
|
|
2121
|
+
var client = connect()
|
|
2122
|
+
var testTopic = 'test'
|
|
2123
|
+
var testMessage = 'message'
|
|
2124
|
+
var mid = 253
|
|
2125
|
+
|
|
2126
|
+
client.once('connect', function () {
|
|
2127
|
+
client.subscribe(testTopic, {qos: 2})
|
|
2128
|
+
})
|
|
2129
|
+
|
|
2130
|
+
client.on('packetreceive', (packet) => {
|
|
2131
|
+
if (packet.cmd === 'pubrel') {
|
|
2132
|
+
should(client.incomingStore._inflights.size).be.equal(1)
|
|
2133
|
+
}
|
|
2134
|
+
})
|
|
2135
|
+
|
|
2136
|
+
server.once('client', function (serverClient) {
|
|
2137
|
+
serverClient.once('subscribe', function () {
|
|
2138
|
+
serverClient.publish({
|
|
2139
|
+
topic: testTopic,
|
|
2140
|
+
payload: testMessage,
|
|
2141
|
+
qos: 2,
|
|
2142
|
+
messageId: mid
|
|
2143
|
+
})
|
|
2144
|
+
})
|
|
2145
|
+
|
|
2146
|
+
serverClient.once('pubcomp', function () {
|
|
2147
|
+
should(client.incomingStore._inflights.size).be.equal(0)
|
|
2148
|
+
client.removeAllListeners()
|
|
2149
|
+
done()
|
|
2150
|
+
})
|
|
2151
|
+
})
|
|
2152
|
+
})
|
|
2153
|
+
|
|
2154
|
+
function testMultiplePubrel (shouldSendPubcompFail, done) {
|
|
2155
|
+
var client = connect()
|
|
2156
|
+
var testTopic = 'test'
|
|
2157
|
+
var testMessage = 'message'
|
|
2158
|
+
var mid = 253
|
|
2159
|
+
var pubcompCount = 0
|
|
2160
|
+
var pubrelCount = 0
|
|
2161
|
+
var handleMessageCount = 0
|
|
2162
|
+
var emitMessageCount = 0
|
|
2163
|
+
var origSendPacket = client._sendPacket
|
|
2164
|
+
var shouldSendFail
|
|
2165
|
+
|
|
2166
|
+
client.handleMessage = function (packet, callback) {
|
|
2167
|
+
handleMessageCount++
|
|
2168
|
+
callback()
|
|
2169
|
+
}
|
|
2170
|
+
|
|
2171
|
+
client.on('message', function () {
|
|
2172
|
+
emitMessageCount++
|
|
2173
|
+
})
|
|
2174
|
+
|
|
2175
|
+
client._sendPacket = function (packet, sendDone) {
|
|
2176
|
+
shouldSendFail = packet.cmd === 'pubcomp' && shouldSendPubcompFail
|
|
2177
|
+
if (sendDone) {
|
|
2178
|
+
sendDone(shouldSendFail ? new Error('testing pubcomp failure') : undefined)
|
|
2179
|
+
}
|
|
2180
|
+
|
|
2181
|
+
// send the mocked response
|
|
2182
|
+
switch (packet.cmd) {
|
|
2183
|
+
case 'subscribe':
|
|
2184
|
+
const suback = {cmd: 'suback', messageId: packet.messageId, granted: [2]}
|
|
2185
|
+
client._handlePacket(suback, function (err) {
|
|
2186
|
+
should(err).not.be.ok()
|
|
2187
|
+
})
|
|
2188
|
+
break
|
|
2189
|
+
case 'pubrec':
|
|
2190
|
+
case 'pubcomp':
|
|
2191
|
+
// for both pubrec and pubcomp, reply with pubrel, simulating the server not receiving the pubcomp
|
|
2192
|
+
if (packet.cmd === 'pubcomp') {
|
|
2193
|
+
pubcompCount++
|
|
2194
|
+
if (pubcompCount === 2) {
|
|
2195
|
+
// end the test once the client has gone through two rounds of replying to pubrel messages
|
|
2196
|
+
pubrelCount.should.be.exactly(2)
|
|
2197
|
+
handleMessageCount.should.be.exactly(1)
|
|
2198
|
+
emitMessageCount.should.be.exactly(1)
|
|
2199
|
+
client._sendPacket = origSendPacket
|
|
2200
|
+
done()
|
|
2201
|
+
break
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
|
|
2205
|
+
// simulate the pubrel message, either in response to pubrec or to mock pubcomp failing to be received
|
|
2206
|
+
const pubrel = {cmd: 'pubrel', messageId: mid}
|
|
2207
|
+
pubrelCount++
|
|
2208
|
+
client._handlePacket(pubrel, function (err) {
|
|
2209
|
+
if (shouldSendFail) {
|
|
2210
|
+
should(err).be.ok()
|
|
2211
|
+
} else {
|
|
2212
|
+
should(err).not.be.ok()
|
|
2213
|
+
}
|
|
2214
|
+
})
|
|
2215
|
+
break
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
client.once('connect', function () {
|
|
2220
|
+
client.subscribe(testTopic, {qos: 2})
|
|
2221
|
+
const publish = {cmd: 'publish', topic: testTopic, payload: testMessage, qos: 2, messageId: mid}
|
|
2222
|
+
client._handlePacket(publish, function (err) {
|
|
2223
|
+
should(err).not.be.ok()
|
|
2224
|
+
})
|
|
2225
|
+
})
|
|
2226
|
+
}
|
|
2227
|
+
|
|
2228
|
+
it('handle qos 2 messages exactly once when multiple pubrel received', function (done) {
|
|
2229
|
+
testMultiplePubrel(false, done)
|
|
2230
|
+
})
|
|
2231
|
+
|
|
2232
|
+
it('handle qos 2 messages exactly once when multiple pubrel received and sending pubcomp fails on client', function (done) {
|
|
2233
|
+
testMultiplePubrel(true, done)
|
|
2234
|
+
})
|
|
1761
2235
|
})
|
|
1762
2236
|
|
|
1763
2237
|
describe('auto reconnect', function () {
|
|
@@ -2208,6 +2682,57 @@ module.exports = function (server, config) {
|
|
|
2208
2682
|
})
|
|
2209
2683
|
})
|
|
2210
2684
|
|
|
2685
|
+
it('should clear outgoing if close from server', function (done) {
|
|
2686
|
+
var reconnect = false
|
|
2687
|
+
var client = {}
|
|
2688
|
+
var server2 = new Server(function (c) {
|
|
2689
|
+
c.on('connect', function (packet) {
|
|
2690
|
+
c.connack({returnCode: 0})
|
|
2691
|
+
})
|
|
2692
|
+
c.on('subscribe', function (packet) {
|
|
2693
|
+
if (reconnect) {
|
|
2694
|
+
c.suback({
|
|
2695
|
+
messageId: packet.messageId,
|
|
2696
|
+
granted: packet.subscriptions.map(function (e) {
|
|
2697
|
+
return e.qos
|
|
2698
|
+
})
|
|
2699
|
+
})
|
|
2700
|
+
} else {
|
|
2701
|
+
c.destroy()
|
|
2702
|
+
}
|
|
2703
|
+
})
|
|
2704
|
+
})
|
|
2705
|
+
|
|
2706
|
+
server2.listen(port + 50, function () {
|
|
2707
|
+
client = mqtt.connect({
|
|
2708
|
+
port: port + 50,
|
|
2709
|
+
host: 'localhost',
|
|
2710
|
+
clean: true,
|
|
2711
|
+
clientId: 'cid1',
|
|
2712
|
+
reconnectPeriod: 0
|
|
2713
|
+
})
|
|
2714
|
+
|
|
2715
|
+
client.on('connect', function () {
|
|
2716
|
+
client.subscribe('test', {qos: 2}, function (e) {
|
|
2717
|
+
if (!e) {
|
|
2718
|
+
client.end()
|
|
2719
|
+
}
|
|
2720
|
+
})
|
|
2721
|
+
})
|
|
2722
|
+
|
|
2723
|
+
client.on('close', function () {
|
|
2724
|
+
if (reconnect) {
|
|
2725
|
+
server2.close()
|
|
2726
|
+
done()
|
|
2727
|
+
} else {
|
|
2728
|
+
Object.keys(client.outgoing).length.should.equal(0)
|
|
2729
|
+
reconnect = true
|
|
2730
|
+
client.reconnect()
|
|
2731
|
+
}
|
|
2732
|
+
})
|
|
2733
|
+
})
|
|
2734
|
+
})
|
|
2735
|
+
|
|
2211
2736
|
it('should resend in-flight QoS 1 publish messages from the client if clean is false', function (done) {
|
|
2212
2737
|
var reconnect = false
|
|
2213
2738
|
var client = {}
|
|
@@ -2482,6 +3007,7 @@ module.exports = function (server, config) {
|
|
|
2482
3007
|
|
|
2483
3008
|
context('with alternate server client', function () {
|
|
2484
3009
|
var cachedClientListeners
|
|
3010
|
+
var connack = version === 5 ? { reasonCode: 0 } : { returnCode: 0 }
|
|
2485
3011
|
|
|
2486
3012
|
beforeEach(function () {
|
|
2487
3013
|
cachedClientListeners = server.listeners('client')
|
|
@@ -2503,7 +3029,7 @@ module.exports = function (server, config) {
|
|
|
2503
3029
|
server.on('client', function (serverClient) {
|
|
2504
3030
|
serverClient.on('connect', function () {
|
|
2505
3031
|
connectCount++
|
|
2506
|
-
serverClient.connack(
|
|
3032
|
+
serverClient.connack(connack)
|
|
2507
3033
|
})
|
|
2508
3034
|
|
|
2509
3035
|
serverClient.on('subscribe', function () {
|
|
@@ -2532,7 +3058,7 @@ module.exports = function (server, config) {
|
|
|
2532
3058
|
|
|
2533
3059
|
server.on('client', function (serverClient) {
|
|
2534
3060
|
serverClient.on('connect', function () {
|
|
2535
|
-
serverClient.connack(
|
|
3061
|
+
serverClient.connack(connack)
|
|
2536
3062
|
})
|
|
2537
3063
|
|
|
2538
3064
|
serverClient.on('subscribe', function () {
|