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 LessBusyWorkerChoiceStrategy
19 } = require('../../../lib/pools/selection-strategies/less-busy-worker-choice-strategy')
21 FairShareWorkerChoiceStrategy
22 } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy')
24 WeightedRoundRobinWorkerChoiceStrategy
25 } = require('../../../lib/pools/selection-strategies/weighted-round-robin-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(0)
63 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
64 WorkerChoiceStrategies
.ROUND_ROBIN
66 workerChoiceStrategyContext
.workerChoiceStrategies
.set(
67 workerChoiceStrategyContext
.workerChoiceStrategyType
,
68 WorkerChoiceStrategyStub
70 const chosenWorkerKey
= workerChoiceStrategyContext
.execute()
72 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
73 workerChoiceStrategyContext
.workerChoiceStrategyType
76 expect(chosenWorkerKey
).toBe(0)
79 it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
80 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
83 const WorkerChoiceStrategyStub
= sinon
.createStubInstance(
84 RoundRobinWorkerChoiceStrategy
,
86 choose
: sinon
.stub().returns(0)
89 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
90 WorkerChoiceStrategies
.ROUND_ROBIN
92 workerChoiceStrategyContext
.workerChoiceStrategies
.set(
93 workerChoiceStrategyContext
.workerChoiceStrategyType
,
94 WorkerChoiceStrategyStub
96 const chosenWorkerKey
= workerChoiceStrategyContext
.execute()
98 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
99 workerChoiceStrategyContext
.workerChoiceStrategyType
102 expect(chosenWorkerKey
).toBe(0)
105 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
106 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
110 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
111 WorkerChoiceStrategies
.ROUND_ROBIN
113 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
114 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
115 WorkerChoiceStrategies
.ROUND_ROBIN
117 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
119 WorkerChoiceStrategies
.ROUND_ROBIN
122 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
123 WorkerChoiceStrategies
.ROUND_ROBIN
125 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
126 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
127 WorkerChoiceStrategies
.ROUND_ROBIN
131 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
132 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
136 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
137 WorkerChoiceStrategies
.ROUND_ROBIN
139 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
140 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
141 WorkerChoiceStrategies
.ROUND_ROBIN
143 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
145 WorkerChoiceStrategies
.ROUND_ROBIN
148 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
149 WorkerChoiceStrategies
.ROUND_ROBIN
151 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
152 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
153 WorkerChoiceStrategies
.ROUND_ROBIN
157 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
158 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
161 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
163 WorkerChoiceStrategies
.LESS_USED
166 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
167 WorkerChoiceStrategies
.LESS_USED
169 ).toBeInstanceOf(LessUsedWorkerChoiceStrategy
)
170 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
171 WorkerChoiceStrategies
.LESS_USED
175 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
176 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
179 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
181 WorkerChoiceStrategies
.LESS_USED
184 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
185 WorkerChoiceStrategies
.LESS_USED
187 ).toBeInstanceOf(LessUsedWorkerChoiceStrategy
)
188 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
189 WorkerChoiceStrategies
.LESS_USED
193 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => {
194 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
197 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
199 WorkerChoiceStrategies
.LESS_BUSY
202 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
203 WorkerChoiceStrategies
.LESS_BUSY
205 ).toBeInstanceOf(LessBusyWorkerChoiceStrategy
)
206 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
207 WorkerChoiceStrategies
.LESS_BUSY
211 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => {
212 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
215 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
217 WorkerChoiceStrategies
.LESS_BUSY
220 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
221 WorkerChoiceStrategies
.LESS_BUSY
223 ).toBeInstanceOf(LessBusyWorkerChoiceStrategy
)
224 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
225 WorkerChoiceStrategies
.LESS_BUSY
229 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
230 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
233 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
235 WorkerChoiceStrategies
.FAIR_SHARE
238 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
239 WorkerChoiceStrategies
.FAIR_SHARE
241 ).toBeInstanceOf(FairShareWorkerChoiceStrategy
)
242 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
243 WorkerChoiceStrategies
.FAIR_SHARE
247 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
248 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
251 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
253 WorkerChoiceStrategies
.FAIR_SHARE
256 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
257 WorkerChoiceStrategies
.FAIR_SHARE
259 ).toBeInstanceOf(FairShareWorkerChoiceStrategy
)
260 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
261 WorkerChoiceStrategies
.FAIR_SHARE
265 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
266 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
269 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
271 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
274 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
275 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
277 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy
)
278 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
279 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
283 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
284 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
287 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
289 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
292 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
293 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
295 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy
)
296 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
297 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
301 it('Verify that getWorkerChoiceStrategy() default return ROUND_ROBIN strategy', () => {
302 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
306 workerChoiceStrategyContext
.getWorkerChoiceStrategy(fixedPool
)
307 expect(strategy
).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
310 it('Verify that getWorkerChoiceStrategy() can return ROUND_ROBIN strategy', () => {
311 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
314 const strategy
= workerChoiceStrategyContext
.getWorkerChoiceStrategy(
316 WorkerChoiceStrategies
.ROUND_ROBIN
318 expect(strategy
).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
321 it('Verify that getWorkerChoiceStrategy() can return LESS_USED strategy', () => {
322 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
325 const strategy
= workerChoiceStrategyContext
.getWorkerChoiceStrategy(
327 WorkerChoiceStrategies
.LESS_USED
329 expect(strategy
).toBeInstanceOf(LessUsedWorkerChoiceStrategy
)
332 it('Verify that getWorkerChoiceStrategy() can return LESS_BUSY strategy', () => {
333 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
336 const strategy
= workerChoiceStrategyContext
.getWorkerChoiceStrategy(
338 WorkerChoiceStrategies
.LESS_BUSY
340 expect(strategy
).toBeInstanceOf(LessBusyWorkerChoiceStrategy
)
343 it('Verify that getWorkerChoiceStrategy() can return FAIR_SHARE strategy', () => {
344 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
347 const strategy
= workerChoiceStrategyContext
.getWorkerChoiceStrategy(
349 WorkerChoiceStrategies
.FAIR_SHARE
351 expect(strategy
).toBeInstanceOf(FairShareWorkerChoiceStrategy
)
354 it('Verify that getWorkerChoiceStrategy() can return WEIGHTED_ROUND_ROBIN strategy', () => {
355 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
358 const strategy
= workerChoiceStrategyContext
.getWorkerChoiceStrategy(
360 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
362 expect(strategy
).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy
)
365 it('Verify that getWorkerChoiceStrategy() throw error on unknown strategy', () => {
366 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
370 workerChoiceStrategyContext
.getWorkerChoiceStrategy(
375 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")