From 30500265f2a78b3d472c9c1d0ca5e006306113aa Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 17 Sep 2023 22:09:28 +0200 Subject: [PATCH] test: add test for pool side hasTaskFunction() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- docs/api.md | 14 +++++++--- tests/pools/abstract/abstract-pool.test.js | 32 +++++++++++++++++++++- tests/worker/abstract-worker.test.js | 2 +- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/docs/api.md b/docs/api.md index 7bddac43..d2cc2ba7 100644 --- a/docs/api.md +++ b/docs/api.md @@ -51,6 +51,12 @@ This method is available on both pool implementations and will start the minimum This method is available on both pool implementations and will call the terminate method on each worker. +### `pool.hasTaskFunction(name)` + +`name` (mandatory) The task function name + +This method is available on both pool implementations and returns a boolean. + ### `pool.listTaskFunctionNames()` This method is available on both pool implementations and returns an array of the task function names. @@ -148,20 +154,20 @@ An object with these properties: `name` (mandatory) The task function name -This method is available on both worker implementations and returns a boolean. +This method is available on both worker implementations and returns `{ status: boolean, error?: Error }`. #### `YourWorker.addTaskFunction(name, fn)` `name` (mandatory) The task function name `fn` (mandatory) The task function -This method is available on both worker implementations and returns a boolean. +This method is available on both worker implementations and returns `{ status: boolean, error?: Error }`. #### `YourWorker.removeTaskFunction(name)` `name` (mandatory) The task function name -This method is available on both worker implementations and returns a boolean. +This method is available on both worker implementations and returns `{ status: boolean, error?: Error }`. #### `YourWorker.listTaskFunctionNames()` @@ -171,4 +177,4 @@ This method is available on both worker implementations and returns an array of `name` (mandatory) The task function name -This method is available on both worker implementations and returns a boolean. +This method is available on both worker implementations and returns `{ status: boolean, error?: Error }`. diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 93e7c256..9dca322e 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -1243,7 +1243,7 @@ describe('Abstract pool test suite', () => { await pool.destroy() }) - it('Verify that listTaskFunctions() is working', async () => { + it('Verify that listTaskFunctionNames() is working', async () => { const dynamicThreadPool = new DynamicThreadPool( Math.floor(numberOfWorkers / 2), numberOfWorkers, @@ -1271,6 +1271,36 @@ describe('Abstract pool test suite', () => { await fixedClusterPool.destroy() }) + it('Verify that hasTaskFunction() is working', async () => { + const dynamicThreadPool = new DynamicThreadPool( + Math.floor(numberOfWorkers / 2), + numberOfWorkers, + './tests/worker-files/thread/testMultipleTaskFunctionsWorker.js' + ) + await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1) + expect(dynamicThreadPool.hasTaskFunction(DEFAULT_TASK_NAME)).toBe(true) + expect(dynamicThreadPool.hasTaskFunction('jsonIntegerSerialization')).toBe( + true + ) + expect(dynamicThreadPool.hasTaskFunction('factorial')).toBe(true) + expect(dynamicThreadPool.hasTaskFunction('fibonacci')).toBe(true) + expect(dynamicThreadPool.hasTaskFunction('unknown')).toBe(false) + const fixedClusterPool = new FixedClusterPool( + numberOfWorkers, + './tests/worker-files/cluster/testMultipleTaskFunctionsWorker.js' + ) + await waitPoolEvents(fixedClusterPool, PoolEvents.ready, 1) + expect(dynamicThreadPool.hasTaskFunction(DEFAULT_TASK_NAME)).toBe(true) + expect(dynamicThreadPool.hasTaskFunction('jsonIntegerSerialization')).toBe( + true + ) + expect(dynamicThreadPool.hasTaskFunction('factorial')).toBe(true) + expect(dynamicThreadPool.hasTaskFunction('fibonacci')).toBe(true) + expect(dynamicThreadPool.hasTaskFunction('unknown')).toBe(false) + await dynamicThreadPool.destroy() + await fixedClusterPool.destroy() + }) + it('Verify that multiple task functions worker is working', async () => { const pool = new DynamicClusterPool( Math.floor(numberOfWorkers / 2), diff --git a/tests/worker/abstract-worker.test.js b/tests/worker/abstract-worker.test.js index 87a78f5d..13f3ae17 100644 --- a/tests/worker/abstract-worker.test.js +++ b/tests/worker/abstract-worker.test.js @@ -339,7 +339,7 @@ describe('Abstract worker test suite', () => { expect(worker.getMainWorker().send.calledOnce).toBe(true) }) - it('Verify that listTaskFunctions() works', () => { + it('Verify that listTaskFunctionNames() works', () => { const fn1 = () => { return 1 } -- 2.34.1