build(deps-dev): apply updates
[poolifier.git] / tests / pools / cluster / fixed.test.mjs
index cf00239514cd4864c297872dec4c42a2d5dea321..a78a4509fb263b987d08e5cb0e9d1e7f6d607070 100644 (file)
@@ -1,3 +1,5 @@
+import cluster from 'node:cluster'
+
 import { expect } from 'expect'
 
 import { FixedClusterPool, PoolEvents } from '../../../lib/index.cjs'
@@ -12,7 +14,7 @@ describe('Fixed cluster pool test suite', () => {
     numberOfWorkers,
     './tests/worker-files/cluster/testWorker.cjs',
     {
-      errorHandler: e => console.error(e)
+      errorHandler: e => console.error(e),
     }
   )
   const queuePool = new FixedClusterPool(
@@ -21,9 +23,9 @@ describe('Fixed cluster pool test suite', () => {
     {
       enableTasksQueue: true,
       tasksQueueOptions: {
-        concurrency: tasksConcurrency
+        concurrency: tasksConcurrency,
       },
-      errorHandler: e => console.error(e)
+      errorHandler: e => console.error(e),
     }
   )
   const emptyPool = new FixedClusterPool(
@@ -39,14 +41,14 @@ describe('Fixed cluster pool test suite', () => {
     numberOfWorkers,
     './tests/worker-files/cluster/errorWorker.cjs',
     {
-      errorHandler: e => console.error(e)
+      errorHandler: e => console.error(e),
     }
   )
   const asyncErrorPool = new FixedClusterPool(
     numberOfWorkers,
     './tests/worker-files/cluster/asyncErrorWorker.cjs',
     {
-      errorHandler: e => console.error(e)
+      errorHandler: e => console.error(e),
     }
   )
   const asyncPool = new FixedClusterPool(
@@ -66,11 +68,11 @@ describe('Fixed cluster pool test suite', () => {
 
   it('Verify that the function is executed in a worker cluster', async () => {
     let result = await pool.execute({
-      function: TaskFunctions.fibonacci
+      function: TaskFunctions.fibonacci,
     })
-    expect(result).toBe(75025)
+    expect(result).toBe(354224848179262000000)
     result = await pool.execute({
-      function: TaskFunctions.factorial
+      function: TaskFunctions.factorial,
     })
     expect(result).toBe(9.33262154439441e157)
   })
@@ -85,7 +87,7 @@ describe('Fixed cluster pool test suite', () => {
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.cjs',
       {
-        errorHandler: e => console.error(e)
+        errorHandler: e => console.error(e),
       }
     )
     expect(pool.emitter.eventNames()).toStrictEqual([])
@@ -209,7 +211,7 @@ describe('Fixed cluster pool test suite', () => {
     expect(taskError).toStrictEqual({
       name: DEFAULT_TASK_NAME,
       message: 'Error Message from ClusterWorker',
-      data
+      data,
     })
     expect(
       errorPool.workerNodes.some(
@@ -226,7 +228,7 @@ describe('Fixed cluster pool test suite', () => {
       taskError = e
     })
     expect(asyncErrorPool.emitter.eventNames()).toStrictEqual([
-      PoolEvents.taskError
+      PoolEvents.taskError,
     ])
     let inError
     try {
@@ -240,7 +242,7 @@ describe('Fixed cluster pool test suite', () => {
     expect(taskError).toStrictEqual({
       name: DEFAULT_TASK_NAME,
       message: 'Error Message from ClusterWorker:async',
-      data
+      data,
     })
     expect(
       asyncErrorPool.workerNodes.some(
@@ -265,12 +267,15 @@ describe('Fixed cluster pool test suite', () => {
     pool.emitter.on(PoolEvents.destroy, () => ++poolDestroy)
     expect(pool.emitter.eventNames()).toStrictEqual([
       PoolEvents.busy,
-      PoolEvents.destroy
+      PoolEvents.destroy,
     ])
     await pool.destroy()
     const numberOfExitEvents = await exitPromise
     expect(pool.started).toBe(false)
-    expect(pool.emitter.eventNames()).toStrictEqual([])
+    expect(pool.emitter.eventNames()).toStrictEqual([
+      PoolEvents.busy,
+      PoolEvents.destroy,
+    ])
     expect(pool.readyEventEmitted).toBe(false)
     expect(pool.workerNodes.length).toBe(0)
     expect(numberOfExitEvents).toBe(numberOfWorkers)
@@ -282,20 +287,24 @@ describe('Fixed cluster pool test suite', () => {
     let pool = new FixedClusterPool(numberOfWorkers, workerFilePath)
     expect(pool.opts.env).toBeUndefined()
     expect(pool.opts.settings).toBeUndefined()
+    expect(cluster.settings).toMatchObject({
+      exec: workerFilePath,
+      silent: false,
+    })
     await pool.destroy()
     pool = new FixedClusterPool(numberOfWorkers, workerFilePath, {
       env: { TEST: 'test' },
-      settings: { args: ['--use', 'http'], silent: true }
+      settings: { args: ['--use', 'http'], silent: true },
     })
     expect(pool.opts.env).toStrictEqual({ TEST: 'test' })
     expect(pool.opts.settings).toStrictEqual({
       args: ['--use', 'http'],
-      silent: true
+      silent: true,
     })
-    expect({ ...pool.opts.settings, exec: workerFilePath }).toStrictEqual({
+    expect(cluster.settings).toMatchObject({
       args: ['--use', 'http'],
       silent: true,
-      exec: workerFilePath
+      exec: workerFilePath,
     })
     await pool.destroy()
   })
@@ -324,7 +333,8 @@ describe('Fixed cluster pool test suite', () => {
     await expect(pool.destroyWorkerNode(workerNodeKey)).resolves.toBeUndefined()
     expect(disconnectEvent).toBe(1)
     expect(exitEvent).toBe(1)
-    expect(pool.workerNodes.length).toBe(numberOfWorkers - 1)
+    // Simulates an illegitimate worker node destroy and the minimum number of worker nodes is guaranteed
+    expect(pool.workerNodes.length).toBe(numberOfWorkers)
     await pool.destroy()
   })