From 4f0b85b32d9241579bbfd4759c44d96e4c968a25 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 7 Jun 2023 00:07:53 +0200 Subject: [PATCH] test: improve task error handling MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- tests/pools/cluster/dynamic.test.js | 10 +++++----- tests/pools/cluster/fixed.test.js | 18 +++++++++--------- tests/pools/thread/dynamic.test.js | 10 +++++----- tests/pools/thread/fixed.test.js | 18 +++++++++--------- tests/test-utils.js | 17 ++++++++++++++++- 5 files changed, 44 insertions(+), 29 deletions(-) diff --git a/tests/pools/cluster/dynamic.test.js b/tests/pools/cluster/dynamic.test.js index c737c1d9..291629f5 100644 --- a/tests/pools/cluster/dynamic.test.js +++ b/tests/pools/cluster/dynamic.test.js @@ -37,7 +37,7 @@ describe('Dynamic cluster pool test suite', () => { // The `busy` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool. // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool. expect(poolBusy).toBe(max + 1) - const numberOfExitEvents = await TestUtils.waitExits(pool, max - min) + const numberOfExitEvents = await TestUtils.waitWorkerExits(pool, max - min) expect(numberOfExitEvents).toBe(max - min) }) @@ -47,18 +47,18 @@ describe('Dynamic cluster pool test suite', () => { pool.execute() } expect(pool.workerNodes.length).toBeGreaterThan(min) - await TestUtils.waitExits(pool, max - min) + await TestUtils.waitWorkerExits(pool, max - min) expect(pool.workerNodes.length).toBe(min) for (let i = 0; i < max * 2; i++) { pool.execute() } expect(pool.workerNodes.length).toBeGreaterThan(min) - await TestUtils.waitExits(pool, max - min) + await TestUtils.waitWorkerExits(pool, max - min) expect(pool.workerNodes.length).toBe(min) }) it('Shutdown test', async () => { - const exitPromise = TestUtils.waitExits(pool, min) + const exitPromise = TestUtils.waitWorkerExits(pool, min) await pool.destroy() const numberOfExitEvents = await exitPromise expect(numberOfExitEvents).toBe(min) @@ -98,7 +98,7 @@ describe('Dynamic cluster pool test suite', () => { longRunningPool.execute() } expect(longRunningPool.workerNodes.length).toBe(max) - await TestUtils.waitExits(longRunningPool, max - min) + await TestUtils.waitWorkerExits(longRunningPool, max - min) expect(longRunningPool.workerNodes.length).toBe(min) expect( longRunningPool.workerChoiceStrategyContext.workerChoiceStrategies.get( diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index 2cba2d8b..c6cdce54 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -157,10 +157,10 @@ describe('Fixed cluster pool test suite', () => { it('Verify that error handling is working properly:async', async () => { const data = { f: 10 } - // let taskError - // errorPool.emitter.on(PoolEvents.taskError, e => { - // taskError = e - // }) + let taskError + asyncErrorPool.emitter.on(PoolEvents.taskError, e => { + taskError = e + }) let inError try { await asyncErrorPool.execute(data) @@ -170,10 +170,10 @@ describe('Fixed cluster pool test suite', () => { expect(inError).toBeDefined() expect(typeof inError === 'string').toBe(true) expect(inError).toBe('Error Message from ClusterWorker:async') - // expect(taskError).toStrictEqual({ - // error: 'Error Message from ClusterWorker:async', - // errorData: data - // }) + expect(taskError).toStrictEqual({ + error: 'Error Message from ClusterWorker:async', + errorData: data + }) expect( asyncErrorPool.workerNodes.some( workerNode => workerNode.tasksUsage.error === 1 @@ -191,7 +191,7 @@ describe('Fixed cluster pool test suite', () => { }) it('Shutdown test', async () => { - const exitPromise = TestUtils.waitExits(pool, numberOfWorkers) + const exitPromise = TestUtils.waitWorkerExits(pool, numberOfWorkers) await pool.destroy() const numberOfExitEvents = await exitPromise expect(numberOfExitEvents).toBe(numberOfWorkers) diff --git a/tests/pools/thread/dynamic.test.js b/tests/pools/thread/dynamic.test.js index 1f2a6463..404f2113 100644 --- a/tests/pools/thread/dynamic.test.js +++ b/tests/pools/thread/dynamic.test.js @@ -37,7 +37,7 @@ describe('Dynamic thread pool test suite', () => { // The `busy` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool. // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool. expect(poolBusy).toBe(max + 1) - const numberOfExitEvents = await TestUtils.waitExits(pool, max - min) + const numberOfExitEvents = await TestUtils.waitWorkerExits(pool, max - min) expect(numberOfExitEvents).toBe(max - min) }) @@ -47,18 +47,18 @@ describe('Dynamic thread pool test suite', () => { pool.execute() } expect(pool.workerNodes.length).toBe(max) - await TestUtils.waitExits(pool, max - min) + await TestUtils.waitWorkerExits(pool, max - min) expect(pool.workerNodes.length).toBe(min) for (let i = 0; i < max * 2; i++) { pool.execute() } expect(pool.workerNodes.length).toBe(max) - await TestUtils.waitExits(pool, max - min) + await TestUtils.waitWorkerExits(pool, max - min) expect(pool.workerNodes.length).toBe(min) }) it('Shutdown test', async () => { - const exitPromise = TestUtils.waitExits(pool, min) + const exitPromise = TestUtils.waitWorkerExits(pool, min) await pool.destroy() const numberOfExitEvents = await exitPromise expect(numberOfExitEvents).toBe(min) @@ -98,7 +98,7 @@ describe('Dynamic thread pool test suite', () => { longRunningPool.execute() } expect(longRunningPool.workerNodes.length).toBe(max) - await TestUtils.waitExits(longRunningPool, max - min) + await TestUtils.waitWorkerExits(longRunningPool, max - min) expect(longRunningPool.workerNodes.length).toBe(min) expect( longRunningPool.workerChoiceStrategyContext.workerChoiceStrategies.get( diff --git a/tests/pools/thread/fixed.test.js b/tests/pools/thread/fixed.test.js index 48dd10b6..5dd97665 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -159,10 +159,10 @@ describe('Fixed thread pool test suite', () => { it('Verify that error handling is working properly:async', async () => { const data = { f: 10 } - // let taskError - // errorPool.emitter.on(PoolEvents.taskError, e => { - // taskError = e - // }) + let taskError + asyncErrorPool.emitter.on(PoolEvents.taskError, e => { + taskError = e + }) let inError try { await asyncErrorPool.execute(data) @@ -174,10 +174,10 @@ describe('Fixed thread pool test suite', () => { expect(inError.message).toBeDefined() expect(typeof inError.message === 'string').toBe(true) expect(inError.message).toBe('Error Message from ThreadWorker:async') - // expect(taskError).toStrictEqual({ - // error: new Error('Error Message from ThreadWorker:async'), - // errorData: data - // }) + expect(taskError).toStrictEqual({ + error: new Error('Error Message from ThreadWorker:async'), + errorData: data + }) expect( asyncErrorPool.workerNodes.some( workerNode => workerNode.tasksUsage.error === 1 @@ -195,7 +195,7 @@ describe('Fixed thread pool test suite', () => { }) it('Shutdown test', async () => { - const exitPromise = TestUtils.waitExits(pool, numberOfThreads) + const exitPromise = TestUtils.waitWorkerExits(pool, numberOfThreads) await pool.destroy() const numberOfExitEvents = await exitPromise expect(numberOfExitEvents).toBe(numberOfThreads) diff --git a/tests/test-utils.js b/tests/test-utils.js index 0e71c718..cba320a7 100644 --- a/tests/test-utils.js +++ b/tests/test-utils.js @@ -1,7 +1,7 @@ const { WorkerFunctions } = require('./test-types') class TestUtils { - static async waitExits (pool, numberOfExitEventsToWait) { + static async waitWorkerExits (pool, numberOfExitEventsToWait) { return new Promise(resolve => { let exitEvents = 0 if (numberOfExitEventsToWait === 0) { @@ -18,6 +18,21 @@ class TestUtils { }) } + static async waitPoolEvents (pool, poolEvent, numberOfEventsToWait) { + return new Promise(resolve => { + let events = 0 + if (numberOfEventsToWait === 0) { + resolve(events) + } + pool.emitter.on(poolEvent, () => { + ++events + if (events === numberOfEventsToWait) { + resolve(events) + } + }) + }) + } + static async sleep (ms) { return new Promise(resolve => setTimeout(resolve, ms)) } -- 2.34.1