Call callback when end called on unconnected client

Closes #2108
This commit is contained in:
Brian M. Carlson 2020-02-20 17:37:26 -06:00
parent 29877530c6
commit 7c7a3b884d
2 changed files with 24 additions and 2 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.pending && !this.connection.stream.connecting) {
if (cb) {
cb()
} else {
return 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
@ -554,10 +563,10 @@ Client.prototype.end = function (cb) {
}
if (cb) {
this.connection.once('end', cb)
this.connection.stream.once('close', cb)
} else {
return new this._Promise((resolve) => {
this.connection.once('end', resolve)
this.connection.stream.once('close', resolve)
})
}
}

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()
})