fix: add maximum tasks queue size to worker usage data
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
index 12237d93f7bba6c752852d7b49c72da8253a492c..c1e3a323997bd4623089cc4484a0680f4a659f6f 100644 (file)
@@ -94,7 +94,8 @@ describe('Abstract pool test suite', () => {
     )
     expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
       runTime: { median: false },
-      waitTime: { median: false }
+      waitTime: { median: false },
+      elu: { median: false }
     })
     expect(pool.opts.messageHandler).toBeUndefined()
     expect(pool.opts.errorHandler).toBeUndefined()
@@ -147,21 +148,22 @@ describe('Abstract pool test suite', () => {
           numberOfWorkers,
           './tests/worker-files/thread/testWorker.js',
           {
-            enableTasksQueue: true,
-            tasksQueueOptions: { concurrency: 0 }
+            workerChoiceStrategy: 'invalidStrategy'
           }
         )
-    ).toThrowError("Invalid worker tasks concurrency '0'")
+    ).toThrowError("Invalid worker choice strategy 'invalidStrategy'")
     expect(
       () =>
         new FixedThreadPool(
           numberOfWorkers,
           './tests/worker-files/thread/testWorker.js',
           {
-            workerChoiceStrategy: 'invalidStrategy'
+            workerChoiceStrategyOptions: 'invalidOptions'
           }
         )
-    ).toThrowError("Invalid worker choice strategy 'invalidStrategy'")
+    ).toThrowError(
+      'Invalid worker choice strategy options: must be a plain object'
+    )
     expect(
       () =>
         new FixedThreadPool(
@@ -174,6 +176,51 @@ describe('Abstract pool test suite', () => {
     ).toThrowError(
       'Invalid worker choice strategy options: must have a weight for each worker node'
     )
+    expect(
+      () =>
+        new FixedThreadPool(
+          numberOfWorkers,
+          './tests/worker-files/thread/testWorker.js',
+          {
+            workerChoiceStrategyOptions: { measurement: 'invalidMeasurement' }
+          }
+        )
+    ).toThrowError(
+      "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'"
+    )
+    expect(
+      () =>
+        new FixedThreadPool(
+          numberOfWorkers,
+          './tests/worker-files/thread/testWorker.js',
+          {
+            enableTasksQueue: true,
+            tasksQueueOptions: { concurrency: 0 }
+          }
+        )
+    ).toThrowError("Invalid worker tasks concurrency '0'")
+    expect(
+      () =>
+        new FixedThreadPool(
+          numberOfWorkers,
+          './tests/worker-files/thread/testWorker.js',
+          {
+            enableTasksQueue: true,
+            tasksQueueOptions: 'invalidTasksQueueOptions'
+          }
+        )
+    ).toThrowError('Invalid tasks queue options: must be a plain object')
+    expect(
+      () =>
+        new FixedThreadPool(
+          numberOfWorkers,
+          './tests/worker-files/thread/testWorker.js',
+          {
+            enableTasksQueue: true,
+            tasksQueueOptions: { concurrency: 0.2 }
+          }
+        )
+    ).toThrowError('Invalid worker tasks concurrency: must be an integer')
   })
 
   it('Verify that worker choice strategy options can be set', async () => {
@@ -184,13 +231,15 @@ describe('Abstract pool test suite', () => {
     )
     expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
       runTime: { median: false },
-      waitTime: { median: false }
+      waitTime: { median: false },
+      elu: { median: false }
     })
     for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
       .workerChoiceStrategies) {
       expect(workerChoiceStrategy.opts).toStrictEqual({
         runTime: { median: false },
-        waitTime: { median: false }
+        waitTime: { median: false },
+        elu: { median: false }
       })
     }
     expect(
@@ -206,16 +255,25 @@ describe('Abstract pool test suite', () => {
         average: false,
         median: false
       },
-      elu: false
+      elu: {
+        aggregate: true,
+        average: true,
+        median: false
+      }
+    })
+    pool.setWorkerChoiceStrategyOptions({
+      runTime: { median: true },
+      elu: { median: true }
     })
-    pool.setWorkerChoiceStrategyOptions({ runTime: { median: true } })
     expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
-      runTime: { median: true }
+      runTime: { median: true },
+      elu: { median: true }
     })
     for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
       .workerChoiceStrategies) {
       expect(workerChoiceStrategy.opts).toStrictEqual({
-        runTime: { median: true }
+        runTime: { median: true },
+        elu: { median: true }
       })
     }
     expect(
@@ -231,16 +289,25 @@ describe('Abstract pool test suite', () => {
         average: false,
         median: false
       },
-      elu: false
+      elu: {
+        aggregate: true,
+        average: false,
+        median: true
+      }
+    })
+    pool.setWorkerChoiceStrategyOptions({
+      runTime: { median: false },
+      elu: { median: false }
     })
-    pool.setWorkerChoiceStrategyOptions({ runTime: { median: false } })
     expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
-      runTime: { median: false }
+      runTime: { median: false },
+      elu: { median: false }
     })
     for (const [, workerChoiceStrategy] of pool.workerChoiceStrategyContext
       .workerChoiceStrategies) {
       expect(workerChoiceStrategy.opts).toStrictEqual({
-        runTime: { median: false }
+        runTime: { median: false },
+        elu: { median: false }
       })
     }
     expect(
@@ -256,8 +323,27 @@ describe('Abstract pool test suite', () => {
         average: false,
         median: false
       },
-      elu: false
+      elu: {
+        aggregate: true,
+        average: true,
+        median: false
+      }
     })
+    expect(() =>
+      pool.setWorkerChoiceStrategyOptions('invalidWorkerChoiceStrategyOptions')
+    ).toThrowError(
+      'Invalid worker choice strategy options: must be a plain object'
+    )
+    expect(() =>
+      pool.setWorkerChoiceStrategyOptions({ weights: {} })
+    ).toThrowError(
+      'Invalid worker choice strategy options: must have a weight for each worker node'
+    )
+    expect(() =>
+      pool.setWorkerChoiceStrategyOptions({ measurement: 'invalidMeasurement' })
+    ).toThrowError(
+      "Invalid worker choice strategy options: invalid measurement 'invalidMeasurement'"
+    )
     await pool.destroy()
   })
 
@@ -289,9 +375,15 @@ describe('Abstract pool test suite', () => {
     expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 1 })
     pool.setTasksQueueOptions({ concurrency: 2 })
     expect(pool.opts.tasksQueueOptions).toStrictEqual({ concurrency: 2 })
+    expect(() =>
+      pool.setTasksQueueOptions('invalidTasksQueueOptions')
+    ).toThrowError('Invalid tasks queue options: must be a plain object')
     expect(() => pool.setTasksQueueOptions({ concurrency: 0 })).toThrowError(
       "Invalid worker tasks concurrency '0'"
     )
+    expect(() => pool.setTasksQueueOptions({ concurrency: 0.2 })).toThrowError(
+      'Invalid worker tasks concurrency: must be an integer'
+    )
     await pool.destroy()
   })
 
@@ -363,6 +455,7 @@ describe('Abstract pool test suite', () => {
           executed: 0,
           executing: 0,
           queued: 0,
+          maxQueued: 0,
           failed: 0
         },
         runTime: {
@@ -377,7 +470,21 @@ describe('Abstract pool test suite', () => {
           median: 0,
           history: expect.any(CircularArray)
         },
-        elu: undefined
+        elu: {
+          idle: {
+            aggregate: 0,
+            average: 0,
+            median: 0,
+            history: expect.any(CircularArray)
+          },
+          active: {
+            aggregate: 0,
+            average: 0,
+            median: 0,
+            history: expect.any(CircularArray)
+          },
+          utilization: 0
+        }
       })
     }
     await pool.destroy()
@@ -392,6 +499,7 @@ describe('Abstract pool test suite', () => {
       expect(workerNode.tasksQueue).toBeDefined()
       expect(workerNode.tasksQueue).toBeInstanceOf(Queue)
       expect(workerNode.tasksQueue.size).toBe(0)
+      expect(workerNode.tasksQueue.maxSize).toBe(0)
     }
     await pool.destroy()
   })
@@ -412,6 +520,7 @@ describe('Abstract pool test suite', () => {
           executed: 0,
           executing: maxMultiplier,
           queued: 0,
+          maxQueued: 0,
           failed: 0
         },
         runTime: {
@@ -426,7 +535,21 @@ describe('Abstract pool test suite', () => {
           median: 0,
           history: expect.any(CircularArray)
         },
-        elu: undefined
+        elu: {
+          idle: {
+            aggregate: 0,
+            average: 0,
+            median: 0,
+            history: expect.any(CircularArray)
+          },
+          active: {
+            aggregate: 0,
+            average: 0,
+            median: 0,
+            history: expect.any(CircularArray)
+          },
+          utilization: 0
+        }
       })
     }
     await Promise.all(promises)
@@ -436,6 +559,7 @@ describe('Abstract pool test suite', () => {
           executed: maxMultiplier,
           executing: 0,
           queued: 0,
+          maxQueued: 0,
           failed: 0
         },
         runTime: {
@@ -450,7 +574,21 @@ describe('Abstract pool test suite', () => {
           median: 0,
           history: expect.any(CircularArray)
         },
-        elu: undefined
+        elu: {
+          idle: {
+            aggregate: 0,
+            average: 0,
+            median: 0,
+            history: expect.any(CircularArray)
+          },
+          active: {
+            aggregate: 0,
+            average: 0,
+            median: 0,
+            history: expect.any(CircularArray)
+          },
+          utilization: 0
+        }
       })
     }
     await pool.destroy()
@@ -474,6 +612,7 @@ describe('Abstract pool test suite', () => {
           executed: expect.any(Number),
           executing: 0,
           queued: 0,
+          maxQueued: 0,
           failed: 0
         },
         runTime: {
@@ -488,7 +627,21 @@ describe('Abstract pool test suite', () => {
           median: 0,
           history: expect.any(CircularArray)
         },
-        elu: undefined
+        elu: {
+          idle: {
+            aggregate: 0,
+            average: 0,
+            median: 0,
+            history: expect.any(CircularArray)
+          },
+          active: {
+            aggregate: 0,
+            average: 0,
+            median: 0,
+            history: expect.any(CircularArray)
+          },
+          utilization: 0
+        }
       })
       expect(workerNode.workerUsage.tasks.executed).toBeGreaterThan(0)
       expect(workerNode.workerUsage.tasks.executed).toBeLessThanOrEqual(
@@ -502,6 +655,7 @@ describe('Abstract pool test suite', () => {
           executed: 0,
           executing: 0,
           queued: 0,
+          maxQueued: 0,
           failed: 0
         },
         runTime: {
@@ -516,7 +670,21 @@ describe('Abstract pool test suite', () => {
           median: 0,
           history: expect.any(CircularArray)
         },
-        elu: undefined
+        elu: {
+          idle: {
+            aggregate: 0,
+            average: 0,
+            median: 0,
+            history: expect.any(CircularArray)
+          },
+          active: {
+            aggregate: 0,
+            average: 0,
+            median: 0,
+            history: expect.any(CircularArray)
+          },
+          utilization: 0
+        }
       })
       expect(workerNode.workerUsage.runTime.history.length).toBe(0)
       expect(workerNode.workerUsage.waitTime.history.length).toBe(0)