feat: add reusable jobqueue library
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
26
tests/WorkerPool.test.ts
Normal file
26
tests/WorkerPool.test.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { WorkerPool } from '../src/index.js';
|
||||
import { waitFor } from './helpers.js';
|
||||
|
||||
describe('WorkerPool', () => {
|
||||
it('respects concurrency limits', async () => {
|
||||
const pool = new WorkerPool(2);
|
||||
let active = 0;
|
||||
let maxActive = 0;
|
||||
|
||||
const tasks = Array.from({ length: 5 }, (_, index) =>
|
||||
pool.run(async () => {
|
||||
active += 1;
|
||||
maxActive = Math.max(maxActive, active);
|
||||
await new Promise((resolve) => {
|
||||
setTimeout(resolve, 20 + index);
|
||||
});
|
||||
active -= 1;
|
||||
}),
|
||||
);
|
||||
|
||||
await Promise.all(tasks);
|
||||
await waitFor(() => pool.activeCount === 0 && pool.pendingCount === 0);
|
||||
|
||||
expect(maxActive).toBe(2);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user