X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fselection-strategies%2Fselection-strategies.test.js;h=2fff1b13772e43c4acad99ad48e7441fda474dee;hb=da3098610d6cf6155bbbe53c84b6ac6ccd400ff5;hp=49e87a92dcd1649878c7d5659c4aac1ed5928db7;hpb=6e7fa401ce994a6ddb74d8ff1cc9e5a171270afd;p=poolifier.git diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index 49e87a92..2fff1b13 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', () => { @@ -11,7 +12,8 @@ describe('Selection strategies test suite', () => { it('Verify that WorkerChoiceStrategies enumeration provides string values', () => { expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN') - expect(WorkerChoiceStrategies.LESS_RECENTLY_USED).toBe('LESS_RECENTLY_USED') + expect(WorkerChoiceStrategies.LESS_USED).toBe('LESS_USED') + expect(WorkerChoiceStrategies.LESS_BUSY).toBe('LESS_BUSY') expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE') expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe( 'WEIGHTED_ROUND_ROBIN' @@ -31,6 +33,24 @@ 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.workerChoiceStrategies.get( + WorkerChoiceStrategies.ROUND_ROBIN + ).nextWorkerNodeId + ).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, @@ -52,8 +72,13 @@ describe('Selection strategies test suite', () => { ) pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) expect( - pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() - .requiredStatistics.runTime + 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( @@ -63,8 +88,13 @@ describe('Selection strategies test suite', () => { ) pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) expect( - pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() - .requiredStatistics.runTime + 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() @@ -109,41 +139,105 @@ describe('Selection strategies test suite', () => { await pool.destroy() }) - it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => { + 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.chooseWorkerNode()[1].worker.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.chooseWorkerNode()[1].worker.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.workerChoiceStrategies.get( + WorkerChoiceStrategies.ROUND_ROBIN + ).nextWorkerNodeId + ).toBeDefined() + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.ROUND_ROBIN + ).nextWorkerNodeId + ).toBe(0) + await pool.destroy() + pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } + ) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.ROUND_ROBIN + ).nextWorkerNodeId + ).toBeDefined() + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.ROUND_ROBIN + ).nextWorkerNodeId + ).toBe(0) + // We need to clean up the resources after our test + await pool.destroy() + }) + + it('Verify LESS_USED strategy is taken at pool creation', async () => { const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', - { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED } ) expect(pool.opts.workerChoiceStrategy).toBe( - WorkerChoiceStrategies.LESS_RECENTLY_USED + WorkerChoiceStrategies.LESS_USED ) // We need to clean up the resources after our test await pool.destroy() }) - it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => { + it('Verify LESS_USED strategy can be set after pool creation', async () => { const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED) + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED) expect(pool.opts.workerChoiceStrategy).toBe( - WorkerChoiceStrategies.LESS_RECENTLY_USED + WorkerChoiceStrategies.LESS_USED ) // We need to clean up the resources after our test await pool.destroy() }) - it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => { + it('Verify LESS_USED strategy default tasks usage statistics requirements', async () => { let pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED) + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().runTime + ).toBe(false) expect( - pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() - .requiredStatistics.runTime + pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime + ).toBe(false) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime ).toBe(false) await pool.destroy() pool = new DynamicThreadPool( @@ -151,22 +245,27 @@ describe('Selection strategies test suite', () => { max, './tests/worker-files/thread/testWorker.js' ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED) + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_USED) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().runTime + ).toBe(false) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime + ).toBe(false) expect( - pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() - .requiredStatistics.runTime + pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime ).toBe(false) // We need to clean up the resources after our test await pool.destroy() }) - it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => { + it('Verify LESS_USED strategy can be run in a fixed pool', async () => { const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', - { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED } ) - // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` + // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose` const promises = [] for (let i = 0; i < max * 2; i++) { promises.push(pool.execute()) @@ -176,14 +275,108 @@ describe('Selection strategies test suite', () => { await pool.destroy() }) - it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => { + it('Verify LESS_USED strategy can be run in a dynamic pool', async () => { const pool = new DynamicThreadPool( min, max, './tests/worker-files/thread/testWorker.js', - { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED } ) - // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` + // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose` + const promises = [] + for (let i = 0; i < max * 2; i++) { + promises.push(pool.execute()) + } + await Promise.all(promises) + // We need to clean up the resources after our test + await pool.destroy() + }) + + it('Verify LESS_BUSY strategy is taken at pool creation', async () => { + const pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY } + ) + expect(pool.opts.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.LESS_BUSY + ) + // We need to clean up the resources after our test + await pool.destroy() + }) + + it('Verify LESS_BUSY strategy can be set after pool creation', async () => { + const pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js' + ) + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY) + expect(pool.opts.workerChoiceStrategy).toBe( + WorkerChoiceStrategies.LESS_BUSY + ) + // We need to clean up the resources after our test + await pool.destroy() + }) + + it('Verify LESS_BUSY strategy default tasks usage statistics requirements', async () => { + let pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js' + ) + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY) + 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, + max, + './tests/worker-files/thread/testWorker.js' + ) + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY) + 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() + }) + + it('Verify LESS_BUSY strategy can be run in a fixed pool', async () => { + const pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY } + ) + // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose` + const promises = [] + for (let i = 0; i < max * 2; i++) { + promises.push(pool.execute()) + } + await Promise.all(promises) + // We need to clean up the resources after our test + await pool.destroy() + }) + + it('Verify LESS_BUSY strategy can be run in a dynamic pool', async () => { + const pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY } + ) + // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose` const promises = [] for (let i = 0; i < max * 2; i++) { promises.push(pool.execute()) @@ -202,16 +395,18 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.FAIR_SHARE ) - for (const worker of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) { + for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.keys()) { expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - worker - ).start + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.get(workerNodeKey).start ).toBe(0) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - worker - ).end + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.get(workerNodeKey).end ).toBe(0) } // We need to clean up the resources after our test @@ -238,9 +433,14 @@ describe('Selection strategies test suite', () => { ) pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) expect( - pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() - .requiredStatistics.runTime + 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, @@ -249,9 +449,14 @@ describe('Selection strategies test suite', () => { ) pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) expect( - pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() - .requiredStatistics.runTime + 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() }) @@ -268,6 +473,11 @@ describe('Selection strategies test suite', () => { promises.push(pool.execute()) } await Promise.all(promises) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.FAIR_SHARE + ).workerLastVirtualTaskTimestamp.size + ).toBe(pool.workerNodes.length) // We need to clean up the resources after our test await pool.destroy() }) @@ -281,34 +491,45 @@ 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) + // if (process.platform !== 'win32') { + // expect( + // pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + // WorkerChoiceStrategies.FAIR_SHARE + // ).workerLastVirtualTaskTimestamp.size + // ).toBe(pool.workerNodes.length) + // } // We need to clean up the resources after our test 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 - .workerLastVirtualTaskTimestamp - ).toBeUndefined() + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.FAIR_SHARE + ).workerLastVirtualTaskTimestamp + ).toBeDefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) - for (const worker of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) { + for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.keys()) { expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - worker - ).start + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.get(workerNodeKey).start ).toBe(0) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - worker - ).end + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.get(workerNodeKey).end ).toBe(0) } await pool.destroy() @@ -318,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 worker of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) { + for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.keys()) { expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - worker - ).start + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.get(workerNodeKey).start ).toBe(0) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get( - worker - ).end + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.FAIR_SHARE) + .workerLastVirtualTaskTimestamp.get(workerNodeKey).end ).toBe(0) } // We need to clean up the resources after our test @@ -347,11 +571,28 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN ) - for (const worker of pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.keys()) { + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).currentWorkerNodeId + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).defaultWorkerWeight + ).toBeGreaterThan(0) + for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .workersTaskRunTime.keys()) { + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .workersTaskRunTime.get(workerNodeKey).weight + ).toBeGreaterThan(0) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get( - worker - ).runTime + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .workersTaskRunTime.get(workerNodeKey).runTime ).toBe(0) } // We need to clean up the resources after our test @@ -378,9 +619,14 @@ describe('Selection strategies test suite', () => { ) pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) expect( - pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() - .requiredStatistics.runTime + 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, @@ -389,9 +635,14 @@ describe('Selection strategies test suite', () => { ) pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) expect( - pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() - .requiredStatistics.runTime + 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() }) @@ -408,6 +659,11 @@ describe('Selection strategies test suite', () => { promises.push(pool.execute()) } await Promise.all(promises) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).workersTaskRunTime.size + ).toBe(pool.workerNodes.length) // We need to clean up the resources after our test await pool.destroy() }) @@ -421,28 +677,63 @@ describe('Selection strategies test suite', () => { ) // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` const promises = [] - for (let i = 0; i < max * 2; i++) { + const maxMultiplier = + 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) + if (process.platform !== 'win32') { + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).workersTaskRunTime.size + ).toBe(pool.workerNodes.length) + } // We need to clean up the resources after our test 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 - ).toBeUndefined() + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).currentWorkerNodeId + ).toBeDefined() + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).defaultWorkerWeight + ).toBeDefined() + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).workersTaskRunTime + ).toBeDefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) - for (const worker of pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.keys()) { + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).currentWorkerNodeId + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).defaultWorkerWeight + ).toBeGreaterThan(0) + for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .workersTaskRunTime.keys()) { expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get( - worker - ).runTime + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .workersTaskRunTime.get(workerNodeKey).runTime ).toBe(0) } await pool.destroy() @@ -452,15 +743,38 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js' ) expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy - .workersTaskRunTime - ).toBeUndefined() + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).currentWorkerNodeId + ).toBeDefined() + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).defaultWorkerWeight + ).toBeDefined() + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).workersTaskRunTime + ).toBeDefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) - for (const worker of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workersTaskRunTime.keys()) { + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).currentWorkerNodeId + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ).defaultWorkerWeight + ).toBeGreaterThan(0) + for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .workersTaskRunTime.keys()) { expect( - pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workersTaskRunTime.get( - worker - ).runTime + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + .workersTaskRunTime.get(workerNodeKey).runTime ).toBe(0) } // We need to clean up the resources after our test @@ -477,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'") ) }) })