X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fselection-strategies%2Fselection-strategies.test.js;h=dde60e2685cdfd521708d3869fb935bb4e5cf691;hb=010d7020b316c0dd9a7a95a3ae1cba074ab2303e;hp=93649c39c2c667c8b5b518e9e1067abc8ad8b2cd;hpb=b3432a63039e7cb70c0448da5518690e457cd47e;p=poolifier.git diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index 93649c39..dde60e26 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -2,21 +2,25 @@ const { expect } = require('expect') const { WorkerChoiceStrategies, DynamicThreadPool, - FixedThreadPool + FixedThreadPool, + FixedClusterPool } = require('../../../lib/index') describe('Selection strategies test suite', () => { + const min = 0 + const max = 3 + 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' ) }) it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => { - const min = 0 - const max = 3 const pool = new DynamicThreadPool( min, max, @@ -29,36 +33,142 @@ describe('Selection strategies test suite', () => { await pool.destroy() }) - it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => { - const min = 0 - const max = 3 - const pool = new DynamicThreadPool( - min, + it('Verify available strategies are taken at pool creation', async () => { + for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) { + const pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy } + ) + expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy) + expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy + ) + await pool.destroy() + } + }) + + it('Verify available strategies can be set after pool creation', async () => { + for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) { + const pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js' + ) + pool.setWorkerChoiceStrategy(workerChoiceStrategy) + expect(pool.opts.workerChoiceStrategy).toBe(workerChoiceStrategy) + expect(pool.workerChoiceStrategyContext.workerChoiceStrategy).toBe( + workerChoiceStrategy + ) + await pool.destroy() + } + }) + + it('Verify available strategies default internals at pool creation', async () => { + const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) - expect(pool.opts.workerChoiceStrategy).toBe( - WorkerChoiceStrategies.ROUND_ROBIN + for (const workerChoiceStrategy of Object.values(WorkerChoiceStrategies)) { + if (workerChoiceStrategy === WorkerChoiceStrategies.ROUND_ROBIN) { + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ).nextWorkerNodeId + ).toBe(0) + } else if (workerChoiceStrategy === WorkerChoiceStrategies.FAIR_SHARE) { + for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(workerChoiceStrategy) + .workerLastVirtualTaskTimestamp.keys()) { + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(workerChoiceStrategy) + .workerLastVirtualTaskTimestamp.get(workerNodeKey).start + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(workerChoiceStrategy) + .workerLastVirtualTaskTimestamp.get(workerNodeKey).end + ).toBe(0) + } + } else if ( + workerChoiceStrategy === WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ) { + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ).currentWorkerNodeId + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ).defaultWorkerWeight + ).toBeGreaterThan(0) + for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(workerChoiceStrategy) + .workersTaskRunTime.keys()) { + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(workerChoiceStrategy) + .workersTaskRunTime.get(workerNodeKey).weight + ).toBeGreaterThan(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(workerChoiceStrategy) + .workersTaskRunTime.get(workerNodeKey).runTime + ).toBe(0) + } + } + } + await pool.destroy() + }) + + it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => { + const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN + let pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy } + ) + 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, + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy } ) + 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() }) it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => { - const max = 3 const pool = new FixedThreadPool( max, './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++) { - promises.push(pool.execute({ test: 'test' })) + promises.push(pool.execute()) } await Promise.all(promises) // We need to clean up the resources after our test @@ -66,121 +176,437 @@ describe('Selection strategies test suite', () => { }) it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => { - const min = 0 - const max = 3 const pool = new DynamicThreadPool( min, max, './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++) { - promises.push(pool.execute({ test: 'test' })) + promises.push(pool.execute()) } await Promise.all(promises) // 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 max = 3 - const pool = new FixedThreadPool( + it('Verify ROUND_ROBIN strategy runtime behavior', async () => { + const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN + let pool = new FixedClusterPool( + max, + './tests/worker-files/cluster/testWorker.js', + { workerChoiceStrategy } + ) + 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', - { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } + { workerChoiceStrategy } ) - expect(pool.opts.workerChoiceStrategy).toBe( - WorkerChoiceStrategies.LESS_RECENTLY_USED + 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 () => { + const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN + let pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } + ) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ).nextWorkerNodeId + ).toBeDefined() + pool.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).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( + workerChoiceStrategy + ).nextWorkerNodeId + ).toBeDefined() + pool.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).nextWorkerNodeId + ).toBe(0) + // We need to clean up the resources after our test + await pool.destroy() + }) + + it('Verify LESS_USED strategy default tasks usage statistics requirements', async () => { + const workerChoiceStrategy = WorkerChoiceStrategies.LESS_USED + let pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy } + ) + 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, + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy } ) + 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() }) - it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => { - const max = 3 + it('Verify LESS_USED strategy can be run in a fixed pool', async () => { const pool = new FixedThreadPool( max, - './tests/worker-files/thread/testWorker.js' + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED } ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED) - expect(pool.opts.workerChoiceStrategy).toBe( - WorkerChoiceStrategies.LESS_RECENTLY_USED + // 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_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_USED } ) + // 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_RECENTLY_USED strategy can be run in a fixed pool', async () => { - const max = 3 + it('Verify LESS_BUSY strategy default tasks usage statistics requirements', async () => { + const workerChoiceStrategy = WorkerChoiceStrategies.LESS_BUSY + let pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy } + ) + 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', + { workerChoiceStrategy } + ) + 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_RECENTLY_USED } + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY } ) - // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` + // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose` const promises = [] for (let i = 0; i < max * 2; i++) { - promises.push(pool.execute({ test: 'test' })) + promises.push(pool.execute()) } await Promise.all(promises) // We need to clean up the resources after our test await pool.destroy() }) - it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => { - const min = 0 - const max = 3 + 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_RECENTLY_USED } + { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY } ) - // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` + // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose` const promises = [] for (let i = 0; i < max * 2; i++) { - promises.push(pool.execute({ test: 'test' })) + promises.push(pool.execute()) } await Promise.all(promises) // We need to clean up the resources after our test await pool.destroy() }) - it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => { - const max = 3 - const pool = new FixedThreadPool( + it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => { + const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE + let pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', - { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } + { workerChoiceStrategy } ) - expect(pool.opts.workerChoiceStrategy).toBe( - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + 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, + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy } ) + 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() }) - it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => { - const max = 3 + it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => { const pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } + ) + // TODO: Create a better test to cover `FairShareChoiceStrategy#choose` + const promises = [] + for (let i = 0; i < max * 2; i++) { + promises.push(pool.execute()) + } + await Promise.all(promises) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).workerLastVirtualTaskTimestamp.size + ).toBe(pool.workerNodes.length) + // We need to clean up the resources after our test + await pool.destroy() + }) + + it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => { + const pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } + ) + // TODO: Create a better test to cover `FairShareChoiceStrategy#choose` + const promises = [] + 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( + // pool.workerChoiceStrategyContext.workerChoiceStrategy + // ).workerLastVirtualTaskTimestamp.size + // ).toBe(pool.workerNodes.length) + // } + // We need to clean up the resources after our test + await pool.destroy() + }) + + it('Verify FAIR_SHARE strategy can be run in a dynamic pool with median run time statistic', async () => { + const pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js', + { + workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE, + workerChoiceStrategyOptions: { + medRunTime: true + } + } + ) + // TODO: Create a better test to cover `FairShareChoiceStrategy#choose` + const promises = [] + const maxMultiplier = 2 + for (let i = 0; i < max * maxMultiplier; i++) { + promises.push(pool.execute()) + } + await Promise.all(promises) + for (const workerNode of pool.workerNodes) { + expect(workerNode.tasksUsage.avgRunTime).toBeDefined() + expect(workerNode.tasksUsage.avgRunTime).toBe(0) + expect(workerNode.tasksUsage.medRunTime).toBeDefined() + expect(workerNode.tasksUsage.medRunTime).toBeGreaterThan(0) + } + // if (process.platform !== 'win32') { + // expect( + // pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + // pool.workerChoiceStrategyContext.workerChoiceStrategy + // ).workerLastVirtualTaskTimestamp.size + // ).toBe(pool.workerNodes.length) + // } + // We need to clean up the resources after our test + await pool.destroy() + }) + + it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => { + const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE + let pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js' ) - pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) - expect(pool.opts.workerChoiceStrategy).toBe( - WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ).workerLastVirtualTaskTimestamp + ).toBeDefined() + pool.setWorkerChoiceStrategy(workerChoiceStrategy) + for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) + .workerLastVirtualTaskTimestamp.keys()) { + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) + .workerLastVirtualTaskTimestamp.get(workerNodeKey).start + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) + .workerLastVirtualTaskTimestamp.get(workerNodeKey).end + ).toBe(0) + } + await pool.destroy() + pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js' + ) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ).workerLastVirtualTaskTimestamp + ).toBeDefined() + pool.setWorkerChoiceStrategy(workerChoiceStrategy) + for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) + .workerLastVirtualTaskTimestamp.keys()) { + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) + .workerLastVirtualTaskTimestamp.get(workerNodeKey).start + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) + .workerLastVirtualTaskTimestamp.get(workerNodeKey).end + ).toBe(0) + } + // We need to clean up the resources after our test + await pool.destroy() + }) + + it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => { + const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + let pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy } ) + 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, + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy } + ) + 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() }) it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => { - const max = 3 const pool = new FixedThreadPool( max, './tests/worker-files/thread/testWorker.js', @@ -189,16 +615,19 @@ describe('Selection strategies test suite', () => { // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` const promises = [] for (let i = 0; i < max * 2; i++) { - promises.push(pool.execute({ test: 'test' })) + promises.push(pool.execute()) } await Promise.all(promises) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).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 can be run in a dynamic pool', async () => { - const min = 0 - const max = 3 const pool = new DynamicThreadPool( min, max, @@ -207,17 +636,148 @@ describe('Selection strategies test suite', () => { ) // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` const promises = [] - for (let i = 0; i < max * 2; i++) { - promises.push(pool.execute({ test: 'test' })) + const maxMultiplier = + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).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( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).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 can be run in a dynamic pool with median run time statistic', async () => { + const pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js', + { + workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN, + workerChoiceStrategyOptions: { + medRunTime: true + } + } + ) + // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` + const promises = [] + const maxMultiplier = 2 + for (let i = 0; i < max * maxMultiplier; i++) { + promises.push(pool.execute()) } await Promise.all(promises) + for (const workerNode of pool.workerNodes) { + expect(workerNode.tasksUsage.avgRunTime).toBeDefined() + expect(workerNode.tasksUsage.avgRunTime).toBe(0) + expect(workerNode.tasksUsage.medRunTime).toBeDefined() + expect(workerNode.tasksUsage.medRunTime).toBeGreaterThan(0) + } + // if (process.platform !== 'win32') { + // expect( + // pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + // pool.workerChoiceStrategyContext.workerChoiceStrategy + // ).workersTaskRunTime.size + // ).toBe(pool.workerNodes.length) + // } // We need to clean up the resources after our test await pool.destroy() }) - it('Verify unknown strategies throw error', () => { - const min = 1 - const max = 3 + it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => { + const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + let pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js' + ) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ).currentWorkerNodeId + ).toBeDefined() + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ).defaultWorkerWeight + ).toBeDefined() + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ).workersTaskRunTime + ).toBeDefined() + pool.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).currentWorkerNodeId + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).defaultWorkerWeight + ).toBeGreaterThan(0) + for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) + .workersTaskRunTime.keys()) { + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) + .workersTaskRunTime.get(workerNodeKey).runTime + ).toBe(0) + } + await pool.destroy() + pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js' + ) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ).currentWorkerNodeId + ).toBeDefined() + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ).defaultWorkerWeight + ).toBeDefined() + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + workerChoiceStrategy + ).workersTaskRunTime + ).toBeDefined() + pool.setWorkerChoiceStrategy(workerChoiceStrategy) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).currentWorkerNodeId + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).defaultWorkerWeight + ).toBeGreaterThan(0) + for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) + .workersTaskRunTime.keys()) { + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies + .get(pool.workerChoiceStrategyContext.workerChoiceStrategy) + .workersTaskRunTime.get(workerNodeKey).runTime + ).toBe(0) + } + // We need to clean up the resources after our test + await pool.destroy() + }) + + it('Verify unknown strategy throw error', () => { expect( () => new DynamicThreadPool( @@ -226,8 +786,6 @@ describe('Selection strategies test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy: 'UNKNOWN_STRATEGY' } ) - ).toThrowError( - new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found") - ) + ).toThrowError("Invalid worker choice strategy 'UNKNOWN_STRATEGY'") }) })