test: add tests for worker choice strategies using median run time
[poolifier.git] / tests / pools / selection-strategies / selection-strategies.test.js
index 52285d7163f1228a5691b26bb8e1b19e6bf12af5..dde60e2685cdfd521708d3869fb935bb4e5cf691 100644 (file)
@@ -478,6 +478,42 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
+  it('Verify FAIR_SHARE strategy can be run in a dynamic pool with median run time statistic', async () => {
+    const pool = new DynamicThreadPool(
+      min,
+      max,
+      './tests/worker-files/thread/testWorker.js',
+      {
+        workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE,
+        workerChoiceStrategyOptions: {
+          medRunTime: true
+        }
+      }
+    )
+    // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
+    const promises = []
+    const maxMultiplier = 2
+    for (let i = 0; i < max * maxMultiplier; i++) {
+      promises.push(pool.execute())
+    }
+    await Promise.all(promises)
+    for (const workerNode of pool.workerNodes) {
+      expect(workerNode.tasksUsage.avgRunTime).toBeDefined()
+      expect(workerNode.tasksUsage.avgRunTime).toBe(0)
+      expect(workerNode.tasksUsage.medRunTime).toBeDefined()
+      expect(workerNode.tasksUsage.medRunTime).toBeGreaterThan(0)
+    }
+    // if (process.platform !== 'win32') {
+    //   expect(
+    //     pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+    //       pool.workerChoiceStrategyContext.workerChoiceStrategy
+    //     ).workerLastVirtualTaskTimestamp.size
+    //   ).toBe(pool.workerNodes.length)
+    // }
+    // We need to clean up the resources after our test
+    await pool.destroy()
+  })
+
   it('Verify FAIR_SHARE strategy internals are resets after setting it', async () => {
     const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
     let pool = new FixedThreadPool(
@@ -619,6 +655,42 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
+  it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool with median run time statistic', async () => {
+    const pool = new DynamicThreadPool(
+      min,
+      max,
+      './tests/worker-files/thread/testWorker.js',
+      {
+        workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
+        workerChoiceStrategyOptions: {
+          medRunTime: true
+        }
+      }
+    )
+    // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
+    const promises = []
+    const maxMultiplier = 2
+    for (let i = 0; i < max * maxMultiplier; i++) {
+      promises.push(pool.execute())
+    }
+    await Promise.all(promises)
+    for (const workerNode of pool.workerNodes) {
+      expect(workerNode.tasksUsage.avgRunTime).toBeDefined()
+      expect(workerNode.tasksUsage.avgRunTime).toBe(0)
+      expect(workerNode.tasksUsage.medRunTime).toBeDefined()
+      expect(workerNode.tasksUsage.medRunTime).toBeGreaterThan(0)
+    }
+    // if (process.platform !== 'win32') {
+    //   expect(
+    //     pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
+    //       pool.workerChoiceStrategyContext.workerChoiceStrategy
+    //     ).workersTaskRunTime.size
+    //   ).toBe(pool.workerNodes.length)
+    // }
+    // We need to clean up the resources after our test
+    await pool.destroy()
+  })
+
   it('Verify WEIGHTED_ROUND_ROBIN strategy internals are resets after setting it', async () => {
     const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
     let pool = new FixedThreadPool(
@@ -705,7 +777,7 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
-  it('Verify unknown strategies throw error', () => {
+  it('Verify unknown strategy throw error', () => {
     expect(
       () =>
         new DynamicThreadPool(
@@ -714,8 +786,6 @@ describe('Selection strategies test suite', () => {
           './tests/worker-files/thread/testWorker.js',
           { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
         )
-    ).toThrowError(
-      new Error("Invalid worker choice strategy 'UNKNOWN_STRATEGY'")
-    )
+    ).toThrowError("Invalid worker choice strategy 'UNKNOWN_STRATEGY'")
   })
 })