X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fselection-strategies%2Fselection-strategies.test.js;h=93d85ea6e419e52a1d3e861f18b485ea688fd580;hb=2ced693a7db771482e1be9fa5e8490127ee0196e;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..93d85ea6 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -2,10 +2,14 @@ const { expect } = require('expect') const { WorkerChoiceStrategies, DynamicThreadPool, - FixedThreadPool + FixedThreadPool, + FixedClusterPool } = 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 +20,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 +32,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 +63,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 +72,7 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .requiredStatistics.runTime ).toBe(false) + await pool.destroy() pool = new DynamicThreadPool( min, max, @@ -73,7 +88,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 +99,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 +107,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,15 +119,67 @@ 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 await pool.destroy() }) + it('Verify ROUND_ROBIN strategy runtime behavior', async () => { + let pool = new FixedClusterPool( + max, + './tests/worker-files/cluster/testWorker.js' + ) + let results = new Set() + for (let i = 0; i < max; i++) { + results.add(pool.chooseWorker().id) + } + expect(results.size).toBe(max) + await pool.destroy() + pool = new FixedThreadPool(max, './tests/worker-files/thread/testWorker.js') + results = new Set() + for (let i = 0; i < max; i++) { + results.add(pool.chooseWorker().threadId) + } + expect(results.size).toBe(max) + await pool.destroy() + }) + + it('Verify ROUND_ROBIN strategy internals are resets after setting it', async () => { + let pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } + ) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerIndex + ).toBeUndefined() + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerIndex + ).toBe(0) + await pool.destroy() + pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } + ) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerChoiceStrategy.nextWorkerIndex + ).toBeUndefined() + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerChoiceStrategy.nextWorkerIndex + ).toBe(0) + // We need to clean up the resources after our test + await pool.destroy() + }) + 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 +193,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 +206,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 +215,7 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .requiredStatistics.runTime ).toBe(false) + await pool.destroy() pool = new DynamicThreadPool( min, max, @@ -169,7 +231,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 +239,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 +247,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 +256,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 +264,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 +272,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 +304,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 +313,7 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .requiredStatistics.runTime ).toBe(true) + await pool.destroy() pool = new DynamicThreadPool( min, max, @@ -259,7 +329,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 +337,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 +345,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 +354,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 internals 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 +423,33 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN ) + 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 +463,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 +472,7 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .requiredStatistics.runTime ).toBe(true) + await pool.destroy() pool = new DynamicThreadPool( min, max, @@ -349,7 +488,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 +496,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 +504,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 +513,89 @@ 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 internals are resets after setting it', async () => { + let pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js' + ) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .currentWorkerIndex + ).toBeUndefined() + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .defaultWorkerWeight + ).toBeUndefined() + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workersTaskRunTime + ).toBeUndefined() + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + 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).runTime + ).toBe(0) + } + await pool.destroy() + pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js' + ) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerChoiceStrategy.currentWorkerIndex + ).toBeUndefined() + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerChoiceStrategy.defaultWorkerWeight + ).toBeUndefined() + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerChoiceStrategy.workersTaskRunTime + ).toBeUndefined() + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerChoiceStrategy.currentWorkerIndex + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerChoiceStrategy.defaultWorkerWeight + ).toBeGreaterThan(0) + 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(