test: improve worker choice strategies testing
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 30 May 2023 16:34:52 +0000 (18:34 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 30 May 2023 16:34:52 +0000 (18:34 +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 db45fa077620d225d4a8367ebd3d37c9d7cc9da7..19cb3eb849406a726832537c0a7a1a36c0d1cf19 100644 (file)
@@ -372,15 +372,16 @@ describe('Abstract pool test suite', () => {
       './tests/worker-files/thread/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())
     }
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksUsage).toStrictEqual({
-        run: numberOfWorkers * 2,
+        run: numberOfWorkers * maxMultiplier,
         running: 0,
-        runTime: expect.any(Number),
+        runTime: 0,
         runTimeHistory: expect.any(CircularArray),
         avgRunTime: 0,
         medRunTime: 0,
@@ -390,12 +391,9 @@ describe('Abstract pool test suite', () => {
         medWaitTime: 0,
         error: 0
       })
-      expect(workerNode.tasksUsage.runTime).toBeGreaterThanOrEqual(0)
-      expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0)
     }
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
     for (const workerNode of pool.workerNodes) {
-      expect(workerNode.tasksUsage).toBeDefined()
       expect(workerNode.tasksUsage).toStrictEqual({
         run: 0,
         running: 0,
@@ -410,6 +408,7 @@ describe('Abstract pool test suite', () => {
         error: 0
       })
       expect(workerNode.tasksUsage.runTimeHistory.length).toBe(0)
+      expect(workerNode.tasksUsage.waitTimeHistory.length).toBe(0)
     }
     await pool.destroy()
   })
index 42f03b893d7c4f950e1950d3a23a5aec64fb88f4..4e5390d18f713ce306ed0a9c5cbb31a67e2e70e1 100644 (file)
@@ -89,8 +89,8 @@ describe('Fixed cluster pool test suite', () => {
   })
 
   it('Verify that tasks queuing is working', async () => {
-    const maxMultiplier = 2
     const promises = new Set()
+    const maxMultiplier = 2
     for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) {
       promises.add(queuePool.execute())
     }
@@ -110,6 +110,9 @@ 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.tasksQueue.size).toBe(0)
     }
     promises.clear()
index 977706cce8a7e5c913d483b6e97edf6572b3f3f1..19ac5ea3a9a930c67996efd31fb5a683aa856685 100644 (file)
@@ -156,10 +156,10 @@ describe('Selection strategies test suite', () => {
       { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
     )
     // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
-    const promises = []
+    const promises = new Set()
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
-      promises.push(pool.execute())
+      promises.add(pool.execute())
     }
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
@@ -177,6 +177,7 @@ describe('Selection strategies test suite', () => {
         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()
@@ -190,10 +191,10 @@ describe('Selection strategies test suite', () => {
       { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
     )
     // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
-    const promises = []
+    const promises = new Set()
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
-      promises.push(pool.execute())
+      promises.add(pool.execute())
     }
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
@@ -211,6 +212,7 @@ describe('Selection strategies test suite', () => {
         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()
@@ -327,10 +329,10 @@ describe('Selection strategies test suite', () => {
       { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED }
     )
     // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose`
-    const promises = []
+    const promises = new Set()
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
-      promises.push(pool.execute())
+      promises.add(pool.execute())
     }
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
@@ -348,6 +350,7 @@ describe('Selection strategies test suite', () => {
         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()
@@ -361,10 +364,10 @@ describe('Selection strategies test suite', () => {
       { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED }
     )
     // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose`
-    const promises = []
+    const promises = new Set()
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
-      promises.push(pool.execute())
+      promises.add(pool.execute())
     }
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
@@ -382,6 +385,7 @@ describe('Selection strategies test suite', () => {
         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()
@@ -432,10 +436,10 @@ describe('Selection strategies test suite', () => {
       { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY }
     )
     // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose`
-    const promises = []
+    const promises = new Set()
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
-      promises.push(pool.execute())
+      promises.add(pool.execute())
     }
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
@@ -453,6 +457,7 @@ describe('Selection strategies test suite', () => {
         error: 0
       })
       expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
+      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier)
       expect(workerNode.tasksUsage.runTime).toBeGreaterThan(0)
     }
     // We need to clean up the resources after our test
@@ -467,10 +472,10 @@ describe('Selection strategies test suite', () => {
       { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY }
     )
     // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose`
-    const promises = []
+    const promises = new Set()
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
-      promises.push(pool.execute())
+      promises.add(pool.execute())
     }
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
@@ -488,6 +493,7 @@ describe('Selection strategies test suite', () => {
         error: 0
       })
       expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
+      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier)
       expect(workerNode.tasksUsage.runTime).toBeGreaterThan(0)
     }
     // We need to clean up the resources after our test
@@ -539,10 +545,10 @@ describe('Selection strategies test suite', () => {
       { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
     )
     // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
-    const promises = []
+    const promises = new Set()
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
-      promises.push(pool.execute())
+      promises.add(pool.execute())
     }
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
@@ -560,6 +566,7 @@ describe('Selection strategies test suite', () => {
         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)
     }
@@ -580,10 +587,10 @@ describe('Selection strategies test suite', () => {
       { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
     )
     // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
-    const promises = []
+    const promises = new Set()
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
-      promises.push(pool.execute())
+      promises.add(pool.execute())
     }
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
@@ -601,6 +608,7 @@ describe('Selection strategies test suite', () => {
         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)
     }
@@ -626,10 +634,10 @@ describe('Selection strategies test suite', () => {
       }
     )
     // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
-    const promises = []
+    const promises = new Set()
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
-      promises.push(pool.execute())
+      promises.add(pool.execute())
     }
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
@@ -647,6 +655,7 @@ describe('Selection strategies test suite', () => {
         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)
     }
@@ -778,10 +787,10 @@ describe('Selection strategies test suite', () => {
       { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
     )
     // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
-    const promises = []
+    const promises = new Set()
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
-      promises.push(pool.execute())
+      promises.add(pool.execute())
     }
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
@@ -798,9 +807,10 @@ describe('Selection strategies test suite', () => {
         medWaitTime: 0,
         error: 0
       })
-      expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
-      expect(workerNode.tasksUsage.runTime).toBeGreaterThan(0)
-      expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThan(0)
+      expect(workerNode.tasksUsage.run).toBeGreaterThanOrEqual(0)
+      expect(workerNode.tasksUsage.run).toBeLessThanOrEqual(max * maxMultiplier)
+      expect(workerNode.tasksUsage.runTime).toBeGreaterThanOrEqual(0)
+      expect(workerNode.tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0)
     }
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
@@ -824,10 +834,10 @@ describe('Selection strategies test suite', () => {
       { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
     )
     // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
-    const promises = []
+    const promises = new Set()
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
-      promises.push(pool.execute())
+      promises.add(pool.execute())
     }
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
@@ -845,6 +855,7 @@ describe('Selection strategies test suite', () => {
         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)
     }
@@ -875,10 +886,10 @@ describe('Selection strategies test suite', () => {
       }
     )
     // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
-    const promises = []
+    const promises = new Set()
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
-      promises.push(pool.execute())
+      promises.add(pool.execute())
     }
     await Promise.all(promises)
     for (const workerNode of pool.workerNodes) {
@@ -896,6 +907,7 @@ describe('Selection strategies test suite', () => {
         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 2d689feda6a86b2fda9f03fbbbd2d62fec6f9bef..8a9922b0b376ce5d726aad6efc210abdc2c96668 100644 (file)
@@ -89,8 +89,8 @@ describe('Fixed thread pool test suite', () => {
   })
 
   it('Verify that tasks queuing is working', async () => {
-    const maxMultiplier = 2
     const promises = new Set()
+    const maxMultiplier = 2
     for (let i = 0; i < numberOfThreads * maxMultiplier; i++) {
       promises.add(queuePool.execute())
     }
@@ -110,6 +110,9 @@ 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.tasksQueue.size).toBe(0)
     }
     promises.clear()