node-postgres/test/events.js
Cody Greene eca2ea0ede emit "connect" event only on success and avoid double callback (#22)
* fail: "connect" event only on success

Double callback invocation will also cause this to fail.

* avoid double callback: _create

If `client.connect` returns an error, then the callback for `Pool#_create` is only invoked once. Also the `connect` event is only emitted on a successful connection, the client is otherwise rather useless.

* legacy compat; don't use Object.assign

* legacy compat; events.EventEmitter
2016-08-05 16:22:50 -05:00

72 lines
1.8 KiB
JavaScript

var expect = require('expect.js')
var EventEmitter = require('events').EventEmitter
var describe = require('mocha').describe
var it = require('mocha').it
var objectAssign = require('object-assign')
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()
})
})
it('emits "connect" only with a successful 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._create(function (err) {
if (err) done()
else done(new Error('expected failure'))
})
})
it('emits acquire every time a client is acquired', function (done) {
var pool = new Pool()
var acquireCount = 0
pool.on('acquire', function (client) {
expect(client).to.be.ok()
acquireCount++
})
for (var i = 0; i < 10; i++) {
pool.connect(function (err, client, release) {
err ? done(err) : release()
release()
if (err) return done(err)
})
pool.query('SELECT now()')
}
setTimeout(function () {
expect(acquireCount).to.be(20)
pool.end(done)
}, 40)
})
})
function mockClient (methods) {
return function () {
var client = new EventEmitter()
objectAssign(client, methods)
return client
}
}