X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=tests%2Fpools%2Fabstract%2Fabstract-pool.test.js;h=658be6a9ef99ed63afb692ba864a4745ebf66f89;hb=aee467366d8c393b79e7af82c6a7ab12338ee64e;hp=54b32ec07a9f64d8afbbc4cfb284dea925d99d81;hpb=6db75ad932064c1415ff6f03645929530209a5fe;p=poolifier.git diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 54b32ec0..658be6a9 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -1,29 +1,33 @@ const { expect } = require('expect') const { + DynamicThreadPool, FixedClusterPool, FixedThreadPool, + PoolEvents, WorkerChoiceStrategies } = require('../../../lib/index') -const numberOfWorkers = 1 -const workerNotFoundInTasksUsageMapError = new Error( - 'Worker could not be found in worker tasks usage map' -) -class StubPoolWithWorkerTasksUsageMapClear extends FixedThreadPool { - removeAllWorker () { - this.workersTasksUsage.clear() + +describe('Abstract pool test suite', () => { + const numberOfWorkers = 1 + const workerNotFoundInPoolError = new Error( + 'Worker could not be found in the pool' + ) + class StubPoolWithRemoveAllWorker extends FixedThreadPool { + removeAllWorker () { + this.workers = [] + this.promiseResponseMap.clear() + } } -} -class StubPoolWithIsMainMethod extends FixedThreadPool { - isMain () { - return false + class StubPoolWithIsMain extends FixedThreadPool { + isMain () { + return false + } } -} -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', { @@ -58,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' + ) ) }) @@ -67,13 +73,13 @@ 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' @@ -87,13 +93,13 @@ describe('Abstract pool test suite', () => { 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, enableEvents: false, messageHandler: testHandler, errorHandler: testHandler, @@ -104,30 +110,17 @@ describe('Abstract pool test suite', () => { expect(pool.opts.enableEvents).toBe(false) expect(pool.emitter).toBeUndefined() expect(pool.opts.workerChoiceStrategy).toBe( - WorkerChoiceStrategies.LESS_RECENTLY_USED + WorkerChoiceStrategies.LESS_USED ) 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( - numberOfWorkers, - './tests/worker-files/cluster/testWorker.js' - ) - // Simulate worker not found. - pool.removeAllWorker() - expect(() => pool.increaseWorkerRunningTasks()).toThrowError( - workerNotFoundInTasksUsageMapError - ) - pool.destroy() - }) - - it('Simulate worker not found during decreaseWorkerRunningTasks', () => { - const pool = new StubPoolWithWorkerTasksUsageMapClear( + it('Simulate worker not found during getWorkerTasksUsage', async () => { + const pool = new StubPoolWithRemoveAllWorker( numberOfWorkers, './tests/worker-files/cluster/testWorker.js', { @@ -136,99 +129,105 @@ describe('Abstract pool test suite', () => { ) // Simulate worker not found. pool.removeAllWorker() - expect(() => pool.decreaseWorkerRunningTasks()).toThrowError( - workerNotFoundInTasksUsageMapError + expect(() => pool.getWorkerTasksUsage()).toThrowError( + workerNotFoundInPoolError ) - pool.destroy() + await pool.destroy() }) - it('Simulate worker not found during stepWorkerRunTasks', () => { - 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', - { - errorHandler: e => console.error(e) - } - ) - // Simulate worker not found. - pool.removeAllWorker() - expect(() => pool.stepWorkerRunTasks()).toThrowError( - workerNotFoundInTasksUsageMapError + './tests/worker-files/cluster/testWorker.js' ) - pool.destroy() + for (const workerItem of pool.workers) { + expect(workerItem.tasksUsage).toBeDefined() + expect(workerItem.tasksUsage.run).toBe(0) + expect(workerItem.tasksUsage.running).toBe(0) + expect(workerItem.tasksUsage.runTime).toBe(0) + expect(workerItem.tasksUsage.avgRunTime).toBe(0) + expect(workerItem.tasksUsage.error).toBe(0) + } + await pool.destroy() }) - it('Simulate worker not found during updateWorkerTasksRunTime with strategy not requiring it', () => { - const pool = new StubPoolWithWorkerTasksUsageMapClear( + it('Verify that worker pool tasks usage are computed', async () => { + const pool = new FixedClusterPool( numberOfWorkers, - './tests/worker-files/cluster/testWorker.js', - { - errorHandler: e => console.error(e) - } + './tests/worker-files/cluster/testWorker.js' ) - // Simulate worker not found. - pool.removeAllWorker() - expect(() => pool.updateWorkerTasksRunTime()).not.toThrowError() - pool.destroy() + const promises = [] + for (let i = 0; i < numberOfWorkers * 2; i++) { + promises.push(pool.execute()) + } + for (const workerItem of pool.workers) { + expect(workerItem.tasksUsage).toBeDefined() + expect(workerItem.tasksUsage.run).toBe(0) + expect(workerItem.tasksUsage.running).toBe(numberOfWorkers * 2) + expect(workerItem.tasksUsage.runTime).toBe(0) + expect(workerItem.tasksUsage.avgRunTime).toBe(0) + expect(workerItem.tasksUsage.error).toBe(0) + } + await Promise.all(promises) + for (const workerItem of pool.workers) { + expect(workerItem.tasksUsage).toBeDefined() + expect(workerItem.tasksUsage.run).toBe(numberOfWorkers * 2) + expect(workerItem.tasksUsage.running).toBe(0) + expect(workerItem.tasksUsage.runTime).toBeGreaterThanOrEqual(0) + expect(workerItem.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) + expect(workerItem.tasksUsage.error).toBe(0) + } + 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 reset at worker choice strategy change', async () => { + const pool = new DynamicThreadPool( 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 - ) - pool.destroy() - }) - - it('Verify that worker pool tasks usage are initialized', () => { - const pool = new FixedClusterPool( numberOfWorkers, - './tests/worker-files/cluster/testWorker.js' + './tests/worker-files/thread/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) + const promises = [] + for (let i = 0; i < numberOfWorkers * 2; i++) { + promises.push(pool.execute()) + } + await Promise.all(promises) + for (const workerItem of pool.workers) { + expect(workerItem.tasksUsage).toBeDefined() + expect(workerItem.tasksUsage.run).toBe(numberOfWorkers * 2) + expect(workerItem.tasksUsage.running).toBe(0) + expect(workerItem.tasksUsage.runTime).toBeGreaterThanOrEqual(0) + expect(workerItem.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) + expect(workerItem.tasksUsage.error).toBe(0) } - pool.destroy() + pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) + for (const workerItem of pool.workers) { + expect(workerItem.tasksUsage).toBeDefined() + expect(workerItem.tasksUsage.run).toBe(0) + expect(workerItem.tasksUsage.running).toBe(0) + expect(workerItem.tasksUsage.runTime).toBe(0) + expect(workerItem.tasksUsage.avgRunTime).toBe(0) + expect(workerItem.tasksUsage.error).toBe(0) + } + await pool.destroy() }) - it('Verify that worker pool tasks usage are computed', async () => { - const pool = new FixedClusterPool( + it("Verify that pool event emitter 'full' event can register a callback", async () => { + const pool = new DynamicThreadPool( numberOfWorkers, - './tests/worker-files/cluster/testWorker.js' + 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()) } - 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) - } 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) - } - pool.destroy() + // The `full` event is triggered when the number of submitted tasks at once reach the number of dynamic pool workers. + // 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 () => { @@ -238,7 +237,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()) } @@ -246,6 +245,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() }) })