From: Jérôme Benoit Date: Mon, 11 Dec 2023 14:20:33 +0000 (+0100) Subject: fix: fix worker options handling X-Git-Tag: v3.0.11~5 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=9b5c72ff77584e1e2610710b66261eb31dabb954;p=poolifier.git fix: fix worker options handling Signed-off-by: Jérôme Benoit --- diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 15259106..ae95dcf8 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -79,7 +79,6 @@ export abstract class AbstractWorker< /** * Constructs a new poolifier worker. * - * @param type - The type of async event. * @param isMain - Whether this is the main worker or not. * @param mainWorker - Reference to main worker. * @param taskFunctions - Task function(s) processed by the worker when the pool's `execution` function is invoked. The first function is the default function. diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index fdbab44e..756550e3 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -30,7 +30,7 @@ export class ThreadWorker< /** * Message port used to communicate with the main worker. */ - private port!: MessagePort + private port?: MessagePort /** * Constructs a new poolifier thread worker. * @@ -41,7 +41,7 @@ export class ThreadWorker< taskFunctions: TaskFunction | TaskFunctions, opts: WorkerOptions = {} ) { - super(isMainThread, parentPort as MessagePort, taskFunctions) + super(isMainThread, parentPort as MessagePort, taskFunctions, opts) } /** @inheritDoc */ @@ -81,7 +81,7 @@ export class ThreadWorker< /** @inheritDoc */ protected sendToMainWorker (message: MessageValue): void { - this.port.postMessage({ ...message, workerId: this.id }) + this.port?.postMessage({ ...message, workerId: this.id }) } /** diff --git a/tests/pools/cluster/dynamic.test.mjs b/tests/pools/cluster/dynamic.test.mjs index 93f995fc..6e8580e9 100644 --- a/tests/pools/cluster/dynamic.test.mjs +++ b/tests/pools/cluster/dynamic.test.mjs @@ -37,10 +37,10 @@ describe('Dynamic cluster pool test suite', () => { expect(poolBusy).toBe(1) const numberOfExitEvents = await waitWorkerEvents(pool, 'exit', max - min) expect(numberOfExitEvents).toBe(max - min) + expect(pool.workerNodes.length).toBe(min) }) it('Verify scale worker up and down is working', async () => { - expect(pool.workerNodes.length).toBe(min) for (let i = 0; i < max * 2; i++) { pool.execute() } diff --git a/tests/pools/thread/dynamic.test.mjs b/tests/pools/thread/dynamic.test.mjs index bf7f9527..efcc417e 100644 --- a/tests/pools/thread/dynamic.test.mjs +++ b/tests/pools/thread/dynamic.test.mjs @@ -37,10 +37,10 @@ describe('Dynamic thread pool test suite', () => { expect(poolBusy).toBe(1) const numberOfExitEvents = await waitWorkerEvents(pool, 'exit', max - min) expect(numberOfExitEvents).toBe(max - min) + expect(pool.workerNodes.length).toBe(min) }) it('Verify scale thread up and down is working', async () => { - expect(pool.workerNodes.length).toBe(min) for (let i = 0; i < max * 2; i++) { pool.execute() }