diff --git a/packages/pg-pool/index.js b/packages/pg-pool/index.js index 94004e031..c4e9b36aa 100644 --- a/packages/pg-pool/index.js +++ b/packages/pg-pool/index.js @@ -156,7 +156,7 @@ class Pool extends EventEmitter { throw new Error('unexpected condition') } - _remove(client) { + _remove(client, callback) { const removed = removeWhere(this._idle, (item) => item.client === client) if (removed !== undefined) { @@ -164,8 +164,14 @@ class Pool extends EventEmitter { } this._clients = this._clients.filter((c) => c !== client) - client.end() - this.emit('remove', client) + const context = this + client.end(() => { + context.emit('remove', client) + + if (typeof callback === 'function') { + callback() + } + }) } connect(cb) { @@ -342,18 +348,15 @@ class Pool extends EventEmitter { if (client._poolUseCount >= this.options.maxUses) { this.log('remove expended client') } - this._remove(client) - this._pulseQueue() - return + + return this._remove(client, this._pulseQueue.bind(this)) } const isExpired = this._expired.has(client) if (isExpired) { this.log('remove expired client') this._expired.delete(client) - this._remove(client) - this._pulseQueue() - return + return this._remove(client, this._pulseQueue.bind(this)) } // idle timeout @@ -361,7 +364,7 @@ class Pool extends EventEmitter { if (this.options.idleTimeoutMillis) { tid = setTimeout(() => { this.log('remove idle client') - this._remove(client) + this._remove(client, this._pulseQueue.bind(this)) }, this.options.idleTimeoutMillis) if (this.options.allowExitOnIdle) { diff --git a/packages/pg-pool/test/ending.js b/packages/pg-pool/test/ending.js index e1839b46c..d3aa81f05 100644 --- a/packages/pg-pool/test/ending.js +++ b/packages/pg-pool/test/ending.js @@ -37,4 +37,14 @@ describe('pool ending', () => { expect(res.rows[0].name).to.equal('brianc') }) ) + + it('pool.end() - finish pending queries', async () => { + const pool = new Pool({ max: 20 }) + let completed = 0 + for (let x = 1; x <= 20; x++) { + pool.query('SELECT $1::text as name', ['brianc']).then(() => completed++) + } + await pool.end() + expect(completed).to.equal(20) + }) }) diff --git a/packages/pg-pool/test/idle-timeout.js b/packages/pg-pool/test/idle-timeout.js index 0bb097565..588cf9bd4 100644 --- a/packages/pg-pool/test/idle-timeout.js +++ b/packages/pg-pool/test/idle-timeout.js @@ -25,6 +25,7 @@ describe('idle timeout', () => { it( 'times out and removes clients when others are also removed', co.wrap(function* () { + let currentClient = 1 const pool = new Pool({ idleTimeoutMillis: 10 }) const clientA = yield pool.connect() const clientB = yield pool.connect() @@ -35,7 +36,12 @@ describe('idle timeout', () => { pool.on('remove', () => { expect(pool.idleCount).to.equal(0) expect(pool.totalCount).to.equal(0) - resolve() + + if (currentClient >= 2) { + resolve() + } else { + currentClient++ + } }) }) @@ -44,7 +50,7 @@ describe('idle timeout', () => { try { yield Promise.race([removal, timeout]) } finally { - pool.end() + yield pool.end() } }) )