Cleanup tests code
[poolifier.git] / tests / pools / selection-strategies / selection-strategies.test.js
index b12a8df4b726438ba173f867ea59e19123f00fb1..a2365a5d777c77cbe3ffe56fd7ebc4065fdf04e5 100644 (file)
@@ -6,6 +6,9 @@ const {
 } = require('../../../lib/index')
 
 describe('Selection strategies test suite', () => {
+  const min = 0
+  const max = 3
+
   it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
     expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN')
     expect(WorkerChoiceStrategies.LESS_RECENTLY_USED).toBe('LESS_RECENTLY_USED')
@@ -16,8 +19,6 @@ describe('Selection strategies test suite', () => {
   })
 
   it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
-    const min = 0
-    const max = 3
     const pool = new DynamicThreadPool(
       min,
       max,
@@ -31,8 +32,6 @@ describe('Selection strategies test suite', () => {
   })
 
   it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
-    const min = 0
-    const max = 3
     const pool = new DynamicThreadPool(
       min,
       max,
@@ -46,8 +45,31 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
+  it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
+    let pool = new FixedThreadPool(
+      max,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .requiredStatistics.runTime
+    ).toBe(false)
+    pool = new DynamicThreadPool(
+      min,
+      max,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .requiredStatistics.runTime
+    ).toBe(false)
+    // We need to clean up the resources after our test
+    await pool.destroy()
+  })
+
   it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
-    const max = 3
     const pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js',
@@ -59,7 +81,7 @@ describe('Selection strategies test suite', () => {
     // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
     const promises = []
     for (let i = 0; i < max * 2; i++) {
-      promises.push(pool.execute({ test: 'test' }))
+      promises.push(pool.execute())
     }
     await Promise.all(promises)
     // We need to clean up the resources after our test
@@ -67,8 +89,6 @@ describe('Selection strategies test suite', () => {
   })
 
   it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
-    const min = 0
-    const max = 3
     const pool = new DynamicThreadPool(
       min,
       max,
@@ -81,7 +101,7 @@ describe('Selection strategies test suite', () => {
     // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
     const promises = []
     for (let i = 0; i < max * 2; i++) {
-      promises.push(pool.execute({ test: 'test' }))
+      promises.push(pool.execute())
     }
     await Promise.all(promises)
     // We need to clean up the resources after our test
@@ -89,7 +109,6 @@ describe('Selection strategies test suite', () => {
   })
 
   it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
-    const max = 3
     const pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js',
@@ -103,7 +122,6 @@ describe('Selection strategies test suite', () => {
   })
 
   it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
-    const max = 3
     const pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js'
@@ -116,8 +134,31 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
+  it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => {
+    let pool = new FixedThreadPool(
+      max,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .requiredStatistics.runTime
+    ).toBe(false)
+    pool = new DynamicThreadPool(
+      min,
+      max,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .requiredStatistics.runTime
+    ).toBe(false)
+    // We need to clean up the resources after our test
+    await pool.destroy()
+  })
+
   it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
-    const max = 3
     const pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js',
@@ -126,7 +167,7 @@ describe('Selection strategies test suite', () => {
     // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
     const promises = []
     for (let i = 0; i < max * 2; i++) {
-      promises.push(pool.execute({ test: 'test' }))
+      promises.push(pool.execute())
     }
     await Promise.all(promises)
     // We need to clean up the resources after our test
@@ -134,8 +175,6 @@ describe('Selection strategies test suite', () => {
   })
 
   it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
-    const min = 0
-    const max = 3
     const pool = new DynamicThreadPool(
       min,
       max,
@@ -145,7 +184,7 @@ describe('Selection strategies test suite', () => {
     // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
     const promises = []
     for (let i = 0; i < max * 2; i++) {
-      promises.push(pool.execute({ test: 'test' }))
+      promises.push(pool.execute())
     }
     await Promise.all(promises)
     // We need to clean up the resources after our test
@@ -153,7 +192,6 @@ describe('Selection strategies test suite', () => {
   })
 
   it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
-    const max = 3
     const pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js',
@@ -167,7 +205,6 @@ describe('Selection strategies test suite', () => {
   })
 
   it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
-    const max = 3
     const pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js'
@@ -180,8 +217,31 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
+  it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
+    let pool = new FixedThreadPool(
+      max,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .requiredStatistics.runTime
+    ).toBe(true)
+    pool = new DynamicThreadPool(
+      min,
+      max,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .requiredStatistics.runTime
+    ).toBe(true)
+    // We need to clean up the resources after our test
+    await pool.destroy()
+  })
+
   it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
-    const max = 3
     const pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js',
@@ -190,7 +250,7 @@ describe('Selection strategies test suite', () => {
     // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
     const promises = []
     for (let i = 0; i < max * 2; i++) {
-      promises.push(pool.execute({ test: 'test' }))
+      promises.push(pool.execute())
     }
     await Promise.all(promises)
     // We need to clean up the resources after our test
@@ -198,8 +258,6 @@ describe('Selection strategies test suite', () => {
   })
 
   it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
-    const min = 0
-    const max = 3
     const pool = new DynamicThreadPool(
       min,
       max,
@@ -209,7 +267,7 @@ describe('Selection strategies test suite', () => {
     // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
     const promises = []
     for (let i = 0; i < max * 2; i++) {
-      promises.push(pool.execute({ test: 'test' }))
+      promises.push(pool.execute())
     }
     await Promise.all(promises)
     // We need to clean up the resources after our test
@@ -217,7 +275,6 @@ describe('Selection strategies test suite', () => {
   })
 
   it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
-    const max = 3
     const pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js',
@@ -231,7 +288,6 @@ describe('Selection strategies test suite', () => {
   })
 
   it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
-    const max = 3
     const pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js'
@@ -244,8 +300,31 @@ describe('Selection strategies test suite', () => {
     await pool.destroy()
   })
 
+  it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
+    let pool = new FixedThreadPool(
+      max,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .requiredStatistics.runTime
+    ).toBe(true)
+    pool = new DynamicThreadPool(
+      min,
+      max,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
+    expect(
+      pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
+        .requiredStatistics.runTime
+    ).toBe(true)
+    // We need to clean up the resources after our test
+    await pool.destroy()
+  })
+
   it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
-    const max = 3
     const pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.js',
@@ -254,7 +333,7 @@ describe('Selection strategies test suite', () => {
     // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
     const promises = []
     for (let i = 0; i < max * 2; i++) {
-      promises.push(pool.execute({ test: 'test' }))
+      promises.push(pool.execute())
     }
     await Promise.all(promises)
     // We need to clean up the resources after our test
@@ -262,8 +341,6 @@ describe('Selection strategies test suite', () => {
   })
 
   it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
-    const min = 0
-    const max = 3
     const pool = new DynamicThreadPool(
       min,
       max,
@@ -273,7 +350,7 @@ describe('Selection strategies test suite', () => {
     // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
     const promises = []
     for (let i = 0; i < max * 2; i++) {
-      promises.push(pool.execute({ test: 'test' }))
+      promises.push(pool.execute())
     }
     await Promise.all(promises)
     // We need to clean up the resources after our test
@@ -281,8 +358,6 @@ describe('Selection strategies test suite', () => {
   })
 
   it('Verify unknown strategies throw error', () => {
-    const min = 1
-    const max = 3
     expect(
       () =>
         new DynamicThreadPool(