Merge branch 'master' of github.com:poolifier/poolifier into interleaved-weighted...
[poolifier.git] / tests / pools / selection-strategies / worker-choice-strategy-context.test.js
index 23d954c9f13159940e542c8774a3d22feefeaaad..f04013d99c92ad0e61dae665f79df61dda1d2274 100644 (file)
@@ -4,7 +4,7 @@ const {
   FixedThreadPool,
   DynamicThreadPool,
   WorkerChoiceStrategies
-} = require('../../../lib/index')
+} = require('../../../lib')
 const {
   WorkerChoiceStrategyContext
 } = require('../../../lib/pools/selection-strategies/worker-choice-strategy-context')
@@ -85,6 +85,41 @@ describe('Worker choice strategy context test suite', () => {
     expect(chosenWorkerKey).toBe(0)
   })
 
+  it('Verify that execute() throws error if null or undefined is returned', () => {
+    const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+      fixedPool
+    )
+    const WorkerChoiceStrategyUndefinedStub = sinon.createStubInstance(
+      RoundRobinWorkerChoiceStrategy,
+      {
+        choose: sinon.stub().returns(undefined)
+      }
+    )
+    const WorkerChoiceStrategyNullStub = sinon.createStubInstance(
+      RoundRobinWorkerChoiceStrategy,
+      {
+        choose: sinon.stub().returns(null)
+      }
+    )
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
+      WorkerChoiceStrategies.ROUND_ROBIN
+    )
+    workerChoiceStrategyContext.workerChoiceStrategies.set(
+      workerChoiceStrategyContext.workerChoiceStrategy,
+      WorkerChoiceStrategyUndefinedStub
+    )
+    expect(() => workerChoiceStrategyContext.execute()).toThrowError(
+      new Error('Worker node key chosen is null or undefined')
+    )
+    workerChoiceStrategyContext.workerChoiceStrategies.set(
+      workerChoiceStrategyContext.workerChoiceStrategy,
+      WorkerChoiceStrategyNullStub
+    )
+    expect(() => workerChoiceStrategyContext.execute()).toThrowError(
+      new Error('Worker node key chosen is null or undefined')
+    )
+  })
+
   it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       dynamicPool
@@ -116,11 +151,6 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
     )
-    expect(
-      workerChoiceStrategyContext.workerChoiceStrategies.get(
-        workerChoiceStrategy
-      ).isDynamicPool
-    ).toBe(false)
     expect(
       workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategy
@@ -145,11 +175,6 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       dynamicPool
     )
-    expect(
-      workerChoiceStrategyContext.workerChoiceStrategies.get(
-        workerChoiceStrategy
-      ).isDynamicPool
-    ).toBe(true)
     expect(
       workerChoiceStrategyContext.workerChoiceStrategies.get(
         workerChoiceStrategy
@@ -174,11 +199,6 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
     )
-    expect(
-      workerChoiceStrategyContext.workerChoiceStrategies.get(
-        workerChoiceStrategy
-      ).isDynamicPool
-    ).toBe(false)
     workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
     expect(
       workerChoiceStrategyContext.workerChoiceStrategies.get(
@@ -195,11 +215,6 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       dynamicPool
     )
-    expect(
-      workerChoiceStrategyContext.workerChoiceStrategies.get(
-        workerChoiceStrategy
-      ).isDynamicPool
-    ).toBe(true)
     workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
     expect(
       workerChoiceStrategyContext.workerChoiceStrategies.get(
@@ -216,11 +231,6 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
     )
-    expect(
-      workerChoiceStrategyContext.workerChoiceStrategies.get(
-        workerChoiceStrategy
-      ).isDynamicPool
-    ).toBe(false)
     workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
     expect(
       workerChoiceStrategyContext.workerChoiceStrategies.get(
@@ -237,11 +247,6 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       dynamicPool
     )
-    expect(
-      workerChoiceStrategyContext.workerChoiceStrategies.get(
-        workerChoiceStrategy
-      ).isDynamicPool
-    ).toBe(true)
     workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
     expect(
       workerChoiceStrategyContext.workerChoiceStrategies.get(
@@ -258,11 +263,6 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
     )
-    expect(
-      workerChoiceStrategyContext.workerChoiceStrategies.get(
-        workerChoiceStrategy
-      ).isDynamicPool
-    ).toBe(false)
     workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
     expect(
       workerChoiceStrategyContext.workerChoiceStrategies.get(
@@ -279,11 +279,6 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       dynamicPool
     )
-    expect(
-      workerChoiceStrategyContext.workerChoiceStrategies.get(
-        workerChoiceStrategy
-      ).isDynamicPool
-    ).toBe(true)
     workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
     expect(
       workerChoiceStrategyContext.workerChoiceStrategies.get(
@@ -300,11 +295,6 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
     )
-    expect(
-      workerChoiceStrategyContext.workerChoiceStrategies.get(
-        workerChoiceStrategy
-      ).isDynamicPool
-    ).toBe(false)
     workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
     expect(
       workerChoiceStrategyContext.workerChoiceStrategies.get(
@@ -321,11 +311,6 @@ describe('Worker choice strategy context test suite', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       dynamicPool
     )
-    expect(
-      workerChoiceStrategyContext.workerChoiceStrategies.get(
-        workerChoiceStrategy
-      ).isDynamicPool
-    ).toBe(true)
     workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
     expect(
       workerChoiceStrategyContext.workerChoiceStrategies.get(
@@ -336,4 +321,61 @@ describe('Worker choice strategy context test suite', () => {
       workerChoiceStrategy
     )
   })
+
+  it('Verify that worker choice strategy options enable median run time pool statistics', () => {
+    const wwrWorkerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+    let workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+      fixedPool,
+      wwrWorkerChoiceStrategy,
+      {
+        medRunTime: true
+      }
+    )
+    expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe(
+      false
+    )
+    expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe(
+      true
+    )
+    workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+      dynamicPool,
+      wwrWorkerChoiceStrategy,
+      {
+        medRunTime: true
+      }
+    )
+    expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe(
+      false
+    )
+    expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe(
+      true
+    )
+    const fsWorkerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
+    workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+      fixedPool,
+      fsWorkerChoiceStrategy,
+      {
+        medRunTime: true
+      }
+    )
+    expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe(
+      false
+    )
+    expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe(
+      true
+    )
+    workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+      dynamicPool,
+      fsWorkerChoiceStrategy,
+      {
+        medRunTime: true
+      }
+    )
+    expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe(
+      false
+    )
+    expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe(
+      true
+    )
+  })
 })