From: Jérôme Benoit Date: Thu, 13 Oct 2022 18:57:25 +0000 (+0200) Subject: Fix strategies internals reset X-Git-Tag: v2.3.1~10 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=a6f7f1b40b3591b19a58f69b22e751c4c4311522;p=poolifier.git Fix strategies internals reset Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index d624bdd2..3ba59b42 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -35,7 +35,7 @@ export abstract class AbstractWorkerChoiceStrategy< ) {} /** @inheritDoc */ - public abstract resetStatistics (): boolean + public abstract reset (): boolean /** @inheritDoc */ public abstract choose (): Worker diff --git a/src/pools/selection-strategies/dynamic-pool-worker-choice-strategy.ts b/src/pools/selection-strategies/dynamic-pool-worker-choice-strategy.ts index 8d8b8518..81f3df51 100644 --- a/src/pools/selection-strategies/dynamic-pool-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/dynamic-pool-worker-choice-strategy.ts @@ -43,8 +43,8 @@ export class DynamicPoolWorkerChoiceStrategy< } /** @inheritDoc */ - public resetStatistics (): boolean { - return this.workerChoiceStrategy.resetStatistics() + public reset (): boolean { + return this.workerChoiceStrategy.reset() } /** @inheritDoc */ diff --git a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts index ee9351ec..3735f972 100644 --- a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts @@ -37,7 +37,7 @@ export class FairShareWorkerChoiceStrategy< > = new Map() /** @inheritDoc */ - public resetStatistics (): boolean { + public reset (): boolean { this.workerLastVirtualTaskTimestamp.clear() return true } diff --git a/src/pools/selection-strategies/less-recently-used-worker-choice-strategy.ts b/src/pools/selection-strategies/less-recently-used-worker-choice-strategy.ts index 0e2a2bf4..342ca6eb 100644 --- a/src/pools/selection-strategies/less-recently-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-recently-used-worker-choice-strategy.ts @@ -14,7 +14,7 @@ export class LessRecentlyUsedWorkerChoiceStrategy< Response > extends AbstractWorkerChoiceStrategy { /** @inheritDoc */ - public resetStatistics (): boolean { + public reset (): boolean { return true } diff --git a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts index 082c7d21..0a9fbb7b 100644 --- a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts @@ -19,7 +19,8 @@ export class RoundRobinWorkerChoiceStrategy< private nextWorkerIndex: number = 0 /** @inheritDoc */ - public resetStatistics (): boolean { + public reset (): boolean { + this.nextWorkerIndex = 0 return true } diff --git a/src/pools/selection-strategies/selection-strategies-types.ts b/src/pools/selection-strategies/selection-strategies-types.ts index f875c290..499b8e48 100644 --- a/src/pools/selection-strategies/selection-strategies-types.ts +++ b/src/pools/selection-strategies/selection-strategies-types.ts @@ -49,9 +49,9 @@ export interface IWorkerChoiceStrategy { */ readonly requiredStatistics: RequiredStatistics /** - * Resets strategy internal statistics. + * Resets strategy internals (counters, statistics, etc.). */ - resetStatistics(): boolean + reset(): boolean /** * Chooses a worker in the pool. */ diff --git a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts index 925f318c..752ae933 100644 --- a/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts @@ -62,7 +62,9 @@ export class WeightedRoundRobinWorkerChoiceStrategy< } /** @inheritDoc */ - public resetStatistics (): boolean { + public reset (): boolean { + this.previousWorkerIndex = 0 + this.currentWorkerIndex = 0 this.workersTaskRunTime.clear() this.initWorkersTaskRunTime() return true diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index 49144475..836a1cb5 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -77,7 +77,7 @@ export class WorkerChoiceStrategyContext< public setWorkerChoiceStrategy ( workerChoiceStrategy: WorkerChoiceStrategy ): void { - this.workerChoiceStrategy?.resetStatistics() + this.workerChoiceStrategy?.reset() this.workerChoiceStrategy = this.getPoolWorkerChoiceStrategy( workerChoiceStrategy ) diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index faa28a29..d9a01684 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -125,6 +125,32 @@ describe('Selection strategies test suite', () => { 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 } + ) + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy().nextWorkerIndex + ).toBe(0) + await pool.destroy() + pool = new DynamicThreadPool( + min, + max, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } + ) + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerChoiceStrategy.nextWorkerIndex + ).toBe(0) + // 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 pool = new FixedThreadPool( max, @@ -307,7 +333,7 @@ describe('Selection strategies test suite', () => { 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' @@ -470,7 +496,7 @@ describe('Selection strategies test suite', () => { 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' @@ -480,6 +506,18 @@ describe('Selection strategies test suite', () => { .workersTaskRunTime ).toBeUndefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .previousWorkerIndex + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .currentWorkerIndex + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .defaultWorkerWeight + ).toBeGreaterThan(0) for (const worker of pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() .workersTaskRunTime.keys()) { @@ -500,6 +538,18 @@ describe('Selection strategies test suite', () => { .workerChoiceStrategy.workersTaskRunTime ).toBeUndefined() pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerChoiceStrategy.previousWorkerIndex + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerChoiceStrategy.currentWorkerIndex + ).toBe(0) + expect( + pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() + .workerChoiceStrategy.defaultWorkerWeight + ).toBeGreaterThan(0) for (const worker of pool.workerChoiceStrategyContext .getWorkerChoiceStrategy() .workerChoiceStrategy.workersTaskRunTime.keys()) {