X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=tests%2Fpools%2Fselection-strategies%2Fselection-strategies.test.js;h=eb30d0e17753e668ea5d9df34a8cbc734dd85210;hb=9458090a8699203af83383930938b2a6ac53e29d;hp=19ac5ea3a9a930c67996efd31fb5a683aa856685;hpb=ee9f52959d4996f56712791e73994dfbeb0370f0;p=poolifier.git diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index 19ac5ea3..eb30d0e1 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -19,6 +19,9 @@ describe('Selection strategies test suite', () => { expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe( 'WEIGHTED_ROUND_ROBIN' ) + expect(WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN).toBe( + 'INTERLEAVED_WEIGHTED_ROUND_ROBIN' + ) }) it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => { @@ -164,7 +167,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: expect.any(Number), + run: maxMultiplier, running: 0, runTime: 0, runTimeHistory: expect.any(CircularArray), @@ -176,9 +179,12 @@ describe('Selection strategies test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) } + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.ROUND_ROBIN + ).nextWorkerNodeId + ).toBe(0) // We need to clean up the resources after our test await pool.destroy() }) @@ -199,7 +205,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: expect.any(Number), + run: maxMultiplier, running: 0, runTime: 0, runTimeHistory: expect.any(CircularArray), @@ -211,9 +217,12 @@ describe('Selection strategies test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) } + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + WorkerChoiceStrategies.ROUND_ROBIN + ).nextWorkerNodeId + ).toBe(0) // We need to clean up the resources after our test await pool.destroy() }) @@ -337,7 +346,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: expect.any(Number), + run: maxMultiplier, running: 0, runTime: 0, runTimeHistory: expect.any(CircularArray), @@ -349,8 +358,6 @@ describe('Selection strategies test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) } // We need to clean up the resources after our test await pool.destroy() @@ -372,7 +379,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: expect.any(Number), + run: maxMultiplier, running: 0, runTime: 0, runTimeHistory: expect.any(CircularArray), @@ -384,8 +391,6 @@ describe('Selection strategies test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) } // We need to clean up the resources after our test await pool.destroy() @@ -553,7 +558,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: expect.any(Number), + run: maxMultiplier, running: 0, runTime: expect.any(Number), runTimeHistory: expect.any(CircularArray), @@ -565,8 +570,6 @@ describe('Selection strategies test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) expect(workerNode.tasksUsage.runTime).toBeGreaterThan(0) expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThan(0) } @@ -595,7 +598,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: expect.any(Number), + run: maxMultiplier, running: 0, runTime: expect.any(Number), runTimeHistory: expect.any(CircularArray), @@ -607,8 +610,6 @@ describe('Selection strategies test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) expect(workerNode.tasksUsage.runTime).toBeGreaterThan(0) expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThan(0) } @@ -642,7 +643,7 @@ describe('Selection strategies test suite', () => { await Promise.all(promises) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksUsage).toStrictEqual({ - run: expect.any(Number), + run: maxMultiplier, running: 0, runTime: expect.any(Number), runTimeHistory: expect.any(CircularArray), @@ -654,8 +655,6 @@ describe('Selection strategies test suite', () => { medWaitTime: 0, error: 0 }) - expect(workerNode.tasksUsage.run).toBeGreaterThan(0) - expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier) expect(workerNode.tasksUsage.runTime).toBeGreaterThan(0) expect(workerNode.tasksUsage.medRunTime).toBeGreaterThan(0) } @@ -1003,6 +1002,164 @@ describe('Selection strategies test suite', () => { await pool.destroy() }) + it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => { + const workerChoiceStrategy = + WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN + let pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy } + ) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics() + ).toStrictEqual({ + runTime: false, + avgRunTime: false, + medRunTime: false, + waitTime: false, + avgWaitTime: false, + medWaitTime: false + }) + await pool.destroy() + pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy } + ) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics() + ).toStrictEqual({ + runTime: false, + avgRunTime: false, + medRunTime: false, + waitTime: false, + avgWaitTime: false, + medWaitTime: false + }) + // We need to clean up the resources after our test + await pool.destroy() + }) + + it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => { + const pool = new FixedThreadPool( + max, + './tests/worker-files/thread/testWorker.js', + { + workerChoiceStrategy: + WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN + } + ) + // TODO: Create a better test to cover `InterleavedWeightedRoundRobinWorkerChoiceStrategy#choose` + const promises = new Set() + const maxMultiplier = 2 + for (let i = 0; i < max * maxMultiplier; i++) { + promises.add(pool.execute()) + } + await Promise.all(promises) + for (const workerNode of pool.workerNodes) { + expect(workerNode.tasksUsage).toStrictEqual({ + run: maxMultiplier, + running: 0, + runTime: 0, + runTimeHistory: expect.any(CircularArray), + avgRunTime: 0, + medRunTime: 0, + waitTime: 0, + waitTimeHistory: expect.any(CircularArray), + avgWaitTime: 0, + medWaitTime: 0, + error: 0 + }) + } + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).defaultWorkerWeight + ).toBeGreaterThan(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).currentRoundId + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).currentWorkerNodeId + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).roundWeights + ).toStrictEqual([ + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).defaultWorkerWeight + ]) + // We need to clean up the resources after our test + await pool.destroy() + }) + + it('Verify INTERLEAVED_WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => { + const pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js', + { + workerChoiceStrategy: + WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN + } + ) + // TODO: Create a better test to cover `InterleavedWeightedRoundRobinWorkerChoiceStrategy#choose` + const promises = new Set() + const maxMultiplier = 2 + for (let i = 0; i < max * maxMultiplier; i++) { + promises.add(pool.execute()) + } + await Promise.all(promises) + for (const workerNode of pool.workerNodes) { + expect(workerNode.tasksUsage).toStrictEqual({ + run: maxMultiplier, + running: 0, + runTime: 0, + runTimeHistory: expect.any(CircularArray), + avgRunTime: 0, + medRunTime: 0, + waitTime: 0, + waitTimeHistory: expect.any(CircularArray), + avgWaitTime: 0, + medWaitTime: 0, + error: 0 + }) + } + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).defaultWorkerWeight + ).toBeGreaterThan(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).currentRoundId + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).currentWorkerNodeId + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).roundWeights + ).toStrictEqual([ + pool.workerChoiceStrategyContext.workerChoiceStrategies.get( + pool.workerChoiceStrategyContext.workerChoiceStrategy + ).defaultWorkerWeight + ]) + // We need to clean up the resources after our test + await pool.destroy() + }) + it('Verify unknown strategy throw error', () => { expect( () =>