From b97d82d8304962979d02da4ac29f997b454c7d84 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 11 Jul 2023 18:29:07 +0200 Subject: [PATCH] fix: fix pool statuses semantic MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/abstract-pool.ts | 38 +++++++++++++++------- tests/pools/abstract/abstract-pool.test.js | 12 +++++-- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index a3b46cee..6047b2f7 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -128,7 +128,13 @@ export abstract class AbstractPool< this.setupHook() - while (this.workerNodes.length < this.numberOfWorkers) { + while ( + this.workerNodes.reduce( + (accumulator, workerNode) => + !workerNode.info.dynamic ? accumulator + 1 : accumulator, + 0 + ) < this.numberOfWorkers + ) { this.createAndSetupWorker() } @@ -172,9 +178,9 @@ export abstract class AbstractPool< throw new RangeError( 'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size' ) - } else if (min === 0 && max === 0) { + } else if (max === 0) { throw new RangeError( - 'Cannot instantiate a dynamic pool with a minimum pool size and a maximum pool size equal to zero' + 'Cannot instantiate a dynamic pool with a pool size equal to zero' ) } else if (min === max) { throw new RangeError( @@ -411,16 +417,24 @@ export abstract class AbstractPool< } private get starting (): boolean { - return this.workerNodes.length < this.minSize + return ( + this.workerNodes.reduce( + (accumulator, workerNode) => + !workerNode.info.dynamic ? accumulator + 1 : accumulator, + 0 + ) < this.minSize + ) } private get ready (): boolean { return ( - this.workerNodes.length >= this.minSize && - this.workerNodes.every( - (workerNode, workerNodeKey) => - workerNodeKey < this.minSize && workerNode.info.ready - ) + this.workerNodes.reduce( + (accumulator, workerNode) => + !workerNode.info.dynamic && workerNode.info.ready + ? accumulator + 1 + : accumulator, + 0 + ) >= this.minSize ) } @@ -979,7 +993,9 @@ export abstract class AbstractPool< } }) const workerInfo = this.getWorkerInfo(this.getWorkerNodeKey(worker)) - workerInfo.ready = true + if (this.workerChoiceStrategyContext.getStrategyPolicy().useDynamicWorker) { + workerInfo.ready = true + } workerInfo.dynamic = true this.sendToWorker(worker, { checkAlive: true, @@ -1135,7 +1151,7 @@ export abstract class AbstractPool< */ private pushWorkerNode (worker: Worker): number { const workerNode = new WorkerNode(worker, this.worker) - // Flag the worker as ready at pool startup. + // Flag the worker node as ready at pool startup. if (this.starting) { workerNode.info.ready = true } diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index c57a8f0c..2c8d2318 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -106,7 +106,7 @@ describe('Abstract pool test suite', () => { new DynamicThreadPool(0, 0, './tests/worker-files/thread/testWorker.js') ).toThrowError( new RangeError( - 'Cannot instantiate a dynamic pool with a minimum pool size and a maximum pool size equal to zero' + 'Cannot instantiate a dynamic pool with a pool size equal to zero' ) ) }) @@ -655,7 +655,13 @@ describe('Abstract pool test suite', () => { } }) expect(workerNode.usage.tasks.executed).toBeGreaterThan(0) - expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(maxMultiplier) + expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual( + numberOfWorkers * maxMultiplier + ) + expect(workerNode.usage.runTime.history.length).toBe(0) + expect(workerNode.usage.waitTime.history.length).toBe(0) + expect(workerNode.usage.elu.idle.history.length).toBe(0) + expect(workerNode.usage.elu.active.history.length).toBe(0) } pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE) for (const workerNode of pool.workerNodes) { @@ -684,6 +690,8 @@ describe('Abstract pool test suite', () => { }) expect(workerNode.usage.runTime.history.length).toBe(0) expect(workerNode.usage.waitTime.history.length).toBe(0) + expect(workerNode.usage.elu.idle.history.length).toBe(0) + expect(workerNode.usage.elu.active.history.length).toBe(0) } await pool.destroy() }) -- 2.34.1