From 15df9a4301ef0a7d6983b6c77702359f2be1cba2 Mon Sep 17 00:00:00 2001 From: cordovapolymer Date: Wed, 15 Nov 2017 10:20:23 -0500 Subject: [PATCH 1/2] Emit 'unsubsribe' event when client has clean=true on disconnect #145 --- lib/handlers/unsubscribe.js | 2 +- test/basic.js | 37 +++++++++++++++++++++++++++++++++++-- test/meta.js | 2 +- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/lib/handlers/unsubscribe.js b/lib/handlers/unsubscribe.js index 620b5256..2daa6cf1 100644 --- a/lib/handlers/unsubscribe.js +++ b/lib/handlers/unsubscribe.js @@ -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) { client.broker.emit('unsubscribe', packet.unsubscriptions, client) } diff --git a/test/basic.js b/test/basic.js index 3f4affc7..60f5a85d 100644 --- a/test/basic.js +++ b/test/basic.js @@ -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') + }) }) }) diff --git a/test/meta.js b/test/meta.js index 27ab4e3a..9c3a5f21 100644 --- a/test/meta.js +++ b/test/meta.js @@ -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, [ From 26c94e90607cb9d5024a8cca320c6f3947cc6dca Mon Sep 17 00:00:00 2001 From: cordovapolymer Date: Thu, 16 Nov 2017 13:43:52 -0500 Subject: [PATCH 2/2] if packet.close === true && client.clean === false we should not emit 'unsubscribe' --- lib/handlers/unsubscribe.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/handlers/unsubscribe.js b/lib/handlers/unsubscribe.js index 2daa6cf1..5a5b8ac9 100644 --- a/lib/handlers/unsubscribe.js +++ b/lib/handlers/unsubscribe.js @@ -68,7 +68,7 @@ function completeUnsubscribe (err) { return client.emit('error', err) } - if (client.clean === true && packet.unsubscriptions.length > 0) { + if ((!packet.close || client.clean === true) && packet.unsubscriptions.length > 0) { client.broker.emit('unsubscribe', packet.unsubscriptions, client) }