Skip to content

Commit

Permalink
test case for retained message on negated subsciption bug (#227)
Browse files Browse the repository at this point in the history
* test case for retained message on negated subsciption bug

* Fixed indentation issues

* reduced timer

* Fix for negated subscription bug
  • Loading branch information
GavinDmello authored and mcollina committed Feb 1, 2019
1 parent 003a06b commit 0c51869
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/handlers/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ function storeSubscriptions (sub, done) {
var broker = client.broker
var perst = broker.persistence

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
Expand All @@ -79,21 +84,20 @@ function storeSubscriptions (sub, done) {
return done(null, sub)
}

perst.addSubscriptions(client, packet.subscriptions, function (err) {
perst.addSubscriptions(client, packet.subscriptions, function addSub (err) {
done(err, sub)
})
}

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

var client = this.client
var broker = client.broker
var func = nop

if (!sub) {
return done()
}

switch (sub.qos) {
case 2:
case 1:
Expand Down Expand Up @@ -145,7 +149,12 @@ function completeSubscribe (err) {
}

if (packet.messageId) {
write(client, new SubAck(packet, granted), done)
write(client, new SubAck(packet, granted), nop)
}

// negated subscription check
if (this.granted && this.granted[0] === 128) {
return done()
} else {
done()
}
Expand Down
40 changes: 40 additions & 0 deletions test/client-pub-sub.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,43 @@ test('get message when client disconnects', function (t) {
t.equal(client2, packet.payload.toString())
})
})

test('should not receive a message on negated subscription', function (t) {
t.plan(3)

var broker = aedes()
broker.authorizeSubscribe = function (client, sub, callback) {
callback(null, null)
}

broker.on('client', function (client) {
broker.publish({
topic: 'hello',
payload: Buffer.from('world'),
qos: 0,
retain: true
}, function (err) {
t.error(err, 'no error')
client.subscribe({
topic: 'hello',
qos: 0
}, function (err) {
t.error(err, 'no error')
})
})
})

var s = connect(setup(broker))
var receivedPacket = null
s.outStream.once('data', function (packet) {
receivedPacket = packet
})

setTimeout(function () {
if (receivedPacket != null) {
t.fail('Packet should not be received')
} else {
t.pass('Message not received')
}
}, 100)
})

0 comments on commit 0c51869

Please sign in to comment.