Skip to content

Commit

Permalink
Stop ingesting packets after the client has errored
Browse files Browse the repository at this point in the history
Fixed #21.
  • Loading branch information
mcollina committed Jan 1, 2016
1 parent 32f3354 commit a968782
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/client.js
Expand Up @@ -158,6 +158,9 @@ Client.prototype.close = function (done) {
finish()
}

this.parser.removeAllListeners('packet')
conn.removeAllListeners('readable')

if (this._keepaliveTimer) {
this._keepaliveTimer.clear()
this._keepaliveTimer = null
Expand Down
30 changes: 30 additions & 0 deletions test/regr-21.js
@@ -0,0 +1,30 @@
'use strict'

var test = require('tape').test
var helper = require('./helper')
var setup = helper.setup
var connect = helper.connect

test('after an error, outstanding packets are discarded', function (t) {
t.plan(1)
var s = connect(setup(), {
keepalive: 1000
})
var packet = {
cmd: 'publish',
topic: 'hello',
payload: 'world'
}

s.broker.mq.on('hello', function (msg, cb) {
t.pass('first msg received')
s.inStream.emit('error', new Error('something went wrong'))
setImmediate(cb)
})

s.inStream.write(packet)
setImmediate(function () {
s.inStream.write(packet)
s.inStream.write(packet)
})
})

0 comments on commit a968782

Please sign in to comment.