From 8139fbbcb271b9479cbb81b21d45ee6183043e48 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 20 May 2024 21:24:07 +0200 Subject: [PATCH] test: improve coverage MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- tests/pools/abstract-pool.test.mjs | 69 +++++++++++++++++++ .../cluster/testTaskFunctionObjectsWorker.cjs | 21 ++++++ .../thread/testTaskFunctionObjectsWorker.mjs | 20 ++++++ 3 files changed, 110 insertions(+) create mode 100644 tests/worker-files/cluster/testTaskFunctionObjectsWorker.cjs create mode 100644 tests/worker-files/thread/testTaskFunctionObjectsWorker.mjs diff --git a/tests/pools/abstract-pool.test.mjs b/tests/pools/abstract-pool.test.mjs index e8dfcb8a..b862479b 100644 --- a/tests/pools/abstract-pool.test.mjs +++ b/tests/pools/abstract-pool.test.mjs @@ -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 index 00000000..cb81d15b --- /dev/null +++ b/tests/worker-files/cluster/testTaskFunctionObjectsWorker.cjs @@ -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 index 00000000..6b5a7eda --- /dev/null +++ b/tests/worker-files/thread/testTaskFunctionObjectsWorker.mjs @@ -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 + } +) -- 2.34.1