Skip to content

Commit

Permalink
Merge 7621199 into abcecd3
Browse files Browse the repository at this point in the history
  • Loading branch information
gnought committed Jul 20, 2019
2 parents abcecd3 + 7621199 commit d5578d7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
9 changes: 7 additions & 2 deletions aedes.js
Expand Up @@ -132,6 +132,7 @@ function Aedes (opts) {

// metadata
this.connectedClients = 0
this.closed = false
}

util.inherits(Aedes, EE)
Expand Down Expand Up @@ -280,14 +281,18 @@ function closeClient (client, cb) {
this.clients[client].close(cb)
}

Aedes.prototype.close = function (cb) {
Aedes.prototype.close = function (cb = noop) {
var that = this
if (this.closed) {
this.emit('closed')
return cb()
}
this.closed = true
clearInterval(this._heartbeatInterval)
clearInterval(this._clearWillInterval)
this._parallel(this, closeClient, Object.keys(this.clients), doneClose)
function doneClose () {
that.emit('closed')
cb = cb || noop
that.mq.close(cb)
}
}
Expand Down
19 changes: 12 additions & 7 deletions test/basic.js
Expand Up @@ -324,18 +324,23 @@ test('broker closes', function (t) {
})

test('broker closes gracefully', function (t) {
t.plan(4)
t.plan(7)

var broker = aedes()
var client1 = noError(connect(setup(broker, false)))
var client2 = noError(connect(setup(broker, false)))
eos(client1.conn, t.pass.bind('client1 closes'))
eos(client2.conn, t.pass.bind('client2 closes'))

setImmediate(() => {
broker.close(function (err) {
t.error(err, 'no error')
t.ok(broker.mq.closed, 'broker mq closes')
t.equal(broker.connectedClients, 2, '2 connected clients')
eos(client1.conn, t.pass.bind('client1 closes'))
eos(client2.conn, t.pass.bind('client2 closes'))

setImmediate(() => {
broker.close(function (err) {
t.error(err, 'no error')
t.ok(broker.mq.closed, 'broker mq closes')
t.ok(broker.closed, 'broker closes')
t.equal(broker.connectedClients, 0, 'no connected clients')
})
})
})
})
Expand Down

0 comments on commit d5578d7

Please sign in to comment.