When connection fail, emit the error. (#28)

* When connection fail, emit the error.

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

* Add test for connection error.
This commit is contained in:
Yanlong Wang 2016-09-14 10:26:03 +08:00 committed by Brian C
parent cf28f9357f
commit 6a7edabc22
2 changed files with 25 additions and 2 deletions

View File

@ -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)

View File

@ -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({
@ -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'))
})