refactor(ci): cleanup GH actions
[poolifier.git] / tests / pools / thread / fixed.test.mjs
index fd7f5935697d9e2074d1f9734b6b2345159e9775..044eb8083df9a40c8a241c6c76e042bac5546f67 100644 (file)
@@ -1,8 +1,9 @@
 import { expect } from 'expect'
-import { FixedThreadPool, PoolEvents } from '../../../lib/index.js'
-import { TaskFunctions } from '../../test-types.js'
-import { waitPoolEvents, waitWorkerEvents } from '../../test-utils.js'
-import { DEFAULT_TASK_NAME } from '../../../lib/utils.js'
+
+import { FixedThreadPool, PoolEvents } from '../../../lib/index.cjs'
+import { DEFAULT_TASK_NAME } from '../../../lib/utils.cjs'
+import { TaskFunctions } from '../../test-types.cjs'
+import { waitPoolEvents, waitWorkerEvents } from '../../test-utils.cjs'
 
 describe('Fixed thread pool test suite', () => {
   const numberOfThreads = 6
@@ -11,7 +12,7 @@ describe('Fixed thread pool test suite', () => {
     numberOfThreads,
     './tests/worker-files/thread/testWorker.mjs',
     {
-      errorHandler: e => console.error(e)
+      errorHandler: e => console.error(e),
     }
   )
   const queuePool = new FixedThreadPool(
@@ -20,9 +21,9 @@ describe('Fixed thread pool test suite', () => {
     {
       enableTasksQueue: true,
       tasksQueueOptions: {
-        concurrency: tasksConcurrency
+        concurrency: tasksConcurrency,
       },
-      errorHandler: e => console.error(e)
+      errorHandler: e => console.error(e),
     }
   )
   const emptyPool = new FixedThreadPool(
@@ -38,14 +39,14 @@ describe('Fixed thread pool test suite', () => {
     numberOfThreads,
     './tests/worker-files/thread/errorWorker.mjs',
     {
-      errorHandler: e => console.error(e)
+      errorHandler: e => console.error(e),
     }
   )
   const asyncErrorPool = new FixedThreadPool(
     numberOfThreads,
     './tests/worker-files/thread/asyncErrorWorker.mjs',
     {
-      errorHandler: e => console.error(e)
+      errorHandler: e => console.error(e),
     }
   )
   const asyncPool = new FixedThreadPool(
@@ -65,11 +66,11 @@ describe('Fixed thread pool test suite', () => {
 
   it('Verify that the function is executed in a worker thread', 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)
   })
@@ -84,7 +85,7 @@ describe('Fixed thread pool test suite', () => {
       numberOfThreads,
       './tests/worker-files/thread/testWorker.mjs',
       {
-        errorHandler: e => console.error(e)
+        errorHandler: e => console.error(e),
       }
     )
     expect(pool.emitter.eventNames()).toStrictEqual([])
@@ -130,6 +131,7 @@ describe('Fixed thread pool test suite', () => {
       expect(workerNode.usage.tasks.maxQueued).toBe(
         maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
       )
+      expect(workerNode.usage.tasks.sequentiallyStolen).toBe(0)
       expect(workerNode.usage.tasks.stolen).toBe(0)
     }
     expect(queuePool.info.executedTasks).toBe(0)
@@ -157,6 +159,12 @@ describe('Fixed thread pool test suite', () => {
       expect(workerNode.usage.tasks.maxQueued).toBe(
         maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency
       )
+      expect(workerNode.usage.tasks.sequentiallyStolen).toBeGreaterThanOrEqual(
+        0
+      )
+      expect(workerNode.usage.tasks.sequentiallyStolen).toBeLessThanOrEqual(
+        numberOfThreads * maxMultiplier
+      )
       expect(workerNode.usage.tasks.stolen).toBeGreaterThanOrEqual(0)
       expect(workerNode.usage.tasks.stolen).toBeLessThanOrEqual(
         numberOfThreads * maxMultiplier
@@ -187,7 +195,7 @@ describe('Fixed thread pool test suite', () => {
     try {
       result = await pool.execute(undefined, undefined, [
         new ArrayBuffer(16),
-        new MessageChannel().port1
+        new MessageChannel().port1,
       ])
     } catch (e) {
       error = e
@@ -196,14 +204,15 @@ describe('Fixed thread pool test suite', () => {
     expect(error).toBeUndefined()
     try {
       result = await pool.execute(undefined, undefined, [
-        new SharedArrayBuffer(16)
+        new SharedArrayBuffer(16),
       ])
     } catch (e) {
       error = e
     }
     expect(result).toStrictEqual({ ok: 1 })
-    expect(error).toStrictEqual(
-      new TypeError('Found invalid object in transferList')
+    expect(error).toBeInstanceOf(Error)
+    expect(error.message).toMatch(
+      /Found invalid (object|value) in transferList/
     )
   })
 
@@ -229,7 +238,7 @@ describe('Fixed thread pool test suite', () => {
     expect(taskError).toStrictEqual({
       name: DEFAULT_TASK_NAME,
       message: new Error('Error Message from ThreadWorker'),
-      data
+      data,
     })
     expect(
       errorPool.workerNodes.some(
@@ -246,7 +255,7 @@ describe('Fixed thread pool test suite', () => {
       taskError = e
     })
     expect(asyncErrorPool.emitter.eventNames()).toStrictEqual([
-      PoolEvents.taskError
+      PoolEvents.taskError,
     ])
     let inError
     try {
@@ -262,7 +271,7 @@ describe('Fixed thread pool test suite', () => {
     expect(taskError).toStrictEqual({
       name: DEFAULT_TASK_NAME,
       message: new Error('Error Message from ThreadWorker:async'),
-      data
+      data,
     })
     expect(
       asyncErrorPool.workerNodes.some(
@@ -287,11 +296,16 @@ describe('Fixed thread 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([
+      PoolEvents.busy,
+      PoolEvents.destroy,
+    ])
+    expect(pool.readyEventEmitted).toBe(false)
     expect(pool.workerNodes.length).toBe(0)
     expect(numberOfExitEvents).toBe(numberOfThreads)
     expect(poolDestroy).toBe(1)
@@ -305,12 +319,12 @@ describe('Fixed thread pool test suite', () => {
     pool = new FixedThreadPool(numberOfThreads, workerFilePath, {
       workerOptions: {
         env: { TEST: 'test' },
-        name: 'test'
-      }
+        name: 'test',
+      },
     })
     expect(pool.opts.workerOptions).toStrictEqual({
       env: { TEST: 'test' },
-      name: 'test'
+      name: 'test',
     })
     await pool.destroy()
   })
@@ -334,13 +348,14 @@ describe('Fixed thread pool test suite', () => {
     })
     await expect(pool.destroyWorkerNode(workerNodeKey)).resolves.toBeUndefined()
     expect(exitEvent).toBe(1)
-    expect(pool.workerNodes.length).toBe(numberOfThreads - 1)
+    // Simulates an illegitimate worker node destroy and the minimum number of worker nodes is guaranteed
+    expect(pool.workerNodes.length).toBe(numberOfThreads)
     await pool.destroy()
   })
 
   it('Verify that a pool with zero worker fails', () => {
     expect(
       () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.mjs')
-    ).toThrowError('Cannot instantiate a fixed pool with zero worker')
+    ).toThrow('Cannot instantiate a fixed pool with zero worker')
   })
 })