Skip to content

Commit

Permalink
Fix bug while client subscribing to an Array of topics. (#240)
Browse files Browse the repository at this point in the history
* Fix bug while client subscribing to an Array of topics.

* keep original subscription negating logic to pass test case.

* Fix bug to persistent negated topic properly

* prevent Undefined Error

* restrict condition

* add test case for 'negate subscription with correct persistence'
  • Loading branch information
seeLuck authored and mcollina committed Jun 3, 2019
1 parent 0d74bcb commit 90c7f22
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/handlers/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function SubscribeState (client, packet, finish, granted) {
this.packet = packet
this.finish = finish
this.granted = granted
this.subsIndex = 0
}

function handleSubscribe (client, packet, done) {
Expand Down Expand Up @@ -67,12 +68,10 @@ function storeSubscriptions (sub, done) {

if (!sub) {
this.granted.push(128)
return done(null, sub)
}

if (sub && packet.subscriptions[0].topic !== sub.topic) {
// don't call addSubscriptions per topic,
// TODO change aedes subscribe handle, but this is a fast & dirty fix for now
if (packet.subscriptions.length > 0 && ++this.subsIndex < packet.subscriptions.length) {
// TODO change aedes subscribe handle, but this fix bugs for now.
return done(null, sub)
}

Expand Down
34 changes: 34 additions & 0 deletions test/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,40 @@ test('negate multiple subscriptions', function (t) {
})
})

test('negate subscription with correct persistence', function (t) {
t.plan(7)

var broker = aedes()
broker.authorizeSubscribe = function (client, sub, cb) {
t.ok(client, 'client exists')
if (sub.topic === 'hello') {
sub = null
}
cb(null, sub)
}

var s = connect(setup(broker), { clean: false, clientId: 'abcde' })
s.outStream.once('data', function (packet) {
t.equal(packet.cmd, 'suback')
t.deepEqual(packet.granted, [128, 0])
t.notEqual(broker.persistence._subscriptions.get('abcde'), undefined)
t.deepEqual(broker.persistence._subscriptions.get('abcde').size, 2)
t.equal(packet.messageId, 24)
})

s.inStream.write({
cmd: 'subscribe',
messageId: 24,
subscriptions: [{
topic: 'hello',
qos: 0
}, {
topic: 'world',
qos: 0
}]
})
})

test('negate multiple subscriptions random times', function (t) {
t.plan(5)

Expand Down

0 comments on commit 90c7f22

Please sign in to comment.