From 3feeab69ae95bfd01f460760035daad32801d71a Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 18 Sep 2023 23:35:56 +0200 Subject: [PATCH] refactor: check addTaskFunction() arguments MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/abstract-pool.ts | 15 ++++++++++++--- src/pools/pool.ts | 4 ++-- tests/pools/abstract/abstract-pool.test.js | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 779d2b59..1bd5ee2c 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -846,13 +846,22 @@ export abstract class AbstractPool< /** @inheritDoc */ public async addTaskFunction ( name: string, - taskFunction: TaskFunction + fn: TaskFunction ): Promise { - this.taskFunctions.set(name, taskFunction) + if (typeof name !== 'string') { + throw new TypeError('name argument must be a string') + } + if (typeof name === 'string' && name.trim().length === 0) { + throw new TypeError('name argument must not be an empty string') + } + if (typeof fn !== 'function') { + throw new TypeError('fn argument must be a function') + } + this.taskFunctions.set(name, fn) return await this.sendTaskFunctionOperationToWorkers({ taskFunctionOperation: 'add', taskFunctionName: name, - taskFunction: taskFunction.toString() + taskFunction: fn.toString() }) } diff --git a/src/pools/pool.ts b/src/pools/pool.ts index 212253ff..7efb3808 100644 --- a/src/pools/pool.ts +++ b/src/pools/pool.ts @@ -273,12 +273,12 @@ export interface IPool< * If a task function with the same name already exists, it will be overwritten. * * @param name - The name of the task function. - * @param taskFunction - The task function. + * @param fn - The task function. * @returns `true` if the task function was added, `false` otherwise. */ readonly addTaskFunction: ( name: string, - taskFunction: TaskFunction + fn: TaskFunction ) => Promise /** * Removes a task function from this pool. diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index e9cca732..99ebbfa1 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -1280,6 +1280,20 @@ describe('Abstract pool test suite', () => { './tests/worker-files/thread/testWorker.js' ) await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1) + await expect( + dynamicThreadPool.addTaskFunction(0, () => {}) + ).rejects.toThrowError(new TypeError('name argument must be a string')) + await expect( + dynamicThreadPool.addTaskFunction('', () => {}) + ).rejects.toThrowError( + new TypeError('name argument must not be an empty string') + ) + await expect( + dynamicThreadPool.addTaskFunction('test', 0) + ).rejects.toThrowError(new TypeError('fn argument must be a function')) + await expect( + dynamicThreadPool.addTaskFunction('test', '') + ).rejects.toThrowError(new TypeError('fn argument must be a function')) expect(dynamicThreadPool.listTaskFunctionNames()).toStrictEqual([ DEFAULT_TASK_NAME, 'test' -- 2.34.1