refactor: use PoolEvents enum
[poolifier.git] / tests / pools / thread / fixed.test.mjs
index a1cb8dd294b100263e0bb97e898b2890e02d4c43..3e10a92d0be8156f4dfd22289a0d3197a532fb1b 100644 (file)
@@ -1,22 +1,23 @@
 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
   const tasksConcurrency = 2
   const pool = new FixedThreadPool(
     numberOfThreads,
-    './tests/worker-files/thread/testWorker.js',
+    './tests/worker-files/thread/testWorker.mjs',
     {
       errorHandler: e => console.error(e)
     }
   )
   const queuePool = new FixedThreadPool(
     numberOfThreads,
-    './tests/worker-files/thread/testWorker.js',
+    './tests/worker-files/thread/testWorker.mjs',
     {
       enableTasksQueue: true,
       tasksQueueOptions: {
@@ -27,30 +28,30 @@ describe('Fixed thread pool test suite', () => {
   )
   const emptyPool = new FixedThreadPool(
     numberOfThreads,
-    './tests/worker-files/thread/emptyWorker.js',
+    './tests/worker-files/thread/emptyWorker.mjs',
     { exitHandler: () => console.info('empty pool worker exited') }
   )
   const echoPool = new FixedThreadPool(
     numberOfThreads,
-    './tests/worker-files/thread/echoWorker.js'
+    './tests/worker-files/thread/echoWorker.mjs'
   )
   const errorPool = new FixedThreadPool(
     numberOfThreads,
-    './tests/worker-files/thread/errorWorker.js',
+    './tests/worker-files/thread/errorWorker.mjs',
     {
       errorHandler: e => console.error(e)
     }
   )
   const asyncErrorPool = new FixedThreadPool(
     numberOfThreads,
-    './tests/worker-files/thread/asyncErrorWorker.js',
+    './tests/worker-files/thread/asyncErrorWorker.mjs',
     {
       errorHandler: e => console.error(e)
     }
   )
   const asyncPool = new FixedThreadPool(
     numberOfThreads,
-    './tests/worker-files/thread/asyncWorker.js'
+    './tests/worker-files/thread/asyncWorker.mjs'
   )
 
   after('Destroy all pools', async () => {
@@ -82,7 +83,7 @@ describe('Fixed thread pool test suite', () => {
   it("Verify that 'ready' event is emitted", async () => {
     const pool = new FixedThreadPool(
       numberOfThreads,
-      './tests/worker-files/thread/testWorker.js',
+      './tests/worker-files/thread/testWorker.mjs',
       {
         errorHandler: e => console.error(e)
       }
@@ -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
@@ -202,8 +210,9 @@ describe('Fixed thread pool test suite', () => {
       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/
     )
   })
 
@@ -292,13 +301,18 @@ describe('Fixed thread pool test suite', () => {
     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)
   })
 
   it('Verify that thread pool options are checked', async () => {
-    const workerFilePath = './tests/worker-files/thread/testWorker.js'
+    const workerFilePath = './tests/worker-files/thread/testWorker.mjs'
     let pool = new FixedThreadPool(numberOfThreads, workerFilePath)
     expect(pool.opts.workerOptions).toBeUndefined()
     await pool.destroy()
@@ -316,7 +330,7 @@ describe('Fixed thread pool test suite', () => {
   })
 
   it('Should work even without opts in input', async () => {
-    const workerFilePath = './tests/worker-files/thread/testWorker.js'
+    const workerFilePath = './tests/worker-files/thread/testWorker.mjs'
     const pool = new FixedThreadPool(numberOfThreads, workerFilePath)
     const res = await pool.execute()
     expect(res).toStrictEqual({ ok: 1 })
@@ -325,7 +339,7 @@ describe('Fixed thread pool test suite', () => {
   })
 
   it('Verify destroyWorkerNode()', async () => {
-    const workerFilePath = './tests/worker-files/thread/testWorker.js'
+    const workerFilePath = './tests/worker-files/thread/testWorker.mjs'
     const pool = new FixedThreadPool(numberOfThreads, workerFilePath)
     const workerNodeKey = 0
     let exitEvent = 0
@@ -338,9 +352,9 @@ describe('Fixed thread pool test suite', () => {
     await pool.destroy()
   })
 
-  it('Verify that a pool with zero worker fails', async () => {
+  it('Verify that a pool with zero worker fails', () => {
     expect(
-      () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js')
-    ).toThrowError('Cannot instantiate a fixed pool with zero worker')
+      () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.mjs')
+    ).toThrow('Cannot instantiate a fixed pool with zero worker')
   })
 })