chore: v2.4.5
[poolifier.git] / tests / pools / selection-strategies / selection-strategies.test.js
index 7187a9007179bb5f7afbf511d055f8a5507d7fab..2fff1b13772e43c4acad99ad48e7441fda474dee 100644 (file)
@@ -43,7 +43,9 @@ describe('Selection strategies test suite', () => {
       WorkerChoiceStrategies.ROUND_ROBIN
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.nextWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.ROUND_ROBIN
+      ).nextWorkerNodeId
     ).toBe(0)
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -75,6 +77,9 @@ describe('Selection strategies test suite', () => {
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
     ).toBe(false)
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
+    ).toBe(false)
     await pool.destroy()
     pool = new DynamicThreadPool(
       min,
@@ -88,6 +93,9 @@ describe('Selection strategies test suite', () => {
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
     ).toBe(false)
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
+    ).toBe(false)
     // We need to clean up the resources after our test
     await pool.destroy()
   })
@@ -138,14 +146,14 @@ describe('Selection strategies test suite', () => {
     )
     let results = new Set()
     for (let i = 0; i < max; i++) {
-      results.add(pool.chooseWorker()[1].id)
+      results.add(pool.chooseWorkerNode()[1].worker.id)
     }
     expect(results.size).toBe(max)
     await pool.destroy()
     pool = new FixedThreadPool(max, './tests/worker-files/thread/testWorker.js')
     results = new Set()
     for (let i = 0; i < max; i++) {
-      results.add(pool.chooseWorker()[1].threadId)
+      results.add(pool.chooseWorkerNode()[1].worker.threadId)
     }
     expect(results.size).toBe(max)
     await pool.destroy()
@@ -158,11 +166,15 @@ describe('Selection strategies test suite', () => {
       { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.nextWorkerId
-    ).toBeUndefined()
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.ROUND_ROBIN
+      ).nextWorkerNodeId
+    ).toBeDefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.nextWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.ROUND_ROBIN
+      ).nextWorkerNodeId
     ).toBe(0)
     await pool.destroy()
     pool = new DynamicThreadPool(
@@ -172,13 +184,15 @@ describe('Selection strategies test suite', () => {
       { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
-        .nextWorkerId
-    ).toBeUndefined()
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.ROUND_ROBIN
+      ).nextWorkerNodeId
+    ).toBeDefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
-        .nextWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.ROUND_ROBIN
+      ).nextWorkerNodeId
     ).toBe(0)
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -222,6 +236,9 @@ describe('Selection strategies test suite', () => {
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
     ).toBe(false)
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
+    ).toBe(false)
     await pool.destroy()
     pool = new DynamicThreadPool(
       min,
@@ -235,6 +252,9 @@ describe('Selection strategies test suite', () => {
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
     ).toBe(false)
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
+    ).toBe(false)
     // We need to clean up the resources after our test
     await pool.destroy()
   })
@@ -310,6 +330,9 @@ describe('Selection strategies test suite', () => {
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
     ).toBe(false)
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
+    ).toBe(false)
     await pool.destroy()
     pool = new DynamicThreadPool(
       min,
@@ -323,6 +346,9 @@ describe('Selection strategies test suite', () => {
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
     ).toBe(false)
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
+    ).toBe(false)
     // We need to clean up the resources after our test
     await pool.destroy()
   })
@@ -369,16 +395,18 @@ describe('Selection strategies test suite', () => {
     expect(pool.opts.workerChoiceStrategy).toBe(
       WorkerChoiceStrategies.FAIR_SHARE
     )
-    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) {
+    for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
+      .get(WorkerChoiceStrategies.FAIR_SHARE)
+      .workerLastVirtualTaskTimestamp.keys()) {
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
-          workerKey
-        ).start
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .workerLastVirtualTaskTimestamp.get(workerNodeKey).start
       ).toBe(0)
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
-          workerKey
-        ).end
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .workerLastVirtualTaskTimestamp.get(workerNodeKey).end
       ).toBe(0)
     }
     // We need to clean up the resources after our test
@@ -410,6 +438,9 @@ describe('Selection strategies test suite', () => {
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
     ).toBe(true)
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
+    ).toBe(false)
     await pool.destroy()
     pool = new DynamicThreadPool(
       min,
@@ -423,6 +454,9 @@ describe('Selection strategies test suite', () => {
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
     ).toBe(true)
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
+    ).toBe(false)
     // We need to clean up the resources after our test
     await pool.destroy()
   })
@@ -440,9 +474,10 @@ describe('Selection strategies test suite', () => {
     }
     await Promise.all(promises)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy
-        .workerLastVirtualTaskTimestamp.size
-    ).toBe(pool.workers.length)
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.FAIR_SHARE
+      ).workerLastVirtualTaskTimestamp.size
+    ).toBe(pool.workerNodes.length)
     // We need to clean up the resources after our test
     await pool.destroy()
   })
@@ -463,9 +498,10 @@ describe('Selection strategies test suite', () => {
     await Promise.all(promises)
     // if (process.platform !== 'win32') {
     //   expect(
-    //     pool.workerChoiceStrategyContext.workerChoiceStrategy
-    //       .workerChoiceStrategy.workerLastVirtualTaskTimestamp.size
-    //   ).toBe(pool.workers.length)
+    //     pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+    //       WorkerChoiceStrategies.FAIR_SHARE
+    //     ).workerLastVirtualTaskTimestamp.size
+    //   ).toBe(pool.workerNodes.length)
     // }
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -477,20 +513,23 @@ describe('Selection strategies test suite', () => {
       './tests/worker-files/thread/testWorker.js'
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy
-        .workerLastVirtualTaskTimestamp
-    ).toBeUndefined()
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.FAIR_SHARE
+      ).workerLastVirtualTaskTimestamp
+    ).toBeDefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
-    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) {
+    for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
+      .get(WorkerChoiceStrategies.FAIR_SHARE)
+      .workerLastVirtualTaskTimestamp.keys()) {
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
-          workerKey
-        ).start
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .workerLastVirtualTaskTimestamp.get(workerNodeKey).start
       ).toBe(0)
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
-          workerKey
-        ).end
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .workerLastVirtualTaskTimestamp.get(workerNodeKey).end
       ).toBe(0)
     }
     await pool.destroy()
@@ -500,20 +539,23 @@ describe('Selection strategies test suite', () => {
       './tests/worker-files/thread/testWorker.js'
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
-        .workerLastVirtualTaskTimestamp
-    ).toBeUndefined()
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.FAIR_SHARE
+      ).workerLastVirtualTaskTimestamp
+    ).toBeDefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
-    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) {
+    for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
+      .get(WorkerChoiceStrategies.FAIR_SHARE)
+      .workerLastVirtualTaskTimestamp.keys()) {
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
-          workerKey
-        ).start
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .workerLastVirtualTaskTimestamp.get(workerNodeKey).start
       ).toBe(0)
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
-          workerKey
-        ).end
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.FAIR_SHARE)
+          .workerLastVirtualTaskTimestamp.get(workerNodeKey).end
       ).toBe(0)
     }
     // We need to clean up the resources after our test
@@ -530,21 +572,27 @@ describe('Selection strategies test suite', () => {
       WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.currentWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).currentWorkerNodeId
     ).toBe(0)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.defaultWorkerWeight
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).defaultWorkerWeight
     ).toBeGreaterThan(0)
-    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.keys()) {
+    for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
+      .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+      .workersTaskRunTime.keys()) {
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get(
-          workerKey
-        ).weight
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+          .workersTaskRunTime.get(workerNodeKey).weight
       ).toBeGreaterThan(0)
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get(
-          workerKey
-        ).runTime
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+          .workersTaskRunTime.get(workerNodeKey).runTime
       ).toBe(0)
     }
     // We need to clean up the resources after our test
@@ -576,6 +624,9 @@ describe('Selection strategies test suite', () => {
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
     ).toBe(true)
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
+    ).toBe(false)
     await pool.destroy()
     pool = new DynamicThreadPool(
       min,
@@ -589,6 +640,9 @@ describe('Selection strategies test suite', () => {
     expect(
       pool.workerChoiceStrategyContext.getRequiredStatistics().avgRunTime
     ).toBe(true)
+    expect(
+      pool.workerChoiceStrategyContext.getRequiredStatistics().medRunTime
+    ).toBe(false)
     // We need to clean up the resources after our test
     await pool.destroy()
   })
@@ -606,9 +660,10 @@ describe('Selection strategies test suite', () => {
     }
     await Promise.all(promises)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime
-        .size
-    ).toBe(pool.workers.length)
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).workersTaskRunTime.size
+    ).toBe(pool.workerNodes.length)
     // We need to clean up the resources after our test
     await pool.destroy()
   })
@@ -623,17 +678,19 @@ describe('Selection strategies test suite', () => {
     // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
     const promises = []
     const maxMultiplier =
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
-        .defaultWorkerWeight * 2
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).defaultWorkerWeight * 50
     for (let i = 0; i < max * maxMultiplier; i++) {
       promises.push(pool.execute())
     }
     await Promise.all(promises)
     if (process.platform !== 'win32') {
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy
-          .workerChoiceStrategy.workersTaskRunTime.size
-      ).toBe(pool.workers.length)
+        pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+          WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+        ).workersTaskRunTime.size
+      ).toBe(pool.workerNodes.length)
     }
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -645,26 +702,38 @@ describe('Selection strategies test suite', () => {
       './tests/worker-files/thread/testWorker.js'
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.currentWorkerId
-    ).toBeUndefined()
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).currentWorkerNodeId
+    ).toBeDefined()
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.defaultWorkerWeight
-    ).toBeUndefined()
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).defaultWorkerWeight
+    ).toBeDefined()
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime
-    ).toBeUndefined()
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).workersTaskRunTime
+    ).toBeDefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.currentWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).currentWorkerNodeId
     ).toBe(0)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.defaultWorkerWeight
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).defaultWorkerWeight
     ).toBeGreaterThan(0)
-    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.keys()) {
+    for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
+      .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+      .workersTaskRunTime.keys()) {
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get(
-          workerKey
-        ).runTime
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+          .workersTaskRunTime.get(workerNodeKey).runTime
       ).toBe(0)
     }
     await pool.destroy()
@@ -674,31 +743,38 @@ describe('Selection strategies test suite', () => {
       './tests/worker-files/thread/testWorker.js'
     )
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
-        .currentWorkerId
-    ).toBeUndefined()
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).currentWorkerNodeId
+    ).toBeDefined()
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
-        .defaultWorkerWeight
-    ).toBeUndefined()
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).defaultWorkerWeight
+    ).toBeDefined()
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
-        .workersTaskRunTime
-    ).toBeUndefined()
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).workersTaskRunTime
+    ).toBeDefined()
     pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
-        .currentWorkerId
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).currentWorkerNodeId
     ).toBe(0)
     expect(
-      pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
-        .defaultWorkerWeight
+      pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+        WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+      ).defaultWorkerWeight
     ).toBeGreaterThan(0)
-    for (const workerKey of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workersTaskRunTime.keys()) {
+    for (const workerNodeKey of pool.workerChoiceStrategyContext.workerChoiceStrategies
+      .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+      .workersTaskRunTime.keys()) {
       expect(
-        pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workersTaskRunTime.get(
-          workerKey
-        ).runTime
+        pool.workerChoiceStrategyContext.workerChoiceStrategies
+          .get(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+          .workersTaskRunTime.get(workerNodeKey).runTime
       ).toBe(0)
     }
     // We need to clean up the resources after our test
@@ -715,7 +791,7 @@ describe('Selection strategies test suite', () => {
           { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
         )
     ).toThrowError(
-      new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
+      new Error("Invalid worker choice strategy 'UNKNOWN_STRATEGY'")
     )
   })
 })