From 9b5c72ff77584e1e2610710b66261eb31dabb954 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 11 Dec 2023 15:20:33 +0100 Subject: [PATCH] fix: fix worker options handling MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/worker/abstract-worker.ts | 1 - src/worker/thread-worker.ts | 6 +++--- tests/pools/cluster/dynamic.test.mjs | 2 +- tests/pools/thread/dynamic.test.mjs | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) 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() } -- 2.34.1