Skip to content

Commit

Permalink
Fix disconnection tests for pg-pool 2.0.7 (#1946)
Browse files Browse the repository at this point in the history
* Require latest pg-pool ^2.0.7

to limit variability of next pg’s installations.

* Ignore EPIPE when writing termination message

I don’t know why this wasn’t necessary for tests to pass before…

* Fix disconnection tests for pg-pool 2.0.7

In pg-pool 2.0.7, checked-out clients became responsible for their own 'error' events.

brianc/node-pg-pool#123
  • Loading branch information
charmander authored and brianc committed Nov 11, 2019
1 parent fde5ec5 commit caa6517
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
5 changes: 2 additions & 3 deletions lib/connection.js
Expand Up @@ -64,9 +64,8 @@ Connection.prototype.connect = function (port, host) {
})

const reportStreamError = function (error) {
// don't raise ECONNRESET errors - they can & should be ignored
// during disconnect
if (self._ending && error.code === 'ECONNRESET') {
// errors about disconnections should be ignored during disconnect
if (self._ending && (error.code === 'ECONNRESET' || error.code === 'EPIPE')) {
return
}
self.emit('error', error)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -22,7 +22,7 @@
"buffer-writer": "2.0.0",
"packet-reader": "1.0.0",
"pg-connection-string": "0.1.3",
"pg-pool": "^2.0.4",
"pg-pool": "^2.0.7",
"pg-types": "^2.1.0",
"pgpass": "1.x",
"semver": "4.3.2"
Expand Down
32 changes: 16 additions & 16 deletions test/integration/connection-pool/error-tests.js
Expand Up @@ -8,15 +8,13 @@ suite.test('connecting to invalid port', (cb) => {
pool.connect().catch(e => cb())
})

suite.test('errors emitted on pool', (cb) => {
suite.test('errors emitted on checked-out clients', (cb) => {
// make pool hold 2 clients
const pool = new pg.Pool({ max: 2 })
// get first client
pool.connect(assert.success(function (client, done) {
client.id = 1
client.query('SELECT NOW()', function () {
pool.connect(assert.success(function (client2, done2) {
client2.id = 2
var pidColName = 'procpid'
helper.versionGTE(client2, 90200, assert.success(function (isGreater) {
var killIdleQuery = 'SELECT pid, (SELECT pg_terminate_backend(pid)) AS killed FROM pg_stat_activity WHERE state = $1'
Expand All @@ -26,10 +24,9 @@ suite.test('errors emitted on pool', (cb) => {
params = ['%IDLE%']
}

pool.once('error', (err, brokenClient) => {
assert.ok(err)
assert.ok(brokenClient)
assert.equal(client.id, brokenClient.id)
client.once('error', (err) => {
client.on('error', (err) => {})
done(err)
cb()
})

Expand Down Expand Up @@ -57,18 +54,18 @@ suite.test('connection-level errors cause queued queries to fail', (cb) => {
}
}))

pool.once('error', assert.calls((err, brokenClient) => {
assert.equal(client, brokenClient)
client.once('error', assert.calls((err) => {
client.on('error', (err) => {})
}))

client.query('SELECT 1', assert.calls((err) => {
if (helper.args.native) {
assert.ok(err)
assert.equal(err.message, 'terminating connection due to administrator command')
} else {
assert.equal(err.message, 'Connection terminated unexpectedly')
}

done()
done(err)
pool.end()
cb()
}))
Expand All @@ -86,13 +83,16 @@ suite.test('connection-level errors cause future queries to fail', (cb) => {
}
}))

pool.once('error', assert.calls((err, brokenClient) => {
assert.equal(client, brokenClient)

client.once('error', assert.calls((err) => {
client.on('error', (err) => {})
client.query('SELECT 1', assert.calls((err) => {
assert.equal(err.message, 'Client has encountered a connection error and is not queryable')
if (helper.args.native) {
assert.equal(err.message, 'terminating connection due to administrator command')
} else {
assert.equal(err.message, 'Client has encountered a connection error and is not queryable')
}

done()
done(err)
pool.end()
cb()
}))
Expand Down
14 changes: 8 additions & 6 deletions test/integration/gh-issues/130-tests.js
Expand Up @@ -5,19 +5,21 @@ var exec = require('child_process').exec
helper.pg.defaults.poolIdleTimeout = 1000

const pool = new helper.pg.Pool()
pool.connect(function (err, client) {
pool.connect(function (err, client, done) {
assert.ifError(err)
client.once('error', function (err) {
client.on('error', (err) => {})
done(err)
})
client.query('SELECT pg_backend_pid()', function (err, result) {
assert.ifError(err)
var pid = result.rows[0].pg_backend_pid
var psql = 'psql'
if (helper.args.host) psql = psql + ' -h ' + helper.args.host
if (helper.args.port) psql = psql + ' -p ' + helper.args.port
if (helper.args.user) psql = psql + ' -U ' + helper.args.user
exec(psql + ' -c "select pg_terminate_backend(' + pid + ')" template1', assert.calls(function (error, stdout, stderr) {
assert.isNull(error)
assert.ifError(error)
}))
})
})

pool.on('error', function (err, client) {
// swallow errors
})

0 comments on commit caa6517

Please sign in to comment.