fix: fix pool ready status with zero min size dynamic pool
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 23 Aug 2024 13:32:56 +0000 (15:32 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 23 Aug 2024 13:32:56 +0000 (15:32 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts
tests/pools/abstract-pool.test.mjs
tests/pools/cluster/dynamic.test.mjs
tests/pools/cluster/fixed.test.mjs
tests/pools/thread/dynamic.test.mjs
tests/pools/thread/fixed.test.mjs

index f04f03f89634139f4deef78a81b507dfb2ab3219..7fbdd0b95c39667f60a1755a4d51392775f9c55b 100644 (file)
@@ -917,7 +917,6 @@ export abstract class AbstractPool<
   private checkAndEmitEmptyEvent (): void {
     if (this.empty) {
       this.emitter?.emit(PoolEvents.empty, this.info)
-      this.readyEventEmitted = false
     }
   }
 
@@ -2293,9 +2292,12 @@ export abstract class AbstractPool<
    * @returns The pool readiness boolean status.
    */
   private get ready (): boolean {
-    if (this.empty) {
+    if (!this.started) {
       return false
     }
+    if (this.empty) {
+      return true
+    }
     return (
       this.workerNodes.reduce(
         (accumulator, workerNode) =>
index 0e933d7aaffd23bff7cf8088542c4e57f10e66e2..60532effd45915c7c17fc9b47ac687bc65ba218c 100644 (file)
@@ -77,6 +77,9 @@ describe('Abstract pool test suite', () => {
     expect(pool.starting).toBe(false)
     expect(pool.destroying).toBe(false)
     await pool.destroy()
+    expect(pool.started).toBe(false)
+    expect(pool.starting).toBe(false)
+    expect(pool.destroying).toBe(false)
   })
 
   it('Verify that filePath is checked', () => {
index 2ac250ca411e4f4c59c7d92fc97094d99e385d22..d9b33d40823c6035c16577741a99f0b7754a6d29 100644 (file)
@@ -75,7 +75,7 @@ describe('Dynamic cluster pool test suite', () => {
     ])
     await pool.destroy()
     const numberOfExitEvents = await exitPromise
-    expect(pool.started).toBe(false)
+    expect(pool.info.started).toBe(false)
     expect(pool.emitter.eventNames()).toStrictEqual([
       PoolEvents.busy,
       PoolEvents.destroy,
@@ -161,12 +161,16 @@ describe('Dynamic cluster pool test suite', () => {
         max,
         './tests/worker-files/cluster/testWorker.cjs',
         {
+          startWorkers: false,
           workerChoiceStrategy,
         }
       )
-      expect(pool.starting).toBe(false)
-      expect(pool.readyEventEmitted).toBe(false)
       for (let run = 0; run < 2; run++) {
+        expect(pool.info.started).toBe(false)
+        expect(pool.info.ready).toBe(false)
+        pool.start()
+        expect(pool.info.started).toBe(true)
+        expect(pool.info.ready).toBe(true)
         // eslint-disable-next-line @typescript-eslint/no-unused-expressions
         run % 2 !== 0 && pool.enableTasksQueue(true)
         const maxMultiplier = 4
@@ -176,15 +180,13 @@ describe('Dynamic cluster pool test suite', () => {
           promises.add(pool.execute())
         }
         await Promise.all(promises)
-        expect(pool.readyEventEmitted).toBe(true)
         expect(pool.workerNodes.length).toBeGreaterThan(pool.info.minSize)
         expect(pool.workerNodes.length).toBeLessThanOrEqual(pool.info.maxSize)
         await waitPoolEvents(pool, PoolEvents.empty, 1)
-        expect(pool.readyEventEmitted).toBe(false)
         expect(pool.workerNodes.length).toBe(pool.info.minSize)
+        // We need to clean up the resources after our test
+        await pool.destroy()
       }
-      // We need to clean up the resources after our test
-      await pool.destroy()
     }
   })
 })
index 30bce79e7108714bc457e3b159ab681969f36878..88a7a6f7bb89c3d3e8ec81693f0046e047f51265 100644 (file)
@@ -274,7 +274,7 @@ describe('Fixed cluster pool test suite', () => {
     ])
     await pool.destroy()
     const numberOfExitEvents = await exitPromise
-    expect(pool.started).toBe(false)
+    expect(pool.info.started).toBe(false)
     expect(pool.emitter.eventNames()).toStrictEqual([
       PoolEvents.busy,
       PoolEvents.destroy,
index 1c626d61a1b8210493f9a934e1ec1bfca0698b3e..9a7082a50b155f642f72504866f8651f9b74bf6d 100644 (file)
@@ -75,7 +75,7 @@ describe('Dynamic thread pool test suite', () => {
     ])
     await pool.destroy()
     const numberOfExitEvents = await exitPromise
-    expect(pool.started).toBe(false)
+    expect(pool.info.started).toBe(false)
     expect(pool.emitter.eventNames()).toStrictEqual([
       PoolEvents.busy,
       PoolEvents.destroy,
@@ -161,12 +161,16 @@ describe('Dynamic thread pool test suite', () => {
         max,
         './tests/worker-files/thread/testWorker.mjs',
         {
+          startWorkers: false,
           workerChoiceStrategy,
         }
       )
-      expect(pool.starting).toBe(false)
-      expect(pool.readyEventEmitted).toBe(false)
       for (let run = 0; run < 2; run++) {
+        expect(pool.info.started).toBe(false)
+        expect(pool.info.ready).toBe(false)
+        pool.start()
+        expect(pool.info.started).toBe(true)
+        expect(pool.info.ready).toBe(true)
         // eslint-disable-next-line @typescript-eslint/no-unused-expressions
         run % 2 !== 0 && pool.enableTasksQueue(true)
         const maxMultiplier = 4
@@ -176,15 +180,13 @@ describe('Dynamic thread pool test suite', () => {
           promises.add(pool.execute())
         }
         await Promise.all(promises)
-        expect(pool.readyEventEmitted).toBe(true)
         expect(pool.workerNodes.length).toBeGreaterThan(pool.info.minSize)
         expect(pool.workerNodes.length).toBeLessThanOrEqual(pool.info.maxSize)
         await waitPoolEvents(pool, PoolEvents.empty, 1)
-        expect(pool.readyEventEmitted).toBe(false)
         expect(pool.workerNodes.length).toBe(pool.info.minSize)
+        // We need to clean up the resources after our test
+        await pool.destroy()
       }
-      // We need to clean up the resources after our test
-      await pool.destroy()
     }
   })
 })
index 78ef51ff013f45e9b548fb23ea83f8f47e6085b6..0a527ff13565dfd83d89bdb99ec2b9f6b0560464 100644 (file)
@@ -304,7 +304,7 @@ describe('Fixed thread pool test suite', () => {
     ])
     await pool.destroy()
     const numberOfExitEvents = await exitPromise
-    expect(pool.started).toBe(false)
+    expect(pool.info.started).toBe(false)
     expect(pool.emitter.eventNames()).toStrictEqual([
       PoolEvents.busy,
       PoolEvents.destroy,