Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close aedes broker gracefully #279

Merged
merged 2 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions aedes.js
Original file line number Diff line number Diff line change
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,17 @@ 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) {
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
Original file line number Diff line number Diff line change
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