mirror of
https://github.com/brianc/node-postgres.git
synced 2026-01-18 15:55:05 +00:00
* Revert "When connection fail, emit the error. (#28)" This reverts commit 6a7edabc22e36db7386c97ee93f08f957364f37d. The callback passed to `Pool.prototype.connect` should be responsible for handling connection errors. The `error` event is documented to be: > Emitted whenever an idle client in the pool encounters an error. This isn’t the case of an idle client in the pool; it never makes it into the pool. It also breaks tests on pg’s master because of nonspecific dependencies. * Don’t create promises when callbacks are provided It’s incorrect to do so. One consequence is that a rejected promise will be unhandled, which is currently annoying, but also dangerous in the future: > DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. The way callbacks are used currently also causes #24 (hiding of errors thrown synchronously from the callback). One fix for that would be to call them asynchronously from inside the `new Promise()` executor: process.nextTick(cb, error); I don’t think it’s worth implementing, though, since it would still be backwards-incompatible – just less obvious about it. Also fixes a bug where the `Pool.prototype.connect` callback would be called twice if there was an error. * Use Node-0.10-compatible `process.nextTick`
72 lines
1.8 KiB
JavaScript
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
|
|
}
|
|
}
|