X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fabstract%2Fabstract-pool.test.js;h=273de9a038f0a02eb0e4d554bd717107e06394f3;hb=a8884ffdad6658f854f4048d768f7e56cf8d5032;hp=24c7debd3c86deda03f4e312682d60546ed07c61;hpb=bf9549aef8a23a3931e19040dadb7f1e8e6422b5;p=poolifier.git diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index 24c7debd..273de9a0 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -1,33 +1,33 @@ const { expect } = require('expect') const { FixedClusterPool, + DynamicThreadPool, FixedThreadPool, 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 workerNotFoundInTasksUsageMapError = new Error( + 'Worker could not be found in workers tasks usage map' + ) + class StubPoolWithRemoveAllWorker extends FixedThreadPool { + removeAllWorker () { + this.workers = [] + this.workersTasksUsage.clear() + this.promiseMap.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', { @@ -77,7 +77,7 @@ describe('Abstract pool test suite', () => { ) }) - 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' @@ -91,7 +91,7 @@ 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, @@ -114,11 +114,11 @@ describe('Abstract pool test suite', () => { 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('Simulate worker not found during increaseWorkerRunningTasks', async () => { + const pool = new StubPoolWithRemoveAllWorker( numberOfWorkers, './tests/worker-files/cluster/testWorker.js' ) @@ -127,11 +127,11 @@ describe('Abstract pool test suite', () => { expect(() => pool.increaseWorkerRunningTasks()).toThrowError( workerNotFoundInTasksUsageMapError ) - pool.destroy() + await pool.destroy() }) - it('Simulate worker not found during decreaseWorkerRunningTasks', () => { - const pool = new StubPoolWithWorkerTasksUsageMapClear( + it('Simulate worker not found during decreaseWorkerRunningTasks', async () => { + const pool = new StubPoolWithRemoveAllWorker( numberOfWorkers, './tests/worker-files/cluster/testWorker.js', { @@ -143,11 +143,11 @@ describe('Abstract pool test suite', () => { expect(() => pool.decreaseWorkerRunningTasks()).toThrowError( workerNotFoundInTasksUsageMapError ) - pool.destroy() + await pool.destroy() }) - it('Simulate worker not found during stepWorkerRunTasks', () => { - const pool = new StubPoolWithWorkerTasksUsageMapClear( + it('Simulate worker not found during stepWorkerRunTasks', async () => { + const pool = new StubPoolWithRemoveAllWorker( numberOfWorkers, './tests/worker-files/cluster/testWorker.js', { @@ -159,11 +159,11 @@ describe('Abstract pool test suite', () => { expect(() => pool.stepWorkerRunTasks()).toThrowError( workerNotFoundInTasksUsageMapError ) - pool.destroy() + await pool.destroy() }) - it('Simulate worker not found during updateWorkerTasksRunTime', () => { - const pool = new StubPoolWithWorkerTasksUsageMapClear( + it('Simulate worker not found during updateWorkerTasksRunTime with strategy not requiring it', async () => { + const pool = new StubPoolWithRemoveAllWorker( numberOfWorkers, './tests/worker-files/cluster/testWorker.js', { @@ -172,13 +172,28 @@ describe('Abstract pool test suite', () => { ) // Simulate worker not found. pool.removeAllWorker() + expect(() => pool.updateWorkerTasksRunTime()).not.toThrowError() + await pool.destroy() + }) + + it('Simulate worker not found during updateWorkerTasksRunTime with strategy requiring it', async () => { + const pool = new StubPoolWithRemoveAllWorker( + 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() + await pool.destroy() }) - it('Verify that worker pool tasks usage are initialized', () => { + it('Verify that worker pool tasks usage are initialized', async () => { const pool = new FixedClusterPool( numberOfWorkers, './tests/worker-files/cluster/testWorker.js' @@ -190,7 +205,7 @@ describe('Abstract pool test suite', () => { expect(tasksUsage.runTime).toBe(0) expect(tasksUsage.avgRunTime).toBe(0) } - pool.destroy() + await pool.destroy() }) it('Verify that worker pool tasks usage are computed', async () => { @@ -200,7 +215,7 @@ describe('Abstract pool test suite', () => { ) const promises = [] for (let i = 0; i < numberOfWorkers * 2; i++) { - promises.push(pool.execute({ test: 'test' })) + promises.push(pool.execute()) } for (const tasksUsage of pool.workersTasksUsage.values()) { expect(tasksUsage).toBeDefined() @@ -217,7 +232,36 @@ describe('Abstract pool test suite', () => { expect(tasksUsage.runTime).toBeGreaterThanOrEqual(0) expect(tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0) } - pool.destroy() + await pool.destroy() + }) + + it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => { + const pool = new DynamicThreadPool( + numberOfWorkers, + numberOfWorkers, + './tests/worker-files/thread/testWorker.js' + ) + const promises = [] + for (let i = 0; i < numberOfWorkers * 2; i++) { + 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) + } + 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) + } + await pool.destroy() }) it("Verify that pool event emitter 'busy' event can register a callback", async () => { @@ -229,12 +273,12 @@ describe('Abstract pool test suite', () => { let poolBusy = 0 pool.emitter.on('busy', () => poolBusy++) for (let i = 0; i < numberOfWorkers * 2; i++) { - promises.push(pool.execute({ test: 'test' })) + promises.push(pool.execute()) } await Promise.all(promises) // 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() }) })