From 1c8b6b93cfa108a0ad0e0940f1bb26ecd101087b Mon Sep 17 00:00:00 2001 From: Brian C Date: Tue, 25 Feb 2020 08:48:58 -0600 Subject: [PATCH] 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> --- packages/pg/lib/client.js | 9 +++++++++ .../pg/test/integration/gh-issues/2108-tests.js | 13 +++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 packages/pg/test/integration/gh-issues/2108-tests.js diff --git a/packages/pg/lib/client.js b/packages/pg/lib/client.js index 05efbdc5..cdae3b7c 100644 --- a/packages/pg/lib/client.js +++ b/packages/pg/lib/client.js @@ -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 diff --git a/packages/pg/test/integration/gh-issues/2108-tests.js b/packages/pg/test/integration/gh-issues/2108-tests.js new file mode 100644 index 00000000..9832dae3 --- /dev/null +++ b/packages/pg/test/integration/gh-issues/2108-tests.js @@ -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() +})