1 const { expect
} = require('expect')
2 const sinon
= require('sinon')
7 } = require('../../../lib')
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 LeastUsedWorkerChoiceStrategy
16 } = require('../../../lib/pools/selection-strategies/least-used-worker-choice-strategy')
18 LeastBusyWorkerChoiceStrategy
19 } = require('../../../lib/pools/selection-strategies/least-busy-worker-choice-strategy')
21 LeastEluWorkerChoiceStrategy
22 } = require('../../../lib/pools/selection-strategies/least-elu-worker-choice-strategy')
24 FairShareWorkerChoiceStrategy
25 } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy')
27 WeightedRoundRobinWorkerChoiceStrategy
28 } = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy')
30 InterleavedWeightedRoundRobinWorkerChoiceStrategy
31 } = require('../../../lib/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy')
33 describe('Worker choice strategy context test suite', () => {
36 let fixedPool
, dynamicPool
39 fixedPool
= new FixedThreadPool(
41 './tests/worker-files/thread/testWorker.js'
43 dynamicPool
= new DynamicThreadPool(
46 './tests/worker-files/thread/testWorker.js'
55 await fixedPool
.destroy()
56 await dynamicPool
.destroy()
59 it('Verify that constructor() initializes the context with all the available worker choice strategies', () => {
60 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
63 expect(workerChoiceStrategyContext
.workerChoiceStrategies
.size
).toBe(
64 Object
.keys(WorkerChoiceStrategies
).length
68 it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
69 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
72 const WorkerChoiceStrategyStub
= sinon
.createStubInstance(
73 RoundRobinWorkerChoiceStrategy
,
75 choose
: sinon
.stub().returns(0)
78 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
79 WorkerChoiceStrategies
.ROUND_ROBIN
81 workerChoiceStrategyContext
.workerChoiceStrategies
.set(
82 workerChoiceStrategyContext
.workerChoiceStrategy
,
83 WorkerChoiceStrategyStub
85 const chosenWorkerKey
= workerChoiceStrategyContext
.execute()
87 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
88 workerChoiceStrategyContext
.workerChoiceStrategy
91 expect(chosenWorkerKey
).toBe(0)
94 it('Verify that execute() throws error if null or undefined is returned after retries', () => {
95 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
98 const WorkerChoiceStrategyUndefinedStub
= sinon
.createStubInstance(
99 RoundRobinWorkerChoiceStrategy
,
101 choose
: sinon
.stub().returns(undefined)
104 const WorkerChoiceStrategyNullStub
= sinon
.createStubInstance(
105 RoundRobinWorkerChoiceStrategy
,
107 choose
: sinon
.stub().returns(null)
110 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
111 WorkerChoiceStrategies
.ROUND_ROBIN
113 workerChoiceStrategyContext
.workerChoiceStrategies
.set(
114 workerChoiceStrategyContext
.workerChoiceStrategy
,
115 WorkerChoiceStrategyUndefinedStub
117 expect(() => workerChoiceStrategyContext
.execute()).toThrowError(
119 'Worker node key chosen is null or undefined after 6 retries'
122 workerChoiceStrategyContext
.workerChoiceStrategies
.set(
123 workerChoiceStrategyContext
.workerChoiceStrategy
,
124 WorkerChoiceStrategyNullStub
126 expect(() => workerChoiceStrategyContext
.execute()).toThrowError(
128 'Worker node key chosen is null or undefined after 6 retries'
133 it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
134 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
137 const WorkerChoiceStrategyStub
= sinon
.createStubInstance(
138 RoundRobinWorkerChoiceStrategy
,
140 choose
: sinon
.stub().returns(0)
143 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
144 WorkerChoiceStrategies
.ROUND_ROBIN
146 workerChoiceStrategyContext
.workerChoiceStrategies
.set(
147 workerChoiceStrategyContext
.workerChoiceStrategy
,
148 WorkerChoiceStrategyStub
150 const chosenWorkerKey
= workerChoiceStrategyContext
.execute()
152 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
153 workerChoiceStrategyContext
.workerChoiceStrategy
156 expect(chosenWorkerKey
).toBe(0)
159 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
160 const workerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
161 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
165 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
168 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
169 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
172 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
174 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
177 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
178 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
183 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
184 const workerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
185 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
189 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
192 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
193 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
196 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
198 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
201 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
202 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
207 it('Verify that setWorkerChoiceStrategy() works with LEAST_USED and fixed pool', () => {
208 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_USED
209 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
212 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
214 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
217 ).toBeInstanceOf(LeastUsedWorkerChoiceStrategy
)
218 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
223 it('Verify that setWorkerChoiceStrategy() works with LEAST_USED and dynamic pool', () => {
224 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_USED
225 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
228 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
230 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
233 ).toBeInstanceOf(LeastUsedWorkerChoiceStrategy
)
234 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
239 it('Verify that setWorkerChoiceStrategy() works with LEAST_BUSY and fixed pool', () => {
240 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_BUSY
241 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
244 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
246 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
249 ).toBeInstanceOf(LeastBusyWorkerChoiceStrategy
)
250 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
255 it('Verify that setWorkerChoiceStrategy() works with LEAST_BUSY and dynamic pool', () => {
256 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_BUSY
257 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
260 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
262 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
265 ).toBeInstanceOf(LeastBusyWorkerChoiceStrategy
)
266 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
271 it('Verify that setWorkerChoiceStrategy() works with LEAST_ELU and fixed pool', () => {
272 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_ELU
273 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
276 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
278 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
281 ).toBeInstanceOf(LeastEluWorkerChoiceStrategy
)
282 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
287 it('Verify that setWorkerChoiceStrategy() works with LEAST_ELU and dynamic pool', () => {
288 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_ELU
289 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
292 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
294 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
297 ).toBeInstanceOf(LeastEluWorkerChoiceStrategy
)
298 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
303 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
304 const workerChoiceStrategy
= WorkerChoiceStrategies
.FAIR_SHARE
305 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
308 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
310 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
313 ).toBeInstanceOf(FairShareWorkerChoiceStrategy
)
314 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
319 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
320 const workerChoiceStrategy
= WorkerChoiceStrategies
.FAIR_SHARE
321 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
324 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
326 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
329 ).toBeInstanceOf(FairShareWorkerChoiceStrategy
)
330 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
335 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
336 const workerChoiceStrategy
= WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
337 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
340 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
342 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
345 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy
)
346 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
351 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
352 const workerChoiceStrategy
= WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
353 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
356 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
358 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
361 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy
)
362 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
367 it('Verify that setWorkerChoiceStrategy() works with INTERLEAVED_WEIGHTED_ROUND_ROBIN and fixed pool', () => {
368 const workerChoiceStrategy
=
369 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
370 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
373 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
375 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
378 ).toBeInstanceOf(InterleavedWeightedRoundRobinWorkerChoiceStrategy
)
379 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
384 it('Verify that setWorkerChoiceStrategy() works with INTERLEAVED_WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
385 const workerChoiceStrategy
=
386 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
387 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
390 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
392 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
395 ).toBeInstanceOf(InterleavedWeightedRoundRobinWorkerChoiceStrategy
)
396 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
401 it('Verify that worker choice strategy options enable median runtime pool statistics', () => {
402 const wwrWorkerChoiceStrategy
= WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
403 let workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
405 wwrWorkerChoiceStrategy
,
407 runTime
: { median
: true }
411 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
415 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
.median
417 workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
419 wwrWorkerChoiceStrategy
,
421 runTime
: { median
: true }
425 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
429 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
.median
431 const fsWorkerChoiceStrategy
= WorkerChoiceStrategies
.FAIR_SHARE
432 workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
434 fsWorkerChoiceStrategy
,
436 runTime
: { median
: true }
440 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
444 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
.median
446 workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
448 fsWorkerChoiceStrategy
,
450 runTime
: { median
: true }
454 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
458 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
.median