fix: fix worker choice strategy options handling
[poolifier.git] / tests / pools / selection-strategies / worker-choice-strategy-context.test.js
CommitLineData
40ad1d27
JB
1const { expect } = require('expect')
2const sinon = require('sinon')
3const {
4 FixedThreadPool,
5 DynamicThreadPool,
6 WorkerChoiceStrategies
7} = require('../../../lib/index')
23ff945a
JB
8const {
9 WorkerChoiceStrategyContext
10} = require('../../../lib/pools/selection-strategies/worker-choice-strategy-context')
40ad1d27
JB
11const {
12 RoundRobinWorkerChoiceStrategy
13} = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy')
14const {
737c6d97
JB
15 LessUsedWorkerChoiceStrategy
16} = require('../../../lib/pools/selection-strategies/less-used-worker-choice-strategy')
168c526f
JB
17const {
18 LessBusyWorkerChoiceStrategy
19} = require('../../../lib/pools/selection-strategies/less-busy-worker-choice-strategy')
40ad1d27 20const {
23ff945a
JB
21 FairShareWorkerChoiceStrategy
22} = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy')
c15273f2
JB
23const {
24 WeightedRoundRobinWorkerChoiceStrategy
fa6f1296 25} = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy')
40ad1d27
JB
26
27describe('Worker choice strategy context test suite', () => {
c15273f2
JB
28 const min = 1
29 const max = 3
40ad1d27 30 let fixedPool, dynamicPool
c15273f2
JB
31
32 before(() => {
33 fixedPool = new FixedThreadPool(
34 max,
35 './tests/worker-files/thread/testWorker.js'
36 )
37 dynamicPool = new DynamicThreadPool(
38 min,
39 max,
40 './tests/worker-files/thread/testWorker.js'
41 )
40ad1d27
JB
42 })
43
44 afterEach(() => {
45 sinon.restore()
46 })
47
fd7ebd49
JB
48 after(async () => {
49 await fixedPool.destroy()
50 await dynamicPool.destroy()
c15273f2
JB
51 })
52
2285a040
JB
53 it('Verify that constructor() initializes the context with all the available worker choice strategies', () => {
54 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
55 fixedPool
56 )
57 expect(workerChoiceStrategyContext.workerChoiceStrategies.size).toBe(
58 Object.keys(WorkerChoiceStrategies).length
59 )
60 })
61
40ad1d27
JB
62 it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
63 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
64 fixedPool
65 )
66 const WorkerChoiceStrategyStub = sinon.createStubInstance(
67 RoundRobinWorkerChoiceStrategy,
68 {
c923ce56 69 choose: sinon.stub().returns(0)
40ad1d27
JB
70 }
71 )
d710242d 72 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
95c83464
JB
73 WorkerChoiceStrategies.ROUND_ROBIN
74 )
75 workerChoiceStrategyContext.workerChoiceStrategies.set(
d710242d 76 workerChoiceStrategyContext.workerChoiceStrategy,
95c83464
JB
77 WorkerChoiceStrategyStub
78 )
c923ce56 79 const chosenWorkerKey = workerChoiceStrategyContext.execute()
40ad1d27 80 expect(
95c83464 81 workerChoiceStrategyContext.workerChoiceStrategies.get(
d710242d 82 workerChoiceStrategyContext.workerChoiceStrategy
95c83464 83 ).choose.calledOnce
40ad1d27 84 ).toBe(true)
c923ce56 85 expect(chosenWorkerKey).toBe(0)
40ad1d27
JB
86 })
87
88 it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
89 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
90 dynamicPool
91 )
92 const WorkerChoiceStrategyStub = sinon.createStubInstance(
93 RoundRobinWorkerChoiceStrategy,
94 {
c923ce56 95 choose: sinon.stub().returns(0)
40ad1d27
JB
96 }
97 )
d710242d 98 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
95c83464
JB
99 WorkerChoiceStrategies.ROUND_ROBIN
100 )
101 workerChoiceStrategyContext.workerChoiceStrategies.set(
d710242d 102 workerChoiceStrategyContext.workerChoiceStrategy,
95c83464
JB
103 WorkerChoiceStrategyStub
104 )
c923ce56 105 const chosenWorkerKey = workerChoiceStrategyContext.execute()
40ad1d27 106 expect(
95c83464 107 workerChoiceStrategyContext.workerChoiceStrategies.get(
d710242d 108 workerChoiceStrategyContext.workerChoiceStrategy
95c83464 109 ).choose.calledOnce
40ad1d27 110 ).toBe(true)
c923ce56 111 expect(chosenWorkerKey).toBe(0)
40ad1d27
JB
112 })
113
114 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
594bfb84 115 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
40ad1d27
JB
116 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
117 fixedPool
118 )
283855e1
JB
119 expect(
120 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 121 workerChoiceStrategy
283855e1
JB
122 ).isDynamicPool
123 ).toBe(false)
95c83464
JB
124 expect(
125 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 126 workerChoiceStrategy
95c83464
JB
127 )
128 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
d710242d 129 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
594bfb84 130 workerChoiceStrategy
40ad1d27 131 )
594bfb84 132 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
95c83464
JB
133 expect(
134 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 135 workerChoiceStrategy
95c83464
JB
136 )
137 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
d710242d 138 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
594bfb84 139 workerChoiceStrategy
b2b1d84e 140 )
40ad1d27
JB
141 })
142
23ff945a 143 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
594bfb84 144 const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
40ad1d27
JB
145 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
146 dynamicPool
147 )
283855e1
JB
148 expect(
149 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 150 workerChoiceStrategy
283855e1
JB
151 ).isDynamicPool
152 ).toBe(true)
95c83464
JB
153 expect(
154 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 155 workerChoiceStrategy
95c83464
JB
156 )
157 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
d710242d 158 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
594bfb84 159 workerChoiceStrategy
40ad1d27 160 )
594bfb84 161 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
95c83464
JB
162 expect(
163 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 164 workerChoiceStrategy
95c83464
JB
165 )
166 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
d710242d 167 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
594bfb84 168 workerChoiceStrategy
b2b1d84e 169 )
40ad1d27
JB
170 })
171
737c6d97 172 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
594bfb84 173 const workerChoiceStrategy = WorkerChoiceStrategies.LESS_USED
40ad1d27
JB
174 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
175 fixedPool
176 )
283855e1
JB
177 expect(
178 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 179 workerChoiceStrategy
283855e1
JB
180 ).isDynamicPool
181 ).toBe(false)
594bfb84 182 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
95c83464
JB
183 expect(
184 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 185 workerChoiceStrategy
95c83464
JB
186 )
187 ).toBeInstanceOf(LessUsedWorkerChoiceStrategy)
d710242d 188 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
594bfb84 189 workerChoiceStrategy
b2b1d84e 190 )
40ad1d27
JB
191 })
192
737c6d97 193 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
594bfb84 194 const workerChoiceStrategy = WorkerChoiceStrategies.LESS_USED
40ad1d27
JB
195 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
196 dynamicPool
197 )
283855e1
JB
198 expect(
199 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 200 workerChoiceStrategy
283855e1
JB
201 ).isDynamicPool
202 ).toBe(true)
594bfb84 203 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
95c83464
JB
204 expect(
205 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 206 workerChoiceStrategy
95c83464
JB
207 )
208 ).toBeInstanceOf(LessUsedWorkerChoiceStrategy)
d710242d 209 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
594bfb84 210 workerChoiceStrategy
b2b1d84e 211 )
168c526f
JB
212 })
213
214 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => {
594bfb84 215 const workerChoiceStrategy = WorkerChoiceStrategies.LESS_BUSY
168c526f
JB
216 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
217 fixedPool
218 )
283855e1
JB
219 expect(
220 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 221 workerChoiceStrategy
283855e1
JB
222 ).isDynamicPool
223 ).toBe(false)
594bfb84 224 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
95c83464
JB
225 expect(
226 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 227 workerChoiceStrategy
95c83464
JB
228 )
229 ).toBeInstanceOf(LessBusyWorkerChoiceStrategy)
d710242d 230 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
594bfb84 231 workerChoiceStrategy
b2b1d84e 232 )
168c526f
JB
233 })
234
235 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => {
594bfb84 236 const workerChoiceStrategy = WorkerChoiceStrategies.LESS_BUSY
168c526f
JB
237 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
238 dynamicPool
239 )
283855e1
JB
240 expect(
241 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 242 workerChoiceStrategy
283855e1
JB
243 ).isDynamicPool
244 ).toBe(true)
594bfb84 245 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
95c83464
JB
246 expect(
247 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 248 workerChoiceStrategy
95c83464
JB
249 )
250 ).toBeInstanceOf(LessBusyWorkerChoiceStrategy)
d710242d 251 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
594bfb84 252 workerChoiceStrategy
b2b1d84e 253 )
40ad1d27 254 })
23ff945a
JB
255
256 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
594bfb84 257 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
23ff945a
JB
258 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
259 fixedPool
260 )
283855e1
JB
261 expect(
262 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 263 workerChoiceStrategy
283855e1
JB
264 ).isDynamicPool
265 ).toBe(false)
594bfb84 266 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
95c83464
JB
267 expect(
268 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 269 workerChoiceStrategy
95c83464
JB
270 )
271 ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
d710242d 272 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
594bfb84 273 workerChoiceStrategy
b2b1d84e 274 )
23ff945a
JB
275 })
276
277 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
594bfb84 278 const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
23ff945a
JB
279 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
280 dynamicPool
281 )
283855e1
JB
282 expect(
283 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 284 workerChoiceStrategy
283855e1
JB
285 ).isDynamicPool
286 ).toBe(true)
594bfb84 287 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
95c83464
JB
288 expect(
289 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 290 workerChoiceStrategy
95c83464
JB
291 )
292 ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
d710242d 293 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
594bfb84 294 workerChoiceStrategy
b2b1d84e 295 )
23ff945a
JB
296 })
297
c15273f2 298 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
594bfb84 299 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
c15273f2
JB
300 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
301 fixedPool
302 )
283855e1
JB
303 expect(
304 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 305 workerChoiceStrategy
283855e1
JB
306 ).isDynamicPool
307 ).toBe(false)
594bfb84 308 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
95c83464
JB
309 expect(
310 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 311 workerChoiceStrategy
95c83464
JB
312 )
313 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
d710242d 314 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
594bfb84 315 workerChoiceStrategy
b2b1d84e 316 )
c15273f2 317 })
23ff945a 318
c15273f2 319 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
594bfb84 320 const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
c15273f2
JB
321 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
322 dynamicPool
323 )
283855e1
JB
324 expect(
325 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 326 workerChoiceStrategy
283855e1
JB
327 ).isDynamicPool
328 ).toBe(true)
594bfb84 329 workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy)
95c83464
JB
330 expect(
331 workerChoiceStrategyContext.workerChoiceStrategies.get(
594bfb84 332 workerChoiceStrategy
95c83464
JB
333 )
334 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
d710242d 335 expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe(
594bfb84 336 workerChoiceStrategy
b2b1d84e 337 )
c15273f2 338 })
2fc5cae3
JB
339
340 it.only('Verify that worker choice strategy options enable median run time pool statistics', () => {
341 const wwrWorkerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
342 let workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
343 fixedPool,
344 wwrWorkerChoiceStrategy,
345 {
346 medRunTime: true
347 }
348 )
349 expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe(
350 false
351 )
352 expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe(
353 true
354 )
355 workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
356 dynamicPool,
357 wwrWorkerChoiceStrategy,
358 {
359 medRunTime: true
360 }
361 )
362 expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe(
363 false
364 )
365 expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe(
366 true
367 )
368 const fsWorkerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE
369 workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
370 fixedPool,
371 fsWorkerChoiceStrategy,
372 {
373 medRunTime: true
374 }
375 )
376 expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe(
377 false
378 )
379 expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe(
380 true
381 )
382 workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
383 dynamicPool,
384 fsWorkerChoiceStrategy,
385 {
386 medRunTime: true
387 }
388 )
389 expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe(
390 false
391 )
392 expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe(
393 true
394 )
395 })
40ad1d27 396})