From: Jérôme Benoit Date: Mon, 18 Sep 2023 21:35:56 +0000 (+0200) Subject: refactor: check addTaskFunction() arguments X-Git-Tag: v2.7.0~1^2~9 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=3feeab69ae95bfd01f460760035daad32801d71a;p=poolifier.git refactor: check addTaskFunction() arguments Signed-off-by: Jérôme Benoit --- 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'