X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fselection-strategies%2Fselection-strategies.test.js;h=c55b971edb8a9ed3e47ccae1db4b0deff206b08b;hb=ffcbbad84f63b8a77f2b1a08f82deef5430f646e;hp=da4fea96e1557401f9bafde36c6416a8a6b50d39;hpb=fd7ebd496ee8e1f95c6c1dc2af5153d73ec3daab;p=poolifier.git diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index da4fea96..c55b971e 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -2,7 +2,8 @@ const { expect } = require('expect') const { WorkerChoiceStrategies, DynamicThreadPool, - FixedThreadPool + FixedThreadPool, + FixedClusterPool } = require('../../../lib/index') describe('Selection strategies test suite', () => { @@ -31,6 +32,22 @@ 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().nextWorkerId + ).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 pool = new DynamicThreadPool( min, @@ -109,6 +126,59 @@ describe('Selection strategies test suite', () => { 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().nextWorkerId + ).toBeUndefined() + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerId + ).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.nextWorkerId + ).toBeUndefined() + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerChoiceStrategy.nextWorkerId + ).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 pool = new FixedThreadPool( max, @@ -202,6 +272,20 @@ 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() }) @@ -277,6 +361,59 @@ describe('Selection strategies test suite', () => { 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 pool = new FixedThreadPool( max, @@ -286,6 +423,27 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN ) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId + ).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() }) @@ -361,6 +519,79 @@ describe('Selection strategies test suite', () => { 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().currentWorkerId + ).toBeUndefined() + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .defaultWorkerWeight + ).toBeUndefined() + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workersTaskRunTime + ).toBeUndefined() + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().currentWorkerId + ).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.currentWorkerId + ).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.currentWorkerId + ).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', () => { expect( () =>