X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fabstract%2Fabstract-pool.test.js;h=a852bab430185b467176fae595fb1f8ddd7fe78f;hb=0d80593b9a7596645612087f687fc6f5cab3101a;hp=54646e3aecbc3b5e87dc5bb791045b678862c6bd;hpb=f06e48d8e14dcfe3277bd16b1bd2463136af13e6;p=poolifier.git diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 54646e3a..a852bab4 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -1,12 +1,14 @@ const { expect } = require('expect') const { + DynamicClusterPool, DynamicThreadPool, FixedClusterPool, FixedThreadPool, PoolEvents, WorkerChoiceStrategies -} = require('../../../lib/index') +} = require('../../../lib') const { CircularArray } = require('../../../lib/circular-array') +const { Queue } = require('../../../lib/queue') describe('Abstract pool test suite', () => { const numberOfWorkers = 1 @@ -15,7 +17,7 @@ describe('Abstract pool test suite', () => { ) class StubPoolWithRemoveAllWorker extends FixedThreadPool { removeAllWorker () { - this.workers = [] + this.workerNodes = [] this.promiseResponseMap.clear() } } @@ -35,7 +37,7 @@ describe('Abstract pool test suite', () => { errorHandler: e => console.error(e) } ) - ).toThrowError(new Error('Cannot start a pool from a worker!')) + ).toThrowError('Cannot start a pool from a worker!') }) it('Verify that filePath is checked', () => { @@ -52,9 +54,7 @@ describe('Abstract pool test suite', () => { it('Verify that numberOfWorkers is checked', () => { expect(() => new FixedThreadPool()).toThrowError( - new Error( - 'Cannot instantiate a pool without specifying the number of workers' - ) + 'Cannot instantiate a pool without specifying the number of workers' ) }) @@ -75,7 +75,7 @@ describe('Abstract pool test suite', () => { new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js') ).toThrowError( new TypeError( - 'Cannot instantiate a pool with a non integer number of workers' + 'Cannot instantiate a pool with a non safe integer number of workers' ) ) }) @@ -87,9 +87,14 @@ describe('Abstract pool test suite', () => { ) expect(pool.opts.enableEvents).toBe(true) expect(pool.emitter).toBeDefined() + expect(pool.opts.enableTasksQueue).toBe(false) + expect(pool.opts.tasksQueueOptions).toBeUndefined() expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.ROUND_ROBIN ) + expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ + medRunTime: false + }) expect(pool.opts.messageHandler).toBeUndefined() expect(pool.opts.errorHandler).toBeUndefined() expect(pool.opts.onlineHandler).toBeUndefined() @@ -101,7 +106,10 @@ describe('Abstract pool test suite', () => { './tests/worker-files/thread/testWorker.js', { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED, + workerChoiceStrategyOptions: { medRunTime: true }, enableEvents: false, + enableTasksQueue: true, + tasksQueueOptions: { concurrency: 2 }, messageHandler: testHandler, errorHandler: testHandler, onlineHandler: testHandler, @@ -110,9 +118,14 @@ describe('Abstract pool test suite', () => { ) expect(pool.opts.enableEvents).toBe(false) expect(pool.emitter).toBeUndefined() + expect(pool.opts.enableTasksQueue).toBe(true) + expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 }) expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.LESS_USED ) + expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ + medRunTime: true + }) expect(pool.opts.messageHandler).toStrictEqual(testHandler) expect(pool.opts.errorHandler).toStrictEqual(testHandler) expect(pool.opts.onlineHandler).toStrictEqual(testHandler) @@ -120,7 +133,115 @@ describe('Abstract pool test suite', () => { await pool.destroy() }) - it('Simulate worker not found during getWorkerTasksUsage', async () => { + it('Verify that pool options are validated', async () => { + 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', + { + workerChoiceStrategy: 'invalidStrategy' + } + ) + ).toThrowError("Invalid worker choice strategy 'invalidStrategy'") + }) + + it('Verify that worker choice strategy options can be set', async () => { + const pool = new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js', + { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE } + ) + expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ + medRunTime: false + }) + for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext + .workerChoiceStrategies) { + expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: false }) + } + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime + ).toBe(true) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime + ).toBe(false) + pool.setWorkerChoiceStrategyOptions({ medRunTime: true }) + expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ + medRunTime: true + }) + for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext + .workerChoiceStrategies) { + expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: true }) + } + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime + ).toBe(false) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime + ).toBe(true) + pool.setWorkerChoiceStrategyOptions({ medRunTime: false }) + expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({ + medRunTime: false + }) + for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext + .workerChoiceStrategies) { + expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: false }) + } + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime + ).toBe(true) + expect( + pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime + ).toBe(false) + await pool.destroy() + }) + + it('Verify that tasks queue can be enabled/disabled', async () => { + const pool = new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js' + ) + expect(pool.opts.enableTasksQueue).toBe(false) + expect(pool.opts.tasksQueueOptions).toBeUndefined() + pool.enableTasksQueue(true) + expect(pool.opts.enableTasksQueue).toBe(true) + expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1 }) + pool.enableTasksQueue(true, { concurrency: 2 }) + expect(pool.opts.enableTasksQueue).toBe(true) + expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 }) + pool.enableTasksQueue(false) + expect(pool.opts.enableTasksQueue).toBe(false) + expect(pool.opts.tasksQueueOptions).toBeUndefined() + await pool.destroy() + }) + + it('Verify that tasks queue options can be set', async () => { + const pool = new FixedThreadPool( + numberOfWorkers, + './tests/worker-files/thread/testWorker.js', + { enableTasksQueue: true } + ) + expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1 }) + pool.setTasksQueueOptions({ concurrency: 2 }) + expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 }) + expect(() => pool.setTasksQueueOptions({ concurrency: 0 })).toThrowError( + "Invalid worker tasks concurrency '0'" + ) + await pool.destroy() + }) + + it('Simulate worker not found at getWorkerTasksUsage()', async () => { const pool = new StubPoolWithRemoveAllWorker( numberOfWorkers, './tests/worker-files/cluster/testWorker.js', @@ -128,8 +249,10 @@ describe('Abstract pool test suite', () => { errorHandler: e => console.error(e) } ) + expect(pool.workerNodes.length).toBe(numberOfWorkers) // Simulate worker not found. pool.removeAllWorker() + expect(pool.workerNodes.length).toBe(0) expect(() => pool.getWorkerTasksUsage()).toThrowError( workerNotFoundInPoolError ) @@ -162,8 +285,8 @@ describe('Abstract pool test suite', () => { ) for (const workerNode of pool.workerNodes) { expect(workerNode.tasksQueue).toBeDefined() - expect(workerNode.tasksQueue).toBeInstanceOf(Array) - expect(workerNode.tasksQueue.length).toBe(0) + expect(workerNode.tasksQueue).toBeInstanceOf(Queue) + expect(workerNode.tasksQueue.size).toBe(0) } await pool.destroy() }) @@ -253,7 +376,7 @@ describe('Abstract pool test suite', () => { promises.push(pool.execute()) } await Promise.all(promises) - // The `full` event is triggered when the number of submitted tasks at once reach the number of dynamic pool workers. + // 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 + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool. expect(poolFull).toBe(numberOfWorkers + 1) await pool.destroy() @@ -276,4 +399,21 @@ describe('Abstract pool test suite', () => { expect(poolBusy).toBe(numberOfWorkers + 1) await pool.destroy() }) + + it('Verify that multiple tasks worker is working', async () => { + const pool = new DynamicClusterPool( + numberOfWorkers, + numberOfWorkers * 2, + './tests/worker-files/cluster/testMultiTasksWorker.js' + ) + const data = { n: 10 } + const result0 = await pool.execute(data) + expect(result0).toBe(false) + const result1 = await pool.execute(data, 'jsonIntegerSerialization') + expect(result1).toBe(false) + const result2 = await pool.execute(data, 'factorial') + expect(result2).toBe(3628800) + const result3 = await pool.execute(data, 'fibonacci') + expect(result3).toBe(89) + }) })