Call callback when end called on unconnected client (#2109)

* Call callback when end called on unconnected client

Closes #2108

* Revert a bit of the change

* Use readyState because pending doesn't exist in node 8.x

* Update packages/pg/lib/client.js

use bring your own promise

Co-Authored-By: Charmander <~@charmander.me>

Co-authored-by: Charmander <~@charmander.me>
This commit is contained in:
Brian C 2020-02-25 08:48:58 -06:00 committed by GitHub
parent 29877530c6
commit 1c8b6b93cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -545,6 +545,15 @@ Client.prototype.query = function (config, values, callback) {
Client.prototype.end = function (cb) {
this._ending = true
// if we have never connected, then end is a noop, callback immediately
if (this.connection.stream.readyState === 'closed') {
if (cb) {
cb()
} else {
return this._Promise.resolve()
}
}
if (this.activeQuery || !this._queryable) {
// if we have an active query we need to force a disconnect
// on the socket - otherwise a hung query could block end forever

View File

@ -0,0 +1,13 @@
"use strict"
var helper = require('./../test-helper')
const suite = new helper.Suite()
suite.test('Closing an unconnected client calls callback', (done) => {
const client = new helper.pg.Client()
client.end(done)
})
suite.testAsync('Closing an unconnected client resolves promise', () => {
const client = new helper.pg.Client()
return client.end()
})