From 33e6bb4c6f1061542693dbd996ffc08c62e8beeb Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 19 Aug 2023 11:28:14 +0200 Subject: [PATCH] fix: ensure pool event full is emitted only once MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- CHANGELOG.md | 5 +++++ src/pools/abstract-pool.ts | 22 ++++++++++++++-------- tests/pools/abstract/abstract-pool.test.js | 4 +--- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae8fefd8..292bba15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Ensure pool event `backPressure` is emitted. +- Ensure pool event `full` is emitted only once. + ## [2.6.29] - 2023-08-18 ### Fixed diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index dceef6de..67086606 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -122,6 +122,8 @@ export abstract class AbstractPool< this.checkAndEmitTaskExecutionEvents.bind(this) this.checkAndEmitTaskQueuingEvents = this.checkAndEmitTaskQueuingEvents.bind(this) + this.checkAndEmitDynamicWorkerCreationEvents = + this.checkAndEmitDynamicWorkerCreationEvents.bind(this) if (this.opts.enableEvents === true) { this.emitter = new PoolEmitter() @@ -749,7 +751,7 @@ export abstract class AbstractPool< await this.destroyWorkerNode(workerNodeKey) }) ) - this.emitter?.emit(PoolEvents.destroy) + this.emitter?.emit(PoolEvents.destroy, this.info) } protected async sendKillMessageToWorker ( @@ -1061,6 +1063,7 @@ export abstract class AbstractPool< if (this.workerChoiceStrategyContext.getStrategyPolicy().useDynamicWorker) { workerInfo.ready = true } + this.checkAndEmitDynamicWorkerCreationEvents() return workerNodeKey } @@ -1227,13 +1230,8 @@ export abstract class AbstractPool< } private checkAndEmitTaskExecutionEvents (): void { - if (this.emitter != null) { - if (this.busy) { - this.emitter.emit(PoolEvents.busy, this.info) - } - if (this.type === PoolTypes.dynamic && this.full) { - this.emitter.emit(PoolEvents.full, this.info) - } + if (this.busy) { + this.emitter?.emit(PoolEvents.busy, this.info) } } @@ -1243,6 +1241,14 @@ export abstract class AbstractPool< } } + private checkAndEmitDynamicWorkerCreationEvents (): void { + if (this.type === PoolTypes.dynamic) { + if (this.full) { + this.emitter?.emit(PoolEvents.full, this.info) + } + } + } + /** * Gets the worker information given its worker node key. * diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index ec552e98..0ecd3944 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -851,9 +851,7 @@ describe('Abstract pool test suite', () => { promises.add(pool.execute()) } await Promise.all(promises) - // The `full` event is triggered when the number of submitted tasks at once reach the maximum number of workers in the dynamic pool. - // So in total numberOfWorkers * 2 - 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool with min = (max = numberOfWorkers) / 2. - expect(poolFull).toBe(numberOfWorkers * 2 - 1) + expect(poolFull).toBe(1) expect(poolInfo).toStrictEqual({ version, type: PoolTypes.dynamic, -- 2.34.1