From d56caac9b583753e39c59c7defd428206e3d067c Mon Sep 17 00:00:00 2001 From: Bert Kleewein Date: Thu, 6 Jul 2023 13:11:05 -0700 Subject: [PATCH 1/3] chore: more code cleanup surrounding client.end callbacks --- test/client.js | 68 +++++++++++++++----------------- test/mqtt.js | 83 ++++++++++++++++++++-------------------- test/mqtt_store.js | 4 +- test/secure_client.js | 12 +++--- test/websocket_client.js | 13 ++++--- 5 files changed, 88 insertions(+), 92 deletions(-) diff --git a/test/client.js b/test/client.js index f1eaa3daf..8add736e1 100644 --- a/test/client.js +++ b/test/client.js @@ -59,12 +59,12 @@ describe('MqttClient', function () { }) describe('message ids', function () { - it('should increment the message id', function () { + it('should increment the message id', function (done) { client = mqtt.connect(config) const currentId = client._nextId() assert.equal(client._nextId(), currentId + 1) - client.end() + client.end((err) => done(err)) }) it('should not throw an error if packet\'s messageId is not found when receiving a pubrel packet', function (done) { @@ -83,9 +83,11 @@ describe('MqttClient', function () { client.on('packetsend', function (packet) { if (packet.cmd === 'pubcomp') { - client.end() - server2.close() - done() + client.end((err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) } }) }) @@ -108,6 +110,9 @@ describe('MqttClient', function () { client.on('message', function (t, p, packet) { if (++count === max) { + // BUGBUG: the client.end callback never gets called here + // client.end((err) => done(err)) + client.end() done() } }) @@ -143,9 +148,9 @@ describe('MqttClient', function () { describe('flushing', function () { it('should attempt to complete pending unsub and send on ping timeout', function (done) { this.timeout(10000) - const server3 = new MqttServer(function (client) { - client.on('connect', function (packet) { - client.connack({ returnCode: 0 }) + const server3 = new MqttServer(function (serverClient) { + serverClient.on('connect', function (packet) { + serverClient.connack({ returnCode: 0 }) }) }).listen(ports.PORTAND72) @@ -168,10 +173,10 @@ describe('MqttClient', function () { unsubscribeCallbackCalled = true }) setTimeout(() => { - client.end(() => { + client.end((err) => { assert.strictEqual(pubCallbackCalled && unsubscribeCallbackCalled, true, 'callbacks not invoked') server3.close() - done() + done(err) }) }, 5000) }) @@ -200,7 +205,7 @@ describe('MqttClient', function () { innerServer.kill('SIGINT') // mocks server shutdown client.once('close', function () { assert.exists(client.reconnectTimer) - client.end(true, done) + client.end(true, (err) => done(err)) }) }) }) @@ -261,7 +266,7 @@ describe('MqttClient', function () { client.on('reconnect', function () { reconnects++ if (reconnects >= expectedReconnects) { - client.end(true, done) + client.end(true, (err) => done(err)) } }) }) @@ -294,6 +299,7 @@ describe('MqttClient', function () { client.end(true, (err) => done(err)) } else { debug('calling client.end()') + // Do not call done. We want to trigger a reconnect here. client.end(true) } }, 2000) @@ -313,26 +319,14 @@ describe('MqttClient', function () { }) const server2 = new MqttServer(function (serverClient) { - serverClient.on('error', function () { }) - debug('setting serverClient connect callback') - serverClient.on('connect', function (packet) { - if (packet.clientId === 'invalid') { - debug('connack with returnCode 2') - serverClient.connack({ returnCode: 2 }) - } else { - debug('connack with returnCode 0') - serverClient.connack({ returnCode: 0 }) - } - }) - }).listen(ports.PORTAND46) - - server2.on('client', function (serverClient) { debug('client received on server2.') debug('subscribing to topic `topic`') client.subscribe('topic', function () { debug('once subscribed to topic, end client, destroy serverClient, and close server.') serverClient.destroy() - server2.close(() => { client.end(true, done) }) + server2.close(() => { + client.end(true, (err) => done(err)) + }) }) serverClient.on('subscribe', function (packet) { @@ -361,7 +355,7 @@ describe('MqttClient', function () { }) } }) - }) + }).listen(ports.PORTAND46) }) it('should not fill the queue of subscribes if it cannot connect', function (done) { @@ -388,9 +382,7 @@ describe('MqttClient', function () { setTimeout(function () { assert.equal(client.queue.length, 1) - client.end(true, () => { - done() - }) + client.end(true, (err) => done(err)) }, 1000) }) }) @@ -424,9 +416,11 @@ describe('MqttClient', function () { server2.on('client', function (serverClient) { client.publish('topic', 'data', { qos: 1 }, function () { - serverClient.destroy() - server2.close() - client.end(true, done) + client.end(true, (err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) }) serverClient.on('publish', function onPublish (packet) { @@ -462,7 +456,7 @@ describe('MqttClient', function () { it('check emit error on checkDisconnection w/o callback', function (done) { this.timeout(15000) - const server118 = new MqttServer(function (client) { + const server2 = new MqttServer(function (client) { client.on('connect', function (packet) { client.connack({ reasonCode: 0 @@ -486,14 +480,12 @@ describe('MqttClient', function () { // wait for the client to receive an error... client.on('error', function (error) { assert.equal(error.message, 'client disconnecting') - server118.close() - done() + server2.close((err) => done(err)) }) client.on('connect', function () { client.end(function () { client._checkDisconnecting() }) - server118.close() }) }) }) diff --git a/test/mqtt.js b/test/mqtt.js index ab79cabd1..24e84c859 100644 --- a/test/mqtt.js +++ b/test/mqtt.js @@ -6,81 +6,79 @@ const mqtt = require('../') describe('mqtt', function () { describe('#connect', function () { - it('should return an MqttClient when connect is called with mqtt:/ url', function () { + it('should return an MqttClient when connect is called with mqtt:/ url', function (done) { const c = mqtt.connect('mqtt://localhost:1883') c.should.be.instanceOf(mqtt.MqttClient) - c.end() + c.end((err) => done(err)) }) it('should throw an error when called with no protocol specified', function () { (function () { - const c = mqtt.connect('foo.bar.com') - c.end() + mqtt.connect('foo.bar.com') }).should.throw('Missing protocol') }) it('should throw an error when called with no protocol specified - with options', function () { (function () { - const c = mqtt.connect('tcp://foo.bar.com', { protocol: null }) - c.end() + mqtt.connect('tcp://foo.bar.com', { protocol: null }) }).should.throw('Missing protocol') }) - it('should return an MqttClient with username option set', function () { + it('should return an MqttClient with username option set', function (done) { const c = mqtt.connect('mqtt://user:pass@localhost:1883') c.should.be.instanceOf(mqtt.MqttClient) c.options.should.have.property('username', 'user') c.options.should.have.property('password', 'pass') - c.end() + c.end((err) => done(err)) }) - it('should return an MqttClient with username and password options set', function () { + it('should return an MqttClient with username and password options set', function (done) { const c = mqtt.connect('mqtt://user@localhost:1883') c.should.be.instanceOf(mqtt.MqttClient) c.options.should.have.property('username', 'user') - c.end() + c.end((err) => done(err)) }) - it('should return an MqttClient with the clientid with random value', function () { + it('should return an MqttClient with the clientid with random value', function (done) { const c = mqtt.connect('mqtt://user@localhost:1883') c.should.be.instanceOf(mqtt.MqttClient) c.options.should.have.property('clientId') - c.end() + c.end((err) => done(err)) }) - it('should return an MqttClient with the clientid with empty string', function () { + it('should return an MqttClient with the clientid with empty string', function (done) { const c = mqtt.connect('mqtt://user@localhost:1883?clientId=') c.should.be.instanceOf(mqtt.MqttClient) c.options.should.have.property('clientId', '') - c.end() + c.end((err) => done(err)) }) - it('should return an MqttClient with the clientid option set', function () { + it('should return an MqttClient with the clientid option set', function (done) { const c = mqtt.connect('mqtt://user@localhost:1883?clientId=123') c.should.be.instanceOf(mqtt.MqttClient) c.options.should.have.property('clientId', '123') - c.end() + c.end((err) => done(err)) }) - it('should return an MqttClient when connect is called with tcp:/ url', function () { + it('should return an MqttClient when connect is called with tcp:/ url', function (done) { const c = mqtt.connect('tcp://localhost') c.should.be.instanceOf(mqtt.MqttClient) - c.end() + c.end((err) => done(err)) }) - it('should return an MqttClient with correct host when called with a host and port', function () { + it('should return an MqttClient with correct host when called with a host and port', function (done) { const c = mqtt.connect('tcp://user:pass@localhost:1883') c.options.should.have.property('hostname', 'localhost') c.options.should.have.property('port', 1883) - c.end() + c.end((err) => done(err)) }) const sslOpts = { @@ -89,7 +87,7 @@ describe('mqtt', function () { caPaths: [path.join(__dirname, 'helpers', 'public-cert.pem')] } - it('should return an MqttClient when connect is called with mqtts:/ url', function () { + it('should return an MqttClient when connect is called with mqtts:/ url', function (done) { const c = mqtt.connect('mqtts://localhost', sslOpts) c.options.should.have.property('protocol', 'mqtts') @@ -97,10 +95,10 @@ describe('mqtt', function () { c.on('error', function () {}) c.should.be.instanceOf(mqtt.MqttClient) - c.end() + c.end((err) => done(err)) }) - it('should return an MqttClient when connect is called with ssl:/ url', function () { + it('should return an MqttClient when connect is called with ssl:/ url', function (done) { const c = mqtt.connect('ssl://localhost', sslOpts) c.options.should.have.property('protocol', 'ssl') @@ -108,10 +106,10 @@ describe('mqtt', function () { c.on('error', function () {}) c.should.be.instanceOf(mqtt.MqttClient) - c.end() + c.end((err) => done(err)) }) - it('should return an MqttClient when connect is called with ws:/ url', function () { + it('should return an MqttClient when connect is called with ws:/ url', function (done) { const c = mqtt.connect('ws://localhost', sslOpts) c.options.should.have.property('protocol', 'ws') @@ -119,10 +117,10 @@ describe('mqtt', function () { c.on('error', function () {}) c.should.be.instanceOf(mqtt.MqttClient) - c.end() + c.end((err) => done(err)) }) - it('should return an MqttClient when connect is called with wss:/ url', function () { + it('should return an MqttClient when connect is called with wss:/ url', function (done) { const c = mqtt.connect('wss://localhost', sslOpts) c.options.should.have.property('protocol', 'wss') @@ -130,7 +128,7 @@ describe('mqtt', function () { c.on('error', function () {}) c.should.be.instanceOf(mqtt.MqttClient) - c.end() + c.end((err) => done(err)) }) const sslOpts2 = { @@ -142,20 +140,18 @@ describe('mqtt', function () { it('should throw an error when it is called with cert and key set but no protocol specified', function () { // to do rewrite wrap function (function () { - const c = mqtt.connect(sslOpts2) - c.end() + mqtt.connect(sslOpts2) }).should.throw('Missing secure protocol key') }) it('should throw an error when it is called with cert and key set and protocol other than allowed: mqtt,mqtts,ws,wss,wxs', function () { (function () { sslOpts2.protocol = 'UNKNOWNPROTOCOL' - const c = mqtt.connect(sslOpts2) - c.end() + mqtt.connect(sslOpts2) }).should.throw() }) - it('should return a MqttClient with mqtts set when connect is called key and cert set and protocol mqtt', function () { + it('should return a MqttClient with mqtts set when connect is called key and cert set and protocol mqtt', function (done) { sslOpts2.protocol = 'mqtt' const c = mqtt.connect(sslOpts2) @@ -164,9 +160,10 @@ describe('mqtt', function () { c.on('error', function () {}) c.should.be.instanceOf(mqtt.MqttClient) + c.end((err) => done(err)) }) - it('should return a MqttClient with mqtts set when connect is called key and cert set and protocol mqtts', function () { + it('should return a MqttClient with mqtts set when connect is called key and cert set and protocol mqtts', function (done) { sslOpts2.protocol = 'mqtts' const c = mqtt.connect(sslOpts2) @@ -175,9 +172,10 @@ describe('mqtt', function () { c.on('error', function () {}) c.should.be.instanceOf(mqtt.MqttClient) + c.end((err) => done(err)) }) - it('should return a MqttClient with wss set when connect is called key and cert set and protocol ws', function () { + it('should return a MqttClient with wss set when connect is called key and cert set and protocol ws', function (done) { sslOpts2.protocol = 'ws' const c = mqtt.connect(sslOpts2) @@ -186,9 +184,10 @@ describe('mqtt', function () { c.on('error', function () {}) c.should.be.instanceOf(mqtt.MqttClient) + c.end((err) => done(err)) }) - it('should return a MqttClient with wss set when connect is called key and cert set and protocol wss', function () { + it('should return a MqttClient with wss set when connect is called key and cert set and protocol wss', function (done) { sslOpts2.protocol = 'wss' const c = mqtt.connect(sslOpts2) @@ -197,33 +196,35 @@ describe('mqtt', function () { c.on('error', function () {}) c.should.be.instanceOf(mqtt.MqttClient) + c.end((err) => done(err)) }) - it('should return an MqttClient with the clientid with option of clientId as empty string', function () { + it('should return an MqttClient with the clientid with option of clientId as empty string', function (done) { const c = mqtt.connect('mqtt://localhost:1883', { clientId: '' }) c.should.be.instanceOf(mqtt.MqttClient) c.options.should.have.property('clientId', '') + c.end((err) => done(err)) }) - it('should return an MqttClient with the clientid with option of clientId empty', function () { + it('should return an MqttClient with the clientid with option of clientId empty', function (done) { const c = mqtt.connect('mqtt://localhost:1883') c.should.be.instanceOf(mqtt.MqttClient) c.options.should.have.property('clientId') - c.end() + c.end((err) => done(err)) }) - it('should return an MqttClient with the clientid with option of with specific clientId', function () { + it('should return an MqttClient with the clientid with option of with specific clientId', function (done) { const c = mqtt.connect('mqtt://localhost:1883', { clientId: '123' }) c.should.be.instanceOf(mqtt.MqttClient) c.options.should.have.property('clientId', '123') - c.end() + c.end((err) => done(err)) }) }) }) diff --git a/test/mqtt_store.js b/test/mqtt_store.js index 62c2f7d47..9fa7e915d 100644 --- a/test/mqtt_store.js +++ b/test/mqtt_store.js @@ -4,6 +4,8 @@ const mqtt = require('../lib/connect') describe('store in lib/connect/index.js (webpack entry point)', function () { it('should create store', function (done) { - done(null, new mqtt.Store()) + const store = new mqtt.Store() + store.should.be.instanceOf(mqtt.Store) + done() }) }) diff --git a/test/secure_client.js b/test/secure_client.js index 90d748d3c..5f5165fbb 100644 --- a/test/secure_client.js +++ b/test/secure_client.js @@ -135,10 +135,9 @@ describe('MqttSecureClient', function () { rejectUnauthorized: true }) - client.once('error', function () { - done() - client.end() - client.on('error', function () {}) + client.once('error', function (err) { + err.should.be.instanceOf(Error) + client.end((err) => done(err)) }) }) @@ -152,9 +151,8 @@ describe('MqttSecureClient', function () { client.on('error', function () {}) - // TODO node v0.8.x emits multiple close events client.once('close', function () { - done() + client.end((err) => done(err)) }) }) @@ -180,7 +178,7 @@ describe('MqttSecureClient', function () { server.once('connect', function () { server.on('secureConnection', server.setupConnection) // reset eventHandler - done() + client.end((err) => done(err)) }) }) }) diff --git a/test/websocket_client.js b/test/websocket_client.js index e91a76623..51a1d10b0 100644 --- a/test/websocket_client.js +++ b/test/websocket_client.js @@ -99,7 +99,7 @@ describe('Websocket Client', function () { assert.strictEqual(client.protocol, 'mqtt') }) mqtt.connect(makeOptions()).on('connect', function () { - this.end(true, done) + this.end(true, (err) => done(err)) }) }) @@ -125,7 +125,7 @@ describe('Websocket Client', function () { .on('connect', function () { assert.equal(this.stream.url, expected) assert.equal(actual, expected) - this.end(true, done) + this.end(true, (err) => done(err)) }) }) @@ -140,7 +140,7 @@ describe('Websocket Client', function () { }) mqtt.connect(opts).on('connect', function () { - this.end(true, done) + this.end(true, (err) => done(err)) }) }) @@ -171,8 +171,11 @@ describe('Websocket Client', function () { assert.equal(client.stream.url, actualURL41, 'Protocol for second client should use the default protocol: wss, on port: port + 41.') assert(serverPort42Connected) c.stream.destroy() - client.end(true, done) - serverPort41.close() + client.end(true, (err1) => { + serverPort41.close((err2) => { + done(err1 || err2) + }) + }) }) serverPort42.once('client', function (c) { serverPort42Connected = true From fec1dc6630e6af4c1c5f1d51381efcf387fbc959 Mon Sep 17 00:00:00 2001 From: Bert Kleewein Date: Fri, 7 Jul 2023 11:09:25 -0700 Subject: [PATCH 2/3] add missing close callback --- test/client.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/client.js b/test/client.js index 8add736e1..89529d7c2 100644 --- a/test/client.js +++ b/test/client.js @@ -148,7 +148,7 @@ describe('MqttClient', function () { describe('flushing', function () { it('should attempt to complete pending unsub and send on ping timeout', function (done) { this.timeout(10000) - const server3 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { serverClient.connack({ returnCode: 0 }) }) @@ -173,10 +173,11 @@ describe('MqttClient', function () { unsubscribeCallbackCalled = true }) setTimeout(() => { - client.end((err) => { + client.end((err1) => { assert.strictEqual(pubCallbackCalled && unsubscribeCallbackCalled, true, 'callbacks not invoked') - server3.close() - done(err) + server2.close((err2) => { + done(err1 || err2) + }) }) }, 5000) }) From 4e1fdbbfa4ca288d3031be3e61ee26f119e07c7f Mon Sep 17 00:00:00 2001 From: Bert Kleewein Date: Fri, 7 Jul 2023 11:31:59 -0700 Subject: [PATCH 3/3] fix cleanup in mqtt5 client tests --- test/client_mqtt5.js | 193 ++++++++++++++++++++++++++++--------------- 1 file changed, 128 insertions(+), 65 deletions(-) diff --git a/test/client_mqtt5.js b/test/client_mqtt5.js index 3eaeb8dbd..f8a84c051 100644 --- a/test/client_mqtt5.js +++ b/test/client_mqtt5.js @@ -26,7 +26,7 @@ describe('MQTT 5.0', function () { } const client = mqtt.connect(opts) let publishCount = 0 - const server103 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { assert.strictEqual(packet.properties.topicAliasMaximum, 3) serverClient.connack({ @@ -88,8 +88,11 @@ describe('MQTT 5.0', function () { assert.strictEqual(topic, 'test2') assert.strictEqual(packet.topic, '') assert.strictEqual(packet.properties.topicAlias, 1) - server103.close() - client.end(true, done) + client.end(true, (err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) break } }) @@ -107,7 +110,7 @@ describe('MQTT 5.0', function () { const client = mqtt.connect(opts) let publishCount = 0 - const server103 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { serverClient.connack({ reasonCode: 0, @@ -129,8 +132,11 @@ describe('MQTT 5.0', function () { case 2: assert.strictEqual(packet.topic, '') assert.strictEqual(packet.properties.topicAlias, 1) - server103.close() - client.end(true, done) + client.end(true, (err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) break } }) @@ -158,7 +164,7 @@ describe('MQTT 5.0', function () { const client = mqtt.connect(opts) let publishCount = 0 - const server103 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { serverClient.connack({ reasonCode: 0, @@ -192,8 +198,11 @@ describe('MQTT 5.0', function () { case 5: assert.strictEqual(packet.topic, 'test4') assert.strictEqual(packet.properties.topicAlias, 2) - server103.close() - client.end(true, done) + client.end(true, (err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) break } }) @@ -233,7 +242,7 @@ describe('MQTT 5.0', function () { let connectCount = 0 let publishCount = 0 - const server103 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { switch (connectCount++) { case 0: @@ -287,8 +296,11 @@ describe('MQTT 5.0', function () { } assert.strictEqual(alias2, undefined) serverClient.puback({ messageId: packet.messageId }) - server103.close() - client.end(true, done) + client.end(true, (err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) break } } @@ -322,7 +334,7 @@ describe('MQTT 5.0', function () { let connectCount = 0 let publishCount = 0 - const server103 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { switch (connectCount++) { case 0: @@ -379,8 +391,11 @@ describe('MQTT 5.0', function () { } assert.strictEqual(alias3, undefined) assert.strictEqual(packet.qos, 0) - server103.close() - client.end(true, done) + client.end(true, (err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) break } } @@ -405,7 +420,7 @@ describe('MQTT 5.0', function () { protocolVersion: 5 } const client = mqtt.connect(opts) - const server103 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { serverClient.connack({ reasonCode: 0, @@ -425,8 +440,11 @@ describe('MQTT 5.0', function () { { properties: { topicAlias: 4 } }, function (error) { assert.strictEqual(error.message, 'Sending Topic Alias out of range') - server103.close() - client.end(true, done) + client.end(true, (err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) }) }) }) @@ -440,7 +458,7 @@ describe('MQTT 5.0', function () { protocolVersion: 5 } const client = mqtt.connect(opts) - const server103 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { serverClient.connack({ reasonCode: 0, @@ -457,8 +475,11 @@ describe('MQTT 5.0', function () { { properties: { topicAlias: 1 } }, function (error) { assert.strictEqual(error.message, 'Sending Topic Alias out of range') - server103.close() - client.end(true, done) + client.end(true, (err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) }) }) }) @@ -475,7 +496,7 @@ describe('MQTT 5.0', function () { } } const client = mqtt.connect(opts) - const server103 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { serverClient.connack({ reasonCode: 0, @@ -494,8 +515,11 @@ describe('MQTT 5.0', function () { client.on('error', function (error) { assert.strictEqual(error.message, 'Received Topic Alias is out of range') - server103.close() - client.end(true, done) + client.end(true, (err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) }) }) @@ -511,7 +535,7 @@ describe('MQTT 5.0', function () { } } const client = mqtt.connect(opts) - const server103 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { serverClient.connack({ reasonCode: 0, @@ -530,8 +554,11 @@ describe('MQTT 5.0', function () { client.on('error', function (error) { assert.strictEqual(error.message, 'Received Topic Alias is out of range') - server103.close() - client.end(true, done) + client.end(true, (err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) }) }) @@ -547,7 +574,7 @@ describe('MQTT 5.0', function () { } } const client = mqtt.connect(opts) - const server103 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { serverClient.connack({ reasonCode: 0, @@ -566,8 +593,11 @@ describe('MQTT 5.0', function () { client.on('error', function (error) { assert.strictEqual(error.message, 'Received unregistered Topic Alias') - server103.close() - client.end(true, done) + client.end(true, (err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) }) }) @@ -611,7 +641,7 @@ describe('MQTT 5.0', function () { it('Change values of some properties by server response', function (done) { this.timeout(15000) - const server116 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { serverClient.connack({ reasonCode: 0, @@ -636,8 +666,11 @@ describe('MQTT 5.0', function () { client.on('connect', function () { assert.strictEqual(client.options.keepalive, 16) assert.strictEqual(client.options.properties.maximumPacketSize, 95) - server116.close() - client.end(true, done) + client.end(true, (err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) }) }) @@ -645,7 +678,7 @@ describe('MQTT 5.0', function () { this.timeout(15000) let tryReconnect = true let reconnectEvent = false - const server316 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { serverClient.connack({ reasonCode: 0, @@ -653,8 +686,11 @@ describe('MQTT 5.0', function () { }) serverClient.on('subscribe', function () { if (!tryReconnect) { - server316.close() - serverClient.end(done) + client.end(true, (err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) } }) }) @@ -688,7 +724,7 @@ describe('MQTT 5.0', function () { // this.timeout(15000) let tryReconnect = true let reconnectEvent = false - const server326 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { serverClient.connack({ reasonCode: 0, @@ -706,8 +742,11 @@ describe('MQTT 5.0', function () { } else { if (!tryReconnect) { assert.strictEqual(packet.properties.userProperties.test, 'test') - serverClient.end(done) - server326.close() + client.end(true, (err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) } } }) @@ -778,7 +817,7 @@ describe('MQTT 5.0', function () { protocolVersion: 5 } const subOptions = { properties: { subscriptionIdentifier: 1234 } } - const server119 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { serverClient.connack({ reasonCode: 0 @@ -786,9 +825,11 @@ describe('MQTT 5.0', function () { }) serverClient.on('subscribe', function (packet) { assert.strictEqual(packet.properties.subscriptionIdentifier, subOptions.properties.subscriptionIdentifier) - server119.close() - serverClient.end() - done() + client.end(true, (err1) => { + server2.close((err2) => { + done(err1 || err2) + }) + }) }) }).listen(ports.PORTAND119) @@ -812,8 +853,11 @@ describe('MQTT 5.0', function () { assert.strictEqual(err.message, 'Publish error: Session taken over') assert.strictEqual(err.code, 142) }) - serverThatSendsErrors.close() - client.end(true, done) + client.end(true, (err1) => { + serverThatSendsErrors.close((err2) => { + done(err1 || err2) + }) + }) }) }) @@ -831,8 +875,11 @@ describe('MQTT 5.0', function () { assert.strictEqual(err.message, 'Publish error: Session taken over') assert.strictEqual(err.code, 142) }) - serverThatSendsErrors.close() - client.end(true, done) + client.end(true, (err1) => { + serverThatSendsErrors.close((err2) => { + done(err1 || err2) + }) + }) }) }) @@ -859,9 +906,11 @@ describe('MQTT 5.0', function () { serverClient.on('puback', function (packet) { assert.strictEqual(packet.reasonCode, 128) - serverClient.end(done) - serverClient.destroy() - serverThatSendsErrors.close() + client.end(true, (err1) => { + serverThatSendsErrors.close((err2) => { + done(err1 || err2) + }) + }) }) }) @@ -873,16 +922,16 @@ describe('MQTT 5.0', function () { it('server side disconnect', function (done) { this.timeout(15000) - const server327 = new MqttServer(function (serverClient) { + const server2 = new MqttServer(function (serverClient) { serverClient.on('connect', function (packet) { serverClient.connack({ reasonCode: 0 }) serverClient.disconnect({ reasonCode: 128 }) - server327.close() + server2.close() }) }) - server327.listen(ports.PORTAND327) + server2.listen(ports.PORTAND327) const opts = { host: 'localhost', port: ports.PORTAND327, @@ -892,7 +941,7 @@ describe('MQTT 5.0', function () { const client = mqtt.connect(opts) client.once('disconnect', function (disconnectPacket) { assert.strictEqual(disconnectPacket.reasonCode, 128) - client.end(true, done) + client.end(true, (err) => done(err)) }) }) @@ -919,9 +968,11 @@ describe('MQTT 5.0', function () { serverClient.on('pubrec', function (packet) { assert.strictEqual(packet.reasonCode, 128) - client.end(true, done) - serverClient.destroy() - serverThatSendsErrors.close() + client.end(true, (err1) => { + serverThatSendsErrors.close((err2) => { + done(err1 || err2) + }) + }) }) }) @@ -956,8 +1007,11 @@ describe('MQTT 5.0', function () { const client = mqtt.connect(opts) client.on('error', function (error) { assert.strictEqual(error.message, 'a/b is not valid') - client.end(true, done) - serverThatSendsErrors.close() + client.end(true, (err1) => { + serverThatSendsErrors.close((err2) => { + done(err1 || err2) + }) + }) }) client.once('connect', function () { client.subscribe('a/b', { qos: 1 }) @@ -989,8 +1043,11 @@ describe('MQTT 5.0', function () { const client = mqtt.connect(opts) client.on('error', function (error) { assert.strictEqual(error.message, 'a/b is not valid') - client.end(true, done) - serverThatSendsErrors.close() + client.end(true, (err1) => { + serverThatSendsErrors.close((err2) => { + done(err1 || err2) + }) + }) }) client.once('connect', function () { client.subscribe('a/b', { qos: 1 }) @@ -1022,8 +1079,11 @@ describe('MQTT 5.0', function () { const client = mqtt.connect(opts) client.on('error', function (error) { assert.strictEqual(error.message, 'Wrong reason code for puback') - client.end(true, done) - serverThatSendsErrors.close() + client.end(true, (err1) => { + serverThatSendsErrors.close((err2) => { + done(err1 || err2) + }) + }) }) client.once('connect', function () { client.subscribe('a/b', { qos: 1 }) @@ -1055,8 +1115,11 @@ describe('MQTT 5.0', function () { const client = mqtt.connect(opts) client.on('error', function (error) { assert.strictEqual(error.message, 'Wrong reason code for pubrec') - client.end(true, done) - serverThatSendsErrors.close() + client.end(true, (err1) => { + serverThatSendsErrors.close((err2) => { + done(err1 || err2) + }) + }) }) client.once('connect', function () { client.subscribe('a/b', { qos: 1 })