From: aardizio Date: Tue, 16 Feb 2021 13:57:53 +0000 (+0100) Subject: Add test cases and fixed some sonar code smells X-Git-Tag: v2.0.0-beta.2~17^2~1 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=3ec964d666b2ffa57b57a37a29542a727fc55ee6;p=poolifier.git Add test cases and fixed some sonar code smells --- 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() + }) +})