X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fabstract%2Fabstract-pool.test.js;h=366d4965960f6eddfb17856331fab8542b44fce3;hb=4d8bf9e40e07bd233be4494fda4e4270fdd8a355;hp=ac776174b31180669196fa5448da3f5e0786dff9;hpb=ee11a4a2effa596d1155bf8857b2920c9e62bae4;p=poolifier.git diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index ac776174..366d4965 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -1,21 +1,26 @@ const { expect } = require('expect') const { + 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 worker tasks usage map' + const workerNotFoundInPoolError = new Error( + 'Worker could not be found in the pool worker nodes' ) - class StubPoolWithWorkerTasksUsageMapClear extends FixedThreadPool { + class StubPoolWithRemoveAllWorker extends FixedThreadPool { removeAllWorker () { - this.workersTasksUsage.clear() + this.workerNodes = [] + this.promiseResponseMap.clear() } } - class StubPoolWithIsMainMethod extends FixedThreadPool { + class StubPoolWithIsMain extends FixedThreadPool { isMain () { return false } @@ -24,14 +29,14 @@ describe('Abstract pool test suite', () => { it('Simulate pool creation from a non main thread/process', () => { expect( () => - new StubPoolWithIsMainMethod( + new StubPoolWithIsMain( numberOfWorkers, './tests/worker-files/thread/testWorker.js', { 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', () => { @@ -48,9 +53,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' ) }) @@ -59,7 +62,9 @@ describe('Abstract pool test suite', () => { () => new FixedClusterPool(-1, './tests/worker-files/cluster/testWorker.js') ).toThrowError( - new Error('Cannot instantiate a pool with a negative number of workers') + new RangeError( + 'Cannot instantiate a pool with a negative number of workers' + ) ) }) @@ -68,34 +73,42 @@ describe('Abstract pool test suite', () => { () => new FixedThreadPool(0.25, './tests/worker-files/thread/testWorker.js') ).toThrowError( - new Error( + new TypeError( 'Cannot instantiate a pool with a non integer number of workers' ) ) }) - it('Verify that pool options are checked', () => { + it('Verify that pool options are checked', async () => { let pool = new FixedThreadPool( numberOfWorkers, './tests/worker-files/thread/testWorker.js' ) 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() expect(pool.opts.exitHandler).toBeUndefined() - pool.destroy() + await pool.destroy() const testHandler = () => console.log('test handler executed') pool = new FixedThreadPool( 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, @@ -104,105 +117,177 @@ 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) expect(pool.opts.exitHandler).toStrictEqual(testHandler) - pool.destroy() + await pool.destroy() }) - it('Simulate worker not found during increaseWorkerRunningTasks', () => { - const pool = new StubPoolWithWorkerTasksUsageMapClear( + 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 } ) - pool.destroy() + 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', () => { - const pool = new StubPoolWithWorkerTasksUsageMapClear( + 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' ) - pool.destroy() + 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', () => { - const pool = new StubPoolWithWorkerTasksUsageMapClear( + 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'" ) - pool.destroy() + await pool.destroy() }) - it('Simulate worker not found during updateWorkerTasksRunTime with strategy not requiring it', () => { - const pool = new StubPoolWithWorkerTasksUsageMapClear( + it('Simulate worker not found at getWorkerTasksUsage()', 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.updateWorkerTasksRunTime()).not.toThrowError() - pool.destroy() + 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', () => { - const pool = new StubPoolWithWorkerTasksUsageMapClear( + 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' ) - pool.destroy() + 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', () => { + it('Verify that worker pool tasks queue are initialized', async () => { const pool = new FixedClusterPool( numberOfWorkers, './tests/worker-files/cluster/testWorker.js' ) - for (const tasksUsage of pool.workersTasksUsage.values()) { - expect(tasksUsage).toBeDefined() - expect(tasksUsage.run).toBe(0) - expect(tasksUsage.running).toBe(0) - expect(tasksUsage.runTime).toBe(0) - expect(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) } - pool.destroy() + await pool.destroy() }) it('Verify that worker pool tasks usage are computed', async () => { @@ -214,26 +299,35 @@ describe('Abstract pool test suite', () => { for (let i = 0; i < numberOfWorkers * 2; i++) { promises.push(pool.execute()) } - for (const tasksUsage of pool.workersTasksUsage.values()) { - expect(tasksUsage).toBeDefined() - expect(tasksUsage.run).toBe(0) - expect(tasksUsage.running).toBe(numberOfWorkers * 2) - expect(tasksUsage.runTime).toBe(0) - expect(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 tasksUsage of pool.workersTasksUsage.values()) { - expect(tasksUsage).toBeDefined() - expect(tasksUsage.run).toBe(numberOfWorkers * 2) - expect(tasksUsage.running).toBe(0) - expect(tasksUsage.runTime).toBeGreaterThanOrEqual(0) - expect(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.destroy() + await pool.destroy() }) it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => { - const pool = new FixedThreadPool( + const pool = new DynamicThreadPool( + numberOfWorkers, numberOfWorkers, './tests/worker-files/thread/testWorker.js' ) @@ -242,22 +336,49 @@ describe('Abstract pool test suite', () => { promises.push(pool.execute()) } await Promise.all(promises) - for (const tasksUsage of pool.workersTasksUsage.values()) { - expect(tasksUsage).toBeDefined() - expect(tasksUsage.run).toBe(numberOfWorkers * 2) - expect(tasksUsage.running).toBe(0) - expect(tasksUsage.runTime).toBeGreaterThanOrEqual(0) - expect(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 tasksUsage of pool.workersTasksUsage.values()) { - expect(tasksUsage).toBeDefined() - expect(tasksUsage.run).toBe(0) - expect(tasksUsage.running).toBe(0) - expect(tasksUsage.runTime).toBe(0) - expect(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()) } - pool.destroy() + 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 () => { @@ -267,7 +388,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()) } @@ -275,6 +396,6 @@ describe('Abstract pool test suite', () => { // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers. // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool. expect(poolBusy).toBe(numberOfWorkers + 1) - pool.destroy() + await pool.destroy() }) })