From: Jérôme Benoit Date: Tue, 6 Jun 2023 22:07:53 +0000 (+0200) Subject: test: improve task error handling X-Git-Tag: v2.6.0~7^2~22 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;ds=sidebyside;h=fe2dcd443af649ee75e8e66a3cff4174f072b015;hp=cc750dcada47e9186271e0918b9b0bb8c77ba639;p=poolifier.git test: improve task error handling Signed-off-by: Jérôme Benoit --- 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)) }