From: Jérôme Benoit Date: Fri, 14 Apr 2023 12:45:11 +0000 (+0200) Subject: test: enhance worker choice strategies tests X-Git-Tag: v2.4.9~28 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=d710242dd39f5dd418b0a89536a9ad88c147fe3b;p=poolifier.git test: enhance worker choice strategies tests Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index 91441ace..b075c4ea 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -35,12 +35,12 @@ export class WorkerChoiceStrategyContext< * Worker choice strategy context constructor. * * @param pool - The pool instance. - * @param workerChoiceStrategyType - The worker choice strategy. + * @param workerChoiceStrategy - The worker choice strategy. * @param opts - The worker choice strategy options. */ public constructor ( pool: IPool, - private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN, + private workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN, opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS ) { this.execute.bind(this) @@ -82,7 +82,7 @@ export class WorkerChoiceStrategyContext< public getRequiredStatistics (): RequiredStatistics { return ( this.workerChoiceStrategies.get( - this.workerChoiceStrategyType + this.workerChoiceStrategy ) as IWorkerChoiceStrategy ).requiredStatistics } @@ -95,10 +95,10 @@ export class WorkerChoiceStrategyContext< public setWorkerChoiceStrategy ( workerChoiceStrategy: WorkerChoiceStrategy ): void { - if (this.workerChoiceStrategyType !== workerChoiceStrategy) { - this.workerChoiceStrategyType = workerChoiceStrategy + if (this.workerChoiceStrategy !== workerChoiceStrategy) { + this.workerChoiceStrategy = workerChoiceStrategy } - this.workerChoiceStrategies.get(this.workerChoiceStrategyType)?.reset() + this.workerChoiceStrategies.get(this.workerChoiceStrategy)?.reset() } /** @@ -109,7 +109,7 @@ export class WorkerChoiceStrategyContext< public execute (): number { return ( this.workerChoiceStrategies.get( - this.workerChoiceStrategyType + this.workerChoiceStrategy ) as IWorkerChoiceStrategy ).choose() } @@ -123,7 +123,7 @@ export class WorkerChoiceStrategyContext< public remove (workerNodeKey: number): boolean { return ( this.workerChoiceStrategies.get( - this.workerChoiceStrategyType + this.workerChoiceStrategy ) as IWorkerChoiceStrategy ).remove(workerNodeKey) } diff --git a/tests/pools/cluster/dynamic.test.js b/tests/pools/cluster/dynamic.test.js index 33a5ab8c..0da467ca 100644 --- a/tests/pools/cluster/dynamic.test.js +++ b/tests/pools/cluster/dynamic.test.js @@ -100,6 +100,11 @@ describe('Dynamic cluster pool test suite', () => { expect(longRunningPool.workerNodes.length).toBe(max) await TestUtils.waitExits(longRunningPool, max - min) expect(longRunningPool.workerNodes.length).toBe(min) + expect( + longRunningPool.workerChoiceStrategyContext.workerChoiceStrategies.get( + longRunningPool.workerChoiceStrategyContext.workerChoiceStrategy + ).nextWorkerNodeId + ).toBeLessThan(longRunningPool.workerNodes.length) // We need to clean up the resources after our test await longRunningPool.destroy() }) diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index 2fff1b13..d4d2ec1b 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -42,9 +42,12 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.ROUND_ROBIN ) + expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.ROUND_ROBIN + ) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - WorkerChoiceStrategies.ROUND_ROBIN + pool.workerChoiceStrategyContext.workerChoiceStrategy ).nextWorkerNodeId ).toBe(0) // We need to clean up the resources after our test @@ -61,6 +64,9 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.ROUND_ROBIN ) + expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.ROUND_ROBIN + ) // We need to clean up the resources after our test await pool.destroy() }) @@ -68,9 +74,9 @@ describe('Selection strategies test suite', () => { it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => { let pool = new FixedThreadPool( max, - './tests/worker-files/thread/testWorker.js' + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN } ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(false) @@ -84,9 +90,9 @@ describe('Selection strategies test suite', () => { pool = new DynamicThreadPool( min, max, - './tests/worker-files/thread/testWorker.js' + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN } ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(false) @@ -106,9 +112,6 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN } ) - expect(pool.opts.workerChoiceStrategy).toBe( - WorkerChoiceStrategies.ROUND_ROBIN - ) // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose` const promises = [] for (let i = 0; i < max * 2; i++) { @@ -126,9 +129,6 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN } ) - expect(pool.opts.workerChoiceStrategy).toBe( - WorkerChoiceStrategies.ROUND_ROBIN - ) // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose` const promises = [] for (let i = 0; i < max * 2; i++) { @@ -173,7 +173,7 @@ describe('Selection strategies test suite', () => { pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - WorkerChoiceStrategies.ROUND_ROBIN + pool.workerChoiceStrategyContext.workerChoiceStrategy ).nextWorkerNodeId ).toBe(0) await pool.destroy() @@ -191,7 +191,7 @@ describe('Selection strategies test suite', () => { pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - WorkerChoiceStrategies.ROUND_ROBIN + pool.workerChoiceStrategyContext.workerChoiceStrategy ).nextWorkerNodeId ).toBe(0) // We need to clean up the resources after our test @@ -207,6 +207,9 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.LESS_USED ) + expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.LESS_USED + ) // We need to clean up the resources after our test await pool.destroy() }) @@ -220,6 +223,9 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.LESS_USED ) + expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.LESS_USED + ) // We need to clean up the resources after our test await pool.destroy() }) @@ -227,9 +233,9 @@ describe('Selection strategies test suite', () => { it('Verify LESS_USED strategy default tasks usage statistics requirements', async () => { let pool = new FixedThreadPool( max, - './tests/worker-files/thread/testWorker.js' + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED } ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED) expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(false) @@ -243,9 +249,9 @@ describe('Selection strategies test suite', () => { pool = new DynamicThreadPool( min, max, - './tests/worker-files/thread/testWorker.js' + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED } ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED) expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(false) @@ -301,6 +307,9 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.LESS_BUSY ) + expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.LESS_BUSY + ) // We need to clean up the resources after our test await pool.destroy() }) @@ -314,6 +323,9 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.LESS_BUSY ) + expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.LESS_BUSY + ) // We need to clean up the resources after our test await pool.destroy() }) @@ -321,9 +333,9 @@ describe('Selection strategies test suite', () => { it('Verify LESS_BUSY strategy default tasks usage statistics requirements', async () => { let pool = new FixedThreadPool( max, - './tests/worker-files/thread/testWorker.js' + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY } ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY) expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(true) @@ -337,9 +349,9 @@ describe('Selection strategies test suite', () => { pool = new DynamicThreadPool( min, max, - './tests/worker-files/thread/testWorker.js' + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY } ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY) expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(true) @@ -395,17 +407,20 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.FAIR_SHARE ) + expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.FAIR_SHARE + ) for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.FAIR_SHARE) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workerLastVirtualTaskTimestamp.keys()) { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.FAIR_SHARE) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workerLastVirtualTaskTimestamp.get(workerNodeKey).start ).toBe(0) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.FAIR_SHARE) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workerLastVirtualTaskTimestamp.get(workerNodeKey).end ).toBe(0) } @@ -422,6 +437,9 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.FAIR_SHARE ) + expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.FAIR_SHARE + ) // We need to clean up the resources after our test await pool.destroy() }) @@ -429,9 +447,9 @@ describe('Selection strategies test suite', () => { it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => { let pool = new FixedThreadPool( max, - './tests/worker-files/thread/testWorker.js' + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(true) @@ -445,9 +463,9 @@ describe('Selection strategies test suite', () => { pool = new DynamicThreadPool( min, max, - './tests/worker-files/thread/testWorker.js' + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(true) @@ -475,7 +493,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - WorkerChoiceStrategies.FAIR_SHARE + pool.workerChoiceStrategyContext.workerChoiceStrategy ).workerLastVirtualTaskTimestamp.size ).toBe(pool.workerNodes.length) // We need to clean up the resources after our test @@ -499,7 +517,7 @@ describe('Selection strategies test suite', () => { // if (process.platform !== 'win32') { // expect( // pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - // WorkerChoiceStrategies.FAIR_SHARE + // pool.workerChoiceStrategyContext.workerChoiceStrategy // ).workerLastVirtualTaskTimestamp.size // ).toBe(pool.workerNodes.length) // } @@ -519,16 +537,16 @@ describe('Selection strategies test suite', () => { ).toBeDefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.FAIR_SHARE) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workerLastVirtualTaskTimestamp.keys()) { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.FAIR_SHARE) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workerLastVirtualTaskTimestamp.get(workerNodeKey).start ).toBe(0) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.FAIR_SHARE) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workerLastVirtualTaskTimestamp.get(workerNodeKey).end ).toBe(0) } @@ -545,16 +563,16 @@ describe('Selection strategies test suite', () => { ).toBeDefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.FAIR_SHARE) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workerLastVirtualTaskTimestamp.keys()) { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.FAIR_SHARE) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workerLastVirtualTaskTimestamp.get(workerNodeKey).start ).toBe(0) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.FAIR_SHARE) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workerLastVirtualTaskTimestamp.get(workerNodeKey).end ).toBe(0) } @@ -571,27 +589,30 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN ) + expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + pool.workerChoiceStrategyContext.workerChoiceStrategy ).currentWorkerNodeId ).toBe(0) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + pool.workerChoiceStrategyContext.workerChoiceStrategy ).defaultWorkerWeight ).toBeGreaterThan(0) for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workersTaskRunTime.keys()) { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workersTaskRunTime.get(workerNodeKey).weight ).toBeGreaterThan(0) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workersTaskRunTime.get(workerNodeKey).runTime ).toBe(0) } @@ -608,6 +629,9 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN ) + expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ) // We need to clean up the resources after our test await pool.destroy() }) @@ -615,9 +639,9 @@ describe('Selection strategies test suite', () => { it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => { let pool = new FixedThreadPool( max, - './tests/worker-files/thread/testWorker.js' + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(true) @@ -631,9 +655,9 @@ describe('Selection strategies test suite', () => { pool = new DynamicThreadPool( min, max, - './tests/worker-files/thread/testWorker.js' + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(true) @@ -661,7 +685,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + pool.workerChoiceStrategyContext.workerChoiceStrategy ).workersTaskRunTime.size ).toBe(pool.workerNodes.length) // We need to clean up the resources after our test @@ -679,7 +703,7 @@ describe('Selection strategies test suite', () => { const promises = [] const maxMultiplier = pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + pool.workerChoiceStrategyContext.workerChoiceStrategy ).defaultWorkerWeight * 50 for (let i = 0; i < max * maxMultiplier; i++) { promises.push(pool.execute()) @@ -688,7 +712,7 @@ describe('Selection strategies test suite', () => { if (process.platform !== 'win32') { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + pool.workerChoiceStrategyContext.workerChoiceStrategy ).workersTaskRunTime.size ).toBe(pool.workerNodes.length) } @@ -719,20 +743,20 @@ describe('Selection strategies test suite', () => { pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + pool.workerChoiceStrategyContext.workerChoiceStrategy ).currentWorkerNodeId ).toBe(0) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + pool.workerChoiceStrategyContext.workerChoiceStrategy ).defaultWorkerWeight ).toBeGreaterThan(0) for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workersTaskRunTime.keys()) { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workersTaskRunTime.get(workerNodeKey).runTime ).toBe(0) } @@ -760,20 +784,20 @@ describe('Selection strategies test suite', () => { pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + pool.workerChoiceStrategyContext.workerChoiceStrategy ).currentWorkerNodeId ).toBe(0) expect( pool.workerChoiceStrategyContext.workerChoiceStrategies.get( - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + pool.workerChoiceStrategyContext.workerChoiceStrategy ).defaultWorkerWeight ).toBeGreaterThan(0) for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workersTaskRunTime.keys()) { expect( pool.workerChoiceStrategyContext.workerChoiceStrategies - .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) .workersTaskRunTime.get(workerNodeKey).runTime ).toBe(0) } diff --git a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js index a8f187ee..c571478c 100644 --- a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js +++ b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js @@ -69,17 +69,17 @@ describe('Worker choice strategy context test suite', () => { choose: sinon.stub().returns(0) } ) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.ROUND_ROBIN ) workerChoiceStrategyContext.workerChoiceStrategies.set( - workerChoiceStrategyContext.workerChoiceStrategyType, + workerChoiceStrategyContext.workerChoiceStrategy, WorkerChoiceStrategyStub ) const chosenWorkerKey = workerChoiceStrategyContext.execute() expect( workerChoiceStrategyContext.workerChoiceStrategies.get( - workerChoiceStrategyContext.workerChoiceStrategyType + workerChoiceStrategyContext.workerChoiceStrategy ).choose.calledOnce ).toBe(true) expect(chosenWorkerKey).toBe(0) @@ -95,17 +95,17 @@ describe('Worker choice strategy context test suite', () => { choose: sinon.stub().returns(0) } ) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.ROUND_ROBIN ) workerChoiceStrategyContext.workerChoiceStrategies.set( - workerChoiceStrategyContext.workerChoiceStrategyType, + workerChoiceStrategyContext.workerChoiceStrategy, WorkerChoiceStrategyStub ) const chosenWorkerKey = workerChoiceStrategyContext.execute() expect( workerChoiceStrategyContext.workerChoiceStrategies.get( - workerChoiceStrategyContext.workerChoiceStrategyType + workerChoiceStrategyContext.workerChoiceStrategy ).choose.calledOnce ).toBe(true) expect(chosenWorkerKey).toBe(0) @@ -125,7 +125,7 @@ describe('Worker choice strategy context test suite', () => { WorkerChoiceStrategies.ROUND_ROBIN ) ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.ROUND_ROBIN ) workerChoiceStrategyContext.setWorkerChoiceStrategy( @@ -136,7 +136,7 @@ describe('Worker choice strategy context test suite', () => { WorkerChoiceStrategies.ROUND_ROBIN ) ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.ROUND_ROBIN ) }) @@ -155,7 +155,7 @@ describe('Worker choice strategy context test suite', () => { WorkerChoiceStrategies.ROUND_ROBIN ) ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.ROUND_ROBIN ) workerChoiceStrategyContext.setWorkerChoiceStrategy( @@ -166,7 +166,7 @@ describe('Worker choice strategy context test suite', () => { WorkerChoiceStrategies.ROUND_ROBIN ) ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.ROUND_ROBIN ) }) @@ -188,7 +188,7 @@ describe('Worker choice strategy context test suite', () => { WorkerChoiceStrategies.LESS_USED ) ).toBeInstanceOf(LessUsedWorkerChoiceStrategy) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.LESS_USED ) }) @@ -210,7 +210,7 @@ describe('Worker choice strategy context test suite', () => { WorkerChoiceStrategies.LESS_USED ) ).toBeInstanceOf(LessUsedWorkerChoiceStrategy) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.LESS_USED ) }) @@ -232,7 +232,7 @@ describe('Worker choice strategy context test suite', () => { WorkerChoiceStrategies.LESS_BUSY ) ).toBeInstanceOf(LessBusyWorkerChoiceStrategy) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.LESS_BUSY ) }) @@ -254,7 +254,7 @@ describe('Worker choice strategy context test suite', () => { WorkerChoiceStrategies.LESS_BUSY ) ).toBeInstanceOf(LessBusyWorkerChoiceStrategy) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.LESS_BUSY ) }) @@ -276,7 +276,7 @@ describe('Worker choice strategy context test suite', () => { WorkerChoiceStrategies.FAIR_SHARE ) ).toBeInstanceOf(FairShareWorkerChoiceStrategy) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.FAIR_SHARE ) }) @@ -298,7 +298,7 @@ describe('Worker choice strategy context test suite', () => { WorkerChoiceStrategies.FAIR_SHARE ) ).toBeInstanceOf(FairShareWorkerChoiceStrategy) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.FAIR_SHARE ) }) @@ -320,7 +320,7 @@ describe('Worker choice strategy context test suite', () => { WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN ) ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN ) }) @@ -342,7 +342,7 @@ describe('Worker choice strategy context test suite', () => { WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN ) ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) - expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN ) }) diff --git a/tests/pools/thread/dynamic.test.js b/tests/pools/thread/dynamic.test.js index feb4955d..11d91128 100644 --- a/tests/pools/thread/dynamic.test.js +++ b/tests/pools/thread/dynamic.test.js @@ -100,6 +100,11 @@ describe('Dynamic thread pool test suite', () => { expect(longRunningPool.workerNodes.length).toBe(max) await TestUtils.waitExits(longRunningPool, max - min) expect(longRunningPool.workerNodes.length).toBe(min) + expect( + longRunningPool.workerChoiceStrategyContext.workerChoiceStrategies.get( + longRunningPool.workerChoiceStrategyContext.workerChoiceStrategy + ).nextWorkerNodeId + ).toBeLessThan(longRunningPool.workerNodes.length) // We need to clean up the resources after our test await longRunningPool.destroy() })