test: improve pool options coverage
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 19 Jun 2023 11:06:27 +0000 (13:06 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 19 Jun 2023 11:06:27 +0000 (13:06 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
CHANGELOG.md
src/pools/abstract-pool.ts
tests/pools/abstract/abstract-pool.test.js

index 9058bed670386b13d551213ac93b50187b5597be..f50ceeab81c0143eb9b755dfa6d5f2403cfec7d3 100644 (file)
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 ### Fixed
 
 - Ensure no tasks are queued when trying to soft kill a dynamic worker.
+- Update strategies internals after statistics computation.
 
 ### Changed
 
index a042e873fef452a603b9837630b323f0da1abaf7..9af3d707c35088356aae00f6192c1a716e9a8bf9 100644 (file)
@@ -29,6 +29,7 @@ import type {
   WorkerUsage
 } from './worker'
 import {
+  Measurements,
   WorkerChoiceStrategies,
   type WorkerChoiceStrategy,
   type WorkerChoiceStrategyOptions
@@ -199,6 +200,16 @@ export abstract class AbstractPool<
         'Invalid worker choice strategy options: must have a weight for each worker node'
       )
     }
+    if (
+      workerChoiceStrategyOptions.measurement != null &&
+      !Object.values(Measurements).includes(
+        workerChoiceStrategyOptions.measurement
+      )
+    ) {
+      throw new Error(
+        `Invalid worker choice strategy options: invalid measurement '${workerChoiceStrategyOptions.measurement}'`
+      )
+    }
   }
 
   private checkValidTasksQueueOptions (
@@ -207,11 +218,20 @@ export abstract class AbstractPool<
     if (tasksQueueOptions != null && !isPlainObject(tasksQueueOptions)) {
       throw new TypeError('Invalid tasks queue options: must be a plain object')
     }
-    if ((tasksQueueOptions?.concurrency as number) <= 0) {
+    if (
+      tasksQueueOptions?.concurrency != null &&
+      !Number.isSafeInteger(tasksQueueOptions.concurrency)
+    ) {
+      throw new TypeError(
+        'Invalid worker tasks concurrency: must be an integer'
+      )
+    }
+    if (
+      tasksQueueOptions?.concurrency != null &&
+      tasksQueueOptions.concurrency <= 0
+    ) {
       throw new Error(
-        `Invalid worker tasks concurrency '${
-          tasksQueueOptions.concurrency as number
-        }'`
+        `Invalid worker tasks concurrency '${tasksQueueOptions.concurrency}'`
       )
     }
   }
index 4b6f46a607b02b3f501d97b87acb588ffb0bf4c0..fc386f375513cbfe35e9eb220ee5598e1e562008 100644 (file)
@@ -148,21 +148,22 @@ describe('Abstract pool test suite', () => {
           numberOfWorkers,
           './tests/worker-files/thread/testWorker.js',
           {
-            enableTasksQueue: true,
-            tasksQueueOptions: { concurrency: 0 }
+            workerChoiceStrategy: 'invalidStrategy'
           }
         )
-    ).toThrowError("Invalid worker tasks concurrency '0'")
+    ).toThrowError("Invalid worker choice strategy 'invalidStrategy'")
     expect(
       () =>
         new FixedThreadPool(
           numberOfWorkers,
           './tests/worker-files/thread/testWorker.js',
           {
-            workerChoiceStrategy: 'invalidStrategy'
+            workerChoiceStrategyOptions: 'invalidOptions'
           }
         )
-    ).toThrowError("Invalid worker choice strategy 'invalidStrategy'")
+    ).toThrowError(
+      'Invalid worker choice strategy options: must be a plain object'
+    )
     expect(
       () =>
         new FixedThreadPool(
@@ -175,6 +176,51 @@ describe('Abstract pool test suite', () => {
     ).toThrowError(
       'Invalid worker choice strategy options: must have a weight for each worker node'
     )
+    expect(
+      () =>
+        new FixedThreadPool(
+          numberOfWorkers,
+          './tests/worker-files/thread/testWorker.js',
+          {
+            workerChoiceStrategyOptions: { measurement: 'invalidMeasurement' }
+          }
+        )
+    ).toThrowError(
+      "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'"
+    )
+    expect(
+      () =>
+        new FixedThreadPool(
+          numberOfWorkers,
+          './tests/worker-files/thread/testWorker.js',
+          {
+            enableTasksQueue: true,
+            tasksQueueOptions: { concurrency: 0 }
+          }
+        )
+    ).toThrowError("Invalid worker tasks concurrency '0'")
+    expect(
+      () =>
+        new FixedThreadPool(
+          numberOfWorkers,
+          './tests/worker-files/thread/testWorker.js',
+          {
+            enableTasksQueue: true,
+            tasksQueueOptions: 'invalidTasksQueueOptions'
+          }
+        )
+    ).toThrowError('Invalid tasks queue options: must be a plain object')
+    expect(
+      () =>
+        new FixedThreadPool(
+          numberOfWorkers,
+          './tests/worker-files/thread/testWorker.js',
+          {
+            enableTasksQueue: true,
+            tasksQueueOptions: { concurrency: 0.2 }
+          }
+        )
+    ).toThrowError('Invalid worker tasks concurrency: must be an integer')
   })
 
   it('Verify that worker choice strategy options can be set', async () => {
@@ -314,9 +360,15 @@ describe('Abstract pool test suite', () => {
     expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1 })
     pool.setTasksQueueOptions({ concurrency: 2 })
     expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 })
+    expect(() =>
+      pool.setTasksQueueOptions('invalidTasksQueueOptions')
+    ).toThrowError('Invalid tasks queue options: must be a plain object')
     expect(() => pool.setTasksQueueOptions({ concurrency: 0 })).toThrowError(
       "Invalid worker tasks concurrency '0'"
     )
+    expect(() => pool.setTasksQueueOptions({ concurrency: 0.2 })).toThrowError(
+      'Invalid worker tasks concurrency: must be an integer'
+    )
     await pool.destroy()
   })