From: Jérôme Benoit Date: Sat, 19 Aug 2023 09:28:14 +0000 (+0200) Subject: fix: ensure pool event full is emitted only once X-Git-Tag: v2.6.30~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=33e6bb4c6f1061542693dbd996ffc08c62e8beeb;p=poolifier.git fix: ensure pool event full is emitted only once Signed-off-by: Jérôme Benoit --- 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,