mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
* Initial work * Make progress on custom pool * Make all original tests pass * Fix test race * Fix test when DNS is missing * Test more error conditions * Add test for byop * Add BYOP tests for errors * Add test for idle client error expunging * Fix typo * Replace var with const/let * Remove var usage * Fix linting * Work on connection timeout * Work on error condition tests * Remove logging * Add connection timeout * Add idle timeout * Test for returning to client to pool after error fixes #48 * Add idleTimeout support to native client * Add pg as peer dependency fixes #45 * Rename properties * Fix lint * use strict * Add draining to pool.end * Ensure ending pools drain properly * Remove yarn.lock * Remove object-assign * Remove node 8 * Remove closure for waiter construction * Ensure client.connect is never sync * Fix lint * Change to es6 class * Code cleanup & lint fixes
88 lines
2.3 KiB
JavaScript
88 lines
2.3 KiB
JavaScript
'use strict'
|
|
|
|
const expect = require('expect.js')
|
|
const EventEmitter = require('events').EventEmitter
|
|
const describe = require('mocha').describe
|
|
const it = require('mocha').it
|
|
const Pool = require('../')
|
|
|
|
describe('events', function () {
|
|
it('emits connect before callback', function (done) {
|
|
const pool = new Pool()
|
|
let 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) {
|
|
const pool = new Pool({
|
|
// This client will always fail to connect
|
|
Client: mockClient({
|
|
connect: function (cb) {
|
|
process.nextTick(() => {
|
|
cb(new Error('bad news'))
|
|
setImmediate(done)
|
|
})
|
|
}
|
|
})
|
|
})
|
|
pool.on('connect', function () {
|
|
throw new Error('should never get here')
|
|
})
|
|
return pool.connect().catch(e => expect(e.message).to.equal('bad news'))
|
|
})
|
|
|
|
it('emits acquire every time a client is acquired', function (done) {
|
|
const pool = new Pool()
|
|
let acquireCount = 0
|
|
pool.on('acquire', function (client) {
|
|
expect(client).to.be.ok()
|
|
acquireCount++
|
|
})
|
|
for (let i = 0; i < 10; i++) {
|
|
pool.connect(function (err, client, release) {
|
|
if (err) return done(err)
|
|
release()
|
|
})
|
|
pool.query('SELECT now()')
|
|
}
|
|
setTimeout(function () {
|
|
expect(acquireCount).to.be(20)
|
|
pool.end(done)
|
|
}, 100)
|
|
})
|
|
|
|
it('emits error and client if an idle client in the pool hits an error', function (done) {
|
|
const pool = new Pool()
|
|
pool.connect(function (err, client) {
|
|
expect(err).to.equal(undefined)
|
|
client.release()
|
|
setImmediate(function () {
|
|
client.emit('error', new Error('problem'))
|
|
})
|
|
pool.once('error', function (err, errClient) {
|
|
expect(err.message).to.equal('problem')
|
|
expect(errClient).to.equal(client)
|
|
done()
|
|
})
|
|
})
|
|
})
|
|
})
|
|
|
|
function mockClient (methods) {
|
|
return function () {
|
|
const client = new EventEmitter()
|
|
Object.assign(client, methods)
|
|
return client
|
|
}
|
|
}
|