Skip to content

Commit

Permalink
Merge pull request #32 from dpatekar/master
Browse files Browse the repository at this point in the history
prevent null client unregister
  • Loading branch information
mcollina committed Feb 24, 2016
2 parents b3cd442 + bb06b94 commit bc07cd0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
15 changes: 11 additions & 4 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,14 @@ function Client (broker, conn) {
}

this.deliverQoS = function deliverQoS (_packet, cb) {
var packet = new QoSPacket(_packet, that)
packet.writeCallback = cb
broker.persistence.outgoingUpdate(that, packet, writeQoS)
// downgrade to qos0 if requested by publish
if (_packet.qos === 0) {
that.deliver0(_packet, cb)
} else {
var packet = new QoSPacket(_packet, that)
packet.writeCallback = cb
broker.persistence.outgoingUpdate(that, packet, writeQoS)
}
}

this._keepaliveTimer = null
Expand Down Expand Up @@ -183,7 +188,9 @@ Client.prototype.close = function (done) {
conn.removeAllListeners('error')
conn.on('error', nop)

that.broker.unregisterClient(that)
if (that.broker.clients[that.id]) {
that.broker.unregisterClient(that)
}

// hack to clean up the write callbacks
// supports streams2 & streams3, so node 0.10, 0.11, and iojs
Expand Down
3 changes: 2 additions & 1 deletion test/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test('authenticate successfully a client with username and password', function (
})

test('authenticate unsuccessfully a client with username and password', function (t) {
t.plan(5)
t.plan(6)

var s = setup()

Expand All @@ -74,6 +74,7 @@ test('authenticate unsuccessfully a client with username and password', function
})

eos(s.outStream, function () {
t.equal(s.broker.connectedClients, 0, 'no connected clients')
t.pass('ended')
})

Expand Down
26 changes: 26 additions & 0 deletions test/qos1.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,3 +545,29 @@ test('upgrade a QoS 0 subscription to QoS 1', function (t) {
})
})
})

test('downgrade QoS 0 publish on QoS 1 subsciption', function (t) {
var s = connect(setup())
var expected = {
cmd: 'publish',
topic: 'hello',
payload: new Buffer('world'),
qos: 0,
length: 12,
retain: false,
dup: false
}

subscribe(t, s, 'hello', 1, function () {
s.outStream.once('data', function (packet) {
t.deepEqual(packet, expected, 'packet matches')
t.end()
})
s.broker.publish({
cmd: 'publish',
topic: 'hello',
payload: 'world',
qos: 0
})
})
})

0 comments on commit bc07cd0

Please sign in to comment.