X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fabstract%2Fabstract-pool.test.js;h=a852bab430185b467176fae595fb1f8ddd7fe78f;hb=0d80593b9a7596645612087f687fc6f5cab3101a;hp=e6a7ad36cc5ae2ae5839fdd42497bbd86ce0cd9c;hpb=ffcbbad84f63b8a77f2b1a08f82deef5430f646e;p=poolifier.git diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index e6a7ad36..a852bab4 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -1,20 +1,24 @@ const { expect } = require('expect') const { - FixedClusterPool, + 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 - const workerNotFoundInTasksUsageMapError = new Error( - 'Worker could not be found in the pool' + const workerNotFoundInPoolError = new Error( + 'Worker could not be found in the pool worker nodes' ) class StubPoolWithRemoveAllWorker extends FixedThreadPool { removeAllWorker () { - this.workers = new Map() - this.promiseMap.clear() + this.workerNodes = [] + this.promiseResponseMap.clear() } } class StubPoolWithIsMain extends FixedThreadPool { @@ -33,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', () => { @@ -50,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' ) }) @@ -73,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' ) ) }) @@ -85,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() @@ -98,8 +105,11 @@ describe('Abstract pool test suite', () => { numberOfWorkers, './tests/worker-files/thread/testWorker.js', { - workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED, + workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED, + workerChoiceStrategyOptions: { medRunTime: true }, enableEvents: false, + enableTasksQueue: true, + tasksQueueOptions: { concurrency: 2 }, messageHandler: testHandler, errorHandler: testHandler, onlineHandler: testHandler, @@ -108,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_RECENTLY_USED + 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) @@ -118,52 +133,115 @@ describe('Abstract pool test suite', () => { await pool.destroy() }) - it('Simulate worker not found during increaseWorkerRunningTasks', async () => { - const pool = new StubPoolWithRemoveAllWorker( + 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/cluster/testWorker.js' - ) - // Simulate worker not found. - pool.removeAllWorker() - expect(() => pool.increaseWorkerRunningTasks()).toThrowError( - workerNotFoundInTasksUsageMapError + './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('Simulate worker not found during decreaseWorkerRunningTasks', async () => { - const pool = new StubPoolWithRemoveAllWorker( + it('Verify that tasks queue can be enabled/disabled', async () => { + const pool = new FixedThreadPool( numberOfWorkers, - './tests/worker-files/cluster/testWorker.js', - { - errorHandler: e => console.error(e) - } - ) - // Simulate worker not found. - pool.removeAllWorker() - expect(() => pool.decreaseWorkerRunningTasks()).toThrowError( - workerNotFoundInTasksUsageMapError + './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('Simulate worker not found during stepWorkerRunTasks', async () => { - const pool = new StubPoolWithRemoveAllWorker( + it('Verify that tasks queue options can be set', async () => { + const pool = new FixedThreadPool( numberOfWorkers, - './tests/worker-files/cluster/testWorker.js', - { - errorHandler: e => console.error(e) - } + './tests/worker-files/thread/testWorker.js', + { enableTasksQueue: true } ) - // Simulate worker not found. - pool.removeAllWorker() - expect(() => pool.stepWorkerRunTasks()).toThrowError( - workerNotFoundInTasksUsageMapError + 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 during updateWorkerTasksRunTime with strategy not requiring it', async () => { + it('Simulate worker not found at getWorkerTasksUsage()', async () => { const pool = new StubPoolWithRemoveAllWorker( numberOfWorkers, './tests/worker-files/cluster/testWorker.js', @@ -171,40 +249,44 @@ describe('Abstract pool test suite', () => { errorHandler: e => console.error(e) } ) + expect(pool.workerNodes.length).toBe(numberOfWorkers) // Simulate worker not found. pool.removeAllWorker() - expect(() => pool.updateWorkerTasksRunTime()).not.toThrowError() + expect(pool.workerNodes.length).toBe(0) + expect(() => pool.getWorkerTasksUsage()).toThrowError( + workerNotFoundInPoolError + ) await pool.destroy() }) - it('Simulate worker not found during updateWorkerTasksRunTime with strategy requiring it', async () => { - const pool = new StubPoolWithRemoveAllWorker( + it('Verify that worker pool tasks usage are initialized', async () => { + const pool = new FixedClusterPool( numberOfWorkers, - './tests/worker-files/cluster/testWorker.js', - { - workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE, - errorHandler: e => console.error(e) - } - ) - // Simulate worker not found. - pool.removeAllWorker() - expect(() => pool.updateWorkerTasksRunTime()).toThrowError( - workerNotFoundInTasksUsageMapError + './tests/worker-files/cluster/testWorker.js' ) + for (const workerNode of pool.workerNodes) { + expect(workerNode.tasksUsage).toBeDefined() + expect(workerNode.tasksUsage.run).toBe(0) + expect(workerNode.tasksUsage.running).toBe(0) + expect(workerNode.tasksUsage.runTime).toBe(0) + expect(workerNode.tasksUsage.runTimeHistory).toBeInstanceOf(CircularArray) + expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) + expect(workerNode.tasksUsage.avgRunTime).toBe(0) + expect(workerNode.tasksUsage.medRunTime).toBe(0) + expect(workerNode.tasksUsage.error).toBe(0) + } await pool.destroy() }) - it('Verify that worker pool tasks usage are initialized', async () => { + it('Verify that worker pool tasks queue are initialized', async () => { const pool = new FixedClusterPool( numberOfWorkers, './tests/worker-files/cluster/testWorker.js' ) - for (const value of pool.workers.values()) { - expect(value.tasksUsage).toBeDefined() - expect(value.tasksUsage.run).toBe(0) - expect(value.tasksUsage.running).toBe(0) - expect(value.tasksUsage.runTime).toBe(0) - expect(value.tasksUsage.avgRunTime).toBe(0) + for (const workerNode of pool.workerNodes) { + expect(workerNode.tasksQueue).toBeDefined() + expect(workerNode.tasksQueue).toBeInstanceOf(Queue) + expect(workerNode.tasksQueue.size).toBe(0) } await pool.destroy() }) @@ -218,20 +300,28 @@ describe('Abstract pool test suite', () => { for (let i = 0; i < numberOfWorkers * 2; i++) { promises.push(pool.execute()) } - for (const value of pool.workers.values()) { - expect(value.tasksUsage).toBeDefined() - expect(value.tasksUsage.run).toBe(0) - expect(value.tasksUsage.running).toBe(numberOfWorkers * 2) - expect(value.tasksUsage.runTime).toBe(0) - expect(value.tasksUsage.avgRunTime).toBe(0) + for (const workerNode of pool.workerNodes) { + expect(workerNode.tasksUsage).toBeDefined() + expect(workerNode.tasksUsage.run).toBe(0) + expect(workerNode.tasksUsage.running).toBe(numberOfWorkers * 2) + expect(workerNode.tasksUsage.runTime).toBe(0) + expect(workerNode.tasksUsage.runTimeHistory).toBeInstanceOf(CircularArray) + expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) + expect(workerNode.tasksUsage.avgRunTime).toBe(0) + expect(workerNode.tasksUsage.medRunTime).toBe(0) + expect(workerNode.tasksUsage.error).toBe(0) } await Promise.all(promises) - for (const value of pool.workers.values()) { - expect(value.tasksUsage).toBeDefined() - expect(value.tasksUsage.run).toBe(numberOfWorkers * 2) - expect(value.tasksUsage.running).toBe(0) - expect(value.tasksUsage.runTime).toBeGreaterThanOrEqual(0) - expect(value.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) + for (const workerNode of pool.workerNodes) { + expect(workerNode.tasksUsage).toBeDefined() + expect(workerNode.tasksUsage.run).toBe(numberOfWorkers * 2) + expect(workerNode.tasksUsage.running).toBe(0) + expect(workerNode.tasksUsage.runTime).toBeGreaterThanOrEqual(0) + expect(workerNode.tasksUsage.runTimeHistory).toBeInstanceOf(CircularArray) + expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) + expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) + expect(workerNode.tasksUsage.medRunTime).toBe(0) + expect(workerNode.tasksUsage.error).toBe(0) } await pool.destroy() }) @@ -247,24 +337,51 @@ describe('Abstract pool test suite', () => { promises.push(pool.execute()) } await Promise.all(promises) - for (const value of pool.workers.values()) { - expect(value.tasksUsage).toBeDefined() - expect(value.tasksUsage.run).toBe(numberOfWorkers * 2) - expect(value.tasksUsage.running).toBe(0) - expect(value.tasksUsage.runTime).toBeGreaterThanOrEqual(0) - expect(value.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) + for (const workerNode of pool.workerNodes) { + expect(workerNode.tasksUsage).toBeDefined() + expect(workerNode.tasksUsage.run).toBe(numberOfWorkers * 2) + expect(workerNode.tasksUsage.running).toBe(0) + expect(workerNode.tasksUsage.runTime).toBeGreaterThanOrEqual(0) + expect(workerNode.tasksUsage.runTimeHistory).toBeInstanceOf(CircularArray) + expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) + expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) + expect(workerNode.tasksUsage.medRunTime).toBe(0) + expect(workerNode.tasksUsage.error).toBe(0) } pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) - for (const value of pool.workers.values()) { - expect(value.tasksUsage).toBeDefined() - expect(value.tasksUsage.run).toBe(0) - expect(value.tasksUsage.running).toBe(0) - expect(value.tasksUsage.runTime).toBe(0) - expect(value.tasksUsage.avgRunTime).toBe(0) + for (const workerNode of pool.workerNodes) { + expect(workerNode.tasksUsage).toBeDefined() + expect(workerNode.tasksUsage.run).toBe(0) + expect(workerNode.tasksUsage.running).toBe(0) + expect(workerNode.tasksUsage.runTime).toBe(0) + expect(workerNode.tasksUsage.runTimeHistory).toBeInstanceOf(CircularArray) + expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0) + expect(workerNode.tasksUsage.avgRunTime).toBe(0) + expect(workerNode.tasksUsage.medRunTime).toBe(0) + expect(workerNode.tasksUsage.error).toBe(0) } await pool.destroy() }) + it("Verify that pool event emitter 'full' event can register a callback", async () => { + const pool = new DynamicThreadPool( + numberOfWorkers, + numberOfWorkers, + './tests/worker-files/thread/testWorker.js' + ) + const promises = [] + let poolFull = 0 + pool.emitter.on(PoolEvents.full, () => ++poolFull) + for (let i = 0; i < numberOfWorkers * 2; i++) { + promises.push(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 + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool. + expect(poolFull).toBe(numberOfWorkers + 1) + await pool.destroy() + }) + it("Verify that pool event emitter 'busy' event can register a callback", async () => { const pool = new FixedThreadPool( numberOfWorkers, @@ -272,7 +389,7 @@ describe('Abstract pool test suite', () => { ) const promises = [] let poolBusy = 0 - pool.emitter.on('busy', () => poolBusy++) + pool.emitter.on(PoolEvents.busy, () => ++poolBusy) for (let i = 0; i < numberOfWorkers * 2; i++) { promises.push(pool.execute()) } @@ -282,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) + }) })