test: improve task error handling
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
index 80f43dda845c5e4e06b5cc0448b8ef4031f3dad7..dbda7af72301be5e29ecc1125a691839ada6f85f 100644 (file)
@@ -193,15 +193,14 @@ describe('Abstract pool test suite', () => {
         medWaitTime: false
       })
     }
-    expect(
-      pool.workerChoiceStrategyContext.getRequiredStatistics()
-    ).toStrictEqual({
+    expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
       runTime: true,
       avgRunTime: true,
       medRunTime: false,
       waitTime: false,
       avgWaitTime: false,
-      medWaitTime: false
+      medWaitTime: false,
+      elu: false
     })
     pool.setWorkerChoiceStrategyOptions({ medRunTime: true })
     expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
@@ -211,15 +210,14 @@ describe('Abstract pool test suite', () => {
       .workerChoiceStrategies) {
       expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: true })
     }
-    expect(
-      pool.workerChoiceStrategyContext.getRequiredStatistics()
-    ).toStrictEqual({
+    expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
       runTime: true,
       avgRunTime: false,
       medRunTime: true,
       waitTime: false,
       avgWaitTime: false,
-      medWaitTime: false
+      medWaitTime: false,
+      elu: false
     })
     pool.setWorkerChoiceStrategyOptions({ medRunTime: false })
     expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
@@ -229,15 +227,14 @@ describe('Abstract pool test suite', () => {
       .workerChoiceStrategies) {
       expect(workerChoiceStrategy.opts).toStrictEqual({ medRunTime: false })
     }
-    expect(
-      pool.workerChoiceStrategyContext.getRequiredStatistics()
-    ).toStrictEqual({
+    expect(pool.workerChoiceStrategyContext.getTaskStatistics()).toStrictEqual({
       runTime: true,
       avgRunTime: true,
       medRunTime: false,
       waitTime: false,
       avgWaitTime: false,
-      medWaitTime: false
+      medWaitTime: false,
+      elu: false
     })
     await pool.destroy()
   })
@@ -336,7 +333,7 @@ describe('Abstract pool test suite', () => {
     )
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
-        run: 0,
+        ran: 0,
         running: 0,
         runTime: 0,
         runTimeHistory: expect.any(CircularArray),
@@ -346,7 +343,8 @@ describe('Abstract pool test suite', () => {
         waitTimeHistory: expect.any(CircularArray),
         avgWaitTime: 0,
         medWaitTime: 0,
-        error: 0
+        error: 0,
+        elu: undefined
       })
     }
     await pool.destroy()
@@ -377,7 +375,7 @@ describe('Abstract pool test suite', () => {
     }
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
-        run: 0,
+        ran: 0,
         running: maxMultiplier,
         runTime: 0,
         runTimeHistory: expect.any(CircularArray),
@@ -387,13 +385,14 @@ describe('Abstract pool test suite', () => {
         waitTimeHistory: expect.any(CircularArray),
         avgWaitTime: 0,
         medWaitTime: 0,
-        error: 0
+        error: 0,
+        elu: undefined
       })
     }
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
-        run: maxMultiplier,
+        ran: maxMultiplier,
         running: 0,
         runTime: 0,
         runTimeHistory: expect.any(CircularArray),
@@ -403,7 +402,8 @@ describe('Abstract pool test suite', () => {
         waitTimeHistory: expect.any(CircularArray),
         avgWaitTime: 0,
         medWaitTime: 0,
-        error: 0
+        error: 0,
+        elu: undefined
       })
     }
     await pool.destroy()
@@ -423,7 +423,7 @@ describe('Abstract pool test suite', () => {
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
-        run: expect.any(Number),
+        ran: expect.any(Number),
         running: 0,
         runTime: 0,
         runTimeHistory: expect.any(CircularArray),
@@ -433,15 +433,16 @@ describe('Abstract pool test suite', () => {
         waitTimeHistory: expect.any(CircularArray),
         avgWaitTime: 0,
         medWaitTime: 0,
-        error: 0
+        error: 0,
+        elu: undefined
       })
-      expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
-      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(maxMultiplier)
+      expect(workerNode.tasksUsage.ran).toBeGreaterThan(0)
+      expect(workerNode.tasksUsage.ran).toBeLessThanOrEqual(maxMultiplier)
     }
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
-        run: 0,
+        ran: 0,
         running: 0,
         runTime: 0,
         runTimeHistory: expect.any(CircularArray),
@@ -451,7 +452,8 @@ describe('Abstract pool test suite', () => {
         waitTimeHistory: expect.any(CircularArray),
         avgWaitTime: 0,
         medWaitTime: 0,
-        error: 0
+        error: 0,
+        elu: undefined
       })
       expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0)
       expect(workerNode.tasksUsage.waitTimeHistory.length).toBe(0)
@@ -467,7 +469,11 @@ describe('Abstract pool test suite', () => {
     )
     const promises = new Set()
     let poolFull = 0
-    pool.emitter.on(PoolEvents.full, () => ++poolFull)
+    let poolInfo
+    pool.emitter.on(PoolEvents.full, info => {
+      ++poolFull
+      poolInfo = info
+    })
     for (let i = 0; i < numberOfWorkers * 2; i++) {
       promises.add(pool.execute())
     }
@@ -475,6 +481,18 @@ describe('Abstract pool test suite', () => {
     // The `full` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool.
     // So in total numberOfWorkers * 2 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool with min = max = numberOfWorkers.
     expect(poolFull).toBe(numberOfWorkers * 2)
+    expect(poolInfo).toStrictEqual({
+      type: PoolTypes.dynamic,
+      worker: WorkerTypes.thread,
+      minSize: expect.any(Number),
+      maxSize: expect.any(Number),
+      workerNodes: expect.any(Number),
+      idleWorkerNodes: expect.any(Number),
+      busyWorkerNodes: expect.any(Number),
+      runningTasks: expect.any(Number),
+      queuedTasks: expect.any(Number),
+      maxQueuedTasks: expect.any(Number)
+    })
     await pool.destroy()
   })
 
@@ -485,7 +503,11 @@ describe('Abstract pool test suite', () => {
     )
     const promises = new Set()
     let poolBusy = 0
-    pool.emitter.on(PoolEvents.busy, () => ++poolBusy)
+    let poolInfo
+    pool.emitter.on(PoolEvents.busy, info => {
+      ++poolBusy
+      poolInfo = info
+    })
     for (let i = 0; i < numberOfWorkers * 2; i++) {
       promises.add(pool.execute())
     }
@@ -493,6 +515,18 @@ describe('Abstract pool test suite', () => {
     // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
     // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool.
     expect(poolBusy).toBe(numberOfWorkers + 1)
+    expect(poolInfo).toStrictEqual({
+      type: PoolTypes.fixed,
+      worker: WorkerTypes.thread,
+      minSize: expect.any(Number),
+      maxSize: expect.any(Number),
+      workerNodes: expect.any(Number),
+      idleWorkerNodes: expect.any(Number),
+      busyWorkerNodes: expect.any(Number),
+      runningTasks: expect.any(Number),
+      queuedTasks: expect.any(Number),
+      maxQueuedTasks: expect.any(Number)
+    })
     await pool.destroy()
   })