Skip to content

Commit

Permalink
Raised connection closed error in setImmedate
Browse files Browse the repository at this point in the history
  • Loading branch information
gnought committed Jul 25, 2019
1 parent 9c64669 commit 6fb4d14
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
3 changes: 1 addition & 2 deletions lib/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ function write (client, packet, done) {
setImmediate(done)
}
} else {
console.log('error')
client._onError(new Error('connection closed'))
setImmediate(client._onError.bind(client, new Error('connection closed')))
}
}

Expand Down
39 changes: 23 additions & 16 deletions test/close_socket_by_other_party.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,28 @@ test('client is closed before authenticate returns', function (t) {
evt.emit('AuthenticateEnd', client)
}
})
broker.on('client', function (client) {
t.fail('should no client registration')
})
broker.on('connackSent', function () {
t.fail('should no connack be sent')
})
broker.on('clientError', function (client, err) {
t.error(err)
})

connect(setup(broker, false))

evt.on('AuthenticateBegin', function (client) {
t.equal(broker.connectedClients, 0)
client.close()
broker.on('client', function (client) {
t.fail('should no client registration')
})
broker.on('connackSent', function () {
t.fail('should no connack be sent')
})
broker.on('clientError', function (client, err) {
t.error(err)
})
})
evt.on('AuthenticateEnd', function (client) {
t.equal(broker.connectedClients, 0)
broker.close()
t.end()
setImmediate(() => {
broker.close()
t.end()
})
})
})

Expand All @@ -57,6 +61,9 @@ test('client is closed before authorizePublish returns', function (t) {
evt.emit('AuthorizePublishEnd', client)
}
})
broker.on('clientError', function (client, err) {
t.equal(err.message, 'connection closed')
})

var s = connect(setup(broker, false))
s.inStream.write({
Expand All @@ -67,17 +74,17 @@ test('client is closed before authorizePublish returns', function (t) {
messageId: 10,
retain: false
})

evt.on('AuthorizePublishBegin', function (client) {
t.equal(broker.connectedClients, 1)
client.close()
broker.on('clientError', function (client, err) {
t.equal(err.message, 'connection closed')
})
})
evt.on('AuthorizePublishEnd', function (client) {
t.equal(broker.connectedClients, 0)
broker.close()
t.end()
setImmediate(() => {
broker.close()
t.end()
})
})
})

Expand Down

0 comments on commit 6fb4d14

Please sign in to comment.