Skip to content

Commit ef7437f

Browse files
committed
fix: concurrency limit is not set correctly
1 parent 465ca2c commit ef7437f

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/ProcessingPriorityQueue.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export default class ProcessingPriorityQueue {
174174
this.process();
175175
}
176176

177-
if (this.currentConcurrencyCount <= this.concurrencyLimit && this.priorityQueue.size > 0) {
177+
if (this.currentConcurrencyCount < this.concurrencyLimit && this.priorityQueue.size > 0) {
178178
this.currentConcurrencyCount++;
179179

180180
const {value: sqItem, valid} = this.poll();

tests/unit/PTask.test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -685,4 +685,38 @@ describe("PriorityTask", () => {
685685
});
686686
expect(PTask.getAllPTasks("krombopulos").length).toBe(1);
687687
});
688+
689+
it("should not schedule more than concurrencyLimit items", (done) => {
690+
const CONCURRENCY_LIMIT = 2;
691+
PTask.setConcurrencyLimit(CONCURRENCY_LIMIT);
692+
693+
const delayedOnRun = async (a: number) => {
694+
await new Promise((resolve) => setTimeout(resolve, 100));
695+
return a;
696+
};
697+
698+
// Prepare tasks
699+
const ptasks = Array.from({ length: CONCURRENCY_LIMIT + 1 }).map((_, i) => {
700+
return new PTask<number, number>({
701+
priority: i,
702+
args: i,
703+
onRun: delayedOnRun,
704+
});
705+
});
706+
707+
// run all tasks
708+
const promises = ptasks.map((ptask) => ptask.run());
709+
710+
setTimeout(() => {
711+
const runningTasks = PTask.getAllPTasks().filter((ptask) => ptask.status === "running");
712+
const pendingTasks = PTask.getAllPTasks().filter((ptask) => ptask.status === "pending");
713+
714+
expect(runningTasks.length).toBe(CONCURRENCY_LIMIT);
715+
expect(pendingTasks.length).toBe(1);
716+
717+
Promise.all(promises).then(() => {
718+
done()
719+
});
720+
}, 10);
721+
})
688722
});

0 commit comments

Comments
 (0)