X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fselection-strategies%2Fselection-strategies.test.js;h=faa28a296b844c050b96706f090165f9676609e6;hb=d2f7b7a2e327fd754babbe16dd7ef8ff72a4a7cf;hp=39895b213ff6baf1e88167827ac8cb0f717002b1;hpb=10fcfaf49c0b6d6f0e1d137eeba1e9d805c9a815;p=poolifier.git diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index 39895b21..faa28a29 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -6,6 +6,9 @@ const { } = require('../../../lib/index') describe('Selection strategies test suite', () => { + const min = 0 + const max = 3 + it('Verify that WorkerChoiceStrategies enumeration provides string values', () => { expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN') expect(WorkerChoiceStrategies.LESS_RECENTLY_USED).toBe('LESS_RECENTLY_USED') @@ -16,8 +19,6 @@ describe('Selection strategies test suite', () => { }) it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => { - const min = 0 - const max = 3 const pool = new DynamicThreadPool( min, max, @@ -30,9 +31,23 @@ describe('Selection strategies test suite', () => { await pool.destroy() }) + it('Verify ROUND_ROBIN strategy is taken at pool creation', async () => { + const pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN } + ) + expect(pool.opts.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.ROUND_ROBIN + ) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerIndex + ).toBe(0) + // We need to clean up the resources after our test + await pool.destroy() + }) + it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => { - const min = 0 - const max = 3 const pool = new DynamicThreadPool( min, max, @@ -47,8 +62,6 @@ describe('Selection strategies test suite', () => { }) it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => { - const min = 0 - const max = 3 let pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' @@ -58,6 +71,7 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .requiredStatistics.runTime ).toBe(false) + await pool.destroy() pool = new DynamicThreadPool( min, max, @@ -73,7 +87,6 @@ describe('Selection strategies test suite', () => { }) it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', @@ -85,7 +98,7 @@ describe('Selection strategies test suite', () => { // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose` const promises = [] for (let i = 0; i < max * 2; i++) { - promises.push(pool.execute({ test: 'test' })) + promises.push(pool.execute()) } await Promise.all(promises) // We need to clean up the resources after our test @@ -93,8 +106,6 @@ describe('Selection strategies test suite', () => { }) it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => { - const min = 0 - const max = 3 const pool = new DynamicThreadPool( min, max, @@ -107,7 +118,7 @@ describe('Selection strategies test suite', () => { // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose` const promises = [] for (let i = 0; i < max * 2; i++) { - promises.push(pool.execute({ test: 'test' })) + promises.push(pool.execute()) } await Promise.all(promises) // We need to clean up the resources after our test @@ -115,7 +126,6 @@ describe('Selection strategies test suite', () => { }) it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', @@ -129,7 +139,6 @@ describe('Selection strategies test suite', () => { }) it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' @@ -143,8 +152,6 @@ describe('Selection strategies test suite', () => { }) it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => { - const min = 0 - const max = 3 let pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' @@ -154,6 +161,7 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .requiredStatistics.runTime ).toBe(false) + await pool.destroy() pool = new DynamicThreadPool( min, max, @@ -169,7 +177,6 @@ describe('Selection strategies test suite', () => { }) it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', @@ -178,7 +185,7 @@ describe('Selection strategies test suite', () => { // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` const promises = [] for (let i = 0; i < max * 2; i++) { - promises.push(pool.execute({ test: 'test' })) + promises.push(pool.execute()) } await Promise.all(promises) // We need to clean up the resources after our test @@ -186,8 +193,6 @@ describe('Selection strategies test suite', () => { }) it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => { - const min = 0 - const max = 3 const pool = new DynamicThreadPool( min, max, @@ -197,7 +202,7 @@ describe('Selection strategies test suite', () => { // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` const promises = [] for (let i = 0; i < max * 2; i++) { - promises.push(pool.execute({ test: 'test' })) + promises.push(pool.execute()) } await Promise.all(promises) // We need to clean up the resources after our test @@ -205,7 +210,6 @@ describe('Selection strategies test suite', () => { }) it('Verify FAIR_SHARE strategy is taken at pool creation', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', @@ -214,12 +218,25 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.FAIR_SHARE ) + for (const worker of pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerLastVirtualTaskTimestamp.keys()) { + expect( + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerLastVirtualTaskTimestamp.get(worker).start + ).toBe(0) + expect( + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerLastVirtualTaskTimestamp.get(worker).end + ).toBe(0) + } // We need to clean up the resources after our test await pool.destroy() }) it('Verify FAIR_SHARE strategy can be set after pool creation', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' @@ -233,8 +250,6 @@ describe('Selection strategies test suite', () => { }) it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => { - const min = 0 - const max = 3 let pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' @@ -244,6 +259,7 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .requiredStatistics.runTime ).toBe(true) + await pool.destroy() pool = new DynamicThreadPool( min, max, @@ -259,7 +275,6 @@ describe('Selection strategies test suite', () => { }) it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', @@ -268,7 +283,7 @@ describe('Selection strategies test suite', () => { // TODO: Create a better test to cover `FairShareChoiceStrategy#choose` const promises = [] for (let i = 0; i < max * 2; i++) { - promises.push(pool.execute({ test: 'test' })) + promises.push(pool.execute()) } await Promise.all(promises) // We need to clean up the resources after our test @@ -276,8 +291,6 @@ describe('Selection strategies test suite', () => { }) it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => { - const min = 0 - const max = 3 const pool = new DynamicThreadPool( min, max, @@ -287,15 +300,67 @@ describe('Selection strategies test suite', () => { // TODO: Create a better test to cover `FairShareChoiceStrategy#choose` const promises = [] for (let i = 0; i < max * 2; i++) { - promises.push(pool.execute({ test: 'test' })) + promises.push(pool.execute()) } await Promise.all(promises) // We need to clean up the resources after our test await pool.destroy() }) + it('Verify FAIR_SHARE strategy statistics are resets after setting it', async () => { + let pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js' + ) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerLastVirtualTaskTimestamp + ).toBeUndefined() + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) + for (const worker of pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerLastVirtualTaskTimestamp.keys()) { + expect( + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerLastVirtualTaskTimestamp.get(worker).start + ).toBe(0) + expect( + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerLastVirtualTaskTimestamp.get(worker).end + ).toBe(0) + } + await pool.destroy() + pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js' + ) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerChoiceStrategy.workerLastVirtualTaskTimestamp + ).toBeUndefined() + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) + for (const worker of pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) { + expect( + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(worker).start + ).toBe(0) + expect( + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(worker).end + ).toBe(0) + } + // We need to clean up the resources after our test + await pool.destroy() + }) + it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', @@ -304,12 +369,37 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN ) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .previousWorkerIndex + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .currentWorkerIndex + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .defaultWorkerWeight + ).toBeGreaterThan(0) + for (const worker of pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workersTaskRunTime.keys()) { + expect( + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workersTaskRunTime.get(worker).weight + ).toBeGreaterThan(0) + expect( + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workersTaskRunTime.get(worker).runTime + ).toBe(0) + } // We need to clean up the resources after our test await pool.destroy() }) it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' @@ -323,8 +413,6 @@ describe('Selection strategies test suite', () => { }) it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => { - const min = 0 - const max = 3 let pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' @@ -334,6 +422,7 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .requiredStatistics.runTime ).toBe(true) + await pool.destroy() pool = new DynamicThreadPool( min, max, @@ -349,7 +438,6 @@ describe('Selection strategies test suite', () => { }) it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', @@ -358,7 +446,7 @@ describe('Selection strategies test suite', () => { // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` const promises = [] for (let i = 0; i < max * 2; i++) { - promises.push(pool.execute({ test: 'test' })) + promises.push(pool.execute()) } await Promise.all(promises) // We need to clean up the resources after our test @@ -366,8 +454,6 @@ describe('Selection strategies test suite', () => { }) it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => { - const min = 0 - const max = 3 const pool = new DynamicThreadPool( min, max, @@ -377,16 +463,57 @@ describe('Selection strategies test suite', () => { // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` const promises = [] for (let i = 0; i < max * 2; i++) { - promises.push(pool.execute({ test: 'test' })) + promises.push(pool.execute()) } await Promise.all(promises) // We need to clean up the resources after our test await pool.destroy() }) + it('Verify WEIGHTED_ROUND_ROBIN strategy statistics are resets after setting it', async () => { + let pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js' + ) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workersTaskRunTime + ).toBeUndefined() + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + for (const worker of pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workersTaskRunTime.keys()) { + expect( + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workersTaskRunTime.get(worker).runTime + ).toBe(0) + } + await pool.destroy() + pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js' + ) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerChoiceStrategy.workersTaskRunTime + ).toBeUndefined() + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + for (const worker of pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerChoiceStrategy.workersTaskRunTime.keys()) { + expect( + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerChoiceStrategy.workersTaskRunTime.get(worker).runTime + ).toBe(0) + } + // We need to clean up the resources after our test + await pool.destroy() + }) + it('Verify unknown strategies throw error', () => { - const min = 1 - const max = 3 expect( () => new DynamicThreadPool(