From f0d7f80366c1012a9c63ad155c51924ffb40c4fd Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 19 Jun 2023 13:06:27 +0200 Subject: [PATCH] test: improve pool options coverage MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 1 + src/pools/abstract-pool.ts | 28 ++++++++-- tests/pools/abstract/abstract-pool.test.js | 62 ++++++++++++++++++++-- 3 files changed, 82 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9058bed6..f50ceeab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Ensure no tasks are queued when trying to soft kill a dynamic worker. +- Update strategies internals after statistics computation. ### Changed diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index a042e873..9af3d707 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -29,6 +29,7 @@ import type { WorkerUsage } from './worker' import { + Measurements, WorkerChoiceStrategies, type WorkerChoiceStrategy, type WorkerChoiceStrategyOptions @@ -199,6 +200,16 @@ export abstract class AbstractPool< 'Invalid worker choice strategy options: must have a weight for each worker node' ) } + if ( + workerChoiceStrategyOptions.measurement != null && + !Object.values(Measurements).includes( + workerChoiceStrategyOptions.measurement + ) + ) { + throw new Error( + `Invalid worker choice strategy options: invalid measurement '${workerChoiceStrategyOptions.measurement}'` + ) + } } private checkValidTasksQueueOptions ( @@ -207,11 +218,20 @@ export abstract class AbstractPool< if (tasksQueueOptions != null && !isPlainObject(tasksQueueOptions)) { throw new TypeError('Invalid tasks queue options: must be a plain object') } - if ((tasksQueueOptions?.concurrency as number) <= 0) { + if ( + tasksQueueOptions?.concurrency != null && + !Number.isSafeInteger(tasksQueueOptions.concurrency) + ) { + throw new TypeError( + 'Invalid worker tasks concurrency: must be an integer' + ) + } + if ( + tasksQueueOptions?.concurrency != null && + tasksQueueOptions.concurrency <= 0 + ) { throw new Error( - `Invalid worker tasks concurrency '${ - tasksQueueOptions.concurrency as number - }'` + `Invalid worker tasks concurrency '${tasksQueueOptions.concurrency}'` ) } } diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 4b6f46a6..fc386f37 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -148,21 +148,22 @@ describe('Abstract pool test suite', () => { numberOfWorkers, './tests/worker-files/thread/testWorker.js', { - enableTasksQueue: true, - tasksQueueOptions: { concurrency: 0 } + workerChoiceStrategy: 'invalidStrategy' } ) - ).toThrowError("Invalid worker tasks concurrency '0'") + ).toThrowError("Invalid worker choice strategy 'invalidStrategy'") expect( () => new FixedThreadPool( numberOfWorkers, './tests/worker-files/thread/testWorker.js', { - workerChoiceStrategy: 'invalidStrategy' + workerChoiceStrategyOptions: 'invalidOptions' } ) - ).toThrowError("Invalid worker choice strategy 'invalidStrategy'") + ).toThrowError( + 'Invalid worker choice strategy options: must be a plain object' + ) expect( () => new FixedThreadPool( @@ -175,6 +176,51 @@ describe('Abstract pool test suite', () => { ).toThrowError( 'Invalid worker choice strategy options: must have a weight for each worker node' ) + expect( + () => + new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js', + { + workerChoiceStrategyOptions: { measurement: 'invalidMeasurement' } + } + ) + ).toThrowError( + "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'" + ) + expect( + () => + new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js', + { + enableTasksQueue: true, + tasksQueueOptions: { concurrency: 0 } + } + ) + ).toThrowError("Invalid worker tasks concurrency '0'") + expect( + () => + new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js', + { + enableTasksQueue: true, + tasksQueueOptions: 'invalidTasksQueueOptions' + } + ) + ).toThrowError('Invalid tasks queue options: must be a plain object') + expect( + () => + new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js', + { + enableTasksQueue: true, + tasksQueueOptions: { concurrency: 0.2 } + } + ) + ).toThrowError('Invalid worker tasks concurrency: must be an integer') }) it('Verify that worker choice strategy options can be set', async () => { @@ -314,9 +360,15 @@ describe('Abstract pool test suite', () => { expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1 }) pool.setTasksQueueOptions({ concurrency: 2 }) expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 }) + expect(() => + pool.setTasksQueueOptions('invalidTasksQueueOptions') + ).toThrowError('Invalid tasks queue options: must be a plain object') expect(() => pool.setTasksQueueOptions({ concurrency: 0 })).toThrowError( "Invalid worker tasks concurrency '0'" ) + expect(() => pool.setTasksQueueOptions({ concurrency: 0.2 })).toThrowError( + 'Invalid worker tasks concurrency: must be an integer' + ) await pool.destroy() }) -- 2.34.1