Fix tests with WRR worker choice strategy
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 10 Oct 2022 12:49:44 +0000 (14:49 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 10 Oct 2022 12:49:44 +0000 (14:49 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
tests/pools/selection-strategies/selection-strategies-utils.test.js
tests/pools/selection-strategies/worker-choice-strategy-context.test.js

index 1a60d119ecb763e2f6bed8aa6c6af8500c6fac18..ac1836e7d341ca525d15bc8ba95d50d54e73f298 100644 (file)
@@ -1,5 +1,5 @@
 const { expect } = require('expect')
-const sinon = require('sinon')
+// const sinon = require('sinon')
 const {
   SelectionStrategiesUtils
 } = require('../../../lib/pools/selection-strategies/selection-strategies-utils')
@@ -16,18 +16,24 @@ const {
 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-choice-strategy')
 
 describe('Selection strategies utils test suite', () => {
+  const max = 3
   let pool
-  beforeEach(() => {
-    pool = sinon.createStubInstance(FixedThreadPool)
+
+  before(() => {
+    pool = new FixedThreadPool(max, './tests/worker-files/thread/testWorker.js')
   })
 
-  afterEach(() => {
-    sinon.restore()
+  // afterEach(() => {
+  //   sinon.restore()
+  // })
+
+  after(() => {
+    pool.destroy()
   })
 
   it('Verify that getWorkerChoiceStrategy() default return ROUND_ROBIN strategy', () => {
@@ -59,13 +65,13 @@ describe('Selection strategies utils test suite', () => {
     expect(strategy).toBeInstanceOf(FairShareWorkerChoiceStrategy)
   })
 
-  // it('Verify that getWorkerChoiceStrategy() can return WEIGHTED_ROUND_ROBIN strategy', () => {
-  //   const strategy = SelectionStrategiesUtils.getWorkerChoiceStrategy(
-  //     pool,
-  //     WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
-  //   )
-  //   expect(strategy).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
-  // })
+  it('Verify that getWorkerChoiceStrategy() can return WEIGHTED_ROUND_ROBIN strategy', () => {
+    const strategy = SelectionStrategiesUtils.getWorkerChoiceStrategy(
+      pool,
+      WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
+    )
+    expect(strategy).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
+  })
 
   it('Verify that getWorkerChoiceStrategy() throw error on unknown strategy', () => {
     expect(() => {
index 017d7db2cdb5ce2d04f6d2da48a5308ab60b8605..ae608fda6133a35c88a4b8fc23d5e6586493e891 100644 (file)
@@ -17,24 +17,39 @@ const {
 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-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(() => {
+    fixedPool.destroy()
+    dynamicPool.destroy()
+  })
+
   it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
     const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
       fixedPool
@@ -143,27 +158,27 @@ describe('Worker choice strategy context test suite', () => {
     )
   })
 
-  // 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 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.workerChoiceStrategy).toBeInstanceOf(
+      DynamicPoolWorkerChoiceStrategy
+    )
+  })
 })