From e172c1fa6eac326604a6107328e08c4f0f20332f Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Mon, 1 Oct 2018 17:13:15 +0300 Subject: [PATCH 1/2] Fix _endCallback --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index ab74209..8e948e8 100644 --- a/index.js +++ b/index.js @@ -223,6 +223,7 @@ class Pool extends EventEmitter { this.log('client failed to connect', err) // remove the dead client from our list of clients this._clients = this._clients.filter(c => c !== client) + if (!this._clients.length && this._endCallback) this._endCallback() if (timeoutHit) { err.message = 'Connection terminated due to connection timeout' } From cf62702580b475be73405074c7773e1adb5f4803 Mon Sep 17 00:00:00 2001 From: Taras Parkhomenko Date: Mon, 3 Jun 2019 12:10:53 +0700 Subject: [PATCH 2/2] Add test for _endCallback fix. --- test/connection-timeout.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/connection-timeout.js b/test/connection-timeout.js index f7a2fd8..c40d1e2 100644 --- a/test/connection-timeout.js +++ b/test/connection-timeout.js @@ -36,6 +36,23 @@ describe('connection timeout', () => { }) }) + it('should call _endCallback if Pool.end was called prior to receiving callback with error', (done) => { + const pool = new Pool({ connectionTimeoutMillis: 10, port: this.port }) + let endCalled = false + pool.connect((err, client, release) => { + expect(err).to.be.an(Error) + expect(err.message).to.contain('timeout') + expect(client).to.equal(undefined) + expect(pool.idleCount).to.equal(0) + expect(endCalled).to.be(true) + done() + }) + pool.end((err) => { + expect(err).to.be(undefined) + endCalled = true + }) + }) + it('should reject promise with an error if timeout is passed', (done) => { const pool = new Pool({ connectionTimeoutMillis: 10, port: this.port }) pool.connect().catch(err => {