1 const { expect
} = require('expect')
2 const sinon
= require('sinon')
7 } = require('../../../lib/index')
9 WorkerChoiceStrategyContext
10 } = require('../../../lib/pools/selection-strategies/worker-choice-strategy-context')
12 RoundRobinWorkerChoiceStrategy
13 } = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy')
15 LessUsedWorkerChoiceStrategy
16 } = require('../../../lib/pools/selection-strategies/less-used-worker-choice-strategy')
18 FairShareWorkerChoiceStrategy
19 } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy')
21 WeightedRoundRobinWorkerChoiceStrategy
22 } = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy')
24 DynamicPoolWorkerChoiceStrategy
25 } = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy')
27 describe('Worker choice strategy context test suite', () => {
30 let fixedPool
, dynamicPool
33 fixedPool
= new FixedThreadPool(
35 './tests/worker-files/thread/testWorker.js'
37 dynamicPool
= new DynamicThreadPool(
40 './tests/worker-files/thread/testWorker.js'
49 await fixedPool
.destroy()
50 await dynamicPool
.destroy()
53 it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
54 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
57 const WorkerChoiceStrategyStub
= sinon
.createStubInstance(
58 RoundRobinWorkerChoiceStrategy
,
60 choose
: sinon
.stub().returns('worker')
63 workerChoiceStrategyContext
.workerChoiceStrategy
= WorkerChoiceStrategyStub
64 const chosenWorker
= workerChoiceStrategyContext
.execute()
66 workerChoiceStrategyContext
.getWorkerChoiceStrategy().choose
.calledOnce
68 expect(chosenWorker
).toBe('worker')
71 it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
72 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
75 const WorkerChoiceStrategyStub
= sinon
.createStubInstance(
76 RoundRobinWorkerChoiceStrategy
,
78 choose
: sinon
.stub().returns('worker')
81 workerChoiceStrategyContext
.workerChoiceStrategy
= WorkerChoiceStrategyStub
82 const chosenWorker
= workerChoiceStrategyContext
.execute()
84 workerChoiceStrategyContext
.getWorkerChoiceStrategy().choose
.calledOnce
86 expect(chosenWorker
).toBe('worker')
89 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
90 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
93 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
94 WorkerChoiceStrategies
.ROUND_ROBIN
97 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
98 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
101 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
102 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
105 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
106 WorkerChoiceStrategies
.ROUND_ROBIN
109 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
110 ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy
)
112 workerChoiceStrategyContext
.getWorkerChoiceStrategy().workerChoiceStrategy
113 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
116 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
117 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
120 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
121 WorkerChoiceStrategies
.LESS_USED
124 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
125 ).toBeInstanceOf(LessUsedWorkerChoiceStrategy
)
128 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
129 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
132 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
133 WorkerChoiceStrategies
.LESS_USED
136 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
137 ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy
)
139 workerChoiceStrategyContext
.getWorkerChoiceStrategy().workerChoiceStrategy
140 ).toBeInstanceOf(LessUsedWorkerChoiceStrategy
)
143 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
144 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
147 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
148 WorkerChoiceStrategies
.FAIR_SHARE
151 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
152 ).toBeInstanceOf(FairShareWorkerChoiceStrategy
)
155 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
156 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
159 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
160 WorkerChoiceStrategies
.FAIR_SHARE
163 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
164 ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy
)
166 workerChoiceStrategyContext
.getWorkerChoiceStrategy().workerChoiceStrategy
167 ).toBeInstanceOf(FairShareWorkerChoiceStrategy
)
170 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
171 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
174 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
175 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
178 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
179 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy
)
182 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
183 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
186 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
187 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
190 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
191 ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy
)
193 workerChoiceStrategyContext
.getWorkerChoiceStrategy().workerChoiceStrategy
194 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy
)