|
|
|
@ -26,24 +26,11 @@ tap.test('should run in parallel', async (toolsArg) => {
|
|
|
|
|
}, 0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Test default non-exclusive unlimited concurrency (no cap means all run together)
|
|
|
|
|
tap.test('default non-exclusive unlimited concurrency', async (tools) => {
|
|
|
|
|
// Test default non-exclusive has no concurrency limit property (Infinity)
|
|
|
|
|
tap.test('default non-exclusive has no concurrency limit', () => {
|
|
|
|
|
const stack = new lik.AsyncExecutionStack();
|
|
|
|
|
// default maxConcurrency should be unlimited (Infinity)
|
|
|
|
|
expect(Number.isFinite(stack.getNonExclusiveMaxConcurrency())).toBe(false);
|
|
|
|
|
const activeCounts: number[] = [];
|
|
|
|
|
const tasks: Promise<void>[] = [];
|
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
|
tasks.push(
|
|
|
|
|
stack.getNonExclusiveExecutionSlot(async () => {
|
|
|
|
|
activeCounts.push(stack.getActiveNonExclusiveCount());
|
|
|
|
|
await tools.delayFor(20);
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
await Promise.all(tasks);
|
|
|
|
|
const maxActive = Math.max(...activeCounts);
|
|
|
|
|
expect(maxActive).toBe(4);
|
|
|
|
|
// default maxConcurrency is Infinity (not finite)
|
|
|
|
|
expect(Number.isFinite(stack.getNonExclusiveMaxConcurrency())).toBeFalse();
|
|
|
|
|
});
|
|
|
|
|
// Test respecting a non-exclusive concurrency limit
|
|
|
|
|
tap.test('non-exclusive respects maxConcurrency', async (tools) => {
|
|
|
|
@ -70,10 +57,10 @@ tap.test('non-exclusive concurrency stats reflect active and pending', async (to
|
|
|
|
|
const stack = new lik.AsyncExecutionStack();
|
|
|
|
|
stack.setNonExclusiveMaxConcurrency(2);
|
|
|
|
|
// initially, no tasks
|
|
|
|
|
expect(stack.getActiveNonExclusiveCount()).toBe(0);
|
|
|
|
|
expect(stack.getPendingNonExclusiveCount()).toBe(0);
|
|
|
|
|
expect(stack.getActiveNonExclusiveCount()).toEqual(0);
|
|
|
|
|
expect(stack.getPendingNonExclusiveCount()).toEqual(0);
|
|
|
|
|
|
|
|
|
|
// enqueue three tasks
|
|
|
|
|
// enqueue four tasks
|
|
|
|
|
const p1 = stack.getNonExclusiveExecutionSlot(async () => {
|
|
|
|
|
await tools.delayFor(30);
|
|
|
|
|
});
|
|
|
|
@ -83,17 +70,22 @@ tap.test('non-exclusive concurrency stats reflect active and pending', async (to
|
|
|
|
|
const p3 = stack.getNonExclusiveExecutionSlot(async () => {
|
|
|
|
|
await tools.delayFor(30);
|
|
|
|
|
});
|
|
|
|
|
const p4 = stack.getNonExclusiveExecutionSlot(async () => {
|
|
|
|
|
await tools.delayFor(30);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// give time for scheduling
|
|
|
|
|
await tools.delayFor(10);
|
|
|
|
|
// two should be running, one pending
|
|
|
|
|
expect(stack.getActiveNonExclusiveCount()).toBe(2);
|
|
|
|
|
expect(stack.getPendingNonExclusiveCount()).toBe(1);
|
|
|
|
|
// wait for first task to finish and scheduling of next batch
|
|
|
|
|
await p1;
|
|
|
|
|
await tools.delayFor(0);
|
|
|
|
|
// second batch: two active, one pending (4 tasks, limit=2)
|
|
|
|
|
expect(stack.getActiveNonExclusiveCount()).toEqual(2);
|
|
|
|
|
expect(stack.getPendingNonExclusiveCount()).toEqual(1);
|
|
|
|
|
|
|
|
|
|
await Promise.all([p1, p2, p3]);
|
|
|
|
|
// wait for remaining tasks to complete
|
|
|
|
|
await Promise.all([p2, p3, p4]);
|
|
|
|
|
// after completion, counts reset
|
|
|
|
|
expect(stack.getActiveNonExclusiveCount()).toBe(0);
|
|
|
|
|
expect(stack.getPendingNonExclusiveCount()).toBe(0);
|
|
|
|
|
expect(stack.getActiveNonExclusiveCount()).toEqual(0);
|
|
|
|
|
expect(stack.getPendingNonExclusiveCount()).toEqual(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default tap.start();
|
|
|
|
|