Tests: also test strategy statistics reset on dynamic pool
[poolifier.git] / tests / pools / selection-strategies / selection-strategies.test.js
CommitLineData
a61a0724 1const { expect } = require('expect')
a35560ba
S
2const {
3 WorkerChoiceStrategies,
4 DynamicThreadPool,
5 FixedThreadPool
15d56315 6} = require('../../../lib/index')
a35560ba
S
7
8describe('Selection strategies test suite', () => {
e1ffb94f
JB
9 const min = 0
10 const max = 3
11
a35560ba
S
12 it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
13 expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN')
14 expect(WorkerChoiceStrategies.LESS_RECENTLY_USED).toBe('LESS_RECENTLY_USED')
23ff945a 15 expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE')
b3432a63
JB
16 expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe(
17 'WEIGHTED_ROUND_ROBIN'
18 )
a35560ba
S
19 })
20
e843b904 21 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
e843b904
JB
22 const pool = new DynamicThreadPool(
23 min,
24 max,
25 './tests/worker-files/thread/testWorker.js'
26 )
27 expect(pool.opts.workerChoiceStrategy).toBe(
28 WorkerChoiceStrategies.ROUND_ROBIN
29 )
30 // We need to clean up the resources after our test
31 await pool.destroy()
32 })
33
34 it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
e843b904
JB
35 const pool = new DynamicThreadPool(
36 min,
37 max,
38 './tests/worker-files/thread/testWorker.js'
39 )
40 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
41 expect(pool.opts.workerChoiceStrategy).toBe(
42 WorkerChoiceStrategies.ROUND_ROBIN
43 )
44 // We need to clean up the resources after our test
45 await pool.destroy()
46 })
47
10fcfaf4 48 it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
10fcfaf4
JB
49 let pool = new FixedThreadPool(
50 max,
51 './tests/worker-files/thread/testWorker.js'
52 )
53 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
54 expect(
55 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
56 .requiredStatistics.runTime
57 ).toBe(false)
fd7ebd49 58 await pool.destroy()
10fcfaf4
JB
59 pool = new DynamicThreadPool(
60 min,
61 max,
62 './tests/worker-files/thread/testWorker.js'
63 )
64 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
65 expect(
66 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
67 .requiredStatistics.runTime
68 ).toBe(false)
69 // We need to clean up the resources after our test
70 await pool.destroy()
71 })
72
bdaf31cd 73 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
bdaf31cd
JB
74 const pool = new FixedThreadPool(
75 max,
76 './tests/worker-files/thread/testWorker.js',
77 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
78 )
79 expect(pool.opts.workerChoiceStrategy).toBe(
80 WorkerChoiceStrategies.ROUND_ROBIN
81 )
82 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
83 const promises = []
84 for (let i = 0; i < max * 2; i++) {
6db75ad9 85 promises.push(pool.execute())
bdaf31cd
JB
86 }
87 await Promise.all(promises)
88 // We need to clean up the resources after our test
89 await pool.destroy()
90 })
91
92 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
bdaf31cd
JB
93 const pool = new DynamicThreadPool(
94 min,
95 max,
96 './tests/worker-files/thread/testWorker.js',
97 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
98 )
99 expect(pool.opts.workerChoiceStrategy).toBe(
100 WorkerChoiceStrategies.ROUND_ROBIN
101 )
102 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
103 const promises = []
104 for (let i = 0; i < max * 2; i++) {
6db75ad9 105 promises.push(pool.execute())
bdaf31cd
JB
106 }
107 await Promise.all(promises)
108 // We need to clean up the resources after our test
109 await pool.destroy()
110 })
111
b98ec2e6 112 it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
a35560ba
S
113 const pool = new FixedThreadPool(
114 max,
115 './tests/worker-files/thread/testWorker.js',
116 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
117 )
b98ec2e6
JB
118 expect(pool.opts.workerChoiceStrategy).toBe(
119 WorkerChoiceStrategies.LESS_RECENTLY_USED
120 )
121 // We need to clean up the resources after our test
122 await pool.destroy()
123 })
a35560ba 124
b98ec2e6 125 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
b98ec2e6
JB
126 const pool = new FixedThreadPool(
127 max,
128 './tests/worker-files/thread/testWorker.js'
129 )
130 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
a35560ba
S
131 expect(pool.opts.workerChoiceStrategy).toBe(
132 WorkerChoiceStrategies.LESS_RECENTLY_USED
133 )
b98ec2e6
JB
134 // We need to clean up the resources after our test
135 await pool.destroy()
136 })
a35560ba 137
10fcfaf4 138 it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => {
10fcfaf4
JB
139 let pool = new FixedThreadPool(
140 max,
141 './tests/worker-files/thread/testWorker.js'
142 )
143 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
144 expect(
145 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
146 .requiredStatistics.runTime
147 ).toBe(false)
fd7ebd49 148 await pool.destroy()
10fcfaf4
JB
149 pool = new DynamicThreadPool(
150 min,
151 max,
152 './tests/worker-files/thread/testWorker.js'
153 )
154 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
155 expect(
156 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
157 .requiredStatistics.runTime
158 ).toBe(false)
159 // We need to clean up the resources after our test
160 await pool.destroy()
161 })
162
ff5e76e1 163 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
b98ec2e6
JB
164 const pool = new FixedThreadPool(
165 max,
166 './tests/worker-files/thread/testWorker.js',
167 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
168 )
a35560ba
S
169 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
170 const promises = []
171 for (let i = 0; i < max * 2; i++) {
6db75ad9 172 promises.push(pool.execute())
a35560ba
S
173 }
174 await Promise.all(promises)
a35560ba
S
175 // We need to clean up the resources after our test
176 await pool.destroy()
177 })
178
ff5e76e1 179 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
ff5e76e1
JB
180 const pool = new DynamicThreadPool(
181 min,
182 max,
183 './tests/worker-files/thread/testWorker.js',
184 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
185 )
186 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
187 const promises = []
188 for (let i = 0; i < max * 2; i++) {
6db75ad9 189 promises.push(pool.execute())
ff5e76e1
JB
190 }
191 await Promise.all(promises)
ff5e76e1
JB
192 // We need to clean up the resources after our test
193 await pool.destroy()
194 })
195
23ff945a 196 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
23ff945a
JB
197 const pool = new FixedThreadPool(
198 max,
199 './tests/worker-files/thread/testWorker.js',
200 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
201 )
202 expect(pool.opts.workerChoiceStrategy).toBe(
203 WorkerChoiceStrategies.FAIR_SHARE
204 )
205 // We need to clean up the resources after our test
206 await pool.destroy()
207 })
208
209 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
23ff945a
JB
210 const pool = new FixedThreadPool(
211 max,
212 './tests/worker-files/thread/testWorker.js'
213 )
214 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
215 expect(pool.opts.workerChoiceStrategy).toBe(
216 WorkerChoiceStrategies.FAIR_SHARE
217 )
218 // We need to clean up the resources after our test
219 await pool.destroy()
220 })
221
10fcfaf4 222 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
10fcfaf4
JB
223 let pool = new FixedThreadPool(
224 max,
225 './tests/worker-files/thread/testWorker.js'
226 )
227 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
228 expect(
229 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
230 .requiredStatistics.runTime
231 ).toBe(true)
fd7ebd49 232 await pool.destroy()
10fcfaf4
JB
233 pool = new DynamicThreadPool(
234 min,
235 max,
236 './tests/worker-files/thread/testWorker.js'
237 )
238 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
239 expect(
240 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
241 .requiredStatistics.runTime
242 ).toBe(true)
243 // We need to clean up the resources after our test
244 await pool.destroy()
245 })
246
23ff945a 247 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
23ff945a
JB
248 const pool = new FixedThreadPool(
249 max,
250 './tests/worker-files/thread/testWorker.js',
251 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
252 )
253 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
254 const promises = []
255 for (let i = 0; i < max * 2; i++) {
6db75ad9 256 promises.push(pool.execute())
23ff945a
JB
257 }
258 await Promise.all(promises)
259 // We need to clean up the resources after our test
260 await pool.destroy()
261 })
262
263 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
23ff945a
JB
264 const pool = new DynamicThreadPool(
265 min,
266 max,
267 './tests/worker-files/thread/testWorker.js',
268 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
269 )
270 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
271 const promises = []
272 for (let i = 0; i < max * 2; i++) {
6db75ad9 273 promises.push(pool.execute())
23ff945a
JB
274 }
275 await Promise.all(promises)
276 // We need to clean up the resources after our test
277 await pool.destroy()
278 })
279
caeb9817 280 it('Verify FAIR_SHARE strategy statistics are resets after setting it', async () => {
f0829c53 281 let pool = new FixedThreadPool(
caeb9817
JB
282 max,
283 './tests/worker-files/thread/testWorker.js'
284 )
285 expect(
286 pool.workerChoiceStrategyContext.workerChoiceStrategy
287 .workerLastVirtualTaskTimestamp
288 ).toBeUndefined()
289 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
290 for (const worker of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) {
291 expect(
292 pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
293 worker
294 ).start
295 ).toBe(0)
296 expect(
297 pool.workerChoiceStrategyContext.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
298 worker
299 ).end
300 ).toBe(0)
301 }
f0829c53
JB
302 await pool.destroy()
303 pool = new DynamicThreadPool(
304 min,
305 max,
306 './tests/worker-files/thread/testWorker.js'
307 )
308 expect(
309 pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
310 .workerLastVirtualTaskTimestamp
311 ).toBeUndefined()
312 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
313 for (const worker of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.keys()) {
314 expect(
315 pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
316 worker
317 ).start
318 ).toBe(0)
319 expect(
320 pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workerLastVirtualTaskTimestamp.get(
321 worker
322 ).end
323 ).toBe(0)
324 }
caeb9817
JB
325 // We need to clean up the resources after our test
326 await pool.destroy()
327 })
328
b3432a63 329 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
b3432a63
JB
330 const pool = new FixedThreadPool(
331 max,
332 './tests/worker-files/thread/testWorker.js',
333 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
334 )
335 expect(pool.opts.workerChoiceStrategy).toBe(
336 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
337 )
338 // We need to clean up the resources after our test
339 await pool.destroy()
340 })
341
342 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
b3432a63
JB
343 const pool = new FixedThreadPool(
344 max,
345 './tests/worker-files/thread/testWorker.js'
346 )
347 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
348 expect(pool.opts.workerChoiceStrategy).toBe(
349 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
350 )
351 // We need to clean up the resources after our test
352 await pool.destroy()
353 })
354
10fcfaf4 355 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
10fcfaf4
JB
356 let pool = new FixedThreadPool(
357 max,
358 './tests/worker-files/thread/testWorker.js'
359 )
360 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
361 expect(
362 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
363 .requiredStatistics.runTime
364 ).toBe(true)
fd7ebd49 365 await pool.destroy()
10fcfaf4
JB
366 pool = new DynamicThreadPool(
367 min,
368 max,
369 './tests/worker-files/thread/testWorker.js'
370 )
371 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
372 expect(
373 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
374 .requiredStatistics.runTime
375 ).toBe(true)
376 // We need to clean up the resources after our test
377 await pool.destroy()
378 })
379
b3432a63 380 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
b3432a63
JB
381 const pool = new FixedThreadPool(
382 max,
383 './tests/worker-files/thread/testWorker.js',
384 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
385 )
386 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
387 const promises = []
388 for (let i = 0; i < max * 2; i++) {
6db75ad9 389 promises.push(pool.execute())
b3432a63
JB
390 }
391 await Promise.all(promises)
392 // We need to clean up the resources after our test
393 await pool.destroy()
394 })
395
396 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
b3432a63
JB
397 const pool = new DynamicThreadPool(
398 min,
399 max,
400 './tests/worker-files/thread/testWorker.js',
401 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
402 )
403 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
404 const promises = []
405 for (let i = 0; i < max * 2; i++) {
6db75ad9 406 promises.push(pool.execute())
b3432a63
JB
407 }
408 await Promise.all(promises)
409 // We need to clean up the resources after our test
410 await pool.destroy()
411 })
412
caeb9817 413 it('Verify WEIGHTED_ROUND_ROBIN strategy statistics are resets after setting it', async () => {
f0829c53 414 let pool = new FixedThreadPool(
caeb9817
JB
415 max,
416 './tests/worker-files/thread/testWorker.js'
417 )
418 expect(
419 pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime
420 ).toBeUndefined()
421 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
422 for (const worker of pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.keys()) {
423 expect(
424 pool.workerChoiceStrategyContext.workerChoiceStrategy.workersTaskRunTime.get(
425 worker
426 ).runTime
427 ).toBe(0)
428 }
f0829c53
JB
429 await pool.destroy()
430 pool = new DynamicThreadPool(
431 min,
432 max,
433 './tests/worker-files/thread/testWorker.js'
434 )
435 expect(
436 pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy
437 .workersTaskRunTime
438 ).toBeUndefined()
439 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
440 for (const worker of pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workersTaskRunTime.keys()) {
441 expect(
442 pool.workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy.workersTaskRunTime.get(
443 worker
444 ).runTime
445 ).toBe(0)
446 }
caeb9817
JB
447 // We need to clean up the resources after our test
448 await pool.destroy()
449 })
450
a35560ba 451 it('Verify unknown strategies throw error', () => {
a35560ba
S
452 expect(
453 () =>
454 new DynamicThreadPool(
455 min,
456 max,
457 './tests/worker-files/thread/testWorker.js',
1927ee67 458 { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
a35560ba
S
459 )
460 ).toThrowError(
461 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
462 )
463 })
464})