1 const { expect
} = require('expect')
2 const sinon
= require('sinon')
7 } = require('../../../lib/index')
9 RoundRobinWorkerChoiceStrategy
10 } = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy')
12 LessRecentlyUsedWorkerChoiceStrategy
13 } = require('../../../lib/pools/selection-strategies/less-recently-used-worker-choice-strategy')
15 WorkerChoiceStrategyContext
16 } = require('../../../lib/pools/selection-strategies/worker-choice-strategy-context')
18 DynamicPoolWorkerChoiceStrategy
19 } = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy')
21 describe('Worker choice strategy context test suite', () => {
22 let fixedPool
, dynamicPool
24 fixedPool
= sinon
.createStubInstance(FixedThreadPool
)
25 dynamicPool
= sinon
.createStubInstance(DynamicThreadPool
)
32 it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
33 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
36 const WorkerChoiceStrategyStub
= sinon
.createStubInstance(
37 RoundRobinWorkerChoiceStrategy
,
39 choose
: sinon
.stub().returns('worker')
42 workerChoiceStrategyContext
.workerChoiceStrategy
= WorkerChoiceStrategyStub
43 const worker
= workerChoiceStrategyContext
.execute()
45 workerChoiceStrategyContext
.workerChoiceStrategy
.choose
.calledOnce
47 expect(worker
).toBe('worker')
50 it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
51 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
54 const WorkerChoiceStrategyStub
= sinon
.createStubInstance(
55 RoundRobinWorkerChoiceStrategy
,
57 choose
: sinon
.stub().returns('worker')
60 workerChoiceStrategyContext
.workerChoiceStrategy
= WorkerChoiceStrategyStub
61 const worker
= workerChoiceStrategyContext
.execute()
63 workerChoiceStrategyContext
.workerChoiceStrategy
.choose
.calledOnce
65 expect(worker
).toBe('worker')
68 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
69 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
72 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
73 WorkerChoiceStrategies
.ROUND_ROBIN
75 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
76 RoundRobinWorkerChoiceStrategy
80 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
81 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
84 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
85 WorkerChoiceStrategies
.ROUND_ROBIN
87 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
88 DynamicPoolWorkerChoiceStrategy
92 it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => {
93 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
96 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
97 WorkerChoiceStrategies
.LESS_RECENTLY_USED
99 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
100 LessRecentlyUsedWorkerChoiceStrategy
104 it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => {
105 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
108 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
109 WorkerChoiceStrategies
.LESS_RECENTLY_USED
111 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
112 DynamicPoolWorkerChoiceStrategy