Skip to content

Commit

Permalink
Merge pull request #130 from mcollina/liveRetain
Browse files Browse the repository at this point in the history
Retain false when subscriber is active
  • Loading branch information
mcollina committed Aug 19, 2017
2 parents 663707a + 2531e36 commit ecdd0cc
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions aedes.js
Expand Up @@ -146,6 +146,7 @@ function storeRetained (_, done) {
}

function emitPacket (_, done) {
this.packet.retain = false
this.broker.mq.emit(this.packet, done)
}

Expand Down
33 changes: 33 additions & 0 deletions test/basic.js
Expand Up @@ -157,6 +157,39 @@ test('unsubscribe', function (t) {
})
})

test('live retain packets', function (t) {
t.plan(5)
var expected = {
cmd: 'publish',
topic: 'hello',
payload: Buffer.from('world'),
retain: false,
dup: false,
length: 12,
qos: 0
}

var s = noError(connect(setup()), t)

subscribe(t, s, 'hello', 0, function () {
s.outStream.on('data', function (packet) {
t.deepEqual(packet, expected)
})

s.broker.publish({
cmd: 'publish',
topic: 'hello',
payload: Buffer.from('world'),
retain: true,
dup: false,
length: 12,
qos: 0
}, function () {
t.pass('publish finished')
})
})
})

test('unsubscribe without subscribe', function (t) {
t.plan(1)

Expand Down
33 changes: 32 additions & 1 deletion test/qos1.js
Expand Up @@ -416,6 +416,37 @@ test('deliver QoS 1 retained messages', function (t) {
})
})

test('deliver QoS 1 retained messages', function (t) {
var broker = aedes()
var publisher = connect(setup(broker))
var subscriber = connect(setup(broker))
var expected = {
cmd: 'publish',
topic: 'hello',
payload: Buffer.from('world'),
qos: 1,
dup: false,
length: 14,
retain: false
}

subscribe(t, subscriber, 'hello', 1, function () {
subscriber.outStream.once('data', function (packet) {
delete packet.messageId
t.deepEqual(packet, expected, 'packet must match')
t.end()
})
publisher.inStream.write({
cmd: 'publish',
topic: 'hello',
payload: 'world',
qos: 1,
messageId: 42,
retain: true
})
})
})

test('deliver QoS 0 retained message with QoS 1 subscription', function (t) {
var broker = aedes()
var publisher = connect(setup(broker))
Expand Down Expand Up @@ -448,7 +479,7 @@ test('deliver QoS 0 retained message with QoS 1 subscription', function (t) {
publisher.inStream.write({
cmd: 'publish',
topic: 'hello',
payload: 'world',
payload: Buffer.from('world'),
qos: 0,
messageId: 42,
retain: true
Expand Down

0 comments on commit ecdd0cc

Please sign in to comment.