X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fabstract%2Fabstract-pool.test.js;h=80ebdb9e7e5a04b07f71f7878ed46318cbcd4fdb;hb=1d71a90829622a3541c2530c90b208e9e72ba9ec;hp=e1c83b6fbfdf91caf6da8ffab5fd8532bd48da7b;hpb=9e619829e79d59d19b11f19ddbb9efe51277da9d;p=poolifier.git diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index e1c83b6f..80ebdb9e 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -9,14 +9,16 @@ const { describe('Abstract pool test suite', () => { const numberOfWorkers = 1 const workerNotFoundInTasksUsageMapError = new Error( - 'Worker could not be found in worker tasks usage map' + 'Worker could not be found in workers tasks usage map' ) - class StubPoolWithWorkerTasksUsageMapClear extends FixedThreadPool { + class StubPoolWithRemoveAllWorker extends FixedThreadPool { removeAllWorker () { + this.workers = [] this.workersTasksUsage.clear() + this.promiseMap.clear() } } - class StubPoolWithIsMainMethod extends FixedThreadPool { + class StubPoolWithIsMain extends FixedThreadPool { isMain () { return false } @@ -25,7 +27,7 @@ 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', { @@ -60,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' + ) ) }) @@ -69,7 +73,7 @@ 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' ) ) @@ -116,7 +120,7 @@ describe('Abstract pool test suite', () => { }) it('Simulate worker not found during increaseWorkerRunningTasks', async () => { - const pool = new StubPoolWithWorkerTasksUsageMapClear( + const pool = new StubPoolWithRemoveAllWorker( numberOfWorkers, './tests/worker-files/cluster/testWorker.js' ) @@ -129,7 +133,7 @@ describe('Abstract pool test suite', () => { }) it('Simulate worker not found during decreaseWorkerRunningTasks', async () => { - const pool = new StubPoolWithWorkerTasksUsageMapClear( + const pool = new StubPoolWithRemoveAllWorker( numberOfWorkers, './tests/worker-files/cluster/testWorker.js', { @@ -145,7 +149,7 @@ describe('Abstract pool test suite', () => { }) it('Simulate worker not found during stepWorkerRunTasks', async () => { - const pool = new StubPoolWithWorkerTasksUsageMapClear( + const pool = new StubPoolWithRemoveAllWorker( numberOfWorkers, './tests/worker-files/cluster/testWorker.js', { @@ -161,7 +165,7 @@ describe('Abstract pool test suite', () => { }) it('Simulate worker not found during updateWorkerTasksRunTime with strategy not requiring it', async () => { - const pool = new StubPoolWithWorkerTasksUsageMapClear( + const pool = new StubPoolWithRemoveAllWorker( numberOfWorkers, './tests/worker-files/cluster/testWorker.js', { @@ -175,7 +179,7 @@ describe('Abstract pool test suite', () => { }) it('Simulate worker not found during updateWorkerTasksRunTime with strategy requiring it', async () => { - const pool = new StubPoolWithWorkerTasksUsageMapClear( + const pool = new StubPoolWithRemoveAllWorker( numberOfWorkers, './tests/worker-files/cluster/testWorker.js', { @@ -234,37 +238,12 @@ describe('Abstract pool test suite', () => { }) it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => { - let pool = new FixedThreadPool( - 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() - pool = new DynamicThreadPool( + const pool = new DynamicThreadPool( numberOfWorkers, numberOfWorkers, './tests/worker-files/thread/testWorker.js' ) - promises.length = 0 + const promises = [] for (let i = 0; i < numberOfWorkers * 2; i++) { promises.push(pool.execute()) }