Skip to content

Commit

Permalink
Make tests less flakey
Browse files Browse the repository at this point in the history
  • Loading branch information
cressie176 committed Aug 12, 2023
1 parent 4a926c9 commit 680b738
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions test/Pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ describe('Pool', () => {
setTimeout(() => {
ok(factory.wasDestroyed('R1'), 'Resource was not destroyed');
done();
}, 100).unref();
}, 100);

await pool.acquire();
});
Expand All @@ -452,7 +452,7 @@ describe('Pool', () => {
const pool = createPool({ factory, acquireTimeout: 200, maxSize: 1 });

const resource1 = await pool.acquire();
setTimeout(() => pool.release(resource1), 100).unref();
setTimeout(() => pool.release(resource1), 100);

const before = Date.now();
const resource2 = await pool.acquire();
Expand All @@ -468,7 +468,7 @@ describe('Pool', () => {
const pool = createPool({ factory, acquireTimeout: 200, maxSize: 1 });

const resource1 = await pool.acquire();
setTimeout(() => pool.destroy(resource1), 100).unref();
setTimeout(() => pool.destroy(resource1), 100);

const before = Date.now();
const resource2 = await pool.acquire();
Expand Down Expand Up @@ -522,7 +522,7 @@ describe('Pool', () => {
eq(size, 0);
eq(acquired, 0);
eq(idle, 0);
}, 100).unref();
}, 100);
});

it('should destroy the supplied resource eventually', async (t, done) => {
Expand All @@ -537,7 +537,7 @@ describe('Pool', () => {
if (!factory.wasDestroyed(resource)) return;
clearInterval(timerId);
done();
}).unref();
});
});

it('should report resource destruction errors via a specific event', async (t, done) => {
Expand Down Expand Up @@ -648,7 +648,7 @@ describe('Pool', () => {
eq(acquired, 0);
eq(bad, 0);
done();
}, 300).unref();
}, 300);
});
});

Expand Down Expand Up @@ -910,18 +910,27 @@ describe('Pool', () => {
const factory = new TestFactory(resources);
const pool = createPool({ factory, maxSize: 1 });

const resource1 = await pool.acquire(); // acquire the only resource
// Acquire the only resource
const resource1 = await pool.acquire();

// Release the resource 200ms after it was acquired
setTimeout(() => pool.release(resource1), 400);

// Call shutdown while the resource is still on loan,
// but after the second acquire
setTimeout(async () => {
const before = Date.now();
await pool.shutdown();
const after = Date.now();
ok(after - before >= 200 + 200 - 100, 'Shutdown did not wait for pending acquitions');
ok(after - before >= 400 - 200 + 200, 'Shutdown did not wait for pending acquitions');
done();
}, 100);
}, 200);

// Create a pending acquisition
const resource2 = await pool.acquire();

setTimeout(() => pool.release(resource1), 200);
await pool.acquire(); // create a pending acquisition
setTimeout(() => pool.release(resource1), 200);
// Release the resource in 200ms after it was acquired
setTimeout(() => pool.release(resource2), 200);
});

it('should reject when the shutdownTimeout is exceeded', async () => {
Expand Down

0 comments on commit 680b738

Please sign in to comment.