Skip to content

Fix disconnection tests for pg-pool 2.0.7 #1946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/connection.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
})