Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for pending validations instead of checking on interval #53

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions src/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,19 +280,9 @@ export class Pool<T> {
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.
Expand Down
25 changes: 25 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down