fix: check worker aliveness in all case
[poolifier.git] / tests / pools / selection-strategies / worker-choice-strategy-context.test.js
index ae608fda6133a35c88a4b8fc23d5e6586493e891..1f4b51cbf4d13956fd9ff5ef6c4550c3630e10f6 100644 (file)
@@ -12,14 +12,17 @@ const {
   RoundRobinWorkerChoiceStrategy
 } = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy')
 const {
-  LessRecentlyUsedWorkerChoiceStrategy
-} = require('../../../lib/pools/selection-strategies/less-recently-used-worker-choice-strategy')
+  LessUsedWorkerChoiceStrategy
+} = require('../../../lib/pools/selection-strategies/less-used-worker-choice-strategy')
+const {
+  LessBusyWorkerChoiceStrategy
+} = require('../../../lib/pools/selection-strategies/less-busy-worker-choice-strategy')
 const {
   FairShareWorkerChoiceStrategy
 } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy')
 const {
   WeightedRoundRobinWorkerChoiceStrategy
-} = require('../../../lib/pools/selection-strategies/weighted-round-robin-choice-strategy')
+} = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy')
 const {
   DynamicPoolWorkerChoiceStrategy
 } = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy')
@@ -45,9 +48,9 @@ describe('Worker choice strategy context test suite', () => {
     sinon.restore()
   })
 
-  after(() => {
-    fixedPool.destroy()
-    dynamicPool.destroy()
+  after(async () => {
+    await fixedPool.destroy()
+    await dynamicPool.destroy()
   })
 
   it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
@@ -57,15 +60,15 @@ describe('Worker choice strategy context test suite', () => {
     const WorkerChoiceStrategyStub = sinon.createStubInstance(
       RoundRobinWorkerChoiceStrategy,
       {
-        choose: sinon.stub().returns('worker')
+        choose: sinon.stub().returns(0)
       }
     )
     workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
-    const worker = workerChoiceStrategyContext.execute()
+    const chosenWorkerKey = workerChoiceStrategyContext.execute()
     expect(
       workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
     ).toBe(true)
-    expect(worker).toBe('worker')
+    expect(chosenWorkerKey).toBe(0)
   })
 
   it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
@@ -75,15 +78,15 @@ describe('Worker choice strategy context test suite', () => {
     const WorkerChoiceStrategyStub = sinon.createStubInstance(
       RoundRobinWorkerChoiceStrategy,
       {
-        choose: sinon.stub().returns('worker')
+        choose: sinon.stub().returns(0)
       }
     )
     workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
-    const worker = workerChoiceStrategyContext.execute()
+    const chosenWorkerKey = workerChoiceStrategyContext.execute()
     expect(
       workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
     ).toBe(true)
-    expect(worker).toBe('worker')
+    expect(chosenWorkerKey).toBe(0)
   })
 
   it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
@@ -108,30 +111,63 @@ describe('Worker choice strategy context test suite', () => {
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
       DynamicPoolWorkerChoiceStrategy
     )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
+    ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
   })
 
-  it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => {
+  it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
     )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
-      WorkerChoiceStrategies.LESS_RECENTLY_USED
+      WorkerChoiceStrategies.LESS_USED
     )
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      LessRecentlyUsedWorkerChoiceStrategy
+      LessUsedWorkerChoiceStrategy
     )
   })
 
-  it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and dynamic pool', () => {
+  it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       dynamicPool
     )
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
-      WorkerChoiceStrategies.LESS_RECENTLY_USED
+      WorkerChoiceStrategies.LESS_USED
     )
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
       DynamicPoolWorkerChoiceStrategy
     )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
+    ).toBeInstanceOf(LessUsedWorkerChoiceStrategy)
+  })
+
+  it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => {
+    const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+      fixedPool
+    )
+    workerChoiceStrategyContext.setWorkerChoiceStrategy(
+      WorkerChoiceStrategies.LESS_BUSY
+    )
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
+      LessBusyWorkerChoiceStrategy
+    )
+  })
+
+  it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => {
+    const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+      dynamicPool
+    )
+    workerChoiceStrategyContext.setWorkerChoiceStrategy(
+      WorkerChoiceStrategies.LESS_BUSY
+    )
+    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
+      DynamicPoolWorkerChoiceStrategy
+    )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
+    ).toBeInstanceOf(LessBusyWorkerChoiceStrategy)
   })
 
   it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
@@ -156,6 +192,9 @@ describe('Worker choice strategy context test suite', () => {
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
       DynamicPoolWorkerChoiceStrategy
     )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
+    ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
   })
 
   it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
@@ -180,5 +219,8 @@ describe('Worker choice strategy context test suite', () => {
     expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
       DynamicPoolWorkerChoiceStrategy
     )
+    expect(
+      workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
+    ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
   })
 })