From: Jérôme Benoit Date: Tue, 30 May 2023 16:34:52 +0000 (+0200) Subject: test: improve worker choice strategies testing X-Git-Tag: v2.5.0~9^2^2~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ee9f52959d4996f56712791e73994dfbeb0370f0;p=poolifier.git test: improve worker choice strategies testing Signed-off-by: Jérôme Benoit --- diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index db45fa07..19cb3eb8 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -372,15 +372,16 @@ describe('Abstract pool test suite', () => { './tests/worker-files/thread/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()) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: numberOfWorkers * 2, + run: numberOfWorkers * maxMultiplier, running: 0, - runTime: expect.any(Number), + runTime: 0, runTimeHistory: expect.any(CircularArray), avgRunTime: 0, medRunTime: 0, @@ -390,12 +391,9 @@ describe('Abstract pool test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.runTime).toBeGreaterThanOrEqual(0) - expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) } pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) for (const workerNode of pool.workerNodes) { - expect(workerNode.tasksUsage).toBeDefined() expect(workerNode.tasksUsage).toStrictEqual({ run: 0, running: 0, @@ -410,6 +408,7 @@ describe('Abstract pool test suite', () => { error: 0 }) expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) + expect(workerNode.tasksUsage.waitTimeHistory.length).toBe(0) } await pool.destroy() }) diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index 42f03b89..4e5390d1 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -89,8 +89,8 @@ describe('Fixed cluster pool test suite', () => { }) it('Verify that tasks queuing is working', async () => { - const maxMultiplier = 2 const promises = new Set() + const maxMultiplier = 2 for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) { promises.add(queuePool.execute()) } @@ -110,6 +110,9 @@ 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.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 977706cc..19ac5ea3 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -156,10 +156,10 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN } ) // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose` - const promises = [] + const promises = new Set() const maxMultiplier = 2 for (let i = 0; i < max * maxMultiplier; i++) { - promises.push(pool.execute()) + promises.add(pool.execute()) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { @@ -177,6 +177,7 @@ describe('Selection strategies test suite', () => { 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() @@ -190,10 +191,10 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN } ) // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose` - const promises = [] + const promises = new Set() const maxMultiplier = 2 for (let i = 0; i < max * maxMultiplier; i++) { - promises.push(pool.execute()) + promises.add(pool.execute()) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { @@ -211,6 +212,7 @@ describe('Selection strategies test suite', () => { 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() @@ -327,10 +329,10 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED } ) // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose` - const promises = [] + const promises = new Set() const maxMultiplier = 2 for (let i = 0; i < max * maxMultiplier; i++) { - promises.push(pool.execute()) + promises.add(pool.execute()) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { @@ -348,6 +350,7 @@ describe('Selection strategies test suite', () => { 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() @@ -361,10 +364,10 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED } ) // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose` - const promises = [] + const promises = new Set() const maxMultiplier = 2 for (let i = 0; i < max * maxMultiplier; i++) { - promises.push(pool.execute()) + promises.add(pool.execute()) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { @@ -382,6 +385,7 @@ describe('Selection strategies test suite', () => { 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() @@ -432,10 +436,10 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY } ) // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose` - const promises = [] + const promises = new Set() const maxMultiplier = 2 for (let i = 0; i < max * maxMultiplier; i++) { - promises.push(pool.execute()) + promises.add(pool.execute()) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { @@ -453,6 +457,7 @@ describe('Selection strategies test suite', () => { error: 0 }) expect(workerNode.tasksUsage.run).toBeGreaterThan(0) + expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) expect(workerNode.tasksUsage.runTime).toBeGreaterThan(0) } // We need to clean up the resources after our test @@ -467,10 +472,10 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY } ) // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose` - const promises = [] + const promises = new Set() const maxMultiplier = 2 for (let i = 0; i < max * maxMultiplier; i++) { - promises.push(pool.execute()) + promises.add(pool.execute()) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { @@ -488,6 +493,7 @@ describe('Selection strategies test suite', () => { error: 0 }) expect(workerNode.tasksUsage.run).toBeGreaterThan(0) + expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) expect(workerNode.tasksUsage.runTime).toBeGreaterThan(0) } // We need to clean up the resources after our test @@ -539,10 +545,10 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } ) // TODO: Create a better test to cover `FairShareChoiceStrategy#choose` - const promises = [] + const promises = new Set() const maxMultiplier = 2 for (let i = 0; i < max * maxMultiplier; i++) { - promises.push(pool.execute()) + promises.add(pool.execute()) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { @@ -560,6 +566,7 @@ describe('Selection strategies test suite', () => { 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) } @@ -580,10 +587,10 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } ) // TODO: Create a better test to cover `FairShareChoiceStrategy#choose` - const promises = [] + const promises = new Set() const maxMultiplier = 2 for (let i = 0; i < max * maxMultiplier; i++) { - promises.push(pool.execute()) + promises.add(pool.execute()) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { @@ -601,6 +608,7 @@ describe('Selection strategies test suite', () => { 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) } @@ -626,10 +634,10 @@ describe('Selection strategies test suite', () => { } ) // TODO: Create a better test to cover `FairShareChoiceStrategy#choose` - const promises = [] + const promises = new Set() const maxMultiplier = 2 for (let i = 0; i < max * maxMultiplier; i++) { - promises.push(pool.execute()) + promises.add(pool.execute()) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { @@ -647,6 +655,7 @@ describe('Selection strategies test suite', () => { 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) } @@ -778,10 +787,10 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } ) // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` - const promises = [] + const promises = new Set() const maxMultiplier = 2 for (let i = 0; i < max * maxMultiplier; i++) { - promises.push(pool.execute()) + promises.add(pool.execute()) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { @@ -798,9 +807,10 @@ describe('Selection strategies test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.runTime).toBeGreaterThan(0) - expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThan(0) + expect(workerNode.tasksUsage.run).toBeGreaterThanOrEqual(0) + expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) + expect(workerNode.tasksUsage.runTime).toBeGreaterThanOrEqual(0) + expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) } expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( @@ -824,10 +834,10 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } ) // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` - const promises = [] + const promises = new Set() const maxMultiplier = 2 for (let i = 0; i < max * maxMultiplier; i++) { - promises.push(pool.execute()) + promises.add(pool.execute()) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { @@ -845,6 +855,7 @@ describe('Selection strategies test suite', () => { 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) } @@ -875,10 +886,10 @@ describe('Selection strategies test suite', () => { } ) // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` - const promises = [] + const promises = new Set() const maxMultiplier = 2 for (let i = 0; i < max * maxMultiplier; i++) { - promises.push(pool.execute()) + promises.add(pool.execute()) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { @@ -896,6 +907,7 @@ describe('Selection strategies test suite', () => { 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 2d689fed..8a9922b0 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -89,8 +89,8 @@ describe('Fixed thread pool test suite', () => { }) it('Verify that tasks queuing is working', async () => { - const maxMultiplier = 2 const promises = new Set() + const maxMultiplier = 2 for (let i = 0; i < numberOfThreads * maxMultiplier; i++) { promises.add(queuePool.execute()) } @@ -110,6 +110,9 @@ 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.tasksQueue.size).toBe(0) } promises.clear()