fix: ensure the task concurrency is respected at queued task
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
index c4fd6c65bcac7dac4b7e65a445b0600b354ee787..2c904362a742eb8594a00a9bf216e7cb3707e455 100644 (file)
@@ -1,3 +1,4 @@
+const { MessageChannel } = require('worker_threads')
 const { expect } = require('expect')
 const {
   DynamicClusterPool,
@@ -85,6 +86,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 +132,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 +147,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 pool size equal to zero'
       )
     )
   })
@@ -424,24 +465,6 @@ describe('Abstract pool test suite', () => {
       numberOfWorkers,
       './tests/worker-files/thread/testWorker.js'
     )
-    expect(pool.info).toStrictEqual({
-      version,
-      type: PoolTypes.fixed,
-      worker: WorkerTypes.thread,
-      ready: false,
-      strategy: WorkerChoiceStrategies.ROUND_ROBIN,
-      minSize: numberOfWorkers,
-      maxSize: numberOfWorkers,
-      workerNodes: numberOfWorkers,
-      idleWorkerNodes: numberOfWorkers,
-      busyWorkerNodes: 0,
-      executedTasks: 0,
-      executingTasks: 0,
-      queuedTasks: 0,
-      maxQueuedTasks: 0,
-      failedTasks: 0
-    })
-    await waitPoolEvents(pool, PoolEvents.ready, 1)
     expect(pool.info).toStrictEqual({
       version,
       type: PoolTypes.fixed,
@@ -465,24 +488,6 @@ describe('Abstract pool test suite', () => {
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js'
     )
-    expect(pool.info).toStrictEqual({
-      version,
-      type: PoolTypes.dynamic,
-      worker: WorkerTypes.cluster,
-      ready: false,
-      strategy: WorkerChoiceStrategies.ROUND_ROBIN,
-      minSize: Math.floor(numberOfWorkers / 2),
-      maxSize: numberOfWorkers,
-      workerNodes: Math.floor(numberOfWorkers / 2),
-      idleWorkerNodes: Math.floor(numberOfWorkers / 2),
-      busyWorkerNodes: 0,
-      executedTasks: 0,
-      executingTasks: 0,
-      queuedTasks: 0,
-      maxQueuedTasks: 0,
-      failedTasks: 0
-    })
-    await waitPoolEvents(pool, PoolEvents.ready, 1)
     expect(pool.info).toStrictEqual({
       version,
       type: PoolTypes.dynamic,
@@ -566,15 +571,6 @@ describe('Abstract pool test suite', () => {
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js'
     )
-    for (const workerNode of pool.workerNodes) {
-      expect(workerNode.info).toStrictEqual({
-        id: expect.any(Number),
-        type: WorkerTypes.cluster,
-        dynamic: false,
-        ready: false
-      })
-    }
-    await waitPoolEvents(pool, PoolEvents.ready, 1)
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.info).toStrictEqual({
         id: expect.any(Number),
@@ -594,16 +590,8 @@ describe('Abstract pool test suite', () => {
         id: expect.any(Number),
         type: WorkerTypes.thread,
         dynamic: false,
-        ready: false
-      })
-    }
-    await waitPoolEvents(pool, PoolEvents.ready, 1)
-    for (const workerNode of pool.workerNodes) {
-      expect(workerNode.info).toStrictEqual({
-        id: expect.any(Number),
-        type: WorkerTypes.thread,
-        dynamic: false,
-        ready: true
+        ready: true,
+        messageChannel: expect.any(MessageChannel)
       })
     }
   })
@@ -710,6 +698,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) {
@@ -738,6 +730,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()
   })
@@ -788,8 +782,8 @@ describe('Abstract pool test suite', () => {
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.js'
     )
-    let poolReady = 0
     let poolInfo
+    let poolReady = 0
     pool.emitter.on(PoolEvents.ready, info => {
       ++poolReady
       poolInfo = info