X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fabstract%2Fabstract-pool.test.js;h=be745d94ce5e4b10c89012846f6f67d85899abc0;hb=bb9423b78d11f9420b9b2a8cb865ea8c1315e642;hp=0890d564d9d673536507872c6ae5c965c592fc9f;hpb=7e653ee033b398cb5877b21dcab0bfe4bf1d7721;p=poolifier.git diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 0890d564..be745d94 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -12,6 +12,7 @@ const { } = require('../../../lib') const { CircularArray } = require('../../../lib/circular-array') const { Deque } = require('../../../lib/deque') +const { DEFAULT_TASK_NAME } = require('../../../lib/utils') const { version } = require('../../../package.json') const { waitPoolEvents } = require('../../test-utils') @@ -44,6 +45,16 @@ describe('Abstract pool test suite', () => { ) }) + it('Verify that pool statuses properties are set', async () => { + const pool = new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js' + ) + expect(pool.starting).toBe(false) + expect(pool.started).toBe(true) + await pool.destroy() + }) + it('Verify that filePath is checked', () => { const expectedError = new Error( 'Please specify a file with a worker implementation' @@ -142,22 +153,22 @@ describe('Abstract pool test suite', () => { ) expect( () => - new DynamicClusterPool( - 1, - 1, - './tests/worker-files/cluster/testWorker.js' - ) + new DynamicThreadPool(0, 0, './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' + 'Cannot instantiate a dynamic pool with a maximum pool size equal to zero' ) ) expect( () => - new DynamicThreadPool(0, 0, './tests/worker-files/thread/testWorker.js') + new DynamicClusterPool( + 1, + 1, + './tests/worker-files/cluster/testWorker.js' + ) ).toThrowError( new RangeError( - 'Cannot instantiate a dynamic pool with a maximum pool size equal to zero' + 'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead' ) ) }) @@ -176,13 +187,13 @@ describe('Abstract pool test suite', () => { WorkerChoiceStrategies.ROUND_ROBIN ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } }) expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } @@ -224,14 +235,14 @@ describe('Abstract pool test suite', () => { WorkerChoiceStrategies.LEAST_USED ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: true }, waitTime: { median: false }, elu: { median: false }, weights: { 0: 300, 1: 200 } }) expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: true }, waitTime: { median: false }, elu: { median: false }, @@ -264,13 +275,13 @@ describe('Abstract pool test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategyOptions: { - choiceRetries: 'invalidChoiceRetries' + retries: 'invalidChoiceRetries' } } ) ).toThrowError( new TypeError( - 'Invalid worker choice strategy options: choice retries must be an integer' + 'Invalid worker choice strategy options: retries must be an integer' ) ) expect( @@ -280,13 +291,13 @@ describe('Abstract pool test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategyOptions: { - choiceRetries: -1 + retries: -1 } } ) ).toThrowError( new RangeError( - "Invalid worker choice strategy options: choice retries '-1' must be greater or equal than zero" + "Invalid worker choice strategy options: retries '-1' must be greater or equal than zero" ) ) expect( @@ -317,6 +328,19 @@ describe('Abstract pool test suite', () => { "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'" ) ) + expect( + () => + new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js', + { + enableTasksQueue: true, + tasksQueueOptions: 'invalidTasksQueueOptions' + } + ) + ).toThrowError( + new TypeError('Invalid tasks queue options: must be a plain object') + ) expect( () => new FixedThreadPool( @@ -339,11 +363,13 @@ describe('Abstract pool test suite', () => { './tests/worker-files/thread/testWorker.js', { enableTasksQueue: true, - tasksQueueOptions: 'invalidTasksQueueOptions' + tasksQueueOptions: { concurrency: -1 } } ) ).toThrowError( - new TypeError('Invalid tasks queue options: must be a plain object') + new RangeError( + 'Invalid worker node tasks concurrency: -1 is a negative integer or zero' + ) ) expect( () => @@ -358,6 +384,64 @@ describe('Abstract pool test suite', () => { ).toThrowError( new TypeError('Invalid worker node tasks concurrency: must be an integer') ) + expect( + () => + new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js', + { + enableTasksQueue: true, + tasksQueueOptions: { queueMaxSize: 2 } + } + ) + ).toThrowError( + new Error( + 'Invalid tasks queue options: queueMaxSize is deprecated, please use size instead' + ) + ) + expect( + () => + new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js', + { + enableTasksQueue: true, + tasksQueueOptions: { size: 0 } + } + ) + ).toThrowError( + new RangeError( + 'Invalid worker node tasks queue size: 0 is a negative integer or zero' + ) + ) + expect( + () => + new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js', + { + enableTasksQueue: true, + tasksQueueOptions: { size: -1 } + } + ) + ).toThrowError( + new RangeError( + 'Invalid worker node tasks queue size: -1 is a negative integer or zero' + ) + ) + expect( + () => + new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js', + { + enableTasksQueue: true, + tasksQueueOptions: { size: 0.2 } + } + ) + ).toThrowError( + new TypeError('Invalid worker node tasks queue size: must be an integer') + ) }) it('Verify that pool worker choice strategy options can be set', async () => { @@ -367,13 +451,13 @@ describe('Abstract pool test suite', () => { { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } ) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } }) expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } @@ -381,7 +465,7 @@ describe('Abstract pool test suite', () => { for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { expect(workerChoiceStrategy.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } @@ -411,13 +495,13 @@ describe('Abstract pool test suite', () => { elu: { median: true } }) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: true }, waitTime: { median: false }, elu: { median: true } }) expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: true }, waitTime: { median: false }, elu: { median: true } @@ -425,7 +509,7 @@ describe('Abstract pool test suite', () => { for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { expect(workerChoiceStrategy.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: true }, waitTime: { median: false }, elu: { median: true } @@ -455,13 +539,13 @@ describe('Abstract pool test suite', () => { elu: { median: false } }) expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } }) expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } @@ -469,7 +553,7 @@ describe('Abstract pool test suite', () => { for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext .workerChoiceStrategies) { expect(workerChoiceStrategy.opts).toStrictEqual({ - choiceRetries: 6, + retries: 6, runTime: { median: false }, waitTime: { median: false }, elu: { median: false } @@ -503,18 +587,18 @@ describe('Abstract pool test suite', () => { ) expect(() => pool.setWorkerChoiceStrategyOptions({ - choiceRetries: 'invalidChoiceRetries' + retries: 'invalidChoiceRetries' }) ).toThrowError( new TypeError( - 'Invalid worker choice strategy options: choice retries must be an integer' + 'Invalid worker choice strategy options: retries must be an integer' ) ) expect(() => - pool.setWorkerChoiceStrategyOptions({ choiceRetries: -1 }) + pool.setWorkerChoiceStrategyOptions({ retries: -1 }) ).toThrowError( new RangeError( - "Invalid worker choice strategy options: choice retries '-1' must be greater or equal than zero" + "Invalid worker choice strategy options: retries '-1' must be greater or equal than zero" ) ) expect(() => @@ -714,6 +798,7 @@ describe('Abstract pool test suite', () => { expect(workerNode.tasksQueue.size).toBe(0) expect(workerNode.tasksQueue.maxSize).toBe(0) } + await pool.destroy() }) it('Verify that pool worker info are initialized', async () => { @@ -743,6 +828,7 @@ describe('Abstract pool test suite', () => { ready: true }) } + await pool.destroy() }) it('Verify that pool execute() arguments are checked', async () => { @@ -1071,7 +1157,7 @@ describe('Abstract pool test suite', () => { ) await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1) expect(dynamicThreadPool.listTaskFunctions()).toStrictEqual([ - 'default', + DEFAULT_TASK_NAME, 'jsonIntegerSerialization', 'factorial', 'fibonacci' @@ -1082,11 +1168,13 @@ describe('Abstract pool test suite', () => { ) await waitPoolEvents(fixedClusterPool, PoolEvents.ready, 1) expect(fixedClusterPool.listTaskFunctions()).toStrictEqual([ - 'default', + DEFAULT_TASK_NAME, 'jsonIntegerSerialization', 'factorial', 'fibonacci' ]) + await dynamicThreadPool.destroy() + await fixedClusterPool.destroy() }) it('Verify that multiple task functions worker is working', async () => { @@ -1108,7 +1196,7 @@ describe('Abstract pool test suite', () => { expect(pool.info.executedTasks).toBe(4) for (const workerNode of pool.workerNodes) { expect(workerNode.info.taskFunctions).toStrictEqual([ - 'default', + DEFAULT_TASK_NAME, 'jsonIntegerSerialization', 'factorial', 'fibonacci' @@ -1143,5 +1231,6 @@ describe('Abstract pool test suite', () => { ).toBeGreaterThanOrEqual(0) } } + await pool.destroy() }) })