From 19c42d90f31326bc9cbc07e4a8c5501fb78142d1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 29 Apr 2024 22:23:59 +0200 Subject: [PATCH] test: improve addTaskFunction() tests 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 | 30 ++++++++++++++++++ tests/worker/abstract-worker.test.mjs | 45 ++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/tests/pools/abstract-pool.test.mjs b/tests/pools/abstract-pool.test.mjs index 7740b079..313cc77a 100644 --- a/tests/pools/abstract-pool.test.mjs +++ b/tests/pools/abstract-pool.test.mjs @@ -1372,6 +1372,36 @@ describe('Abstract pool test suite', () => { await expect(dynamicThreadPool.addTaskFunction('test', '')).rejects.toThrow( new TypeError('taskFunction property must be a function') ) + await expect( + dynamicThreadPool.addTaskFunction('test', { taskFunction: 0 }) + ).rejects.toThrow(new TypeError('taskFunction property must be a function')) + await expect( + dynamicThreadPool.addTaskFunction('test', { taskFunction: '' }) + ).rejects.toThrow(new TypeError('taskFunction property must be a function')) + await expect( + dynamicThreadPool.addTaskFunction('test', { + taskFunction: () => {}, + priority: -21 + }) + ).rejects.toThrow( + new RangeError("Property 'priority' must be between -20 and 19") + ) + await expect( + dynamicThreadPool.addTaskFunction('test', { + taskFunction: () => {}, + priority: 20 + }) + ).rejects.toThrow( + new RangeError("Property 'priority' must be between -20 and 19") + ) + await expect( + dynamicThreadPool.addTaskFunction('test', { + taskFunction: () => {}, + strategy: 'invalidStrategy' + }) + ).rejects.toThrow( + new Error("Invalid worker choice strategy 'invalidStrategy'") + ) expect(dynamicThreadPool.listTaskFunctionsProperties()).toStrictEqual([ { name: DEFAULT_TASK_NAME }, { name: 'test' } diff --git a/tests/worker/abstract-worker.test.mjs b/tests/worker/abstract-worker.test.mjs index 67834a4c..79f85cdb 100644 --- a/tests/worker/abstract-worker.test.mjs +++ b/tests/worker/abstract-worker.test.mjs @@ -177,7 +177,7 @@ describe('Abstract worker test suite', () => { ).toThrow(new TypeError("Invalid property 'priority': ''")) expect( () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: -21 } }) - ).toThrow(new TypeError("Property 'priority' must be between -20 and 19")) + ).toThrow(new RangeError("Property 'priority' must be between -20 and 19")) expect( () => new ThreadWorker({ fn1: { taskFunction: fn1, priority: 20 } }) ).toThrow(new RangeError("Property 'priority' must be between -20 and 19")) @@ -186,9 +186,7 @@ describe('Abstract worker test suite', () => { new ThreadWorker({ fn1: { taskFunction: fn1, strategy: 'invalidStrategy' } }) - ).toThrow( - new RangeError("Invalid worker choice strategy 'invalidStrategy'") - ) + ).toThrow(new Error("Invalid worker choice strategy 'invalidStrategy'")) }) it('Verify that taskFunctions parameter with multiple task functions is taken', () => { @@ -300,12 +298,51 @@ describe('Abstract worker test suite', () => { status: false, error: new TypeError('name parameter is an empty string') }) + expect(worker.addTaskFunction('fn2', 0)).toStrictEqual({ + status: false, + error: new TypeError( + "taskFunction object 'taskFunction' property 'undefined' is not a function" + ) + }) expect(worker.addTaskFunction('fn3', '')).toStrictEqual({ status: false, error: new TypeError( "taskFunction object 'taskFunction' property 'undefined' is not a function" ) }) + expect(worker.addTaskFunction('fn2', { taskFunction: 0 })).toStrictEqual({ + status: false, + error: new TypeError( + "taskFunction object 'taskFunction' property '0' is not a function" + ) + }) + expect(worker.addTaskFunction('fn3', { taskFunction: '' })).toStrictEqual({ + status: false, + error: new TypeError( + "taskFunction object 'taskFunction' property '' is not a function" + ) + }) + expect( + worker.addTaskFunction('fn2', { taskFunction: () => {}, priority: -21 }) + ).toStrictEqual({ + status: false, + error: new RangeError("Property 'priority' must be between -20 and 19") + }) + expect( + worker.addTaskFunction('fn3', { taskFunction: () => {}, priority: 20 }) + ).toStrictEqual({ + status: false, + error: new RangeError("Property 'priority' must be between -20 and 19") + }) + expect( + worker.addTaskFunction('fn2', { + taskFunction: () => {}, + strategy: 'invalidStrategy' + }) + ).toStrictEqual({ + status: false, + error: new Error("Invalid worker choice strategy 'invalidStrategy'") + }) expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toStrictEqual({ taskFunction: expect.any(Function) }) -- 2.34.1