fix: ensure pool event full is emitted only once
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 19 Aug 2023 09:28:14 +0000 (11:28 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 19 Aug 2023 09:28:14 +0000 (11:28 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
CHANGELOG.md
src/pools/abstract-pool.ts
tests/pools/abstract/abstract-pool.test.js

index ae8fefd89c6e7212003d615f43c68a2def3ee9ca..292bba15cd8cdf5f111a9bf8f3bfc7e565903318 100644 (file)
@@ -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
index dceef6decb534f083510d90a947a05861c2ec2df..670866069d97dfff46e817c29f54810370dec552 100644 (file)
@@ -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.
    *
index ec552e986003d859f38fe0557d6d8727c23d7ceb..0ecd39440efb43718400eeed0e3ef2b92e357736 100644 (file)
@@ -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,