fix: ensure the task concurrency is respected at queued task
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 21 Jul 2023 15:21:06 +0000 (17:21 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 21 Jul 2023 15:21:06 +0000 (17:21 +0200)
redistribution

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
tests/pools/abstract/abstract-pool.test.js

index 1222d686b847df02f2f9f90adced68d3cebb6b8f..0d3d21832a715191e6efad9723c6a47454ae2215 100644 (file)
@@ -173,7 +173,11 @@ export abstract class AbstractPool<
 
   protected checkDynamicPoolSize (min: number, max: number): void {
     if (this.type === PoolTypes.dynamic) {
-      if (!Number.isSafeInteger(max)) {
+      if (max == null) {
+        throw new Error(
+          'Cannot instantiate a dynamic pool without specifying the maximum pool size'
+        )
+      } else if (!Number.isSafeInteger(max)) {
         throw new TypeError(
           'Cannot instantiate a dynamic pool with a non safe integer maximum pool size'
         )
@@ -1008,7 +1012,10 @@ export abstract class AbstractPool<
           workerInfo.ready &&
           workerNode.usage.tasks.queued === 0
         ) {
-          if (workerNode.usage.tasks.executing === 0) {
+          if (
+            this.workerNodes[workerNodeId].usage.tasks.executing <
+            (this.opts.tasksQueueOptions?.concurrency as number)
+          ) {
             executeTask = true
           }
           targetWorkerNodeKey = workerNodeId
index 10736b6bcd1c49e4b7dbbd7d8231ca4c93f69599..2c904362a742eb8594a00a9bf216e7cb3707e455 100644 (file)
@@ -86,6 +86,18 @@ describe('Abstract pool test suite', () => {
   })
 
   it('Verify that dynamic pool sizing is checked', () => {
+    expect(
+      () =>
+        new DynamicClusterPool(
+          1,
+          undefined,
+          './tests/worker-files/cluster/testWorker.js'
+        )
+    ).toThrowError(
+      new TypeError(
+        'Cannot instantiate a dynamic pool without specifying the maximum pool size'
+      )
+    )
     expect(
       () =>
         new DynamicThreadPool(
@@ -103,7 +115,7 @@ describe('Abstract pool test suite', () => {
         new DynamicClusterPool(
           0,
           0.5,
-          './tests/worker-files/thread/testWorker.js'
+          './tests/worker-files/cluster/testWorker.js'
         )
     ).toThrowError(
       new TypeError(
@@ -123,7 +135,7 @@ describe('Abstract pool test suite', () => {
         new DynamicClusterPool(
           1,
           1,
-          './tests/worker-files/thread/testWorker.js'
+          './tests/worker-files/cluster/testWorker.js'
         )
     ).toThrowError(
       new RangeError(