Skip to content

Commit

Permalink
Merge pull request #171 from cordovapolymer/master
Browse files Browse the repository at this point in the history
Emit 'unsubsribe' event when client has clean=true on disconnect #145
  • Loading branch information
mcollina authored Nov 16, 2017
2 parents e7a9c92 + 26c94e9 commit 3e7941b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/handlers/unsubscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function completeUnsubscribe (err) {
return client.emit('error', err)
}

if (!packet.close && packet.unsubscriptions.length > 0) {
if ((!packet.close || client.clean === true) && packet.unsubscriptions.length > 0) {
client.broker.emit('unsubscribe', packet.unsubscriptions, client)
}

Expand Down
37 changes: 35 additions & 2 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,22 +213,55 @@ test('unsubscribe without subscribe', function (t) {
})
})

test('unsubscribe on disconnect', function (t) {
var s = noError(connect(setup()), t)
test('unsubscribe on disconnect for a clean=true client', function (t) {
var opts = { clean: true }
var s = noError(connect(setup(), opts), t)

subscribe(t, s, 'hello', 0, function () {
s.conn.emit('close')
s.outStream.on('data', function () {
t.fail('should not receive any more messages')
})
s.broker.once('unsubscribe', function () {
t.pass('should emit unsubscribe')
t.end()
})
s.broker.once('closed', function () {
t.ok(true)
})
s.broker.publish({
cmd: 'publish',
topic: 'hello',
payload: Buffer.from('world')
}, function () {
t.pass('calls the callback')
})
})
})

test('unsubscribe on disconnect for a clean=false client', function (t) {
var opts = { clean: false }
var s = noError(connect(setup(), opts), t)

subscribe(t, s, 'hello', 0, function () {
s.conn.emit('close')
s.outStream.on('data', function () {
t.fail('should not receive any more messages')
})
s.broker.once('unsubscribe', function () {
t.fail('should not emit unsubscribe')
})
s.broker.once('closed', function () {
t.ok(true)
t.end()
})
s.broker.publish({
cmd: 'publish',
topic: 'hello',
payload: Buffer.from('world')
}, function () {
t.pass('calls the callback')
})
})
})

Expand Down
2 changes: 1 addition & 1 deletion test/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ test('emit unsubscribe event', function (t) {
t.plan(6)

var broker = aedes()
var s = connect(setup(broker), { clientId: 'abcde' })
var s = connect(setup(broker), { clean: true, clientId: 'abcde' })

broker.on('unsubscribe', function (unsubscriptions, client) {
t.deepEqual(unsubscriptions, [
Expand Down

0 comments on commit 3e7941b

Please sign in to comment.