perf: use node: prefix to bypass caching call for builtins
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
index 945ee8e7acf6417b2e6816e87bce1dffce48265d..bedb1c6c42a9121697990121185c049c26ed87a4 100644 (file)
@@ -1,3 +1,4 @@
+const { EventEmitter } = require('node:events')
 const { expect } = require('expect')
 const sinon = require('sinon')
 const {
@@ -35,7 +36,7 @@ describe('Abstract pool test suite', () => {
           numberOfWorkers,
           './tests/worker-files/thread/testWorker.js',
           {
-            errorHandler: (e) => console.error(e)
+            errorHandler: e => console.error(e)
           }
         )
     ).toThrowError(
@@ -53,7 +54,6 @@ describe('Abstract pool test suite', () => {
     expect(pool.starting).toBe(false)
     expect(pool.started).toBe(true)
     await pool.destroy()
-    expect(pool.started).toBe(false)
   })
 
   it('Verify that filePath is checked', () => {
@@ -154,22 +154,22 @@ describe('Abstract pool test suite', () => {
     )
     expect(
       () =>
-        new DynamicClusterPool(
-          1,
-          1,
-          './tests/worker-files/cluster/testWorker.js'
-        )
+        new DynamicThreadPool(0, 0, './tests/worker-files/thread/testWorker.js')
     ).toThrowError(
       new RangeError(
-        'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead'
+        'Cannot instantiate a dynamic pool with a maximum pool size equal to zero'
       )
     )
     expect(
       () =>
-        new DynamicThreadPool(0, 0, './tests/worker-files/thread/testWorker.js')
+        new DynamicClusterPool(
+          1,
+          1,
+          './tests/worker-files/cluster/testWorker.js'
+        )
     ).toThrowError(
       new RangeError(
-        'Cannot instantiate a dynamic pool with a maximum pool size equal to zero'
+        'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead'
       )
     )
   })
@@ -179,7 +179,7 @@ describe('Abstract pool test suite', () => {
       numberOfWorkers,
       './tests/worker-files/thread/testWorker.js'
     )
-    expect(pool.emitter).toBeDefined()
+    expect(pool.emitter).toBeInstanceOf(EventEmitter)
     expect(pool.opts.enableEvents).toBe(true)
     expect(pool.opts.restartWorkerOnError).toBe(true)
     expect(pool.opts.enableTasksQueue).toBe(false)
@@ -188,17 +188,26 @@ describe('Abstract pool test suite', () => {
       WorkerChoiceStrategies.ROUND_ROBIN
     )
     expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
-      choiceRetries: 6,
+      retries: 6,
       runTime: { median: false },
       waitTime: { median: false },
       elu: { median: false }
     })
     expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
-      choiceRetries: 6,
+      retries: 6,
       runTime: { median: false },
       waitTime: { median: false },
       elu: { median: false }
     })
+    for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
+      .workerChoiceStrategies) {
+      expect(workerChoiceStrategy.opts).toStrictEqual({
+        retries: 6,
+        runTime: { median: false },
+        waitTime: { median: false },
+        elu: { median: false }
+      })
+    }
     expect(pool.opts.messageHandler).toBeUndefined()
     expect(pool.opts.errorHandler).toBeUndefined()
     expect(pool.opts.onlineHandler).toBeUndefined()
@@ -236,19 +245,29 @@ describe('Abstract pool test suite', () => {
       WorkerChoiceStrategies.LEAST_USED
     )
     expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
-      choiceRetries: 6,
+      retries: 6,
       runTime: { median: true },
       waitTime: { median: false },
       elu: { median: false },
       weights: { 0: 300, 1: 200 }
     })
     expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
-      choiceRetries: 6,
+      retries: 6,
       runTime: { median: true },
       waitTime: { median: false },
       elu: { median: false },
       weights: { 0: 300, 1: 200 }
     })
+    for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
+      .workerChoiceStrategies) {
+      expect(workerChoiceStrategy.opts).toStrictEqual({
+        retries: 6,
+        runTime: { median: true },
+        waitTime: { median: false },
+        elu: { median: false },
+        weights: { 0: 300, 1: 200 }
+      })
+    }
     expect(pool.opts.messageHandler).toStrictEqual(testHandler)
     expect(pool.opts.errorHandler).toStrictEqual(testHandler)
     expect(pool.opts.onlineHandler).toStrictEqual(testHandler)
@@ -276,13 +295,13 @@ describe('Abstract pool test suite', () => {
           './tests/worker-files/thread/testWorker.js',
           {
             workerChoiceStrategyOptions: {
-              choiceRetries: 'invalidChoiceRetries'
+              retries: 'invalidChoiceRetries'
             }
           }
         )
     ).toThrowError(
       new TypeError(
-        'Invalid worker choice strategy options: choice retries must be an integer'
+        'Invalid worker choice strategy options: retries must be an integer'
       )
     )
     expect(
@@ -292,13 +311,13 @@ describe('Abstract pool test suite', () => {
           './tests/worker-files/thread/testWorker.js',
           {
             workerChoiceStrategyOptions: {
-              choiceRetries: -1
+              retries: -1
             }
           }
         )
     ).toThrowError(
       new RangeError(
-        "Invalid worker choice strategy options: choice retries '-1' must be greater or equal than zero"
+        "Invalid worker choice strategy options: retries '-1' must be greater or equal than zero"
       )
     )
     expect(
@@ -452,13 +471,13 @@ describe('Abstract pool test suite', () => {
       { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
     )
     expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
-      choiceRetries: 6,
+      retries: 6,
       runTime: { median: false },
       waitTime: { median: false },
       elu: { median: false }
     })
     expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
-      choiceRetries: 6,
+      retries: 6,
       runTime: { median: false },
       waitTime: { median: false },
       elu: { median: false }
@@ -466,7 +485,7 @@ describe('Abstract pool test suite', () => {
     for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
       .workerChoiceStrategies) {
       expect(workerChoiceStrategy.opts).toStrictEqual({
-        choiceRetries: 6,
+        retries: 6,
         runTime: { median: false },
         waitTime: { median: false },
         elu: { median: false }
@@ -496,13 +515,13 @@ describe('Abstract pool test suite', () => {
       elu: { median: true }
     })
     expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
-      choiceRetries: 6,
+      retries: 6,
       runTime: { median: true },
       waitTime: { median: false },
       elu: { median: true }
     })
     expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
-      choiceRetries: 6,
+      retries: 6,
       runTime: { median: true },
       waitTime: { median: false },
       elu: { median: true }
@@ -510,7 +529,7 @@ describe('Abstract pool test suite', () => {
     for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
       .workerChoiceStrategies) {
       expect(workerChoiceStrategy.opts).toStrictEqual({
-        choiceRetries: 6,
+        retries: 6,
         runTime: { median: true },
         waitTime: { median: false },
         elu: { median: true }
@@ -540,13 +559,13 @@ describe('Abstract pool test suite', () => {
       elu: { median: false }
     })
     expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
-      choiceRetries: 6,
+      retries: 6,
       runTime: { median: false },
       waitTime: { median: false },
       elu: { median: false }
     })
     expect(pool.workerChoiceStrategyContext.opts).toStrictEqual({
-      choiceRetries: 6,
+      retries: 6,
       runTime: { median: false },
       waitTime: { median: false },
       elu: { median: false }
@@ -554,7 +573,7 @@ describe('Abstract pool test suite', () => {
     for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
       .workerChoiceStrategies) {
       expect(workerChoiceStrategy.opts).toStrictEqual({
-        choiceRetries: 6,
+        retries: 6,
         runTime: { median: false },
         waitTime: { median: false },
         elu: { median: false }
@@ -588,18 +607,18 @@ describe('Abstract pool test suite', () => {
     )
     expect(() =>
       pool.setWorkerChoiceStrategyOptions({
-        choiceRetries: 'invalidChoiceRetries'
+        retries: 'invalidChoiceRetries'
       })
     ).toThrowError(
       new TypeError(
-        'Invalid worker choice strategy options: choice retries must be an integer'
+        'Invalid worker choice strategy options: retries must be an integer'
       )
     )
     expect(() =>
-      pool.setWorkerChoiceStrategyOptions({ choiceRetries: -1 })
+      pool.setWorkerChoiceStrategyOptions({ retries: -1 })
     ).toThrowError(
       new RangeError(
-        "Invalid worker choice strategy options: choice retries '-1' must be greater or equal than zero"
+        "Invalid worker choice strategy options: retries '-1' must be greater or equal than zero"
       )
     )
     expect(() =>
@@ -758,17 +777,17 @@ describe('Abstract pool test suite', () => {
           failed: 0
         },
         runTime: {
-          history: expect.any(CircularArray)
+          history: new CircularArray()
         },
         waitTime: {
-          history: expect.any(CircularArray)
+          history: new CircularArray()
         },
         elu: {
           idle: {
-            history: expect.any(CircularArray)
+            history: new CircularArray()
           },
           active: {
-            history: expect.any(CircularArray)
+            history: new CircularArray()
           }
         }
       })
@@ -799,6 +818,7 @@ describe('Abstract pool test suite', () => {
       expect(workerNode.tasksQueue.size).toBe(0)
       expect(workerNode.tasksQueue.maxSize).toBe(0)
     }
+    await pool.destroy()
   })
 
   it('Verify that pool worker info are initialized', async () => {
@@ -828,6 +848,7 @@ describe('Abstract pool test suite', () => {
         ready: true
       })
     }
+    await pool.destroy()
   })
 
   it('Verify that pool execute() arguments are checked', async () => {
@@ -1007,7 +1028,7 @@ describe('Abstract pool test suite', () => {
     )
     let poolInfo
     let poolReady = 0
-    pool.emitter.on(PoolEvents.ready, (info) => {
+    pool.emitter.on(PoolEvents.ready, info => {
       ++poolReady
       poolInfo = info
     })
@@ -1039,7 +1060,7 @@ describe('Abstract pool test suite', () => {
     const promises = new Set()
     let poolBusy = 0
     let poolInfo
-    pool.emitter.on(PoolEvents.busy, (info) => {
+    pool.emitter.on(PoolEvents.busy, info => {
       ++poolBusy
       poolInfo = info
     })
@@ -1077,7 +1098,7 @@ describe('Abstract pool test suite', () => {
     const promises = new Set()
     let poolFull = 0
     let poolInfo
-    pool.emitter.on(PoolEvents.full, (info) => {
+    pool.emitter.on(PoolEvents.full, info => {
       ++poolFull
       poolInfo = info
     })
@@ -1116,7 +1137,7 @@ describe('Abstract pool test suite', () => {
     const promises = new Set()
     let poolBackPressure = 0
     let poolInfo
-    pool.emitter.on(PoolEvents.backPressure, (info) => {
+    pool.emitter.on(PoolEvents.backPressure, info => {
       ++poolBackPressure
       poolInfo = info
     })
@@ -1205,7 +1226,7 @@ describe('Abstract pool test suite', () => {
         expect(workerNode.getTaskFunctionWorkerUsage(name)).toStrictEqual({
           tasks: {
             executed: expect.any(Number),
-            executing: expect.any(Number),
+            executing: 0,
             failed: 0,
             queued: 0,
             stolen: 0
@@ -1226,10 +1247,31 @@ describe('Abstract pool test suite', () => {
           }
         })
         expect(
-          workerNode.getTaskFunctionWorkerUsage(name).tasks.executing
-        ).toBeGreaterThanOrEqual(0)
+          workerNode.getTaskFunctionWorkerUsage(name).tasks.executed
+        ).toBeGreaterThan(0)
       }
+      expect(
+        workerNode.getTaskFunctionWorkerUsage(DEFAULT_TASK_NAME)
+      ).toStrictEqual(
+        workerNode.getTaskFunctionWorkerUsage(workerNode.info.taskFunctions[1])
+      )
     }
     await pool.destroy()
   })
+
+  it('Verify sendKillMessageToWorker()', async () => {
+    const pool = new DynamicClusterPool(
+      Math.floor(numberOfWorkers / 2),
+      numberOfWorkers,
+      './tests/worker-files/cluster/testWorker.js'
+    )
+    const workerNodeKey = 0
+    await expect(
+      pool.sendKillMessageToWorker(
+        workerNodeKey,
+        pool.workerNodes[workerNodeKey].info.id
+      )
+    ).resolves.toBeUndefined()
+    await pool.destroy()
+  })
 })