Add tests for WRR worker choice strategy
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 13 Oct 2022 19:35:50 +0000 (21:35 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 13 Oct 2022 19:35:50 +0000 (21:35 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.js [new file with mode: 0644]
tests/pools/selection-strategies/worker-choice-strategy-context.test.js

diff --git a/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.js b/tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.js
new file mode 100644 (file)
index 0000000..664a447
--- /dev/null
@@ -0,0 +1,41 @@
+const { expect } = require('expect')
+const sinon = require('sinon')
+const { FixedThreadPool } = require('../../../lib/index')
+const {
+  WeightedRoundRobinWorkerChoiceStrategy
+} = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy')
+
+describe('Weighted round robin strategy worker choice strategy test suite', () => {
+  // const min = 1
+  const max = 3
+  let pool
+
+  before(() => {
+    pool = new FixedThreadPool(max, './tests/worker-files/thread/testWorker.js')
+  })
+
+  afterEach(() => {
+    sinon.restore()
+  })
+
+  after(async () => {
+    await pool.destroy()
+  })
+
+  it.only('Verify that reset() resets internals', () => {
+    const strategy = new WeightedRoundRobinWorkerChoiceStrategy(pool)
+    const workersTaskRunTimeClearStub = sinon
+      .stub(strategy.workersTaskRunTime, 'clear')
+      .returns()
+    const initWorkersTaskRunTimeStub = sinon
+      .stub(strategy, 'initWorkersTaskRunTime')
+      .returns()
+    const resetResult = strategy.reset()
+    expect(resetResult).toBe(true)
+    expect(strategy.previousWorkerIndex).toBe(0)
+    expect(strategy.currentWorkerIndex).toBe(0)
+    expect(strategy.defaultWorkerWeight).toBeGreaterThan(0)
+    expect(workersTaskRunTimeClearStub.calledOnce).toBe(true)
+    expect(initWorkersTaskRunTimeStub.calledOnce).toBe(true)
+  })
+})
index c30bda737d35637f2b88020f0ed8441a0fb5c020..8a3a759610d167f86ad5085a4ec730686337e309 100644 (file)
@@ -61,11 +61,11 @@ describe('Worker choice strategy context test suite', () => {
       }
     )
     workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
-    const worker = workerChoiceStrategyContext.execute()
+    const chosenWorker = workerChoiceStrategyContext.execute()
     expect(
       workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
     ).toBe(true)
-    expect(worker).toBe('worker')
+    expect(chosenWorker).toBe('worker')
   })
 
   it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
@@ -79,11 +79,11 @@ describe('Worker choice strategy context test suite', () => {
       }
     )
     workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub
-    const worker = workerChoiceStrategyContext.execute()
+    const chosenWorker = workerChoiceStrategyContext.execute()
     expect(
       workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce
     ).toBe(true)
-    expect(worker).toBe('worker')
+    expect(chosenWorker).toBe('worker')
   })
 
   it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {