From 3ec964d666b2ffa57b57a37a29542a727fc55ee6 Mon Sep 17 00:00:00 2001 From: aardizio Date: Tue, 16 Feb 2021 14:57:53 +0100 Subject: [PATCH] Add test cases and fixed some sonar code smells --- src/pools/cluster/dynamic.ts | 15 +++--- src/pools/thread/dynamic.ts | 15 +++--- src/worker/abstract-worker.ts | 1 - tests/pools/abstract/abstract-pool.test.js | 55 ++++++++++++++++++++++ 4 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 tests/pools/abstract/abstract-pool.test.js diff --git a/src/pools/cluster/dynamic.ts b/src/pools/cluster/dynamic.ts index eb93c0c8..bfafd66b 100644 --- a/src/pools/cluster/dynamic.ts +++ b/src/pools/cluster/dynamic.ts @@ -60,17 +60,16 @@ export class DynamicClusterPool< } // All workers are busy, create a new worker - const worker = this.createAndSetupWorker() - this.registerWorkerMessageListener(worker, message => { - const tasksInProgress = this.tasks.get(worker) - const isKillBehaviorOptionHard = - message.kill === killBehaviorTypes.HARD + const workerCreated = this.createAndSetupWorker() + this.registerWorkerMessageListener(workerCreated, message => { + const tasksInProgress = this.tasks.get(workerCreated) + const isKillBehaviorOptionHard = message.kill === killBehaviorTypes.HARD if (isKillBehaviorOptionHard || tasksInProgress === 0) { // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime) - this.sendToWorker(worker, { kill: 1 }) - void this.destroyWorker(worker) + this.sendToWorker(workerCreated, { kill: 1 }) + void this.destroyWorker(workerCreated) } }) - return worker + return workerCreated } } diff --git a/src/pools/thread/dynamic.ts b/src/pools/thread/dynamic.ts index f068b4a2..4a2fd5e8 100644 --- a/src/pools/thread/dynamic.ts +++ b/src/pools/thread/dynamic.ts @@ -60,17 +60,16 @@ export class DynamicThreadPool< } // All workers are busy, create a new worker - const worker = this.createAndSetupWorker() - this.registerWorkerMessageListener(worker, message => { - const tasksInProgress = this.tasks.get(worker) - const isKillBehaviorOptionHard = - message.kill === killBehaviorTypes.HARD + const workerCreated = this.createAndSetupWorker() + this.registerWorkerMessageListener(workerCreated, message => { + const tasksInProgress = this.tasks.get(workerCreated) + const isKillBehaviorOptionHard = message.kill === killBehaviorTypes.HARD if (isKillBehaviorOptionHard || tasksInProgress === 0) { // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime) - this.sendToWorker(worker, { kill: 1 }) - void this.destroyWorker(worker) + this.sendToWorker(workerCreated, { kill: 1 }) + void this.destroyWorker(workerCreated) } }) - return worker + return workerCreated } } diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index b65ed590..768c8d95 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -3,7 +3,6 @@ import type { Worker } from 'cluster' import type { MessagePort } from 'worker_threads' import type { MessageValue, KillBehavior } from '../utility-types' import type { WorkerOptions } from './worker-options' -// import { killBehaviorTypes } from './worker-options' const defaultMaxInactiveTime = 1000 * 60 // TODO fix this and avoid that SOFT/HARD words are replicated so much times into the project diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js new file mode 100644 index 00000000..d5d87058 --- /dev/null +++ b/tests/pools/abstract/abstract-pool.test.js @@ -0,0 +1,55 @@ +const expect = require('expect') +const { FixedThreadPool } = require('../../../lib/index') +const expectedError = new Error('Worker could not be found in tasks map') + +class StubPoolWithTasksMapClear extends FixedThreadPool { + removeAllWorker () { + this.tasks.clear() + } +} + +class StubPoolWithIsMainMethod extends FixedThreadPool { + isMain () { + return false + } +} + +describe('Abstract pool test suite ', () => { + it('Simulate worker not found during increaseWorkersTask', () => { + const pool = new StubPoolWithTasksMapClear( + 1, + './tests/worker/cluster/testWorker.js', + { + errorHandler: e => console.error(e) + } + ) + // simulate worker not found. + pool.removeAllWorker() + expect(() => pool.increaseWorkersTask()).toThrowError(expectedError) + }) + + it('Simulate worker not found during decreaseWorkersTasks', () => { + const pool = new StubPoolWithTasksMapClear( + 1, + './tests/worker/cluster/testWorker.js', + { + errorHandler: e => console.error(e) + } + ) + // simulate worker not found. + pool.removeAllWorker() + expect(() => pool.decreaseWorkersTasks()).toThrowError(expectedError) + }) + + it('Simulate pool creation from a non main thread/process', () => { + expect(() => { + const pool = new StubPoolWithIsMainMethod( + 1, + './tests/worker/cluster/testWorker.js', + { + errorHandler: e => console.error(e) + } + ) + }).toThrowError() + }) +}) -- 2.34.1