Skip to content

Commit

Permalink
fix(mqtt stores): improve error handling and tests (mqttjs#1133)
Browse files Browse the repository at this point in the history
* fix(mqtt stores): improve error handling and tests

* fix: linting

* fix: remove mqtt5 properties

* fix: remove extra space
  • Loading branch information
Yoseph Maguire authored and Cédric von Allmen committed Nov 27, 2020
1 parent bc3259b commit 7936ae2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
7 changes: 4 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
})
Expand Down
38 changes: 31 additions & 7 deletions test/abstract_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand Down

0 comments on commit 7936ae2

Please sign in to comment.