Skip to content
This repository has been archived by the owner on Dec 30, 2019. It is now read-only.

Commit

Permalink
When connection fail, emit the error. (#28)
Browse files Browse the repository at this point in the history
* When connection fail, emit the error.

If client connect failed, emit the connection error  rather than swallowing it.

* Add test for connection error.
  • Loading branch information
nomagick authored and brianc committed Sep 14, 2016
1 parent cf28f93 commit 6a7edab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Pool.prototype._create = function (cb) {
if (err) {
this.log('client connection error:', err)
cb(err)
this.emit('error', err)
} else {
this.log('client connected')
this.emit('connect', client)
Expand Down
26 changes: 24 additions & 2 deletions test/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('events', function () {
})
})

it('emits "connect" only with a successful connection', function (done) {
it('emits "error" with a failed connection', function (done) {
var pool = new Pool({
// This client will always fail to connect
Client: mockClient({
Expand All @@ -34,7 +34,29 @@ describe('events', function () {
pool.on('connect', function () {
throw new Error('should never get here')
})
pool._create(function (err) {
pool.on('error', function (err) {
if (err) done()
else done(new Error('expected failure'))
})
pool.connect()
})

it('callback err with a failed connection', function (done) {
var pool = new Pool({
// This client will always fail to connect
Client: mockClient({
connect: function (cb) {
process.nextTick(function () { cb(new Error('bad news')) })
}
})
})
pool.on('connect', function () {
throw new Error('should never get here')
})
pool.on('error', function (err) {
if (!err) done(new Error('expected failure'))
})
pool.connect(function (err) {
if (err) done()
else done(new Error('expected failure'))
})
Expand Down

0 comments on commit 6a7edab

Please sign in to comment.