diff --git a/lib/client.js b/lib/client.js index 78f3f52ba..774384f79 100644 --- a/lib/client.js +++ b/lib/client.js @@ -815,13 +815,14 @@ MqttClient.prototype.end = function (force, opts, cb) { function closeStores () { debug('end :: closeStores: closing incoming and outgoing stores') that.disconnected = true - that.incomingStore.close(function () { - that.outgoingStore.close(function () { + that.incomingStore.close(function (e1) { + that.outgoingStore.close(function (e2) { debug('end :: closeStores: emitting end') that.emit('end') if (cb) { + let err = e1 || e2 debug('end :: closeStores: invoking callback with args') - cb() + cb(err) } }) }) diff --git a/test/abstract_client.js b/test/abstract_client.js index 8437f7215..b2577e032 100644 --- a/test/abstract_client.js +++ b/test/abstract_client.js @@ -90,24 +90,48 @@ module.exports = function (server, config) { }) }) - it('should pass store close error to end callback but not to end listeners', function (done) { + it('should pass store close error to end callback but not to end listeners (incomingStore)', function (done) { var store = new Store() - var client = connect({outgoingStore: store}) + var client = connect({ incomingStore: store }) store.close = function (cb) { cb(new Error('test')) } client.once('end', function () { if (arguments.length === 0) { - return done() + return } - throw new Error('no argument shoould be passed to event') + throw new Error('no argument should be passed to event') }) client.once('connect', function () { - client.end(function (test) { - if (test && test.message === 'test') { - return + client.end(function (testError) { + if (testError && testError.message === 'test') { + return done() + } + throw new Error('bad argument passed to callback') + }) + }) + }) + + it('should pass store close error to end callback but not to end listeners (outgoingStore)', function (done) { + var store = new Store() + var client = connect({ outgoingStore: store }) + + store.close = function (cb) { + cb(new Error('test')) + } + client.once('end', function () { + if (arguments.length === 0) { + return + } + throw new Error('no argument should be passed to event') + }) + + client.once('connect', function () { + client.end(function (testError) { + if (testError && testError.message === 'test') { + return done() } throw new Error('bad argument passed to callback') })