From fc027381650624a242a2e7b139976d52c3aa304c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 30 May 2023 20:54:13 +0200 Subject: [PATCH] test: refine worker choice strategies expectation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- tests/pools/abstract/abstract-pool.test.js | 23 ++++++++------- tests/pools/cluster/fixed.test.js | 4 +-- .../selection-strategies.test.js | 28 +++++-------------- tests/pools/thread/fixed.test.js | 4 +-- 4 files changed, 22 insertions(+), 37 deletions(-) diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 19cb3eb8..caf7c8f0 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -11,7 +11,7 @@ const { CircularArray } = require('../../../lib/circular-array') const { Queue } = require('../../../lib/queue') describe('Abstract pool test suite', () => { - const numberOfWorkers = 1 + const numberOfWorkers = 2 class StubPoolWithRemoveAllWorker extends FixedThreadPool { removeAllWorker () { this.workerNodes = [] @@ -106,7 +106,7 @@ describe('Abstract pool test suite', () => { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED, workerChoiceStrategyOptions: { medRunTime: true, - weights: { 0: 300 } + weights: { 0: 300, 1: 200 } }, enableEvents: false, enableTasksQueue: true, @@ -126,7 +126,7 @@ describe('Abstract pool test suite', () => { ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ medRunTime: true, - weights: { 0: 300 } + weights: { 0: 300, 1: 200 } }) expect(pool.opts.messageHandler).toStrictEqual(testHandler) expect(pool.opts.errorHandler).toStrictEqual(testHandler) @@ -328,13 +328,14 @@ describe('Abstract pool test suite', () => { './tests/worker-files/cluster/testWorker.js' ) const promises = [] - for (let i = 0; i < numberOfWorkers * 2; i++) { + const maxMultiplier = 2 + for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) { promises.push(pool.execute()) } for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ run: 0, - running: numberOfWorkers * 2, + running: maxMultiplier, runTime: 0, runTimeHistory: expect.any(CircularArray), avgRunTime: 0, @@ -349,7 +350,7 @@ describe('Abstract pool test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: numberOfWorkers * 2, + run: maxMultiplier, running: 0, runTime: 0, runTimeHistory: expect.any(CircularArray), @@ -368,7 +369,7 @@ describe('Abstract pool test suite', () => { it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => { const pool = new DynamicThreadPool( numberOfWorkers, - numberOfWorkers, + numberOfWorkers * 2, './tests/worker-files/thread/testWorker.js' ) const promises = [] @@ -379,7 +380,7 @@ describe('Abstract pool test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: numberOfWorkers * maxMultiplier, + run: expect.any(Number), running: 0, runTime: 0, runTimeHistory: expect.any(CircularArray), @@ -391,6 +392,8 @@ describe('Abstract pool test suite', () => { medWaitTime: 0, error: 0 }) + expect(workerNode.tasksUsage.run).toBeGreaterThan(0) + expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(maxMultiplier) } pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) for (const workerNode of pool.workerNodes) { @@ -427,8 +430,8 @@ describe('Abstract pool test suite', () => { } await Promise.all(promises) // The `full` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool. - // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool. - expect(poolFull).toBe(numberOfWorkers + 1) + // So in total numberOfWorkers * 2 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool with min = max = numberOfWorkers. + expect(poolFull).toBe(numberOfWorkers * 2) await pool.destroy() }) diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index 4e5390d1..dfd76557 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -110,9 +110,7 @@ describe('Fixed cluster pool test suite', () => { for (const workerNode of queuePool.workerNodes) { expect(workerNode.tasksUsage.running).toBe(0) expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual( - numberOfWorkers * maxMultiplier - ) + expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(maxMultiplier) expect(workerNode.tasksQueue.size).toBe(0) } promises.clear() diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index 19ac5ea3..b10a7b56 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -164,7 +164,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: expect.any(Number), + run: maxMultiplier, running: 0, runTime: 0, runTimeHistory: expect.any(CircularArray), @@ -176,8 +176,6 @@ describe('Selection strategies test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) } // We need to clean up the resources after our test await pool.destroy() @@ -199,7 +197,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: expect.any(Number), + run: maxMultiplier, running: 0, runTime: 0, runTimeHistory: expect.any(CircularArray), @@ -211,8 +209,6 @@ describe('Selection strategies test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) } // We need to clean up the resources after our test await pool.destroy() @@ -337,7 +333,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: expect.any(Number), + run: maxMultiplier, running: 0, runTime: 0, runTimeHistory: expect.any(CircularArray), @@ -349,8 +345,6 @@ describe('Selection strategies test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) } // We need to clean up the resources after our test await pool.destroy() @@ -372,7 +366,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: expect.any(Number), + run: maxMultiplier, running: 0, runTime: 0, runTimeHistory: expect.any(CircularArray), @@ -384,8 +378,6 @@ describe('Selection strategies test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) } // We need to clean up the resources after our test await pool.destroy() @@ -553,7 +545,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: expect.any(Number), + run: maxMultiplier, running: 0, runTime: expect.any(Number), runTimeHistory: expect.any(CircularArray), @@ -565,8 +557,6 @@ describe('Selection strategies test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) expect(workerNode.tasksUsage.runTime).toBeGreaterThan(0) expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThan(0) } @@ -595,7 +585,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: expect.any(Number), + run: maxMultiplier, running: 0, runTime: expect.any(Number), runTimeHistory: expect.any(CircularArray), @@ -607,8 +597,6 @@ describe('Selection strategies test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) expect(workerNode.tasksUsage.runTime).toBeGreaterThan(0) expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThan(0) } @@ -642,7 +630,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: expect.any(Number), + run: maxMultiplier, running: 0, runTime: expect.any(Number), runTimeHistory: expect.any(CircularArray), @@ -654,8 +642,6 @@ describe('Selection strategies test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) expect(workerNode.tasksUsage.runTime).toBeGreaterThan(0) expect(workerNode.tasksUsage.medRunTime).toBeGreaterThan(0) } diff --git a/tests/pools/thread/fixed.test.js b/tests/pools/thread/fixed.test.js index 8a9922b0..0c3ba276 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -110,9 +110,7 @@ describe('Fixed thread pool test suite', () => { for (const workerNode of queuePool.workerNodes) { expect(workerNode.tasksUsage.running).toBe(0) expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual( - numberOfThreads * maxMultiplier - ) + expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(maxMultiplier) expect(workerNode.tasksQueue.size).toBe(0) } promises.clear() -- 2.34.1