X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fselection-strategies%2Fselection-strategies.test.js;h=b8341f09e9093566b28c672fd55a5cf107a966d3;hb=b0afa8e5b4aa17b920ac920a5e517993afccc2a3;hp=7b11bf402e06f981d53dda669809808dbb3c9ca8;hpb=f0829c537c1be61af8472ce3167d7a58fe0c8e69;p=poolifier.git diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index 7b11bf40..b8341f09 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -31,6 +31,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().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 pool = new DynamicThreadPool( min, @@ -109,6 +125,39 @@ describe('Selection strategies test suite', () => { 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 pool = new FixedThreadPool( max, @@ -202,6 +251,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,26 +340,28 @@ describe('Selection strategies test suite', () => { await pool.destroy() }) - it('Verify FAIR_SHARE strategy statistics are resets after setting it', async () => { + 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.workerChoiceStrategy + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .workerLastVirtualTaskTimestamp ).toBeUndefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) - for (const worker of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) { + for (const worker of pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerLastVirtualTaskTimestamp.keys()) { expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - worker - ).start + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerLastVirtualTaskTimestamp.get(worker).start ).toBe(0) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - worker - ).end + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerLastVirtualTaskTimestamp.get(worker).end ).toBe(0) } await pool.destroy() @@ -306,20 +371,22 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js' ) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy - .workerLastVirtualTaskTimestamp + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerChoiceStrategy.workerLastVirtualTaskTimestamp ).toBeUndefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) - for (const worker of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) { + for (const worker of pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) { expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - worker - ).start + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(worker).start ).toBe(0) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - worker - ).end + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(worker).end ).toBe(0) } // We need to clean up the resources after our test @@ -335,6 +402,28 @@ 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() }) @@ -410,20 +499,39 @@ describe('Selection strategies test suite', () => { await pool.destroy() }) - it('Verify WEIGHTED_ROUND_ROBIN strategy statistics are resets after setting it', async () => { + 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.workerChoiceStrategy.workersTaskRunTime + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .currentWorkerIndex + ).toBeUndefined() + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .defaultWorkerWeight + ).toBeUndefined() + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workersTaskRunTime ).toBeUndefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) - for (const worker of pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.keys()) { + 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.workerChoiceStrategy.workersTaskRunTime.get( - worker - ).runTime + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workersTaskRunTime.get(worker).runTime ).toBe(0) } await pool.destroy() @@ -433,15 +541,33 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js' ) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy - .workersTaskRunTime + 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) - for (const worker of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workersTaskRunTime.keys()) { + 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.workerChoiceStrategy.workerChoiceStrategy.workersTaskRunTime.get( - worker - ).runTime + pool.workerChoiceStrategyContext + .getWorkerChoiceStrategy() + .workerChoiceStrategy.workersTaskRunTime.get(worker).runTime ).toBe(0) } // We need to clean up the resources after our test