Add 'connect' event to pool (#7)

* Have pool emit 'connect' callback with client

* Ensure pool emits client on connect event
This commit is contained in:
Brian C 2016-06-22 23:29:35 -05:00 committed by GitHub
parent 7ef08fd861
commit 63caf7cd4c
2 changed files with 26 additions and 0 deletions

View File

@ -14,6 +14,7 @@ var Pool = module.exports = function (options, Client) {
this.options.create = this.options.create || this._create.bind(this)
this.options.destroy = this.options.destroy || this._destroy.bind(this)
this.pool = new genericPool.Pool(this.options)
this.onCreate = this.options.onCreate
}
util.inherits(Pool, EventEmitter)
@ -37,6 +38,7 @@ Pool.prototype._create = function (cb) {
client.connect(function (err) {
this.log('client connected')
this.emit('connect', client)
if (err) {
this.log('client connection error:', err)
cb(err)

24
test/events.js Normal file
View File

@ -0,0 +1,24 @@
var expect = require('expect.js')
var describe = require('mocha').describe
var it = require('mocha').it
var Pool = require('../')
describe('events', function () {
it('emits connect before callback', function (done) {
var pool = new Pool()
var emittedClient = false
pool.on('connect', function (client) {
emittedClient = client
})
pool.connect(function (err, client, release) {
if (err) return done(err)
release()
pool.end()
expect(client).to.be(emittedClient)
done()
})
})
})