test: refine worker choice strategies expectation
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 30 May 2023 18:54:13 +0000 (20:54 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 30 May 2023 18:54:13 +0000 (20:54 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
tests/pools/abstract/abstract-pool.test.js
tests/pools/cluster/fixed.test.js
tests/pools/selection-strategies/selection-strategies.test.js
tests/pools/thread/fixed.test.js

index 19cb3eb849406a726832537c0a7a1a36c0d1cf19..caf7c8f0c0ad28ad03a204dfc7ea8f9b0a569cf8 100644 (file)
@@ -11,7 +11,7 @@ const { CircularArray } = require('../../../lib/circular-array')
 const { Queue } = require('../../../lib/queue')
 
 describe('Abstract pool test suite', () => {
-  const numberOfWorkers = 1
+  const numberOfWorkers = 2
   class StubPoolWithRemoveAllWorker extends FixedThreadPool {
     removeAllWorker () {
       this.workerNodes = []
@@ -106,7 +106,7 @@ describe('Abstract pool test suite', () => {
         workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED,
         workerChoiceStrategyOptions: {
           medRunTime: true,
-          weights: { 0: 300 }
+          weights: { 0: 300, 1: 200 }
         },
         enableEvents: false,
         enableTasksQueue: true,
@@ -126,7 +126,7 @@ describe('Abstract pool test suite', () => {
     )
     expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
       medRunTime: true,
-      weights: { 0: 300 }
+      weights: { 0: 300, 1: 200 }
     })
     expect(pool.opts.messageHandler).toStrictEqual(testHandler)
     expect(pool.opts.errorHandler).toStrictEqual(testHandler)
@@ -328,13 +328,14 @@ describe('Abstract pool test suite', () => {
       './tests/worker-files/cluster/testWorker.js'
     )
     const promises = []
-    for (let i = 0; i < numberOfWorkers * 2; i++) {
+    const maxMultiplier = 2
+    for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) {
       promises.push(pool.execute())
     }
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
         run: 0,
-        running: numberOfWorkers * 2,
+        running: maxMultiplier,
         runTime: 0,
         runTimeHistory: expect.any(CircularArray),
         avgRunTime: 0,
@@ -349,7 +350,7 @@ describe('Abstract pool test suite', () => {
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
-        run: numberOfWorkers * 2,
+        run: maxMultiplier,
         running: 0,
         runTime: 0,
         runTimeHistory: expect.any(CircularArray),
@@ -368,7 +369,7 @@ describe('Abstract pool test suite', () => {
   it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => {
     const pool = new DynamicThreadPool(
       numberOfWorkers,
-      numberOfWorkers,
+      numberOfWorkers * 2,
       './tests/worker-files/thread/testWorker.js'
     )
     const promises = []
@@ -379,7 +380,7 @@ describe('Abstract pool test suite', () => {
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
-        run: numberOfWorkers * maxMultiplier,
+        run: expect.any(Number),
         running: 0,
         runTime: 0,
         runTimeHistory: expect.any(CircularArray),
@@ -391,6 +392,8 @@ describe('Abstract pool test suite', () => {
         medWaitTime: 0,
         error: 0
       })
+      expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
+      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(maxMultiplier)
     }
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
     for (const workerNode of pool.workerNodes) {
@@ -427,8 +430,8 @@ describe('Abstract pool test suite', () => {
     }
     await Promise.all(promises)
     // 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 + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool.
-    expect(poolFull).toBe(numberOfWorkers + 1)
+    // 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)
     await pool.destroy()
   })
 
index 4e5390d18f713ce306ed0a9c5cbb31a67e2e70e1..dfd765574a7437ef9ac128c9a35d7185cb7ab10b 100644 (file)
@@ -110,9 +110,7 @@ describe('Fixed cluster pool test suite', () => {
     for (const workerNode of queuePool.workerNodes) {
       expect(workerNode.tasksUsage.running).toBe(0)
       expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
-      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(
-        numberOfWorkers * maxMultiplier
-      )
+      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(maxMultiplier)
       expect(workerNode.tasksQueue.size).toBe(0)
     }
     promises.clear()
index 19ac5ea3a9a930c67996efd31fb5a683aa856685..b10a7b56d26e60723e25268e0d3597e8d25501da 100644 (file)
@@ -164,7 +164,7 @@ describe('Selection strategies test suite', () => {
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
-        run: expect.any(Number),
+        run: maxMultiplier,
         running: 0,
         runTime: 0,
         runTimeHistory: expect.any(CircularArray),
@@ -176,8 +176,6 @@ describe('Selection strategies test suite', () => {
         medWaitTime: 0,
         error: 0
       })
-      expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
-      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier)
     }
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -199,7 +197,7 @@ describe('Selection strategies test suite', () => {
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
-        run: expect.any(Number),
+        run: maxMultiplier,
         running: 0,
         runTime: 0,
         runTimeHistory: expect.any(CircularArray),
@@ -211,8 +209,6 @@ describe('Selection strategies test suite', () => {
         medWaitTime: 0,
         error: 0
       })
-      expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
-      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier)
     }
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -337,7 +333,7 @@ describe('Selection strategies test suite', () => {
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
-        run: expect.any(Number),
+        run: maxMultiplier,
         running: 0,
         runTime: 0,
         runTimeHistory: expect.any(CircularArray),
@@ -349,8 +345,6 @@ describe('Selection strategies test suite', () => {
         medWaitTime: 0,
         error: 0
       })
-      expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
-      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier)
     }
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -372,7 +366,7 @@ describe('Selection strategies test suite', () => {
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
-        run: expect.any(Number),
+        run: maxMultiplier,
         running: 0,
         runTime: 0,
         runTimeHistory: expect.any(CircularArray),
@@ -384,8 +378,6 @@ describe('Selection strategies test suite', () => {
         medWaitTime: 0,
         error: 0
       })
-      expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
-      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier)
     }
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -553,7 +545,7 @@ describe('Selection strategies test suite', () => {
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
-        run: expect.any(Number),
+        run: maxMultiplier,
         running: 0,
         runTime: expect.any(Number),
         runTimeHistory: expect.any(CircularArray),
@@ -565,8 +557,6 @@ describe('Selection strategies test suite', () => {
         medWaitTime: 0,
         error: 0
       })
-      expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
-      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier)
       expect(workerNode.tasksUsage.runTime).toBeGreaterThan(0)
       expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThan(0)
     }
@@ -595,7 +585,7 @@ describe('Selection strategies test suite', () => {
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
-        run: expect.any(Number),
+        run: maxMultiplier,
         running: 0,
         runTime: expect.any(Number),
         runTimeHistory: expect.any(CircularArray),
@@ -607,8 +597,6 @@ describe('Selection strategies test suite', () => {
         medWaitTime: 0,
         error: 0
       })
-      expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
-      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier)
       expect(workerNode.tasksUsage.runTime).toBeGreaterThan(0)
       expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThan(0)
     }
@@ -642,7 +630,7 @@ describe('Selection strategies test suite', () => {
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
-        run: expect.any(Number),
+        run: maxMultiplier,
         running: 0,
         runTime: expect.any(Number),
         runTimeHistory: expect.any(CircularArray),
@@ -654,8 +642,6 @@ describe('Selection strategies test suite', () => {
         medWaitTime: 0,
         error: 0
       })
-      expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
-      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier)
       expect(workerNode.tasksUsage.runTime).toBeGreaterThan(0)
       expect(workerNode.tasksUsage.medRunTime).toBeGreaterThan(0)
     }
index 8a9922b0b376ce5d726aad6efc210abdc2c96668..0c3ba276f3b4e40c4d2d40e2a5484efd679cb094 100644 (file)
@@ -110,9 +110,7 @@ describe('Fixed thread pool test suite', () => {
     for (const workerNode of queuePool.workerNodes) {
       expect(workerNode.tasksUsage.running).toBe(0)
       expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
-      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(
-        numberOfThreads * maxMultiplier
-      )
+      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(maxMultiplier)
       expect(workerNode.tasksQueue.size).toBe(0)
     }
     promises.clear()