test: improve coverage
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 20 May 2024 19:24:07 +0000 (21:24 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Mon, 20 May 2024 19:24:07 +0000 (21:24 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
tests/pools/abstract-pool.test.mjs
tests/worker-files/cluster/testTaskFunctionObjectsWorker.cjs [new file with mode: 0644]
tests/worker-files/thread/testTaskFunctionObjectsWorker.mjs [new file with mode: 0644]

index e8dfcb8acbdbf6f1b10ea5a24da3516703e8fd83..b862479b82629bde97e5618cb7b11a3eb1c2a3f0 100644 (file)
@@ -1712,6 +1712,75 @@ describe('Abstract pool test suite', () => {
     await pool.destroy()
   })
 
+  it('Verify that task function objects worker is working', async () => {
+    const pool = new DynamicThreadPool(
+      Math.floor(numberOfWorkers / 2),
+      numberOfWorkers,
+      './tests/worker-files/thread/testTaskFunctionObjectsWorker.mjs'
+    )
+    const data = { n: 10 }
+    const result0 = await pool.execute(data)
+    expect(result0).toStrictEqual({ ok: 1 })
+    const result1 = await pool.execute(data, 'jsonIntegerSerialization')
+    expect(result1).toStrictEqual({ ok: 1 })
+    const result2 = await pool.execute(data, 'factorial')
+    expect(result2).toBe(3628800)
+    const result3 = await pool.execute(data, 'fibonacci')
+    expect(result3).toBe(55)
+    expect(pool.info.executingTasks).toBe(0)
+    expect(pool.info.executedTasks).toBe(4)
+    for (const workerNode of pool.workerNodes) {
+      expect(workerNode.info.taskFunctionsProperties).toStrictEqual([
+        { name: DEFAULT_TASK_NAME },
+        { name: 'jsonIntegerSerialization' },
+        { name: 'factorial' },
+        { name: 'fibonacci' }
+      ])
+      expect(workerNode.taskFunctionsUsage.size).toBe(3)
+      expect(workerNode.usage.tasks.executed).toBeGreaterThan(0)
+      for (const taskFunctionProperties of pool.listTaskFunctionsProperties()) {
+        expect(
+          workerNode.getTaskFunctionWorkerUsage(taskFunctionProperties.name)
+        ).toStrictEqual({
+          tasks: {
+            executed: expect.any(Number),
+            executing: 0,
+            failed: 0,
+            queued: 0,
+            sequentiallyStolen: 0,
+            stolen: 0
+          },
+          runTime: {
+            history: expect.any(CircularArray)
+          },
+          waitTime: {
+            history: expect.any(CircularArray)
+          },
+          elu: {
+            idle: {
+              history: expect.any(CircularArray)
+            },
+            active: {
+              history: expect.any(CircularArray)
+            }
+          }
+        })
+        expect(
+          workerNode.getTaskFunctionWorkerUsage(taskFunctionProperties.name)
+            .tasks.executed
+        ).toBeGreaterThan(0)
+      }
+      expect(
+        workerNode.getTaskFunctionWorkerUsage(DEFAULT_TASK_NAME)
+      ).toStrictEqual(
+        workerNode.getTaskFunctionWorkerUsage(
+          workerNode.info.taskFunctionsProperties[1].name
+        )
+      )
+    }
+    await pool.destroy()
+  })
+
   it('Verify sendKillMessageToWorker()', async () => {
     const pool = new DynamicClusterPool(
       Math.floor(numberOfWorkers / 2),
diff --git a/tests/worker-files/cluster/testTaskFunctionObjectsWorker.cjs b/tests/worker-files/cluster/testTaskFunctionObjectsWorker.cjs
new file mode 100644 (file)
index 0000000..cb81d15
--- /dev/null
@@ -0,0 +1,21 @@
+'use strict'
+const { KillBehaviors, ThreadWorker } = require('../../../lib/index.cjs')
+const {
+  factorial,
+  fibonacci,
+  jsonIntegerSerialization
+} = require('../../test-utils.cjs')
+
+module.exports = new ThreadWorker(
+  {
+    jsonIntegerSerialization: {
+      taskFunction: data => jsonIntegerSerialization(data.n)
+    },
+    factorial: { taskFunction: data => factorial(data.n) },
+    fibonacci: { taskFunction: data => fibonacci(data.n) }
+  },
+  {
+    killBehavior: KillBehaviors.HARD,
+    maxInactiveTime: 500
+  }
+)
diff --git a/tests/worker-files/thread/testTaskFunctionObjectsWorker.mjs b/tests/worker-files/thread/testTaskFunctionObjectsWorker.mjs
new file mode 100644 (file)
index 0000000..6b5a7ed
--- /dev/null
@@ -0,0 +1,20 @@
+import { KillBehaviors, ThreadWorker } from '../../../lib/index.cjs'
+import {
+  factorial,
+  fibonacci,
+  jsonIntegerSerialization
+} from '../../test-utils.cjs'
+
+export default new ThreadWorker(
+  {
+    jsonIntegerSerialization: {
+      taskFunction: data => jsonIntegerSerialization(data.n)
+    },
+    factorial: { taskFunction: data => factorial(data.n) },
+    fibonacci: { taskFunction: data => fibonacci(data.n) }
+  },
+  {
+    killBehavior: KillBehaviors.HARD,
+    maxInactiveTime: 500
+  }
+)