Skip to content

Commit

Permalink
Merge pull request #112 from mcollina/fix-109
Browse files Browse the repository at this point in the history
Do not emit the will twice if close() is called twice.
  • Loading branch information
mcollina committed May 19, 2017
2 parents c716b56 + d575c4e commit 478ed2c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ Client.prototype.close = function (done) {
function finish () {
if (!that.disconnected && that.will) {
that.broker.publish(that.will, that, nop)
that.will = null // this function might be called twice
}

conn.removeAllListeners('error')
Expand Down
26 changes: 26 additions & 0 deletions test/will.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,32 @@ test('delivers a will', function (t) {
s.conn.destroy()
})

test('calling close two times should not deliver two wills', function (t) {
t.plan(4)
var opts = {}
var broker = aedes()

broker.on('client', function (client) {
client.close()
client.close()
})

broker.mq.on('mywill', onWill)

// willConnect populates opts with a will
willConnect(setup(broker), opts)

function onWill (packet, cb) {
broker.mq.removeListener('mywill', onWill)
broker.mq.on('mywill', t.fail.bind(t))
t.equal(packet.topic, opts.will.topic, 'topic matches')
t.deepEqual(packet.payload, opts.will.payload, 'payload matches')
t.equal(packet.qos, opts.will.qos, 'qos matches')
t.equal(packet.retain, opts.will.retain, 'retain matches')
cb()
}
})

test('delivers old will in case of a crash', function (t) {
t.plan(7)
var persistence = memory()
Expand Down

0 comments on commit 478ed2c

Please sign in to comment.