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', () => {
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(
118 new TypeError('Worker node key chosen is null or undefined')
120 workerChoiceStrategyContext
.workerChoiceStrategies
.set(
121 workerChoiceStrategyContext
.workerChoiceStrategy
,
122 WorkerChoiceStrategyNullStub
124 expect(() => workerChoiceStrategyContext
.execute()).toThrowError(
125 new TypeError('Worker node key chosen is null or undefined')
129 it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
130 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
133 const WorkerChoiceStrategyStub
= sinon
.createStubInstance(
134 RoundRobinWorkerChoiceStrategy
,
136 choose
: sinon
.stub().returns(0)
139 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
140 WorkerChoiceStrategies
.ROUND_ROBIN
142 workerChoiceStrategyContext
.workerChoiceStrategies
.set(
143 workerChoiceStrategyContext
.workerChoiceStrategy
,
144 WorkerChoiceStrategyStub
146 const chosenWorkerKey
= workerChoiceStrategyContext
.execute()
148 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
149 workerChoiceStrategyContext
.workerChoiceStrategy
152 expect(chosenWorkerKey
).toBe(0)
155 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
156 const workerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
157 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
161 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
164 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
165 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
168 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
170 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
173 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
174 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
179 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
180 const workerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
181 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
185 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
188 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
189 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
192 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
194 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
197 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
198 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
203 it('Verify that setWorkerChoiceStrategy() works with LEAST_USED and fixed pool', () => {
204 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_USED
205 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
208 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
210 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
213 ).toBeInstanceOf(LeastUsedWorkerChoiceStrategy
)
214 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
219 it('Verify that setWorkerChoiceStrategy() works with LEAST_USED and dynamic pool', () => {
220 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_USED
221 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
224 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
226 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
229 ).toBeInstanceOf(LeastUsedWorkerChoiceStrategy
)
230 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
235 it('Verify that setWorkerChoiceStrategy() works with LEAST_BUSY and fixed pool', () => {
236 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_BUSY
237 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
240 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
242 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
245 ).toBeInstanceOf(LeastBusyWorkerChoiceStrategy
)
246 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
251 it('Verify that setWorkerChoiceStrategy() works with LEAST_BUSY and dynamic pool', () => {
252 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_BUSY
253 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
256 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
258 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
261 ).toBeInstanceOf(LeastBusyWorkerChoiceStrategy
)
262 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
267 it('Verify that setWorkerChoiceStrategy() works with LEAST_ELU and fixed pool', () => {
268 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_ELU
269 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
272 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
274 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
277 ).toBeInstanceOf(LeastEluWorkerChoiceStrategy
)
278 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
283 it('Verify that setWorkerChoiceStrategy() works with LEAST_ELU and dynamic pool', () => {
284 const workerChoiceStrategy
= WorkerChoiceStrategies
.LEAST_ELU
285 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
288 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
290 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
293 ).toBeInstanceOf(LeastEluWorkerChoiceStrategy
)
294 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
299 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
300 const workerChoiceStrategy
= WorkerChoiceStrategies
.FAIR_SHARE
301 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
304 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
306 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
309 ).toBeInstanceOf(FairShareWorkerChoiceStrategy
)
310 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
315 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
316 const workerChoiceStrategy
= WorkerChoiceStrategies
.FAIR_SHARE
317 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
320 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
322 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
325 ).toBeInstanceOf(FairShareWorkerChoiceStrategy
)
326 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
331 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
332 const workerChoiceStrategy
= WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
333 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
336 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
338 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
341 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy
)
342 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
347 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
348 const workerChoiceStrategy
= WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
349 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
352 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
354 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
357 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy
)
358 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
363 it('Verify that setWorkerChoiceStrategy() works with INTERLEAVED_WEIGHTED_ROUND_ROBIN and fixed pool', () => {
364 const workerChoiceStrategy
=
365 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
366 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
369 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
371 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
374 ).toBeInstanceOf(InterleavedWeightedRoundRobinWorkerChoiceStrategy
)
375 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
380 it('Verify that setWorkerChoiceStrategy() works with INTERLEAVED_WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
381 const workerChoiceStrategy
=
382 WorkerChoiceStrategies
.INTERLEAVED_WEIGHTED_ROUND_ROBIN
383 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
386 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
388 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
391 ).toBeInstanceOf(InterleavedWeightedRoundRobinWorkerChoiceStrategy
)
392 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
397 it('Verify that worker choice strategy options enable median runtime pool statistics', () => {
398 const wwrWorkerChoiceStrategy
= WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
399 let workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
401 wwrWorkerChoiceStrategy
,
403 runTime
: { median
: true }
407 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
411 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
.median
413 workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
415 wwrWorkerChoiceStrategy
,
417 runTime
: { median
: true }
421 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
425 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
.median
427 const fsWorkerChoiceStrategy
= WorkerChoiceStrategies
.FAIR_SHARE
428 workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
430 fsWorkerChoiceStrategy
,
432 runTime
: { median
: true }
436 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
440 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
.median
442 workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
444 fsWorkerChoiceStrategy
,
446 runTime
: { median
: true }
450 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
454 workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
.median