X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fselection-strategies%2Fselection-strategies.test.js;h=a62571797c08010ef6da62b947c5a81e67af1e84;hb=c923ce5670eeae4194aa996d44a1071e88cb21ad;hp=c55b971edb8a9ed3e47ccae1db4b0deff206b08b;hpb=5a94e4b950eaf2234e07f87261ddea1482e839c6;p=poolifier.git diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index c55b971e..a6257179 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -12,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' @@ -133,14 +134,14 @@ describe('Selection strategies test suite', () => { ) let results = new Set() for (let i = 0; i < max; i++) { - results.add(pool.chooseWorker().id) + results.add(pool.chooseWorker()[1].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) + results.add(pool.chooseWorker()[1].threadId) } expect(results.size).toBe(max) await pool.destroy() @@ -179,38 +180,38 @@ describe('Selection strategies test suite', () => { await pool.destroy() }) - it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => { + 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.getWorkerChoiceStrategy() .requiredStatistics.runTime @@ -221,7 +222,7 @@ 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.getWorkerChoiceStrategy() .requiredStatistics.runTime @@ -230,13 +231,13 @@ describe('Selection strategies test suite', () => { 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()) @@ -246,14 +247,98 @@ 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.getWorkerChoiceStrategy() + .requiredStatistics.runTime + ).toBe(true) + await pool.destroy() + pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js' + ) + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_BUSY) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .requiredStatistics.runTime + ).toBe(true) + // 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()) @@ -272,18 +357,18 @@ describe('Selection strategies test suite', () => { expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.FAIR_SHARE ) - for (const worker of pool.workerChoiceStrategyContext + for (const workerKey of pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() .workerLastVirtualTaskTimestamp.keys()) { expect( pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() - .workerLastVirtualTaskTimestamp.get(worker).start + .workerLastVirtualTaskTimestamp.get(workerKey).start ).toBe(0) expect( pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() - .workerLastVirtualTaskTimestamp.get(worker).end + .workerLastVirtualTaskTimestamp.get(workerKey).end ).toBe(0) } // We need to clean up the resources after our test @@ -371,18 +456,18 @@ describe('Selection strategies test suite', () => { .workerLastVirtualTaskTimestamp ).toBeUndefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) - for (const worker of pool.workerChoiceStrategyContext + for (const workerKey of pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() .workerLastVirtualTaskTimestamp.keys()) { expect( pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() - .workerLastVirtualTaskTimestamp.get(worker).start + .workerLastVirtualTaskTimestamp.get(workerKey).start ).toBe(0) expect( pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() - .workerLastVirtualTaskTimestamp.get(worker).end + .workerLastVirtualTaskTimestamp.get(workerKey).end ).toBe(0) } await pool.destroy() @@ -396,18 +481,20 @@ describe('Selection strategies test suite', () => { .workerChoiceStrategy.workerLastVirtualTaskTimestamp ).toBeUndefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) - for (const worker of pool.workerChoiceStrategyContext + for (const workerKey of pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() .workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) { expect( pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() - .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(worker).start + .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(workerKey) + .start ).toBe(0) expect( pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() - .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(worker).end + .workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(workerKey) + .end ).toBe(0) } // We need to clean up the resources after our test @@ -430,18 +517,18 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .defaultWorkerWeight ).toBeGreaterThan(0) - for (const worker of pool.workerChoiceStrategyContext + for (const workerKey of pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() .workersTaskRunTime.keys()) { expect( pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() - .workersTaskRunTime.get(worker).weight + .workersTaskRunTime.get(workerKey).weight ).toBeGreaterThan(0) expect( pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() - .workersTaskRunTime.get(worker).runTime + .workersTaskRunTime.get(workerKey).runTime ).toBe(0) } // We need to clean up the resources after our test @@ -543,13 +630,13 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .defaultWorkerWeight ).toBeGreaterThan(0) - for (const worker of pool.workerChoiceStrategyContext + for (const workerKey of pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() .workersTaskRunTime.keys()) { expect( pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() - .workersTaskRunTime.get(worker).runTime + .workersTaskRunTime.get(workerKey).runTime ).toBe(0) } await pool.destroy() @@ -579,13 +666,13 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .workerChoiceStrategy.defaultWorkerWeight ).toBeGreaterThan(0) - for (const worker of pool.workerChoiceStrategyContext + for (const workerKey of pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() .workerChoiceStrategy.workersTaskRunTime.keys()) { expect( pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() - .workerChoiceStrategy.workersTaskRunTime.get(worker).runTime + .workerChoiceStrategy.workersTaskRunTime.get(workerKey).runTime ).toBe(0) } // We need to clean up the resources after our test