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