X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fabstract%2Fabstract-pool.test.js;h=6c340ed9f089ea6f9fd5deeb126480dc0e0ab3df;hb=21f710aa73abbb5d90328cfb199adfc0f7a70406;hp=4b6f46a607b02b3f501d97b87acb588ffb0bf4c0;hpb=9adcefabee69d0c8a8f580c2512e35d2c54c8219;p=poolifier.git diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 4b6f46a6..6c340ed9 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -11,15 +11,11 @@ const { } = require('../../../lib') const { CircularArray } = require('../../../lib/circular-array') const { Queue } = require('../../../lib/queue') +const { version } = require('../../../package.json') +const { waitPoolEvents } = require('../../test-utils') describe('Abstract pool test suite', () => { const numberOfWorkers = 2 - class StubPoolWithRemoveAllWorker extends FixedThreadPool { - removeAllWorker () { - this.workerNodes = [] - this.promiseResponseMap.clear() - } - } class StubPoolWithIsMain extends FixedThreadPool { isMain () { return false @@ -79,6 +75,33 @@ describe('Abstract pool test suite', () => { ) }) + it('Verify dynamic pool sizing', () => { + expect( + () => + new DynamicThreadPool(2, 1, './tests/worker-files/thread/testWorker.js') + ).toThrowError( + new RangeError( + 'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size' + ) + ) + expect( + () => + new DynamicThreadPool(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' + ) + ) + expect( + () => + new DynamicThreadPool(0, 0, './tests/worker-files/thread/testWorker.js') + ).toThrowError( + new RangeError( + 'Cannot instantiate a dynamic pool with a minimum pool size and a maximum pool size equal to zero' + ) + ) + }) + it('Verify that pool options are checked', async () => { let pool = new FixedThreadPool( numberOfWorkers, @@ -148,21 +171,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,9 +199,54 @@ 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 () => { + it('Verify that pool worker choice strategy options can be set', async () => { const pool = new FixedThreadPool( numberOfWorkers, './tests/worker-files/thread/testWorker.js', @@ -283,10 +352,25 @@ describe('Abstract pool test suite', () => { median: false } }) + expect(() => + pool.setWorkerChoiceStrategyOptions('invalidWorkerChoiceStrategyOptions') + ).toThrowError( + 'Invalid worker choice strategy options: must be a plain object' + ) + expect(() => + pool.setWorkerChoiceStrategyOptions({ weights: {} }) + ).toThrowError( + 'Invalid worker choice strategy options: must have a weight for each worker node' + ) + expect(() => + pool.setWorkerChoiceStrategyOptions({ measurement: 'invalidMeasurement' }) + ).toThrowError( + "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'" + ) await pool.destroy() }) - it('Verify that tasks queue can be enabled/disabled', async () => { + it('Verify that pool tasks queue can be enabled/disabled', async () => { const pool = new FixedThreadPool( numberOfWorkers, './tests/worker-files/thread/testWorker.js' @@ -305,7 +389,7 @@ describe('Abstract pool test suite', () => { await pool.destroy() }) - it('Verify that tasks queue options can be set', async () => { + it('Verify that pool tasks queue options can be set', async () => { const pool = new FixedThreadPool( numberOfWorkers, './tests/worker-files/thread/testWorker.js', @@ -314,9 +398,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() }) @@ -326,8 +416,11 @@ describe('Abstract pool test suite', () => { './tests/worker-files/thread/testWorker.js' ) expect(pool.info).toStrictEqual({ + version, type: PoolTypes.fixed, worker: WorkerTypes.thread, + ready: false, + strategy: WorkerChoiceStrategies.ROUND_ROBIN, minSize: numberOfWorkers, maxSize: numberOfWorkers, workerNodes: numberOfWorkers, @@ -341,17 +434,20 @@ describe('Abstract pool test suite', () => { }) await pool.destroy() pool = new DynamicClusterPool( + Math.floor(numberOfWorkers / 2), numberOfWorkers, - numberOfWorkers * 2, - './tests/worker-files/thread/testWorker.js' + './tests/worker-files/cluster/testWorker.js' ) expect(pool.info).toStrictEqual({ + version, type: PoolTypes.dynamic, worker: WorkerTypes.cluster, - minSize: numberOfWorkers, - maxSize: numberOfWorkers * 2, - workerNodes: numberOfWorkers, - idleWorkerNodes: numberOfWorkers, + ready: false, + strategy: WorkerChoiceStrategies.ROUND_ROBIN, + minSize: Math.floor(numberOfWorkers / 2), + maxSize: numberOfWorkers, + workerNodes: Math.floor(numberOfWorkers / 2), + idleWorkerNodes: Math.floor(numberOfWorkers / 2), busyWorkerNodes: 0, executedTasks: 0, executingTasks: 0, @@ -362,68 +458,41 @@ describe('Abstract pool test suite', () => { await pool.destroy() }) - it('Simulate worker not found', async () => { - const pool = new StubPoolWithRemoveAllWorker( - numberOfWorkers, - './tests/worker-files/cluster/testWorker.js', - { - errorHandler: e => console.error(e) - } - ) - expect(pool.workerNodes.length).toBe(numberOfWorkers) - // Simulate worker not found. - pool.removeAllWorker() - expect(pool.workerNodes.length).toBe(0) - await pool.destroy() - }) - - it('Verify that worker pool tasks usage are initialized', async () => { + it('Verify that pool worker tasks usage are initialized', async () => { const pool = new FixedClusterPool( numberOfWorkers, './tests/worker-files/cluster/testWorker.js' ) for (const workerNode of pool.workerNodes) { - expect(workerNode.workerUsage).toStrictEqual({ + expect(workerNode.usage).toStrictEqual({ tasks: { executed: 0, executing: 0, queued: 0, + maxQueued: 0, failed: 0 }, runTime: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) }, elu: { idle: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) }, active: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) - }, - utilization: 0 + } } }) } await pool.destroy() }) - it('Verify that worker pool tasks queue are initialized', async () => { - const pool = new FixedClusterPool( + it('Verify that pool worker tasks queue are initialized', async () => { + let pool = new FixedClusterPool( numberOfWorkers, './tests/worker-files/cluster/testWorker.js' ) @@ -431,11 +500,52 @@ describe('Abstract pool test suite', () => { expect(workerNode.tasksQueue).toBeDefined() expect(workerNode.tasksQueue).toBeInstanceOf(Queue) expect(workerNode.tasksQueue.size).toBe(0) + expect(workerNode.tasksQueue.maxSize).toBe(0) + } + await pool.destroy() + pool = new DynamicThreadPool( + Math.floor(numberOfWorkers / 2), + numberOfWorkers, + './tests/worker-files/thread/testWorker.js' + ) + for (const workerNode of pool.workerNodes) { + expect(workerNode.tasksQueue).toBeDefined() + expect(workerNode.tasksQueue).toBeInstanceOf(Queue) + expect(workerNode.tasksQueue.size).toBe(0) + expect(workerNode.tasksQueue.maxSize).toBe(0) + } + }) + + it('Verify that pool worker info are initialized', async () => { + let pool = new FixedClusterPool( + numberOfWorkers, + './tests/worker-files/cluster/testWorker.js' + ) + for (const workerNode of pool.workerNodes) { + expect(workerNode.info).toStrictEqual({ + id: expect.any(Number), + type: WorkerTypes.cluster, + dynamic: false, + ready: false + }) } await pool.destroy() + pool = new DynamicThreadPool( + Math.floor(numberOfWorkers / 2), + numberOfWorkers, + './tests/worker-files/thread/testWorker.js' + ) + for (const workerNode of pool.workerNodes) { + expect(workerNode.info).toStrictEqual({ + id: expect.any(Number), + type: WorkerTypes.thread, + dynamic: false, + ready: false + }) + } }) - it('Verify that worker pool tasks usage are computed', async () => { + it('Verify that pool worker tasks usage are computed', async () => { const pool = new FixedClusterPool( numberOfWorkers, './tests/worker-files/cluster/testWorker.js' @@ -446,86 +556,62 @@ describe('Abstract pool test suite', () => { promises.add(pool.execute()) } for (const workerNode of pool.workerNodes) { - expect(workerNode.workerUsage).toStrictEqual({ + expect(workerNode.usage).toStrictEqual({ tasks: { executed: 0, executing: maxMultiplier, queued: 0, + maxQueued: 0, failed: 0 }, runTime: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) }, elu: { idle: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) }, active: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) - }, - utilization: 0 + } } }) } await Promise.all(promises) for (const workerNode of pool.workerNodes) { - expect(workerNode.workerUsage).toStrictEqual({ + expect(workerNode.usage).toStrictEqual({ tasks: { executed: maxMultiplier, executing: 0, queued: 0, + maxQueued: 0, failed: 0 }, runTime: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) }, elu: { idle: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) }, active: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) - }, - utilization: 0 + } } }) } await pool.destroy() }) - it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => { + it('Verify that pool worker tasks usage are reset at worker choice strategy change', async () => { const pool = new DynamicThreadPool( - numberOfWorkers, + Math.floor(numberOfWorkers / 2), numberOfWorkers, './tests/worker-files/thread/testWorker.js' ) @@ -536,92 +622,66 @@ describe('Abstract pool test suite', () => { } await Promise.all(promises) for (const workerNode of pool.workerNodes) { - expect(workerNode.workerUsage).toStrictEqual({ + expect(workerNode.usage).toStrictEqual({ tasks: { executed: expect.any(Number), executing: 0, queued: 0, + maxQueued: 0, failed: 0 }, runTime: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) }, elu: { idle: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) }, active: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) - }, - utilization: 0 + } } }) - expect(workerNode.workerUsage.tasks.executed).toBeGreaterThan(0) - expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual( - maxMultiplier - ) + expect(workerNode.usage.tasks.executed).toBeGreaterThan(0) + expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(maxMultiplier) } pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) for (const workerNode of pool.workerNodes) { - expect(workerNode.workerUsage).toStrictEqual({ + expect(workerNode.usage).toStrictEqual({ tasks: { executed: 0, executing: 0, queued: 0, + maxQueued: 0, failed: 0 }, runTime: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) }, waitTime: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) }, elu: { idle: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) }, active: { - aggregate: 0, - average: 0, - median: 0, history: expect.any(CircularArray) - }, - utilization: 0 + } } }) - expect(workerNode.workerUsage.runTime.history.length).toBe(0) - expect(workerNode.workerUsage.waitTime.history.length).toBe(0) + expect(workerNode.usage.runTime.history.length).toBe(0) + expect(workerNode.usage.waitTime.history.length).toBe(0) } await pool.destroy() }) it("Verify that pool event emitter 'full' event can register a callback", async () => { const pool = new DynamicThreadPool( - numberOfWorkers, + Math.floor(numberOfWorkers / 2), numberOfWorkers, './tests/worker-files/thread/testWorker.js' ) @@ -636,12 +696,48 @@ describe('Abstract pool test suite', () => { promises.add(pool.execute()) } await Promise.all(promises) - // The `full` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool. - // So in total numberOfWorkers * 2 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool with min = max = numberOfWorkers. - expect(poolFull).toBe(numberOfWorkers * 2) + // The `full` event is triggered when the number of submitted tasks at once reach the maximum number of workers in the dynamic pool. + // So in total numberOfWorkers * 2 - 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool with min = (max = numberOfWorkers) / 2. + expect(poolFull).toBe(numberOfWorkers * 2 - 1) expect(poolInfo).toStrictEqual({ + version, type: PoolTypes.dynamic, worker: WorkerTypes.thread, + ready: expect.any(Boolean), + strategy: WorkerChoiceStrategies.ROUND_ROBIN, + minSize: expect.any(Number), + maxSize: expect.any(Number), + workerNodes: expect.any(Number), + idleWorkerNodes: expect.any(Number), + busyWorkerNodes: expect.any(Number), + executedTasks: expect.any(Number), + executingTasks: expect.any(Number), + queuedTasks: expect.any(Number), + maxQueuedTasks: expect.any(Number), + failedTasks: expect.any(Number) + }) + await pool.destroy() + }) + + it("Verify that pool event emitter 'ready' event can register a callback", async () => { + const pool = new FixedClusterPool( + numberOfWorkers, + './tests/worker-files/cluster/testWorker.js' + ) + let poolReady = 0 + let poolInfo + pool.emitter.on(PoolEvents.ready, info => { + ++poolReady + poolInfo = info + }) + await waitPoolEvents(pool, PoolEvents.ready, 1) + expect(poolReady).toBe(1) + expect(poolInfo).toStrictEqual({ + version, + type: PoolTypes.fixed, + worker: WorkerTypes.cluster, + ready: true, + strategy: WorkerChoiceStrategies.ROUND_ROBIN, minSize: expect.any(Number), maxSize: expect.any(Number), workerNodes: expect.any(Number), @@ -676,8 +772,11 @@ describe('Abstract pool test suite', () => { // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool. expect(poolBusy).toBe(numberOfWorkers + 1) expect(poolInfo).toStrictEqual({ + version, type: PoolTypes.fixed, worker: WorkerTypes.thread, + ready: expect.any(Boolean), + strategy: WorkerChoiceStrategies.ROUND_ROBIN, minSize: expect.any(Number), maxSize: expect.any(Number), workerNodes: expect.any(Number), @@ -694,18 +793,18 @@ describe('Abstract pool test suite', () => { it('Verify that multiple tasks worker is working', async () => { const pool = new DynamicClusterPool( + Math.floor(numberOfWorkers / 2), numberOfWorkers, - numberOfWorkers * 2, './tests/worker-files/cluster/testMultiTasksWorker.js' ) const data = { n: 10 } const result0 = await pool.execute(data) - expect(result0).toBe(false) + expect(result0).toStrictEqual({ ok: 1 }) const result1 = await pool.execute(data, 'jsonIntegerSerialization') - expect(result1).toBe(false) + expect(result1).toStrictEqual({ ok: 1 }) const result2 = await pool.execute(data, 'factorial') expect(result2).toBe(3628800) const result3 = await pool.execute(data, 'fibonacci') - expect(result3).toBe(89) + expect(result3).toBe(55) }) })