From e48fa7a7cd32d0b42df79043b773fcb1e6f2c778 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 23 Aug 2024 15:32:56 +0200 Subject: [PATCH] fix: fix pool ready status with zero min size dynamic pool 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 | 6 ++++-- tests/pools/abstract-pool.test.mjs | 3 +++ tests/pools/cluster/dynamic.test.mjs | 16 +++++++++------- tests/pools/cluster/fixed.test.mjs | 2 +- tests/pools/thread/dynamic.test.mjs | 16 +++++++++------- tests/pools/thread/fixed.test.mjs | 2 +- 6 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index f04f03f8..7fbdd0b9 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -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) => diff --git a/tests/pools/abstract-pool.test.mjs b/tests/pools/abstract-pool.test.mjs index 0e933d7a..60532eff 100644 --- a/tests/pools/abstract-pool.test.mjs +++ b/tests/pools/abstract-pool.test.mjs @@ -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', () => { diff --git a/tests/pools/cluster/dynamic.test.mjs b/tests/pools/cluster/dynamic.test.mjs index 2ac250ca..d9b33d40 100644 --- a/tests/pools/cluster/dynamic.test.mjs +++ b/tests/pools/cluster/dynamic.test.mjs @@ -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() } }) }) diff --git a/tests/pools/cluster/fixed.test.mjs b/tests/pools/cluster/fixed.test.mjs index 30bce79e..88a7a6f7 100644 --- a/tests/pools/cluster/fixed.test.mjs +++ b/tests/pools/cluster/fixed.test.mjs @@ -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, diff --git a/tests/pools/thread/dynamic.test.mjs b/tests/pools/thread/dynamic.test.mjs index 1c626d61..9a7082a5 100644 --- a/tests/pools/thread/dynamic.test.mjs +++ b/tests/pools/thread/dynamic.test.mjs @@ -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() } }) }) diff --git a/tests/pools/thread/fixed.test.mjs b/tests/pools/thread/fixed.test.mjs index 78ef51ff..0a527ff1 100644 --- a/tests/pools/thread/fixed.test.mjs +++ b/tests/pools/thread/fixed.test.mjs @@ -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, -- 2.34.1