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),
--- /dev/null
+'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
+ }
+)
--- /dev/null
+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
+ }
+)