refactor: move message channel property from worker info to node
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
index c57a8f0ca6d371fab542fc578717afb58588d8e5..5c367aa2c0ebf52be17b51dc8f961d70cfabc564 100644 (file)
@@ -29,10 +29,12 @@ describe('Abstract pool test suite', () => {
           numberOfWorkers,
           './tests/worker-files/thread/testWorker.js',
           {
-            errorHandler: e => console.error(e)
+            errorHandler: (e) => console.error(e)
           }
         )
-    ).toThrowError('Cannot start a pool from a worker!')
+    ).toThrowError(
+      'Cannot start a pool from a worker with the same type as the pool'
+    )
   })
 
   it('Verify that filePath is checked', () => {
@@ -85,6 +87,42 @@ describe('Abstract pool test suite', () => {
   })
 
   it('Verify that dynamic pool sizing is checked', () => {
+    expect(
+      () =>
+        new DynamicClusterPool(
+          1,
+          undefined,
+          './tests/worker-files/cluster/testWorker.js'
+        )
+    ).toThrowError(
+      new TypeError(
+        'Cannot instantiate a dynamic pool without specifying the maximum pool size'
+      )
+    )
+    expect(
+      () =>
+        new DynamicThreadPool(
+          0.5,
+          1,
+          './tests/worker-files/thread/testWorker.js'
+        )
+    ).toThrowError(
+      new TypeError(
+        'Cannot instantiate a pool with a non safe integer number of workers'
+      )
+    )
+    expect(
+      () =>
+        new DynamicClusterPool(
+          0,
+          0.5,
+          './tests/worker-files/cluster/testWorker.js'
+        )
+    ).toThrowError(
+      new TypeError(
+        'Cannot instantiate a dynamic pool with a non safe integer maximum pool size'
+      )
+    )
     expect(
       () =>
         new DynamicThreadPool(2, 1, './tests/worker-files/thread/testWorker.js')
@@ -95,7 +133,11 @@ describe('Abstract pool test suite', () => {
     )
     expect(
       () =>
-        new DynamicThreadPool(1, 1, './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 minimum pool size equal to the maximum pool size. Use a fixed pool instead'
@@ -106,7 +148,7 @@ describe('Abstract pool test suite', () => {
         new DynamicThreadPool(0, 0, './tests/worker-files/thread/testWorker.js')
     ).toThrowError(
       new RangeError(
-        'Cannot instantiate a dynamic pool with a minimum pool size and a maximum pool size equal to zero'
+        'Cannot instantiate a dynamic pool with a maximum pool size equal to zero'
       )
     )
   })
@@ -134,7 +176,7 @@ describe('Abstract pool test suite', () => {
     expect(pool.opts.onlineHandler).toBeUndefined()
     expect(pool.opts.exitHandler).toBeUndefined()
     await pool.destroy()
-    const testHandler = () => console.log('test handler executed')
+    const testHandler = () => console.info('test handler executed')
     pool = new FixedThreadPool(
       numberOfWorkers,
       './tests/worker-files/thread/testWorker.js',
@@ -437,8 +479,6 @@ describe('Abstract pool test suite', () => {
       busyWorkerNodes: 0,
       executedTasks: 0,
       executingTasks: 0,
-      queuedTasks: 0,
-      maxQueuedTasks: 0,
       failedTasks: 0
     })
     await pool.destroy()
@@ -460,8 +500,6 @@ describe('Abstract pool test suite', () => {
       busyWorkerNodes: 0,
       executedTasks: 0,
       executingTasks: 0,
-      queuedTasks: 0,
-      maxQueuedTasks: 0,
       failedTasks: 0
     })
     await pool.destroy()
@@ -656,6 +694,10 @@ describe('Abstract pool test suite', () => {
       })
       expect(workerNode.usage.tasks.executed).toBeGreaterThan(0)
       expect(workerNode.usage.tasks.executed).toBeLessThanOrEqual(maxMultiplier)
+      expect(workerNode.usage.runTime.history.length).toBe(0)
+      expect(workerNode.usage.waitTime.history.length).toBe(0)
+      expect(workerNode.usage.elu.idle.history.length).toBe(0)
+      expect(workerNode.usage.elu.active.history.length).toBe(0)
     }
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
     for (const workerNode of pool.workerNodes) {
@@ -684,6 +726,8 @@ describe('Abstract pool test suite', () => {
       })
       expect(workerNode.usage.runTime.history.length).toBe(0)
       expect(workerNode.usage.waitTime.history.length).toBe(0)
+      expect(workerNode.usage.elu.idle.history.length).toBe(0)
+      expect(workerNode.usage.elu.active.history.length).toBe(0)
     }
     await pool.destroy()
   })
@@ -697,7 +741,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
     })
@@ -721,8 +765,6 @@ describe('Abstract pool test suite', () => {
       busyWorkerNodes: expect.any(Number),
       executedTasks: expect.any(Number),
       executingTasks: expect.any(Number),
-      queuedTasks: expect.any(Number),
-      maxQueuedTasks: expect.any(Number),
       failedTasks: expect.any(Number)
     })
     await pool.destroy()
@@ -736,7 +778,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
     })
@@ -755,8 +797,6 @@ describe('Abstract pool test suite', () => {
       busyWorkerNodes: expect.any(Number),
       executedTasks: expect.any(Number),
       executingTasks: expect.any(Number),
-      queuedTasks: expect.any(Number),
-      maxQueuedTasks: expect.any(Number),
       failedTasks: expect.any(Number)
     })
     await pool.destroy()
@@ -770,7 +810,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
     })
@@ -794,18 +834,42 @@ describe('Abstract pool test suite', () => {
       busyWorkerNodes: expect.any(Number),
       executedTasks: expect.any(Number),
       executingTasks: expect.any(Number),
-      queuedTasks: expect.any(Number),
-      maxQueuedTasks: expect.any(Number),
       failedTasks: expect.any(Number)
     })
     await pool.destroy()
   })
 
-  it('Verify that multiple tasks worker is working', async () => {
+  it('Verify that listTaskFunctions() is working', async () => {
+    const dynamicThreadPool = new DynamicThreadPool(
+      Math.floor(numberOfWorkers / 2),
+      numberOfWorkers,
+      './tests/worker-files/thread/testMultipleTaskFunctionsWorker.js'
+    )
+    await waitPoolEvents(dynamicThreadPool, PoolEvents.ready, 1)
+    expect(dynamicThreadPool.listTaskFunctions()).toStrictEqual([
+      'default',
+      'jsonIntegerSerialization',
+      'factorial',
+      'fibonacci'
+    ])
+    const fixedClusterPool = new FixedClusterPool(
+      numberOfWorkers,
+      './tests/worker-files/cluster/testMultipleTaskFunctionsWorker.js'
+    )
+    await waitPoolEvents(fixedClusterPool, PoolEvents.ready, 1)
+    expect(fixedClusterPool.listTaskFunctions()).toStrictEqual([
+      'default',
+      'jsonIntegerSerialization',
+      'factorial',
+      'fibonacci'
+    ])
+  })
+
+  it('Verify that multiple task functions worker is working', async () => {
     const pool = new DynamicClusterPool(
       Math.floor(numberOfWorkers / 2),
       numberOfWorkers,
-      './tests/worker-files/cluster/testMultiTasksWorker.js'
+      './tests/worker-files/cluster/testMultipleTaskFunctionsWorker.js'
     )
     const data = { n: 10 }
     const result0 = await pool.execute(data)