From bb530af4cd6eb644be69335bacfc8168c77b3d40 Mon Sep 17 00:00:00 2001 From: Julian Gautier Date: Tue, 2 Jun 2020 16:02:47 -0700 Subject: [PATCH 1/2] check pending validations before checking on an interval to avoid a 100ms delay --- src/Pool.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Pool.ts b/src/Pool.ts index 5fbdd85..4fe05c5 100644 --- a/src/Pool.ts +++ b/src/Pool.ts @@ -282,6 +282,10 @@ export class Pool { .then(() => { // poll every 100ms and wait that all validations are ready return new Promise((resolve, reject) => { + if (this.numPendingValidations() === 0) { + resolve(); + return; + } const interval = setInterval(() => { if (this.numPendingValidations() === 0) { clearInterval(interval); From 6b7601ca432f2da6be6b2e9e00db3e8052ab3d1c Mon Sep 17 00:00:00 2001 From: Julian Gautier Date: Thu, 4 Jun 2020 09:05:54 -0700 Subject: [PATCH 2/2] update destroy to wait for promises, add regression test --- src/Pool.ts | 16 +++------------- tests.js | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/Pool.ts b/src/Pool.ts index 4fe05c5..f397abf 100644 --- a/src/Pool.ts +++ b/src/Pool.ts @@ -280,19 +280,9 @@ export class Pool { return reflect( Promise.all(this.pendingCreates.map(create => reflect(create.promise))) .then(() => { - // poll every 100ms and wait that all validations are ready - return new Promise((resolve, reject) => { - if (this.numPendingValidations() === 0) { - resolve(); - return; - } - const interval = setInterval(() => { - if (this.numPendingValidations() === 0) { - clearInterval(interval); - resolve(); - } - }, 100); - }); + return Promise.all( + this.pendingValidations.map(validation => reflect(validation.promise)) + ); }) .then(() => { // Wait for all the used resources to be freed. diff --git a/tests.js b/tests.js index b5fc30a..00c3d24 100644 --- a/tests.js +++ b/tests.js @@ -1421,6 +1421,31 @@ describe('Tarn', () => { expect(resources[3].destroyed).to.be.ok(); }); }); + it('should destroy immediately if there are no async validations', done => { + pool = new Pool({ + create: () => { + return Promise.resolve({}); + }, + destroy(res) { + return Promise.resolve({}); + }, + validate(res) { + return true; + }, + reapIntervalMillis: 10, + idleTimeoutMillis: 1, + min: 0, + max: 10 + }); + let destroyed = false; + setImmediate(() => { + expect(destroyed).to.equal(true); + done(); + }); + pool.destroy().then(() => { + destroyed = true; + }); + }); }); describe('acquireTimeout', () => {