From: Jérôme Benoit Date: Fri, 21 Jul 2023 14:39:54 +0000 (+0200) Subject: feat: add dynamic pool sizes type check X-Git-Tag: v2.6.20~8 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=2761efb4184ad22cef774c7707023c354e8f8f04;p=poolifier.git feat: add dynamic pool sizes type check Signed-off-by: Jérôme Benoit --- 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'