From fd7ebd496ee8e1f95c6c1dc2af5153d73ec3daab Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 11 Oct 2022 23:44:29 +0200 Subject: [PATCH] Tests: fix promises handling MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- examples/yourWorker.js | 5 ++- tests/pools/abstract/abstract-pool.test.js | 36 +++++++++---------- .../selection-strategies-utils.test.js | 4 +-- .../selection-strategies.test.js | 4 +++ .../worker-choice-strategy-context.test.js | 6 ++-- 5 files changed, 31 insertions(+), 24 deletions(-) diff --git a/examples/yourWorker.js b/examples/yourWorker.js index 4597f648..3e59f346 100644 --- a/examples/yourWorker.js +++ b/examples/yourWorker.js @@ -1,5 +1,8 @@ 'use strict' const { ThreadWorker } = require('poolifier') +const { isMainThread } = require('worker_threads') + +const debug = false function yourFunction (data) { for (let i = 0; i <= 1000; i++) { @@ -8,7 +11,7 @@ function yourFunction (data) { } JSON.stringify(o) } - // console.log('This is the main thread ' + isMainThread) + debug === true && console.log('This is the main thread ' + isMainThread) return { ok: 1 } } diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index ac776174..0b33e36b 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -74,7 +74,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' @@ -88,7 +88,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, @@ -111,10 +111,10 @@ 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', () => { + it('Simulate worker not found during increaseWorkerRunningTasks', async () => { const pool = new StubPoolWithWorkerTasksUsageMapClear( numberOfWorkers, './tests/worker-files/cluster/testWorker.js' @@ -124,10 +124,10 @@ describe('Abstract pool test suite', () => { expect(() => pool.increaseWorkerRunningTasks()).toThrowError( workerNotFoundInTasksUsageMapError ) - pool.destroy() + await pool.destroy() }) - it('Simulate worker not found during decreaseWorkerRunningTasks', () => { + it('Simulate worker not found during decreaseWorkerRunningTasks', async () => { const pool = new StubPoolWithWorkerTasksUsageMapClear( numberOfWorkers, './tests/worker-files/cluster/testWorker.js', @@ -140,10 +140,10 @@ describe('Abstract pool test suite', () => { expect(() => pool.decreaseWorkerRunningTasks()).toThrowError( workerNotFoundInTasksUsageMapError ) - pool.destroy() + await pool.destroy() }) - it('Simulate worker not found during stepWorkerRunTasks', () => { + it('Simulate worker not found during stepWorkerRunTasks', async () => { const pool = new StubPoolWithWorkerTasksUsageMapClear( numberOfWorkers, './tests/worker-files/cluster/testWorker.js', @@ -156,10 +156,10 @@ describe('Abstract pool test suite', () => { expect(() => pool.stepWorkerRunTasks()).toThrowError( workerNotFoundInTasksUsageMapError ) - pool.destroy() + await pool.destroy() }) - it('Simulate worker not found during updateWorkerTasksRunTime with strategy not requiring it', () => { + it('Simulate worker not found during updateWorkerTasksRunTime with strategy not requiring it', async () => { const pool = new StubPoolWithWorkerTasksUsageMapClear( numberOfWorkers, './tests/worker-files/cluster/testWorker.js', @@ -170,10 +170,10 @@ describe('Abstract pool test suite', () => { // Simulate worker not found. pool.removeAllWorker() expect(() => pool.updateWorkerTasksRunTime()).not.toThrowError() - pool.destroy() + await pool.destroy() }) - it('Simulate worker not found during updateWorkerTasksRunTime with strategy requiring it', () => { + it('Simulate worker not found during updateWorkerTasksRunTime with strategy requiring it', async () => { const pool = new StubPoolWithWorkerTasksUsageMapClear( numberOfWorkers, './tests/worker-files/cluster/testWorker.js', @@ -187,10 +187,10 @@ describe('Abstract pool test suite', () => { 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' @@ -202,7 +202,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 () => { @@ -229,7 +229,7 @@ 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 () => { @@ -257,7 +257,7 @@ describe('Abstract pool test suite', () => { expect(tasksUsage.runTime).toBe(0) expect(tasksUsage.avgRunTime).toBe(0) } - pool.destroy() + await pool.destroy() }) it("Verify that pool event emitter 'busy' event can register a callback", async () => { @@ -275,6 +275,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() }) }) diff --git a/tests/pools/selection-strategies/selection-strategies-utils.test.js b/tests/pools/selection-strategies/selection-strategies-utils.test.js index ba9545f9..945f6eba 100644 --- a/tests/pools/selection-strategies/selection-strategies-utils.test.js +++ b/tests/pools/selection-strategies/selection-strategies-utils.test.js @@ -32,8 +32,8 @@ describe('Selection strategies utils test suite', () => { // sinon.restore() // }) - after(() => { - pool.destroy() + after(async () => { + await pool.destroy() }) it('Verify that getWorkerChoiceStrategy() default return ROUND_ROBIN strategy', () => { diff --git a/tests/pools/selection-strategies/selection-strategies.test.js b/tests/pools/selection-strategies/selection-strategies.test.js index a2365a5d..da4fea96 100644 --- a/tests/pools/selection-strategies/selection-strategies.test.js +++ b/tests/pools/selection-strategies/selection-strategies.test.js @@ -55,6 +55,7 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .requiredStatistics.runTime ).toBe(false) + await pool.destroy() pool = new DynamicThreadPool( min, max, @@ -144,6 +145,7 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .requiredStatistics.runTime ).toBe(false) + await pool.destroy() pool = new DynamicThreadPool( min, max, @@ -227,6 +229,7 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .requiredStatistics.runTime ).toBe(true) + await pool.destroy() pool = new DynamicThreadPool( min, max, @@ -310,6 +313,7 @@ describe('Selection strategies test suite', () => { pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() .requiredStatistics.runTime ).toBe(true) + await pool.destroy() pool = new DynamicThreadPool( min, max, diff --git a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js index 5ffd66a3..c30bda73 100644 --- a/tests/pools/selection-strategies/worker-choice-strategy-context.test.js +++ b/tests/pools/selection-strategies/worker-choice-strategy-context.test.js @@ -45,9 +45,9 @@ describe('Worker choice strategy context test suite', () => { sinon.restore() }) - after(() => { - fixedPool.destroy() - dynamicPool.destroy() + after(async () => { + await fixedPool.destroy() + await dynamicPool.destroy() }) it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => { -- 2.34.1