From: Jérôme Benoit Date: Wed, 5 Apr 2023 17:36:58 +0000 (+0200) Subject: refactor: conditionally reset strategy internals X-Git-Tag: v2.4.2~13 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=b2b1d84ebeb8299fda9b6090c9951e953749bbe0;p=poolifier.git refactor: conditionally reset strategy internals 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 44e7e3f8..5b4a2fb5 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -15,8 +15,8 @@ import type { */ export abstract class AbstractWorkerChoiceStrategy< Worker extends IPoolWorker, - Data, - Response + Data = unknown, + Response = unknown > implements IWorkerChoiceStrategy { /** {@inheritDoc} */ public readonly isDynamicPool: boolean 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 1fe68ffa..60c22a29 100644 --- a/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/fair-share-worker-choice-strategy.ts @@ -23,8 +23,8 @@ interface WorkerVirtualTaskTimestamp { */ export class FairShareWorkerChoiceStrategy< Worker extends IPoolWorker, - Data, - Response + Data = unknown, + Response = unknown > extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { diff --git a/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts b/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts index bddae82d..9952a41d 100644 --- a/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-busy-worker-choice-strategy.ts @@ -14,8 +14,8 @@ import type { */ export class LessBusyWorkerChoiceStrategy< Worker extends IPoolWorker, - Data, - Response + Data = unknown, + Response = unknown > extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { diff --git a/src/pools/selection-strategies/less-used-worker-choice-strategy.ts b/src/pools/selection-strategies/less-used-worker-choice-strategy.ts index f0632db8..01332caf 100644 --- a/src/pools/selection-strategies/less-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/less-used-worker-choice-strategy.ts @@ -11,8 +11,8 @@ import type { IWorkerChoiceStrategy } from './selection-strategies-types' */ export class LessUsedWorkerChoiceStrategy< Worker extends IPoolWorker, - Data, - Response + Data = unknown, + Response = unknown > extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { 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 5ee21920..9b49d764 100644 --- a/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/round-robin-worker-choice-strategy.ts @@ -11,8 +11,8 @@ import type { IWorkerChoiceStrategy } from './selection-strategies-types' */ export class RoundRobinWorkerChoiceStrategy< Worker extends IPoolWorker, - Data, - Response + Data = unknown, + Response = unknown > extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { diff --git a/src/pools/selection-strategies/selection-strategies-utils.ts b/src/pools/selection-strategies/selection-strategies-utils.ts index 3db64566..b0967d09 100644 --- a/src/pools/selection-strategies/selection-strategies-utils.ts +++ b/src/pools/selection-strategies/selection-strategies-utils.ts @@ -20,8 +20,8 @@ import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-w */ export function getWorkerChoiceStrategy< Worker extends IPoolWorker, - Data, - Response + Data = unknown, + Response = unknown > ( pool: IPoolInternal, workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN 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 a8848f14..c700c42a 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 @@ -25,8 +25,8 @@ interface TaskRunTime { */ export class WeightedRoundRobinWorkerChoiceStrategy< Worker extends IPoolWorker, - Data, - Response + Data = unknown, + Response = unknown > extends AbstractWorkerChoiceStrategy implements IWorkerChoiceStrategy { diff --git a/src/pools/selection-strategies/worker-choice-strategy-context.ts b/src/pools/selection-strategies/worker-choice-strategy-context.ts index 41b515b8..605cce38 100644 --- a/src/pools/selection-strategies/worker-choice-strategy-context.ts +++ b/src/pools/selection-strategies/worker-choice-strategy-context.ts @@ -17,8 +17,8 @@ import { getWorkerChoiceStrategy } from './selection-strategies-utils' */ export class WorkerChoiceStrategyContext< Worker extends IPoolWorker, - Data, - Response + Data = unknown, + Response = unknown > { private workerChoiceStrategy: IWorkerChoiceStrategy @@ -32,12 +32,12 @@ export class WorkerChoiceStrategyContext< public constructor ( pool: IPoolInternal, private readonly createWorkerCallback: () => number, - workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN + private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN ) { this.execute.bind(this) this.workerChoiceStrategy = getWorkerChoiceStrategy( pool, - workerChoiceStrategy + workerChoiceStrategyType ) } @@ -59,11 +59,16 @@ export class WorkerChoiceStrategyContext< pool: IPoolInternal, workerChoiceStrategy: WorkerChoiceStrategy ): void { - this.workerChoiceStrategy?.reset() - this.workerChoiceStrategy = getWorkerChoiceStrategy( - pool, - workerChoiceStrategy - ) + if (this.workerChoiceStrategyType === workerChoiceStrategy) { + this.workerChoiceStrategy?.reset() + } else { + this.workerChoiceStrategyType = workerChoiceStrategy + this.workerChoiceStrategy = getWorkerChoiceStrategy< + Worker, + Data, + Response + >(pool, this.workerChoiceStrategyType) + } } /** diff --git a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js index d1cad6a9..bcd84208 100644 --- a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js +++ b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js @@ -90,6 +90,12 @@ describe('Worker choice strategy context test suite', () => { const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( fixedPool ) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( + RoundRobinWorkerChoiceStrategy + ) + expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + WorkerChoiceStrategies.ROUND_ROBIN + ) workerChoiceStrategyContext.setWorkerChoiceStrategy( fixedPool, WorkerChoiceStrategies.ROUND_ROBIN @@ -97,12 +103,21 @@ describe('Worker choice strategy context test suite', () => { expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( RoundRobinWorkerChoiceStrategy ) + expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + WorkerChoiceStrategies.ROUND_ROBIN + ) }) it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => { const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( dynamicPool ) + expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( + RoundRobinWorkerChoiceStrategy + ) + expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + WorkerChoiceStrategies.ROUND_ROBIN + ) workerChoiceStrategyContext.setWorkerChoiceStrategy( dynamicPool, WorkerChoiceStrategies.ROUND_ROBIN @@ -110,6 +125,9 @@ describe('Worker choice strategy context test suite', () => { expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( RoundRobinWorkerChoiceStrategy ) + expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + WorkerChoiceStrategies.ROUND_ROBIN + ) }) it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => { @@ -123,6 +141,9 @@ describe('Worker choice strategy context test suite', () => { expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( LessUsedWorkerChoiceStrategy ) + expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + WorkerChoiceStrategies.LESS_USED + ) }) it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => { @@ -136,6 +157,9 @@ describe('Worker choice strategy context test suite', () => { expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( LessUsedWorkerChoiceStrategy ) + expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + WorkerChoiceStrategies.LESS_USED + ) }) it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => { @@ -149,6 +173,9 @@ describe('Worker choice strategy context test suite', () => { expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( LessBusyWorkerChoiceStrategy ) + expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + WorkerChoiceStrategies.LESS_BUSY + ) }) it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => { @@ -162,6 +189,9 @@ describe('Worker choice strategy context test suite', () => { expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( LessBusyWorkerChoiceStrategy ) + expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + WorkerChoiceStrategies.LESS_BUSY + ) }) it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => { @@ -175,6 +205,9 @@ describe('Worker choice strategy context test suite', () => { expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( FairShareWorkerChoiceStrategy ) + expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + WorkerChoiceStrategies.FAIR_SHARE + ) }) it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => { @@ -188,6 +221,9 @@ describe('Worker choice strategy context test suite', () => { expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( FairShareWorkerChoiceStrategy ) + expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + WorkerChoiceStrategies.FAIR_SHARE + ) }) it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => { @@ -201,6 +237,9 @@ describe('Worker choice strategy context test suite', () => { expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( WeightedRoundRobinWorkerChoiceStrategy ) + expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ) }) it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => { @@ -214,5 +253,8 @@ describe('Worker choice strategy context test suite', () => { expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( WeightedRoundRobinWorkerChoiceStrategy ) + expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe( + WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN + ) }) })