fix: fix pool statuses semantic
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 11 Jul 2023 16:29:07 +0000 (18:29 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 11 Jul 2023 16:29:07 +0000 (18:29 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
tests/pools/abstract/abstract-pool.test.js

index a3b46cee0f52e0be63827488730e671c1f316a98..6047b2f78fe842cc6550ed653382d1b81951ce8d 100644 (file)
@@ -128,7 +128,13 @@ export abstract class AbstractPool<
 
     this.setupHook()
 
-    while (this.workerNodes.length < this.numberOfWorkers) {
+    while (
+      this.workerNodes.reduce(
+        (accumulator, workerNode) =>
+          !workerNode.info.dynamic ? accumulator + 1 : accumulator,
+        0
+      ) < this.numberOfWorkers
+    ) {
       this.createAndSetupWorker()
     }
 
@@ -172,9 +178,9 @@ export abstract class AbstractPool<
         throw new RangeError(
           'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size'
         )
-      } else if (min === 0 && max === 0) {
+      } else if (max === 0) {
         throw new RangeError(
-          'Cannot instantiate a dynamic pool with a minimum pool size and a maximum pool size equal to zero'
+          'Cannot instantiate a dynamic pool with a pool size equal to zero'
         )
       } else if (min === max) {
         throw new RangeError(
@@ -411,16 +417,24 @@ export abstract class AbstractPool<
   }
 
   private get starting (): boolean {
-    return this.workerNodes.length < this.minSize
+    return (
+      this.workerNodes.reduce(
+        (accumulator, workerNode) =>
+          !workerNode.info.dynamic ? accumulator + 1 : accumulator,
+        0
+      ) < this.minSize
+    )
   }
 
   private get ready (): boolean {
     return (
-      this.workerNodes.length >= this.minSize &&
-      this.workerNodes.every(
-        (workerNode, workerNodeKey) =>
-          workerNodeKey < this.minSize && workerNode.info.ready
-      )
+      this.workerNodes.reduce(
+        (accumulator, workerNode) =>
+          !workerNode.info.dynamic && workerNode.info.ready
+            ? accumulator + 1
+            : accumulator,
+        0
+      ) >= this.minSize
     )
   }
 
@@ -979,7 +993,9 @@ export abstract class AbstractPool<
       }
     })
     const workerInfo = this.getWorkerInfo(this.getWorkerNodeKey(worker))
-    workerInfo.ready = true
+    if (this.workerChoiceStrategyContext.getStrategyPolicy().useDynamicWorker) {
+      workerInfo.ready = true
+    }
     workerInfo.dynamic = true
     this.sendToWorker(worker, {
       checkAlive: true,
@@ -1135,7 +1151,7 @@ export abstract class AbstractPool<
    */
   private pushWorkerNode (worker: Worker): number {
     const workerNode = new WorkerNode<Worker, Data>(worker, this.worker)
-    // Flag the worker as ready at pool startup.
+    // Flag the worker node as ready at pool startup.
     if (this.starting) {
       workerNode.info.ready = true
     }
index c57a8f0ca6d371fab542fc578717afb58588d8e5..2c8d23184d4dfb49342cd4998670a1f0da1f7619 100644 (file)
@@ -106,7 +106,7 @@ describe('Abstract pool test suite', () => {
         new DynamicThreadPool(0, 0, './tests/worker-files/thread/testWorker.js')
     ).toThrowError(
       new RangeError(
-        'Cannot instantiate a dynamic pool with a minimum pool size and a maximum pool size equal to zero'
+        'Cannot instantiate a dynamic pool with a pool size equal to zero'
       )
     )
   })
@@ -655,7 +655,13 @@ describe('Abstract pool test suite', () => {
         }
       })
       expect(workerNode.usage.tasks.executed).toBeGreaterThan(0)
-      expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(maxMultiplier)
+      expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(
+        numberOfWorkers * maxMultiplier
+      )
+      expect(workerNode.usage.runTime.history.length).toBe(0)
+      expect(workerNode.usage.waitTime.history.length).toBe(0)
+      expect(workerNode.usage.elu.idle.history.length).toBe(0)
+      expect(workerNode.usage.elu.active.history.length).toBe(0)
     }
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
     for (const workerNode of pool.workerNodes) {
@@ -684,6 +690,8 @@ describe('Abstract pool test suite', () => {
       })
       expect(workerNode.usage.runTime.history.length).toBe(0)
       expect(workerNode.usage.waitTime.history.length).toBe(0)
+      expect(workerNode.usage.elu.idle.history.length).toBe(0)
+      expect(workerNode.usage.elu.active.history.length).toBe(0)
     }
     await pool.destroy()
   })