refactor: apply stricter strategy design pattern requirements on worker
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 5 Apr 2023 15:06:56 +0000 (17:06 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 5 Apr 2023 15:06:56 +0000 (17:06 +0200)
choice code

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
src/pools/selection-strategies/abstract-worker-choice-strategy.ts
src/pools/selection-strategies/fair-share-worker-choice-strategy.ts
src/pools/selection-strategies/less-busy-worker-choice-strategy.ts
src/pools/selection-strategies/less-used-worker-choice-strategy.ts
src/pools/selection-strategies/round-robin-worker-choice-strategy.ts
src/pools/selection-strategies/selection-strategies-types.ts
src/pools/selection-strategies/selection-strategies-utils.ts
src/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.ts
src/pools/selection-strategies/worker-choice-strategy-context.ts
tests/pools/selection-strategies/worker-choice-strategy-context.test.js

index fa1a8ba1382c131d97b2cab70d6d3e1df7bd0829..c1e6ddc09a9a41ce18d34ced63a162225f66818f 100644 (file)
@@ -179,6 +179,7 @@ export abstract class AbstractPool<
       })
     }
     this.workerChoiceStrategyContext.setWorkerChoiceStrategy(
+      this,
       workerChoiceStrategy
     )
   }
index aa370fe433e74065e4155d8612499abef5ec59fb..44e7e3f8b5ea01b7c707e0754f580b9181ea9a4b 100644 (file)
@@ -17,7 +17,7 @@ export abstract class AbstractWorkerChoiceStrategy<
   Worker extends IPoolWorker,
   Data,
   Response
-> implements IWorkerChoiceStrategy {
+> implements IWorkerChoiceStrategy<Worker, Data, Response> {
   /** {@inheritDoc} */
   public readonly isDynamicPool: boolean
   /** {@inheritDoc} */
@@ -32,7 +32,7 @@ export abstract class AbstractWorkerChoiceStrategy<
    * @param pool - The pool instance.
    */
   public constructor (
-    protected readonly pool: IPoolInternal<Worker, Data, Response>
+    public readonly pool: IPoolInternal<Worker, Data, Response>
   ) {
     this.isDynamicPool = this.pool.type === PoolType.DYNAMIC
     this.choose.bind(this)
index 7ef287484f1d00a0002a86a6fd977b99d514e183..1fe68ffae7a92b2397b22050bf12266143168540 100644 (file)
@@ -27,7 +27,7 @@ export class FairShareWorkerChoiceStrategy<
     Response
   >
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
-  implements IWorkerChoiceStrategy {
+  implements IWorkerChoiceStrategy<Worker, Data, Response> {
   /** {@inheritDoc} */
   public readonly requiredStatistics: RequiredStatistics = {
     runTime: true,
index 1d8bbaab5e8900af0187f0b938ab7c57b6d873b1..bddae82de6f7c6f4e4dbed953b5496430e2f071a 100644 (file)
@@ -18,7 +18,7 @@ export class LessBusyWorkerChoiceStrategy<
     Response
   >
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
-  implements IWorkerChoiceStrategy {
+  implements IWorkerChoiceStrategy<Worker, Data, Response> {
   /** {@inheritDoc} */
   public readonly requiredStatistics: RequiredStatistics = {
     runTime: true,
index 3eeb8329aebd14897508c745b3e21fa40499457c..f0632db86bbda11a8945d6670a965d621f04a673 100644 (file)
@@ -15,7 +15,7 @@ export class LessUsedWorkerChoiceStrategy<
     Response
   >
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
-  implements IWorkerChoiceStrategy {
+  implements IWorkerChoiceStrategy<Worker, Data, Response> {
   /** {@inheritDoc} */
   public reset (): boolean {
     return true
index 1499db945f8297181724db968f21b938d8f3618b..5ee219202aff6c7ec44f1af67b1264cdd16e8b40 100644 (file)
@@ -15,7 +15,7 @@ export class RoundRobinWorkerChoiceStrategy<
     Response
   >
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
-  implements IWorkerChoiceStrategy {
+  implements IWorkerChoiceStrategy<Worker, Data, Response> {
   /**
    * Id of the next worker.
    */
index ed6b010397b57e7980c916452c526ab7bb546e66..4fb2f3584aadffcc0f5224fdfb8ce6521a2979a8 100644 (file)
@@ -1,3 +1,6 @@
+import type { IPoolInternal } from '../pool-internal'
+import type { IPoolWorker } from '../pool-worker'
+
 /**
  * Enumeration of worker choice strategies.
  */
@@ -40,13 +43,24 @@ export interface RequiredStatistics {
 /**
  * Worker choice strategy interface.
  */
-export interface IWorkerChoiceStrategy {
+export interface IWorkerChoiceStrategy<
+  Worker extends IPoolWorker,
+  Data = unknown,
+  Response = unknown
+> {
+  /**
+   * The pool instance.
+   * @readonly
+   */
+  readonly pool: IPoolInternal<Worker, Data, Response>
   /**
    * Is the pool attached to the strategy dynamic?.
+   * @readonly
    */
   readonly isDynamicPool: boolean
   /**
    * Required pool tasks usage statistics.
+   * @readonly
    */
   readonly requiredStatistics: RequiredStatistics
   /**
index 10c93335ea98cd1c3b019addb88603ed2749672e..3db6456603b44b7bf3508a2bd375687f4a4d7e04 100644 (file)
@@ -25,7 +25,7 @@ export function getWorkerChoiceStrategy<
 > (
   pool: IPoolInternal<Worker, Data, Response>,
   workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
-): IWorkerChoiceStrategy {
+): IWorkerChoiceStrategy<Worker, Data, Response> {
   switch (workerChoiceStrategy) {
     case WorkerChoiceStrategies.ROUND_ROBIN:
       return new RoundRobinWorkerChoiceStrategy<Worker, Data, Response>(pool)
index 3709cce409b4ba988a728f81f0f8c6eb65f205f8..a8848f14ee80570f7c2d9e294d9d3552cfec3757 100644 (file)
@@ -29,7 +29,7 @@ export class WeightedRoundRobinWorkerChoiceStrategy<
     Response
   >
   extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
-  implements IWorkerChoiceStrategy {
+  implements IWorkerChoiceStrategy<Worker, Data, Response> {
   /** {@inheritDoc} */
   public readonly requiredStatistics: RequiredStatistics = {
     runTime: true,
index 8041e2f6577b7ca046c6e51048ed2a7d873923db..41b515b816d00dc991c27d8143efe80ec7c6116a 100644 (file)
@@ -20,7 +20,7 @@ export class WorkerChoiceStrategyContext<
   Data,
   Response
 > {
-  private workerChoiceStrategy!: IWorkerChoiceStrategy
+  private workerChoiceStrategy: IWorkerChoiceStrategy<Worker, Data, Response>
 
   /**
    * Worker choice strategy context constructor.
@@ -30,12 +30,15 @@ export class WorkerChoiceStrategyContext<
    * @param workerChoiceStrategy - The worker choice strategy.
    */
   public constructor (
-    private readonly pool: IPoolInternal<Worker, Data, Response>,
+    pool: IPoolInternal<Worker, Data, Response>,
     private readonly createWorkerCallback: () => number,
     workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
   ) {
     this.execute.bind(this)
-    this.setWorkerChoiceStrategy(workerChoiceStrategy)
+    this.workerChoiceStrategy = getWorkerChoiceStrategy<Worker, Data, Response>(
+      pool,
+      workerChoiceStrategy
+    )
   }
 
   /**
@@ -53,11 +56,12 @@ export class WorkerChoiceStrategyContext<
    * @param workerChoiceStrategy - The worker choice strategy to set.
    */
   public setWorkerChoiceStrategy (
+    pool: IPoolInternal<Worker, Data, Response>,
     workerChoiceStrategy: WorkerChoiceStrategy
   ): void {
     this.workerChoiceStrategy?.reset()
     this.workerChoiceStrategy = getWorkerChoiceStrategy<Worker, Data, Response>(
-      this.pool,
+      pool,
       workerChoiceStrategy
     )
   }
@@ -70,8 +74,8 @@ export class WorkerChoiceStrategyContext<
   public execute (): number {
     if (
       this.workerChoiceStrategy.isDynamicPool &&
-      !this.pool.full &&
-      this.pool.findFreeWorkerKey() === -1
+      !this.workerChoiceStrategy.pool.full &&
+      this.workerChoiceStrategy.pool.findFreeWorkerKey() === -1
     ) {
       return this.createWorkerCallback()
     }
index 3f65fe8226d62c570f67f548205673960d729af4..d1cad6a95ee8977c0fbeff958e2ab6f7a1d10c94 100644 (file)
@@ -91,6 +91,7 @@ describe('Worker choice strategy context test suite', () => {
       fixedPool
     )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
+      fixedPool,
       WorkerChoiceStrategies.ROUND_ROBIN
     )
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
@@ -103,6 +104,7 @@ describe('Worker choice strategy context test suite', () => {
       dynamicPool
     )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
+      dynamicPool,
       WorkerChoiceStrategies.ROUND_ROBIN
     )
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
@@ -115,6 +117,7 @@ describe('Worker choice strategy context test suite', () => {
       fixedPool
     )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
+      fixedPool,
       WorkerChoiceStrategies.LESS_USED
     )
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
@@ -127,6 +130,7 @@ describe('Worker choice strategy context test suite', () => {
       dynamicPool
     )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
+      dynamicPool,
       WorkerChoiceStrategies.LESS_USED
     )
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
@@ -139,6 +143,7 @@ describe('Worker choice strategy context test suite', () => {
       fixedPool
     )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
+      fixedPool,
       WorkerChoiceStrategies.LESS_BUSY
     )
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
@@ -151,6 +156,7 @@ describe('Worker choice strategy context test suite', () => {
       dynamicPool
     )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
+      dynamicPool,
       WorkerChoiceStrategies.LESS_BUSY
     )
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
@@ -163,6 +169,7 @@ describe('Worker choice strategy context test suite', () => {
       fixedPool
     )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
+      fixedPool,
       WorkerChoiceStrategies.FAIR_SHARE
     )
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
@@ -175,6 +182,7 @@ describe('Worker choice strategy context test suite', () => {
       dynamicPool
     )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
+      dynamicPool,
       WorkerChoiceStrategies.FAIR_SHARE
     )
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
@@ -187,6 +195,7 @@ describe('Worker choice strategy context test suite', () => {
       fixedPool
     )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
+      fixedPool,
       WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
     )
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
@@ -199,6 +208,7 @@ describe('Worker choice strategy context test suite', () => {
       dynamicPool
     )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
+      dynamicPool,
       WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
     )
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(