X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fselection-strategies%2Fselection-strategies.test.js;h=5a7394a57aa5fe5abdf643a43d6bfcf48d35f67a;hb=78099a150dc54d7adab495195fa5f133fd54e114;hp=dd420c7f89b04f86792af87850d85dcbffe4f0d8;hpb=5502c07c81463257e1dd3015ee5a828846ece0b2;p=poolifier.git diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index dd420c7f..5a7394a5 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -43,7 +43,9 @@ describe('Selection strategies test suite', () => { WorkerChoiceStrategies.ROUND_ROBIN ) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.nextWorkerId + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.ROUND_ROBIN + ).nextWorkerId ).toBe(0) // We need to clean up the resources after our test await pool.destroy() @@ -72,6 +74,12 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(false) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime + ).toBe(false) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime + ).toBe(false) await pool.destroy() pool = new DynamicThreadPool( min, @@ -82,6 +90,12 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(false) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime + ).toBe(false) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime + ).toBe(false) // We need to clean up the resources after our test await pool.destroy() }) @@ -152,11 +166,15 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } ) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.nextWorkerId - ).toBeUndefined() + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.ROUND_ROBIN + ).nextWorkerId + ).toBeDefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.nextWorkerId + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.ROUND_ROBIN + ).nextWorkerId ).toBe(0) await pool.destroy() pool = new DynamicThreadPool( @@ -166,13 +184,15 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } ) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy - .nextWorkerId - ).toBeUndefined() + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.ROUND_ROBIN + ).nextWorkerId + ).toBeDefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy - .nextWorkerId + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.ROUND_ROBIN + ).nextWorkerId ).toBe(0) // We need to clean up the resources after our test await pool.destroy() @@ -213,6 +233,12 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(false) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime + ).toBe(false) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime + ).toBe(false) await pool.destroy() pool = new DynamicThreadPool( min, @@ -223,6 +249,12 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(false) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime + ).toBe(false) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime + ).toBe(false) // We need to clean up the resources after our test await pool.destroy() }) @@ -295,6 +327,12 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(true) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime + ).toBe(false) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime + ).toBe(false) await pool.destroy() pool = new DynamicThreadPool( min, @@ -305,6 +343,12 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(true) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime + ).toBe(false) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime + ).toBe(false) // We need to clean up the resources after our test await pool.destroy() }) @@ -351,16 +395,18 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.FAIR_SHARE ) - for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) { + for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.keys()) { expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - workerKey - ).start + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.get(workerKey).start ).toBe(0) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - workerKey - ).end + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.get(workerKey).end ).toBe(0) } // We need to clean up the resources after our test @@ -389,6 +435,12 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(true) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime + ).toBe(true) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime + ).toBe(false) await pool.destroy() pool = new DynamicThreadPool( min, @@ -399,6 +451,12 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(true) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime + ).toBe(true) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime + ).toBe(false) // We need to clean up the resources after our test await pool.destroy() }) @@ -416,8 +474,9 @@ describe('Selection strategies test suite', () => { } await Promise.all(promises) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy - .workerLastVirtualTaskTimestamp.size + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.FAIR_SHARE + ).workerLastVirtualTaskTimestamp.size ).toBe(pool.workers.length) // We need to clean up the resources after our test await pool.destroy() @@ -432,14 +491,18 @@ describe('Selection strategies test suite', () => { ) // TODO: Create a better test to cover `FairShareChoiceStrategy#choose` const promises = [] - for (let i = 0; i < max * 2; i++) { + const maxMultiplier = 2 + for (let i = 0; i < max * maxMultiplier; i++) { promises.push(pool.execute()) } await Promise.all(promises) - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy - .workerLastVirtualTaskTimestamp.size - ).toBe(pool.workers.length) + // if (process.platform !== 'win32') { + // expect( + // pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + // WorkerChoiceStrategies.FAIR_SHARE + // ).workerLastVirtualTaskTimestamp.size + // ).toBe(pool.workers.length) + // } // We need to clean up the resources after our test await pool.destroy() }) @@ -450,20 +513,23 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js' ) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy - .workerLastVirtualTaskTimestamp - ).toBeUndefined() + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.FAIR_SHARE + ).workerLastVirtualTaskTimestamp + ).toBeDefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) - for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) { + for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.keys()) { expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - workerKey - ).start + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.get(workerKey).start ).toBe(0) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - workerKey - ).end + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.get(workerKey).end ).toBe(0) } await pool.destroy() @@ -473,20 +539,23 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js' ) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy - .workerLastVirtualTaskTimestamp - ).toBeUndefined() + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.FAIR_SHARE + ).workerLastVirtualTaskTimestamp + ).toBeDefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) - for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) { + for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.keys()) { expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - workerKey - ).start + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.get(workerKey).start ).toBe(0) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - workerKey - ).end + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.get(workerKey).end ).toBe(0) } // We need to clean up the resources after our test @@ -503,21 +572,27 @@ describe('Selection strategies test suite', () => { WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN ) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.currentWorkerId + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).currentWorkerId ).toBe(0) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.defaultWorkerWeight + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).defaultWorkerWeight ).toBeGreaterThan(0) - for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.keys()) { + for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .workersTaskRunTime.keys()) { expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get( - workerKey - ).weight + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .workersTaskRunTime.get(workerKey).weight ).toBeGreaterThan(0) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get( - workerKey - ).runTime + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .workersTaskRunTime.get(workerKey).runTime ).toBe(0) } // We need to clean up the resources after our test @@ -546,6 +621,12 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(true) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime + ).toBe(true) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime + ).toBe(false) await pool.destroy() pool = new DynamicThreadPool( min, @@ -556,6 +637,12 @@ describe('Selection strategies test suite', () => { expect( pool.workerChoiceStrategyContext.getRequiredStatistics().runTime ).toBe(true) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime + ).toBe(true) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime + ).toBe(false) // We need to clean up the resources after our test await pool.destroy() }) @@ -573,8 +660,9 @@ describe('Selection strategies test suite', () => { } await Promise.all(promises) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime - .size + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).workersTaskRunTime.size ).toBe(pool.workers.length) // We need to clean up the resources after our test await pool.destroy() @@ -590,16 +678,20 @@ describe('Selection strategies test suite', () => { // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` const promises = [] const maxMultiplier = - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy - .defaultWorkerWeight + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).defaultWorkerWeight * 50 for (let i = 0; i < max * maxMultiplier; i++) { promises.push(pool.execute()) } await Promise.all(promises) - expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy - .workersTaskRunTime.size - ).toBe(pool.workers.length) + if (process.platform !== 'win32') { + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).workersTaskRunTime.size + ).toBe(pool.workers.length) + } // We need to clean up the resources after our test await pool.destroy() }) @@ -610,26 +702,38 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js' ) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.currentWorkerId - ).toBeUndefined() + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).currentWorkerId + ).toBeDefined() expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.defaultWorkerWeight - ).toBeUndefined() + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).defaultWorkerWeight + ).toBeDefined() expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime - ).toBeUndefined() + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).workersTaskRunTime + ).toBeDefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.currentWorkerId + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).currentWorkerId ).toBe(0) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.defaultWorkerWeight + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).defaultWorkerWeight ).toBeGreaterThan(0) - for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.keys()) { + for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .workersTaskRunTime.keys()) { expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get( - workerKey - ).runTime + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .workersTaskRunTime.get(workerKey).runTime ).toBe(0) } await pool.destroy() @@ -639,31 +743,38 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js' ) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy - .currentWorkerId - ).toBeUndefined() + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).currentWorkerId + ).toBeDefined() expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy - .defaultWorkerWeight - ).toBeUndefined() + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).defaultWorkerWeight + ).toBeDefined() expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy - .workersTaskRunTime - ).toBeUndefined() + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).workersTaskRunTime + ).toBeDefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy - .currentWorkerId + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).currentWorkerId ).toBe(0) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy - .defaultWorkerWeight + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).defaultWorkerWeight ).toBeGreaterThan(0) - for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workersTaskRunTime.keys()) { + for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .workersTaskRunTime.keys()) { expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workersTaskRunTime.get( - workerKey - ).runTime + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .workersTaskRunTime.get(workerKey).runTime ).toBe(0) } // We need to clean up the resources after our test @@ -680,7 +791,7 @@ describe('Selection strategies test suite', () => { { workerChoiceStrategy: 'UNKNOWN_STRATEGY' } ) ).toThrowError( - new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found") + new Error("Invalid worker choice strategy 'UNKNOWN_STRATEGY'") ) }) })