From 2761efb4184ad22cef774c7707023c354e8f8f04 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 21 Jul 2023 16:39:54 +0200 Subject: [PATCH] feat: add dynamic pool sizes type check 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 | 6 ++++- src/worker/thread-worker.ts | 2 +- tests/pools/abstract/abstract-pool.test.js | 30 +++++++++++++++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 0b359d93..1222d686 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -173,7 +173,11 @@ export abstract class AbstractPool< protected checkDynamicPoolSize (min: number, max: number): void { if (this.type === PoolTypes.dynamic) { - if (min > max) { + if (!Number.isSafeInteger(max)) { + throw new TypeError( + 'Cannot instantiate a dynamic pool with a non safe integer maximum pool size' + ) + } else if (min > max) { throw new RangeError( 'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size' ) diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index 4d4c1c34..ec5dea8e 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -28,7 +28,7 @@ export class ThreadWorker< Response = unknown > extends AbstractWorker { /** - * Message port used to communicate with the main thread. + * Message port used to communicate with the main worker. */ private port!: MessagePort /** diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index b26ca43d..10736b6b 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -86,6 +86,30 @@ describe('Abstract pool test suite', () => { }) it('Verify that dynamic pool sizing is checked', () => { + expect( + () => + new DynamicThreadPool( + 0.5, + 1, + './tests/worker-files/thread/testWorker.js' + ) + ).toThrowError( + new TypeError( + 'Cannot instantiate a pool with a non safe integer number of workers' + ) + ) + expect( + () => + new DynamicClusterPool( + 0, + 0.5, + './tests/worker-files/thread/testWorker.js' + ) + ).toThrowError( + new TypeError( + 'Cannot instantiate a dynamic pool with a non safe integer maximum pool size' + ) + ) expect( () => new DynamicThreadPool(2, 1, './tests/worker-files/thread/testWorker.js') @@ -96,7 +120,11 @@ describe('Abstract pool test suite', () => { ) expect( () => - new DynamicThreadPool(1, 1, './tests/worker-files/thread/testWorker.js') + new DynamicClusterPool( + 1, + 1, + './tests/worker-files/thread/testWorker.js' + ) ).toThrowError( new RangeError( 'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead' -- 2.34.1