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 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 constructor() initializes the context with all the available worker choice strategies', () => {
54 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
57 expect(workerChoiceStrategyContext
.workerChoiceStrategies
.size
).toBe(
58 Object
.keys(WorkerChoiceStrategies
).length
62 it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
63 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
66 const WorkerChoiceStrategyStub
= sinon
.createStubInstance(
67 RoundRobinWorkerChoiceStrategy
,
69 choose
: sinon
.stub().returns(0)
72 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
73 WorkerChoiceStrategies
.ROUND_ROBIN
75 workerChoiceStrategyContext
.workerChoiceStrategies
.set(
76 workerChoiceStrategyContext
.workerChoiceStrategy
,
77 WorkerChoiceStrategyStub
79 const chosenWorkerKey
= workerChoiceStrategyContext
.execute()
81 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
82 workerChoiceStrategyContext
.workerChoiceStrategy
85 expect(chosenWorkerKey
).toBe(0)
88 it('Verify that execute() throws error if null or undefined is returned', () => {
89 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
92 const WorkerChoiceStrategyUndefinedStub
= sinon
.createStubInstance(
93 RoundRobinWorkerChoiceStrategy
,
95 choose
: sinon
.stub().returns(undefined)
98 const WorkerChoiceStrategyNullStub
= sinon
.createStubInstance(
99 RoundRobinWorkerChoiceStrategy
,
101 choose
: sinon
.stub().returns(null)
104 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
105 WorkerChoiceStrategies
.ROUND_ROBIN
107 workerChoiceStrategyContext
.workerChoiceStrategies
.set(
108 workerChoiceStrategyContext
.workerChoiceStrategy
,
109 WorkerChoiceStrategyUndefinedStub
111 expect(() => workerChoiceStrategyContext
.execute()).toThrowError(
112 new Error('Worker node key chosen is null or undefined')
114 workerChoiceStrategyContext
.workerChoiceStrategies
.set(
115 workerChoiceStrategyContext
.workerChoiceStrategy
,
116 WorkerChoiceStrategyNullStub
118 expect(() => workerChoiceStrategyContext
.execute()).toThrowError(
119 new Error('Worker node key chosen is null or undefined')
123 it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
124 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
127 const WorkerChoiceStrategyStub
= sinon
.createStubInstance(
128 RoundRobinWorkerChoiceStrategy
,
130 choose
: sinon
.stub().returns(0)
133 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
134 WorkerChoiceStrategies
.ROUND_ROBIN
136 workerChoiceStrategyContext
.workerChoiceStrategies
.set(
137 workerChoiceStrategyContext
.workerChoiceStrategy
,
138 WorkerChoiceStrategyStub
140 const chosenWorkerKey
= workerChoiceStrategyContext
.execute()
142 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
143 workerChoiceStrategyContext
.workerChoiceStrategy
146 expect(chosenWorkerKey
).toBe(0)
149 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
150 const workerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
151 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
155 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
158 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
159 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
162 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
164 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
167 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
168 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
173 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
174 const workerChoiceStrategy
= WorkerChoiceStrategies
.ROUND_ROBIN
175 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
179 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
182 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
183 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
186 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
188 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
191 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
192 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
197 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
198 const workerChoiceStrategy
= WorkerChoiceStrategies
.LESS_USED
199 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
202 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
204 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
207 ).toBeInstanceOf(LessUsedWorkerChoiceStrategy
)
208 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
213 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
214 const workerChoiceStrategy
= WorkerChoiceStrategies
.LESS_USED
215 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
218 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
220 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
223 ).toBeInstanceOf(LessUsedWorkerChoiceStrategy
)
224 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
229 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => {
230 const workerChoiceStrategy
= WorkerChoiceStrategies
.LESS_BUSY
231 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
234 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
236 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
239 ).toBeInstanceOf(LessBusyWorkerChoiceStrategy
)
240 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
245 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => {
246 const workerChoiceStrategy
= WorkerChoiceStrategies
.LESS_BUSY
247 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
250 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
252 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
255 ).toBeInstanceOf(LessBusyWorkerChoiceStrategy
)
256 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
261 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
262 const workerChoiceStrategy
= WorkerChoiceStrategies
.FAIR_SHARE
263 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
266 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
268 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
271 ).toBeInstanceOf(FairShareWorkerChoiceStrategy
)
272 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
277 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
278 const workerChoiceStrategy
= WorkerChoiceStrategies
.FAIR_SHARE
279 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
282 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
284 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
287 ).toBeInstanceOf(FairShareWorkerChoiceStrategy
)
288 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
293 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
294 const workerChoiceStrategy
= WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
295 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
298 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
300 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
303 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy
)
304 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
309 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
310 const workerChoiceStrategy
= WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
311 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
314 workerChoiceStrategyContext
.setWorkerChoiceStrategy(workerChoiceStrategy
)
316 workerChoiceStrategyContext
.workerChoiceStrategies
.get(
319 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy
)
320 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBe(
325 it('Verify that worker choice strategy options enable median runtime pool statistics', () => {
326 const wwrWorkerChoiceStrategy
= WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
327 let workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
329 wwrWorkerChoiceStrategy
,
334 expect(workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
).toBe(
337 expect(workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
).toBe(
340 workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
342 wwrWorkerChoiceStrategy
,
347 expect(workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
).toBe(
350 expect(workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
).toBe(
353 const fsWorkerChoiceStrategy
= WorkerChoiceStrategies
.FAIR_SHARE
354 workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
356 fsWorkerChoiceStrategy
,
361 expect(workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
).toBe(
364 expect(workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
).toBe(
367 workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
369 fsWorkerChoiceStrategy
,
374 expect(workerChoiceStrategyContext
.getRequiredStatistics().avgRunTime
).toBe(
377 expect(workerChoiceStrategyContext
.getRequiredStatistics().medRunTime
).toBe(