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' } 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 => {