perf: use worker key as much as possible instead of a reference to the
[poolifier.git] / tests / pools / selection-strategies / worker-choice-strategy-context.test.js
index 017d7db2cdb5ce2d04f6d2da48a5308ab60b8605..9ab2babfd31ce26fa6286338ced68d2e4f2ae7ea 100644 (file)
@@ -12,29 +12,47 @@ 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')
+const {
+  WeightedRoundRobinWorkerChoiceStrategy
+} = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy')
 const {
   DynamicPoolWorkerChoiceStrategy
 } = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy')
 
 describe('Worker choice strategy context test suite', () => {
+  const min = 1
+  const max = 3
   let fixedPool, dynamicPool
-  beforeEach(() => {
-    fixedPool = sinon.createStubInstance(FixedThreadPool)
-    dynamicPool = sinon.createStubInstance(DynamicThreadPool)
+
+  before(() => {
+    fixedPool = new FixedThreadPool(
+      max,
+      './tests/worker-files/thread/testWorker.js'
+    )
+    dynamicPool = new DynamicThreadPool(
+      min,
+      max,
+      './tests/worker-files/thread/testWorker.js'
+    )
   })
 
   afterEach(() => {
     sinon.restore()
   })
 
+  after(async () => {
+    await fixedPool.destroy()
+    await dynamicPool.destroy()
+  })
+
   it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
@@ -42,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
+      workerChoiceStrategyContext.getWorkerChoiceStrategy().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', () => {
@@ -60,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
+      workerChoiceStrategyContext.getWorkerChoiceStrategy().choose.calledOnce
     ).toBe(true)
-    expect(worker).toBe('worker')
+    expect(chosenWorkerKey).toBe(0)
   })
 
   it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
@@ -78,9 +96,9 @@ describe('Worker choice strategy context test suite', () => {
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
       WorkerChoiceStrategies.ROUND_ROBIN
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      RoundRobinWorkerChoiceStrategy
-    )
+    expect(
+      workerChoiceStrategyContext.getWorkerChoiceStrategy()
+    ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
   })
 
   it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
@@ -90,33 +108,66 @@ describe('Worker choice strategy context test suite', () => {
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
       WorkerChoiceStrategies.ROUND_ROBIN
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      DynamicPoolWorkerChoiceStrategy
-    )
+    expect(
+      workerChoiceStrategyContext.getWorkerChoiceStrategy()
+    ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy)
+    expect(
+      workerChoiceStrategyContext.getWorkerChoiceStrategy().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
-    )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      LessRecentlyUsedWorkerChoiceStrategy
+      WorkerChoiceStrategies.LESS_USED
     )
+    expect(
+      workerChoiceStrategyContext.getWorkerChoiceStrategy()
+    ).toBeInstanceOf(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.getWorkerChoiceStrategy()
+    ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy)
+    expect(
+      workerChoiceStrategyContext.getWorkerChoiceStrategy().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.getWorkerChoiceStrategy()
+    ).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.getWorkerChoiceStrategy()
+    ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy)
+    expect(
+      workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy
+    ).toBeInstanceOf(LessBusyWorkerChoiceStrategy)
   })
 
   it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
@@ -126,9 +177,9 @@ describe('Worker choice strategy context test suite', () => {
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
       WorkerChoiceStrategies.FAIR_SHARE
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      FairShareWorkerChoiceStrategy
-    )
+    expect(
+      workerChoiceStrategyContext.getWorkerChoiceStrategy()
+    ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
   })
 
   it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
@@ -138,32 +189,38 @@ describe('Worker choice strategy context test suite', () => {
     workerChoiceStrategyContext.setWorkerChoiceStrategy(
       WorkerChoiceStrategies.FAIR_SHARE
     )
-    expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-      DynamicPoolWorkerChoiceStrategy
+    expect(
+      workerChoiceStrategyContext.getWorkerChoiceStrategy()
+    ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy)
+    expect(
+      workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy
+    ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
+  })
+
+  it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
+    const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+      fixedPool
+    )
+    workerChoiceStrategyContext.setWorkerChoiceStrategy(
+      WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
     )
+    expect(
+      workerChoiceStrategyContext.getWorkerChoiceStrategy()
+    ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
   })
 
-  // it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
-  //   const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
-  //     fixedPool
-  //   )
-  //   workerChoiceStrategyContext.setWorkerChoiceStrategy(
-  //     WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
-  //   )
-  //   expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-  //     WeightedRoundRobinWorkerChoiceStrategy
-  //   )
-  // })
-
-  // it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
-  //   const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
-  //     dynamicPool
-  //   )
-  //   workerChoiceStrategyContext.setWorkerChoiceStrategy(
-  //     WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
-  //   )
-  //   expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
-  //     DynamicPoolWorkerChoiceStrategy
-  //   )
-  // })
+  it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
+    const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
+      dynamicPool
+    )
+    workerChoiceStrategyContext.setWorkerChoiceStrategy(
+      WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+    )
+    expect(
+      workerChoiceStrategyContext.getWorkerChoiceStrategy()
+    ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy)
+    expect(
+      workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy
+    ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
+  })
 })