refactor: use PoolEvents enum
[poolifier.git] / tests / pools / cluster / fixed.test.mjs
index 426a64f00d29e051985c93371f34c9fe0ea4c86e..fa609cf357a8ac6763f2848c2102a5a47ffd6a0c 100644 (file)
@@ -1,22 +1,23 @@
 import { expect } from 'expect'
-import { FixedClusterPool, 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 { FixedClusterPool, 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 cluster pool test suite', () => {
   const numberOfWorkers = 8
   const tasksConcurrency = 2
   const pool = new FixedClusterPool(
     numberOfWorkers,
-    './tests/worker-files/cluster/testWorker.js',
+    './tests/worker-files/cluster/testWorker.cjs',
     {
       errorHandler: e => console.error(e)
     }
   )
   const queuePool = new FixedClusterPool(
     numberOfWorkers,
-    './tests/worker-files/cluster/testWorker.js',
+    './tests/worker-files/cluster/testWorker.cjs',
     {
       enableTasksQueue: true,
       tasksQueueOptions: {
@@ -27,30 +28,30 @@ describe('Fixed cluster pool test suite', () => {
   )
   const emptyPool = new FixedClusterPool(
     numberOfWorkers,
-    './tests/worker-files/cluster/emptyWorker.js',
+    './tests/worker-files/cluster/emptyWorker.cjs',
     { exitHandler: () => console.info('empty pool worker exited') }
   )
   const echoPool = new FixedClusterPool(
     numberOfWorkers,
-    './tests/worker-files/cluster/echoWorker.js'
+    './tests/worker-files/cluster/echoWorker.cjs'
   )
   const errorPool = new FixedClusterPool(
     numberOfWorkers,
-    './tests/worker-files/cluster/errorWorker.js',
+    './tests/worker-files/cluster/errorWorker.cjs',
     {
       errorHandler: e => console.error(e)
     }
   )
   const asyncErrorPool = new FixedClusterPool(
     numberOfWorkers,
-    './tests/worker-files/cluster/asyncErrorWorker.js',
+    './tests/worker-files/cluster/asyncErrorWorker.cjs',
     {
       errorHandler: e => console.error(e)
     }
   )
   const asyncPool = new FixedClusterPool(
     numberOfWorkers,
-    './tests/worker-files/cluster/asyncWorker.js'
+    './tests/worker-files/cluster/asyncWorker.cjs'
   )
 
   after('Destroy all pools', async () => {
@@ -82,7 +83,7 @@ describe('Fixed cluster pool test suite', () => {
   it("Verify that 'ready' event is emitted", async () => {
     const pool = new FixedClusterPool(
       numberOfWorkers,
-      './tests/worker-files/cluster/testWorker.js',
+      './tests/worker-files/cluster/testWorker.cjs',
       {
         errorHandler: e => console.error(e)
       }
@@ -130,6 +131,7 @@ describe('Fixed cluster 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 cluster 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(
+        numberOfWorkers * maxMultiplier
+      )
       expect(workerNode.usage.tasks.stolen).toBeGreaterThanOrEqual(0)
       expect(workerNode.usage.tasks.stolen).toBeLessThanOrEqual(
         numberOfWorkers * maxMultiplier
@@ -262,13 +270,18 @@ describe('Fixed cluster 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(numberOfWorkers)
     expect(poolDestroy).toBe(1)
   })
 
   it('Verify that cluster pool options are checked', async () => {
-    const workerFilePath = './tests/worker-files/cluster/testWorker.js'
+    const workerFilePath = './tests/worker-files/cluster/testWorker.cjs'
     let pool = new FixedClusterPool(numberOfWorkers, workerFilePath)
     expect(pool.opts.env).toBeUndefined()
     expect(pool.opts.settings).toBeUndefined()
@@ -291,7 +304,7 @@ describe('Fixed cluster pool test suite', () => {
   })
 
   it('Should work even without opts in input', async () => {
-    const workerFilePath = './tests/worker-files/cluster/testWorker.js'
+    const workerFilePath = './tests/worker-files/cluster/testWorker.cjs'
     const pool = new FixedClusterPool(numberOfWorkers, workerFilePath)
     const res = await pool.execute()
     expect(res).toStrictEqual({ ok: 1 })
@@ -300,7 +313,7 @@ describe('Fixed cluster pool test suite', () => {
   })
 
   it('Verify destroyWorkerNode()', async () => {
-    const workerFilePath = './tests/worker-files/cluster/testWorker.js'
+    const workerFilePath = './tests/worker-files/cluster/testWorker.cjs'
     const pool = new FixedClusterPool(numberOfWorkers, workerFilePath)
     const workerNodeKey = 0
     let disconnectEvent = 0
@@ -321,7 +334,7 @@ describe('Fixed cluster pool test suite', () => {
   it('Verify that a pool with zero worker fails', () => {
     expect(
       () =>
-        new FixedClusterPool(0, './tests/worker-files/cluster/testWorker.js')
+        new FixedClusterPool(0, './tests/worker-files/cluster/testWorker.cjs')
     ).toThrow('Cannot instantiate a fixed pool with zero worker')
   })
 })