X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fabstract%2Fabstract-pool.test.js;h=96dc9a10ad4120a97455cd11d43e9bae38790c3c;hb=ea7a90d36354a4e1c833271571c6f3eb80428600;hp=d9ab30fec9d0a9f11629fcf69ca4b0921469fdb9;hpb=cf597bc58029b9b5bf861c29fa9e917a6a72d859;p=poolifier.git diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index d9ab30fe..96dc9a10 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -4,47 +4,22 @@ const { FixedThreadPool, WorkerChoiceStrategies } = require('../../../lib/index') -const expectedError = new Error('Worker could not be found in tasks map') -const numberOfWorkers = 1 - -class StubPoolWithTasksMapClear extends FixedThreadPool { - removeAllWorker () { - this.tasks.clear() +describe('Abstract pool test suite', () => { + 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() + } } -} - -class StubPoolWithIsMainMethod extends FixedThreadPool { - isMain () { - return false + class StubPoolWithIsMainMethod extends FixedThreadPool { + isMain () { + return false + } } -} - -describe('Abstract pool test suite', () => { - it('Simulate worker not found during increaseWorkersTask', () => { - const pool = new StubPoolWithTasksMapClear( - numberOfWorkers, - './tests/worker-files/thread/testWorker.js' - ) - // Simulate worker not found. - pool.removeAllWorker() - expect(() => pool.increaseWorkersTask()).toThrowError(expectedError) - pool.destroy() - }) - - it('Simulate worker not found during decreaseWorkersTasks', () => { - const pool = new StubPoolWithTasksMapClear( - numberOfWorkers, - './tests/worker-files/thread/testWorker.js', - { - errorHandler: e => console.error(e) - } - ) - // Simulate worker not found. - pool.removeAllWorker() - expect(() => pool.decreaseWorkersTasks()).toThrowError(expectedError) - pool.destroy() - }) it('Simulate pool creation from a non main thread/process', () => { expect( @@ -139,6 +114,124 @@ describe('Abstract pool test suite', () => { 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( + numberOfWorkers, + './tests/worker-files/cluster/testWorker.js', + { + errorHandler: e => console.error(e) + } + ) + // Simulate worker not found. + pool.removeAllWorker() + expect(() => pool.decreaseWorkerRunningTasks()).toThrowError( + workerNotFoundInTasksUsageMapError + ) + pool.destroy() + }) + + it('Simulate worker not found during stepWorkerRunTasks', () => { + const pool = new StubPoolWithWorkerTasksUsageMapClear( + numberOfWorkers, + './tests/worker-files/cluster/testWorker.js', + { + errorHandler: e => console.error(e) + } + ) + // Simulate worker not found. + pool.removeAllWorker() + expect(() => pool.stepWorkerRunTasks()).toThrowError( + workerNotFoundInTasksUsageMapError + ) + pool.destroy() + }) + + it('Simulate worker not found during updateWorkerTasksRunTime with strategy not requiring it', () => { + const pool = new StubPoolWithWorkerTasksUsageMapClear( + numberOfWorkers, + './tests/worker-files/cluster/testWorker.js', + { + errorHandler: e => console.error(e) + } + ) + // Simulate worker not found. + pool.removeAllWorker() + expect(() => pool.updateWorkerTasksRunTime()).not.toThrowError() + pool.destroy() + }) + + it('Simulate worker not found during updateWorkerTasksRunTime with strategy requiring it', () => { + const pool = new StubPoolWithWorkerTasksUsageMapClear( + 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' + ) + 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) + } + pool.destroy() + }) + + it('Verify that worker pool tasks usage are computed', async () => { + const pool = new FixedClusterPool( + numberOfWorkers, + './tests/worker-files/cluster/testWorker.js' + ) + const promises = [] + 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() + }) + it("Verify that pool event emitter 'busy' event can register a callback", async () => { const pool = new FixedThreadPool( numberOfWorkers, @@ -148,7 +241,7 @@ 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.