Merge branch 'master' of github.com:poolifier/poolifier into interleaved-weighted...
[poolifier.git] / tests / pools / selection-strategies / selection-strategies.test.js
index 392aafcf3a58a79f6744a37268b01415243a2b97..ea5fc04a69dd2c1fe6cf8b124c22b63fd2af3638 100644 (file)
@@ -12,12 +12,15 @@ describe('Selection strategies test suite', () => {
 
   it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
     expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN')
-    expect(WorkerChoiceStrategies.LESS_USED).toBe('LESS_USED')
-    expect(WorkerChoiceStrategies.LESS_BUSY).toBe('LESS_BUSY')
+    expect(WorkerChoiceStrategies.LEAST_USED).toBe('LEAST_USED')
+    expect(WorkerChoiceStrategies.LEAST_BUSY).toBe('LEAST_BUSY')
     expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE')
     expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe(
       'WEIGHTED_ROUND_ROBIN'
     )
+    expect(WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN).toBe(
+      'INTERLEAVED_WEIGHTED_ROUND_ROBIN'
+    )
   })
 
   it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
@@ -80,12 +83,12 @@ describe('Selection strategies test suite', () => {
         expect(
           pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
             workerChoiceStrategy
-          ).workersVirtualTaskTimestamp
+          ).workersVirtualTaskEndTimestamp
         ).toBeInstanceOf(Array)
         expect(
           pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
             workerChoiceStrategy
-          ).workersVirtualTaskTimestamp.length
+          ).workersVirtualTaskEndTimestamp.length
         ).toBe(0)
       } else if (
         workerChoiceStrategy === WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
@@ -186,7 +189,7 @@ describe('Selection strategies test suite', () => {
     )
     let results = new Set()
     for (let i = 0; i < max; i++) {
-      results.add(pool.chooseWorkerNode()[1].worker.id)
+      results.add(pool.workerNodes[pool.chooseWorkerNode()].worker.id)
     }
     expect(results.size).toBe(max)
     await pool.destroy()
@@ -197,7 +200,7 @@ describe('Selection strategies test suite', () => {
     )
     results = new Set()
     for (let i = 0; i < max; i++) {
-      results.add(pool.chooseWorkerNode()[1].worker.threadId)
+      results.add(pool.workerNodes[pool.chooseWorkerNode()].worker.threadId)
     }
     expect(results.size).toBe(max)
     await pool.destroy()
@@ -243,8 +246,8 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
-  it('Verify LESS_USED strategy default tasks usage statistics requirements', async () => {
-    const workerChoiceStrategy = WorkerChoiceStrategies.LESS_USED
+  it('Verify LEAST_USED strategy default tasks usage statistics requirements', async () => {
+    const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_USED
     let pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js',
@@ -279,13 +282,13 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
-  it('Verify LESS_USED strategy can be run in a fixed pool', async () => {
+  it('Verify LEAST_USED strategy can be run in a fixed pool', async () => {
     const pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js',
-      { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
+      { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED }
     )
-    // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
+    // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose`
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
       await pool.execute()
@@ -294,14 +297,14 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
-  it('Verify LESS_USED strategy can be run in a dynamic pool', async () => {
+  it('Verify LEAST_USED strategy can be run in a dynamic pool', async () => {
     const pool = new DynamicThreadPool(
       min,
       max,
       './tests/worker-files/thread/testWorker.js',
-      { workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED }
+      { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_USED }
     )
-    // TODO: Create a better test to cover `LessUsedWorkerChoiceStrategy#choose`
+    // TODO: Create a better test to cover `LeastUsedWorkerChoiceStrategy#choose`
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
       await pool.execute()
@@ -310,8 +313,8 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
-  it('Verify LESS_BUSY strategy default tasks usage statistics requirements', async () => {
-    const workerChoiceStrategy = WorkerChoiceStrategies.LESS_BUSY
+  it('Verify LEAST_BUSY strategy default tasks usage statistics requirements', async () => {
+    const workerChoiceStrategy = WorkerChoiceStrategies.LEAST_BUSY
     let pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js',
@@ -346,13 +349,13 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
-  it('Verify LESS_BUSY strategy can be run in a fixed pool', async () => {
+  it('Verify LEAST_BUSY strategy can be run in a fixed pool', async () => {
     const pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js',
-      { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY }
+      { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY }
     )
-    // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
+    // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose`
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
       await pool.execute()
@@ -361,14 +364,14 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
-  it('Verify LESS_BUSY strategy can be run in a dynamic pool', async () => {
+  it('Verify LEAST_BUSY strategy can be run in a dynamic pool', async () => {
     const pool = new DynamicThreadPool(
       min,
       max,
       './tests/worker-files/thread/testWorker.js',
-      { workerChoiceStrategy: WorkerChoiceStrategies.LESS_BUSY }
+      { workerChoiceStrategy: WorkerChoiceStrategies.LEAST_BUSY }
     )
-    // TODO: Create a better test to cover `LessBusyWorkerChoiceStrategy#choose`
+    // TODO: Create a better test to cover `LeastBusyWorkerChoiceStrategy#choose`
     const maxMultiplier = 2
     for (let i = 0; i < max * maxMultiplier; i++) {
       await pool.execute()
@@ -433,7 +436,7 @@ describe('Selection strategies test suite', () => {
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
         pool.workerChoiceStrategyContext.workerChoiceStrategy
-      ).workersVirtualTaskTimestamp.length
+      ).workersVirtualTaskEndTimestamp.length
     ).toBe(pool.workerNodes.length)
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -460,13 +463,13 @@ describe('Selection strategies test suite', () => {
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
         pool.workerChoiceStrategyContext.workerChoiceStrategy
-      ).workersVirtualTaskTimestamp.length
+      ).workersVirtualTaskEndTimestamp.length
     ).toBe(pool.workerNodes.length)
     // We need to clean up the resources after our test
     await pool.destroy()
   })
 
-  it('Verify FAIR_SHARE strategy can be run in a dynamic pool with median run time statistic', async () => {
+  it('Verify FAIR_SHARE strategy can be run in a dynamic pool with median runtime statistic', async () => {
     const pool = new DynamicThreadPool(
       min,
       max,
@@ -492,7 +495,7 @@ describe('Selection strategies test suite', () => {
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
         pool.workerChoiceStrategyContext.workerChoiceStrategy
-      ).workersVirtualTaskTimestamp.length
+      ).workersVirtualTaskEndTimestamp.length
     ).toBe(pool.workerNodes.length)
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -507,31 +510,31 @@ describe('Selection strategies test suite', () => {
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategy
-      ).workersVirtualTaskTimestamp
+      ).workersVirtualTaskEndTimestamp
     ).toBeInstanceOf(Array)
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategy
-      ).workersVirtualTaskTimestamp.length
+      ).workersVirtualTaskEndTimestamp.length
     ).toBe(0)
     pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
       workerChoiceStrategy
-    ).workersVirtualTaskTimestamp[0] = 0
+    ).workersVirtualTaskEndTimestamp[0] = performance.now()
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategy
-      ).workersVirtualTaskTimestamp.length
+      ).workersVirtualTaskEndTimestamp.length
     ).toBe(1)
     pool.setWorkerChoiceStrategy(workerChoiceStrategy)
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategy
-      ).workersVirtualTaskTimestamp
+      ).workersVirtualTaskEndTimestamp
     ).toBeInstanceOf(Array)
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategy
-      ).workersVirtualTaskTimestamp.length
+      ).workersVirtualTaskEndTimestamp.length
     ).toBe(0)
     await pool.destroy()
     pool = new DynamicThreadPool(
@@ -542,31 +545,31 @@ describe('Selection strategies test suite', () => {
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategy
-      ).workersVirtualTaskTimestamp
+      ).workersVirtualTaskEndTimestamp
     ).toBeInstanceOf(Array)
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategy
-      ).workersVirtualTaskTimestamp.length
+      ).workersVirtualTaskEndTimestamp.length
     ).toBe(0)
     pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
       workerChoiceStrategy
-    ).workersVirtualTaskTimestamp[0] = 0
+    ).workersVirtualTaskEndTimestamp[0] = performance.now()
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategy
-      ).workersVirtualTaskTimestamp.length
+      ).workersVirtualTaskEndTimestamp.length
     ).toBe(1)
     pool.setWorkerChoiceStrategy(workerChoiceStrategy)
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategy
-      ).workersVirtualTaskTimestamp
+      ).workersVirtualTaskEndTimestamp
     ).toBeInstanceOf(Array)
     expect(
       pool.workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategy
-      ).workersVirtualTaskTimestamp.length
+      ).workersVirtualTaskEndTimestamp.length
     ).toBe(0)
     // We need to clean up the resources after our test
     await pool.destroy()
@@ -671,7 +674,7 @@ 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 () => {
+  it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool with median runtime statistic', async () => {
     const pool = new DynamicThreadPool(
       min,
       max,