refactor: code cleanups
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 31 Dec 2023 20:43:25 +0000 (21:43 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 31 Dec 2023 20:43:25 +0000 (21:43 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/selection-strategies/abstract-worker-choice-strategy.ts
src/pools/selection-strategies/selection-strategies-types.ts
tests/pools/selection-strategies/worker-choice-strategy-context.test.mjs

index d8d8f32d2f9dc2eee45f63c75f2aa0ece167b108..c2928930724d34a19687bde75de870d1dc9c4afb 100644 (file)
@@ -121,11 +121,6 @@ export abstract class AbstractWorkerChoiceStrategy<
     this.setTaskStatisticsRequirements(this.opts)
   }
 
-  /** @inheritDoc */
-  public hasPoolWorkerNodesReady (): boolean {
-    return this.pool.workerNodes.some(workerNode => workerNode.info.ready)
-  }
-
   /**
    * Whether the worker node is ready or not.
    *
index d30f9eeb5c4007250643d03cf36fd26993586618..f499da7fc9d58458f9977910a18cc9246870ba7b 100644 (file)
@@ -202,10 +202,4 @@ export interface IWorkerChoiceStrategy {
    * @param opts - The worker choice strategy options.
    */
   readonly setOptions: (opts: WorkerChoiceStrategyOptions | undefined) => void
-  /**
-   * Whether the pool has worker nodes ready or not.
-   *
-   * @returns Whether the pool has worker nodes ready or not.
-   */
-  readonly hasPoolWorkerNodesReady: () => boolean
 }
index 77f1d4a1cfa6e63ae382df30d506e17701266576..eb854b58badd66a4a16b1fcd9c49b445d06f8c93 100644 (file)
@@ -60,33 +60,6 @@ describe('Worker choice strategy context test suite', () => {
     )
   })
 
-  it('Verify that execute() return the worker node key chosen by the strategy with fixed pool', () => {
-    const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
-      fixedPool
-    )
-    const workerChoiceStrategyStub = createStubInstance(
-      RoundRobinWorkerChoiceStrategy,
-      {
-        hasPoolWorkerNodesReady: stub().returns(true),
-        choose: stub().returns(0)
-      }
-    )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
-      WorkerChoiceStrategies.ROUND_ROBIN
-    )
-    workerChoiceStrategyContext.workerChoiceStrategies.set(
-      workerChoiceStrategyContext.workerChoiceStrategy,
-      workerChoiceStrategyStub
-    )
-    const chosenWorkerKey = workerChoiceStrategyContext.execute()
-    expect(
-      workerChoiceStrategyContext.workerChoiceStrategies.get(
-        workerChoiceStrategyContext.workerChoiceStrategy
-      ).choose.calledOnce
-    ).toBe(true)
-    expect(chosenWorkerKey).toBe(0)
-  })
-
   it('Verify that execute() throws error if null or undefined is returned after retries', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
@@ -97,7 +70,6 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyUndefinedStub = createStubInstance(
       RoundRobinWorkerChoiceStrategy,
       {
-        hasPoolWorkerNodesReady: stub().returns(true),
         choose: stub().returns(undefined)
       }
     )
@@ -113,7 +85,6 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyNullStub = createStubInstance(
       RoundRobinWorkerChoiceStrategy,
       {
-        hasPoolWorkerNodesReady: stub().returns(true),
         choose: stub().returns(null)
       }
     )
@@ -128,26 +99,25 @@ describe('Worker choice strategy context test suite', () => {
     )
   })
 
-  it('Verify that execute() retry until a worker node is ready and chosen', () => {
+  it('Verify that execute() retry until a worker node is chosen', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
     )
     const workerChoiceStrategyStub = createStubInstance(
       RoundRobinWorkerChoiceStrategy,
       {
-        hasPoolWorkerNodesReady: stub()
+        choose: stub()
           .onCall(0)
-          .returns(false)
+          .returns(undefined)
           .onCall(1)
-          .returns(false)
+          .returns(undefined)
           .onCall(2)
-          .returns(false)
+          .returns(undefined)
           .onCall(3)
-          .returns(false)
+          .returns(undefined)
           .onCall(4)
-          .returns(false)
-          .returns(true),
-        choose: stub().returns(1)
+          .returns(undefined)
+          .returns(1)
       }
     )
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
@@ -162,10 +132,36 @@ describe('Worker choice strategy context test suite', () => {
       workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategyContext.workerChoiceStrategy
       ).choose.callCount
-    ).toBe(1)
+    ).toBe(6)
     expect(chosenWorkerKey).toBe(1)
   })
 
+  it('Verify that execute() return the worker node key chosen by the strategy with fixed pool', () => {
+    const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+      fixedPool
+    )
+    const workerChoiceStrategyStub = createStubInstance(
+      RoundRobinWorkerChoiceStrategy,
+      {
+        choose: stub().returns(0)
+      }
+    )
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+      WorkerChoiceStrategies.ROUND_ROBIN
+    )
+    workerChoiceStrategyContext.workerChoiceStrategies.set(
+      workerChoiceStrategyContext.workerChoiceStrategy,
+      workerChoiceStrategyStub
+    )
+    const chosenWorkerKey = workerChoiceStrategyContext.execute()
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategies.get(
+        workerChoiceStrategyContext.workerChoiceStrategy
+      ).choose.calledOnce
+    ).toBe(true)
+    expect(chosenWorkerKey).toBe(0)
+  })
+
   it('Verify that execute() return the worker node key chosen by the strategy with dynamic pool', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       dynamicPool
@@ -173,7 +169,6 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyStub = createStubInstance(
       RoundRobinWorkerChoiceStrategy,
       {
-        hasPoolWorkerNodesReady: stub().returns(true),
         choose: stub().returns(0)
       }
     )