From d3d4b67d8301265da89328a0a05ab68438add3ed Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 14 Apr 2023 18:40:57 +0200 Subject: [PATCH] test: enhance tasks queueing tests MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- tests/pools/cluster/fixed.test.js | 14 ++++++++------ tests/pools/thread/fixed.test.js | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index ddcca105..c28b8021 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -90,9 +90,11 @@ describe('Fixed cluster pool test suite', () => { it('Verify that tasks queuing is working', async () => { const maxMultiplier = 10 + const promises = new Set() for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) { - queuePool.execute() + promises.add(queuePool.execute()) } + expect(promises.size).toBe(numberOfWorkers * maxMultiplier) for (const workerNode of queuePool.workerNodes) { expect(workerNode.tasksUsage.running).toBeLessThanOrEqual( queuePool.opts.tasksQueueOptions.concurrency @@ -100,17 +102,17 @@ describe('Fixed cluster pool test suite', () => { expect(workerNode.tasksUsage.run).toBe(0) expect(workerNode.tasksQueue.length).toBeGreaterThan(0) } - // FIXME: wait for ongoing tasks to be executed - const promises = [] - for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) { - promises.push(queuePool.execute()) - } + expect(queuePool.numberOfRunningTasks).toBe(numberOfWorkers) + expect(queuePool.numberOfQueuedTasks).toBe( + numberOfWorkers * maxMultiplier - numberOfWorkers + ) await Promise.all(promises) for (const workerNode of queuePool.workerNodes) { expect(workerNode.tasksUsage.running).toBe(0) expect(workerNode.tasksUsage.run).toBeGreaterThan(0) expect(workerNode.tasksQueue.length).toBe(0) } + promises.clear() }) it('Verify that is possible to have a worker that return undefined', async () => { diff --git a/tests/pools/thread/fixed.test.js b/tests/pools/thread/fixed.test.js index 7b4b9bf5..75c4a935 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -90,9 +90,11 @@ describe('Fixed thread pool test suite', () => { it('Verify that tasks queuing is working', async () => { const maxMultiplier = 10 + const promises = new Set() for (let i = 0; i < numberOfThreads * maxMultiplier; i++) { - queuePool.execute() + promises.add(queuePool.execute()) } + expect(promises.size).toBe(numberOfThreads * maxMultiplier) for (const workerNode of queuePool.workerNodes) { expect(workerNode.tasksUsage.running).toBeLessThanOrEqual( queuePool.opts.tasksQueueOptions.concurrency @@ -100,17 +102,17 @@ describe('Fixed thread pool test suite', () => { expect(workerNode.tasksUsage.run).toBe(0) expect(workerNode.tasksQueue.length).toBeGreaterThan(0) } - // FIXME: wait for ongoing tasks to be executed - const promises = [] - for (let i = 0; i < numberOfThreads * maxMultiplier; i++) { - promises.push(queuePool.execute()) - } + expect(queuePool.numberOfRunningTasks).toBe(numberOfThreads) + expect(queuePool.numberOfQueuedTasks).toBe( + numberOfThreads * maxMultiplier - numberOfThreads + ) await Promise.all(promises) for (const workerNode of queuePool.workerNodes) { expect(workerNode.tasksUsage.running).toBe(0) expect(workerNode.tasksUsage.run).toBeGreaterThan(0) expect(workerNode.tasksQueue.length).toBe(0) } + promises.clear() }) it('Verify that is possible to have a worker that return undefined', async () => { -- 2.34.1