From: Jérôme Benoit Date: Tue, 6 Jun 2023 21:54:51 +0000 (+0200) Subject: test: improve task error handling X-Git-Tag: v2.5.4~10 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=d46660cd0cccf38566337876c3fad0c3795aeb94;p=poolifier.git test: improve task error handling Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts index 67332c70..0370212f 100644 --- a/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-busy-worker-choice-strategy.ts @@ -54,10 +54,6 @@ export class LeastBusyWorkerChoiceStrategy< /** @inheritDoc */ public choose (): number { - // const freeWorkerNodeKey = this.findFreeWorkerNodeKey() - // if (freeWorkerNodeKey !== -1) { - // return freeWorkerNodeKey - // } let minRunTime = Infinity let leastBusyWorkerNodeKey!: number for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index e85f1127..dbda7af7 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -469,7 +469,11 @@ describe('Abstract pool test suite', () => { ) const promises = new Set() let poolFull = 0 - pool.emitter.on(PoolEvents.full, () => ++poolFull) + let poolInfo + pool.emitter.on(PoolEvents.full, info => { + ++poolFull + poolInfo = info + }) for (let i = 0; i < numberOfWorkers * 2; i++) { promises.add(pool.execute()) } @@ -477,6 +481,18 @@ describe('Abstract pool test suite', () => { // The `full` 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 * 2 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool with min = max = numberOfWorkers. expect(poolFull).toBe(numberOfWorkers * 2) + expect(poolInfo).toStrictEqual({ + type: PoolTypes.dynamic, + worker: WorkerTypes.thread, + minSize: expect.any(Number), + maxSize: expect.any(Number), + workerNodes: expect.any(Number), + idleWorkerNodes: expect.any(Number), + busyWorkerNodes: expect.any(Number), + runningTasks: expect.any(Number), + queuedTasks: expect.any(Number), + maxQueuedTasks: expect.any(Number) + }) await pool.destroy() }) @@ -487,7 +503,11 @@ describe('Abstract pool test suite', () => { ) const promises = new Set() let poolBusy = 0 - pool.emitter.on(PoolEvents.busy, () => ++poolBusy) + let poolInfo + pool.emitter.on(PoolEvents.busy, info => { + ++poolBusy + poolInfo = info + }) for (let i = 0; i < numberOfWorkers * 2; i++) { promises.add(pool.execute()) } @@ -495,6 +515,18 @@ 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) + expect(poolInfo).toStrictEqual({ + type: PoolTypes.fixed, + worker: WorkerTypes.thread, + minSize: expect.any(Number), + maxSize: expect.any(Number), + workerNodes: expect.any(Number), + idleWorkerNodes: expect.any(Number), + busyWorkerNodes: expect.any(Number), + runningTasks: expect.any(Number), + queuedTasks: expect.any(Number), + maxQueuedTasks: expect.any(Number) + }) await pool.destroy() }) diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index 4980f750..2cba2d8b 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -131,6 +131,10 @@ describe('Fixed cluster pool test suite', () => { it('Verify that error handling is working properly:sync', async () => { const data = { f: 10 } + let taskError + errorPool.emitter.on(PoolEvents.taskError, e => { + taskError = e + }) let inError try { await errorPool.execute(data) @@ -140,6 +144,10 @@ describe('Fixed cluster pool test suite', () => { expect(inError).toBeDefined() expect(typeof inError === 'string').toBe(true) expect(inError).toBe('Error Message from ClusterWorker') + expect(taskError).toStrictEqual({ + error: 'Error Message from ClusterWorker', + errorData: data + }) expect( errorPool.workerNodes.some( workerNode => workerNode.tasksUsage.error === 1 @@ -149,6 +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 inError try { await asyncErrorPool.execute(data) @@ -158,6 +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( asyncErrorPool.workerNodes.some( workerNode => workerNode.tasksUsage.error === 1 diff --git a/tests/pools/thread/fixed.test.js b/tests/pools/thread/fixed.test.js index c8824280..48dd10b6 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -131,6 +131,10 @@ describe('Fixed thread pool test suite', () => { it('Verify that error handling is working properly:sync', async () => { const data = { f: 10 } + let taskError + errorPool.emitter.on(PoolEvents.taskError, e => { + taskError = e + }) let inError try { await errorPool.execute(data) @@ -142,6 +146,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') + expect(taskError).toStrictEqual({ + error: new Error('Error Message from ThreadWorker'), + errorData: data + }) expect( errorPool.workerNodes.some( workerNode => workerNode.tasksUsage.error === 1 @@ -151,6 +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 inError try { await asyncErrorPool.execute(data) @@ -162,6 +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( asyncErrorPool.workerNodes.some( workerNode => workerNode.tasksUsage.error === 1