Add test cases and fixed some sonar code smells
authoraardizio <alessandroardizio94@gmail.com>
Tue, 16 Feb 2021 13:57:53 +0000 (14:57 +0100)
committeraardizio <alessandroardizio94@gmail.com>
Tue, 16 Feb 2021 13:57:53 +0000 (14:57 +0100)
src/pools/cluster/dynamic.ts
src/pools/thread/dynamic.ts
src/worker/abstract-worker.ts
tests/pools/abstract/abstract-pool.test.js [new file with mode: 0644]

index eb93c0c8c6d5ac00908e4311f13b1b4392bc4d64..bfafd66bd0fac3d39754933a215811581815efd3 100644 (file)
@@ -60,17 +60,16 @@ export class DynamicClusterPool<
     }
 
     // All workers are busy, create a new worker
-    const worker = this.createAndSetupWorker()
-    this.registerWorkerMessageListener<Data>(worker, message => {
-      const tasksInProgress = this.tasks.get(worker)
-      const isKillBehaviorOptionHard =
-        message.kill === killBehaviorTypes.HARD
+    const workerCreated = this.createAndSetupWorker()
+    this.registerWorkerMessageListener<Data>(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
   }
 }
index f068b4a2951d5ad126ba29324958f865fa247a7e..4a2fd5e807f1bf992155bc155a89d956616ef589 100644 (file)
@@ -60,17 +60,16 @@ export class DynamicThreadPool<
     }
 
     // All workers are busy, create a new worker
-    const worker = this.createAndSetupWorker()
-    this.registerWorkerMessageListener<Data>(worker, message => {
-      const tasksInProgress = this.tasks.get(worker)
-      const isKillBehaviorOptionHard =
-        message.kill === killBehaviorTypes.HARD
+    const workerCreated = this.createAndSetupWorker()
+    this.registerWorkerMessageListener<Data>(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
   }
 }
index b65ed5901c3fe8d76b5868cb4e78273af9ee209d..768c8d95ee3493267163b07ca7ff332f31caa152 100644 (file)
@@ -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 (file)
index 0000000..d5d8705
--- /dev/null
@@ -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()
+  })
+})