test: add test for worker task function object
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 28 Apr 2024 18:20:52 +0000 (20:20 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Sun, 28 Apr 2024 18:20:52 +0000 (20:20 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/selection-strategies/worker-choice-strategies-context.ts
src/pools/utils.ts
src/worker/utils.ts
tests/worker/abstract-worker.test.mjs

index 2650dd373ddf2b4ffe1395553993ba1792ad1ecd..6b9d726d14b09c7947cc915039b83d712da79a8c 100644 (file)
@@ -72,7 +72,10 @@ export class WorkerChoiceStrategiesContext<
       this.addWorkerChoiceStrategy(workerChoiceStrategy, this.pool, opts)
     }
     this.retriesCount = 0
-    this.retries = getWorkerChoiceStrategiesRetries(this.pool, opts)
+    this.retries = getWorkerChoiceStrategiesRetries<Worker, Data, Response>(
+      this.pool,
+      opts
+    )
   }
 
   /**
index 6a6c1fa9d3aa9d14946d760e09301ad878ce901a..36bd8939fdf82155c208d966d502d3098f11ffe3 100644 (file)
@@ -87,6 +87,19 @@ export const checkDynamicPoolSize = (
   }
 }
 
+export const checkValidPriority = (priority: number | undefined): void => {
+  if (priority != null && !Number.isSafeInteger(priority)) {
+    throw new TypeError(`Invalid priority '${priority}'`)
+  }
+  if (
+    priority != null &&
+    Number.isSafeInteger(priority) &&
+    (priority < -20 || priority > 19)
+  ) {
+    throw new RangeError('Property priority must be between -20 and 19')
+  }
+}
+
 export const checkValidWorkerChoiceStrategy = (
   workerChoiceStrategy: WorkerChoiceStrategy | undefined
 ): void => {
index b5c1725a776572116cef3e51a0c9e477d43c45d6..d0883893581b0a14c24cb415bef585541b71c446 100644 (file)
@@ -1,4 +1,7 @@
-import { checkValidWorkerChoiceStrategy } from '../pools/utils.js'
+import {
+  checkValidPriority,
+  checkValidWorkerChoiceStrategy
+} from '../pools/utils.js'
 import { isPlainObject } from '../utils.js'
 import type { TaskFunctionObject } from './task-functions.js'
 import { KillBehaviors, type WorkerOptions } from './worker-options.js'
@@ -54,11 +57,7 @@ export const checkValidTaskFunctionObjectEntry = <
       `taskFunction object 'taskFunction' property '${fnObj.taskFunction}' is not a function`
     )
   }
-  if (fnObj.priority != null && !Number.isSafeInteger(fnObj.priority)) {
-    throw new TypeError(
-      `taskFunction object 'priority' property '${fnObj.priority}' is not an integer`
-    )
-  }
+  checkValidPriority(fnObj.priority)
   checkValidWorkerChoiceStrategy(fnObj.strategy)
 }
 
index cb91f6963fb92c5275e7d396d1661c22e5ea7b45..ee89911ce5b15bbe83075b94db38a3d1769fc370 100644 (file)
@@ -1,7 +1,12 @@
 import { expect } from 'expect'
 import { restore, stub } from 'sinon'
 
-import { ClusterWorker, KillBehaviors, ThreadWorker } from '../../lib/index.cjs'
+import {
+  ClusterWorker,
+  KillBehaviors,
+  ThreadWorker,
+  WorkerChoiceStrategies
+} from '../../lib/index.cjs'
 import { DEFAULT_TASK_NAME, EMPTY_FUNCTION } from '../../lib/utils.cjs'
 
 describe('Abstract worker test suite', () => {
@@ -153,6 +158,33 @@ describe('Abstract worker test suite', () => {
         "taskFunction object 'taskFunction' property 'undefined' is not a function"
       )
     )
+    expect(() => new ThreadWorker({ fn1: { fn1 } })).toThrow(
+      new TypeError(
+        "taskFunction object 'taskFunction' property 'undefined' is not a function"
+      )
+    )
+    expect(() => new ThreadWorker({ fn2: { taskFunction: fn2 } })).toThrow(
+      new TypeError(
+        "taskFunction object 'taskFunction' property '' is not a function"
+      )
+    )
+    expect(
+      () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: '' } })
+    ).toThrow(new TypeError("Invalid priority ''"))
+    expect(
+      () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: -21 } })
+    ).toThrow(new TypeError('Property priority must be between -20 and 19'))
+    expect(
+      () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: 20 } })
+    ).toThrow(new RangeError('Property priority must be between -20 and 19'))
+    expect(
+      () =>
+        new ThreadWorker({
+          fn1: { taskFunction: fn1, strategy: 'invalidStrategy' }
+        })
+    ).toThrow(
+      new RangeError("Invalid worker choice strategy 'invalidStrategy'")
+    )
   })
 
   it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
@@ -172,6 +204,33 @@ describe('Abstract worker test suite', () => {
     )
   })
 
+  it('Verify that taskFunctions parameter with multiple task functions object is taken', () => {
+    const fn1Obj = {
+      taskFunction: () => {
+        return 1
+      },
+      priority: 5
+    }
+    const fn2Obj = {
+      taskFunction: () => {
+        return 2
+      },
+      priority: 6,
+      strategy: WorkerChoiceStrategies.LESS_BUSY
+    }
+    const worker = new ThreadWorker({
+      fn1: fn1Obj,
+      fn2: fn2Obj
+    })
+    expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(fn1Obj)
+    expect(worker.taskFunctions.get('fn1')).toStrictEqual(fn1Obj)
+    expect(worker.taskFunctions.get('fn2')).toStrictEqual(fn2Obj)
+    expect(worker.taskFunctions.size).toBe(3)
+    expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual(
+      worker.taskFunctions.get('fn1')
+    )
+  })
+
   it('Verify that async kill handler is called when worker is killed', () => {
     const killHandlerStub = stub().returns()
     const worker = new ClusterWorker(() => {}, {