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

Emit 'unsubsribe' event when client has clean=true on disconnect #145 #171

Merged
merged 2 commits into from
Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 (client.clean === true && packet.unsubscriptions.length > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the check for packet.close  must stay. See https://github.com/mcollina/aedes/blob/275bda32461ea8053c8610b8a3e5b6f0f838f1b6/lib/client.js#L214.
Basically if packet.close === true && client.clean === false we should not emit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcollina , thank you for a clarification, please see the new commit

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' })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add another test that fails if the event is emitted when clean is false and the connection ends abruptly?

Copy link
Collaborator

@mcollina mcollina Nov 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forget about it, it's already added.


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