Uniformize some comments
[poolifier.git] / tests / pools / selection-strategies / selection-strategies.test.js
1 const { expect } = require('expect')
2 const {
3 WorkerChoiceStrategies,
4 DynamicThreadPool,
5 FixedThreadPool
6 } = require('../../../lib/index')
7
8 describe('Selection strategies test suite', () => {
9 const min = 0
10 const max = 3
11
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')
15 expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE')
16 expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe(
17 'WEIGHTED_ROUND_ROBIN'
18 )
19 })
20
21 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
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 () => {
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
48 it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
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)
58 pool = new DynamicThreadPool(
59 min,
60 max,
61 './tests/worker-files/thread/testWorker.js'
62 )
63 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
64 expect(
65 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
66 .requiredStatistics.runTime
67 ).toBe(false)
68 // We need to clean up the resources after our test
69 await pool.destroy()
70 })
71
72 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
73 const pool = new FixedThreadPool(
74 max,
75 './tests/worker-files/thread/testWorker.js',
76 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
77 )
78 expect(pool.opts.workerChoiceStrategy).toBe(
79 WorkerChoiceStrategies.ROUND_ROBIN
80 )
81 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
82 const promises = []
83 for (let i = 0; i < max * 2; i++) {
84 promises.push(pool.execute())
85 }
86 await Promise.all(promises)
87 // We need to clean up the resources after our test
88 await pool.destroy()
89 })
90
91 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
92 const pool = new DynamicThreadPool(
93 min,
94 max,
95 './tests/worker-files/thread/testWorker.js',
96 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
97 )
98 expect(pool.opts.workerChoiceStrategy).toBe(
99 WorkerChoiceStrategies.ROUND_ROBIN
100 )
101 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
102 const promises = []
103 for (let i = 0; i < max * 2; i++) {
104 promises.push(pool.execute())
105 }
106 await Promise.all(promises)
107 // We need to clean up the resources after our test
108 await pool.destroy()
109 })
110
111 it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
112 const pool = new FixedThreadPool(
113 max,
114 './tests/worker-files/thread/testWorker.js',
115 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
116 )
117 expect(pool.opts.workerChoiceStrategy).toBe(
118 WorkerChoiceStrategies.LESS_RECENTLY_USED
119 )
120 // We need to clean up the resources after our test
121 await pool.destroy()
122 })
123
124 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
125 const pool = new FixedThreadPool(
126 max,
127 './tests/worker-files/thread/testWorker.js'
128 )
129 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
130 expect(pool.opts.workerChoiceStrategy).toBe(
131 WorkerChoiceStrategies.LESS_RECENTLY_USED
132 )
133 // We need to clean up the resources after our test
134 await pool.destroy()
135 })
136
137 it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => {
138 let pool = new FixedThreadPool(
139 max,
140 './tests/worker-files/thread/testWorker.js'
141 )
142 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
143 expect(
144 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
145 .requiredStatistics.runTime
146 ).toBe(false)
147 pool = new DynamicThreadPool(
148 min,
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 // We need to clean up the resources after our test
158 await pool.destroy()
159 })
160
161 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
162 const pool = new FixedThreadPool(
163 max,
164 './tests/worker-files/thread/testWorker.js',
165 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
166 )
167 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
168 const promises = []
169 for (let i = 0; i < max * 2; i++) {
170 promises.push(pool.execute())
171 }
172 await Promise.all(promises)
173 // We need to clean up the resources after our test
174 await pool.destroy()
175 })
176
177 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
178 const pool = new DynamicThreadPool(
179 min,
180 max,
181 './tests/worker-files/thread/testWorker.js',
182 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
183 )
184 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
185 const promises = []
186 for (let i = 0; i < max * 2; i++) {
187 promises.push(pool.execute())
188 }
189 await Promise.all(promises)
190 // We need to clean up the resources after our test
191 await pool.destroy()
192 })
193
194 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
195 const pool = new FixedThreadPool(
196 max,
197 './tests/worker-files/thread/testWorker.js',
198 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
199 )
200 expect(pool.opts.workerChoiceStrategy).toBe(
201 WorkerChoiceStrategies.FAIR_SHARE
202 )
203 // We need to clean up the resources after our test
204 await pool.destroy()
205 })
206
207 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
208 const pool = new FixedThreadPool(
209 max,
210 './tests/worker-files/thread/testWorker.js'
211 )
212 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
213 expect(pool.opts.workerChoiceStrategy).toBe(
214 WorkerChoiceStrategies.FAIR_SHARE
215 )
216 // We need to clean up the resources after our test
217 await pool.destroy()
218 })
219
220 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
221 let pool = new FixedThreadPool(
222 max,
223 './tests/worker-files/thread/testWorker.js'
224 )
225 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
226 expect(
227 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
228 .requiredStatistics.runTime
229 ).toBe(true)
230 pool = new DynamicThreadPool(
231 min,
232 max,
233 './tests/worker-files/thread/testWorker.js'
234 )
235 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
236 expect(
237 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
238 .requiredStatistics.runTime
239 ).toBe(true)
240 // We need to clean up the resources after our test
241 await pool.destroy()
242 })
243
244 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
245 const pool = new FixedThreadPool(
246 max,
247 './tests/worker-files/thread/testWorker.js',
248 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
249 )
250 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
251 const promises = []
252 for (let i = 0; i < max * 2; i++) {
253 promises.push(pool.execute())
254 }
255 await Promise.all(promises)
256 // We need to clean up the resources after our test
257 await pool.destroy()
258 })
259
260 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
261 const pool = new DynamicThreadPool(
262 min,
263 max,
264 './tests/worker-files/thread/testWorker.js',
265 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
266 )
267 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
268 const promises = []
269 for (let i = 0; i < max * 2; i++) {
270 promises.push(pool.execute())
271 }
272 await Promise.all(promises)
273 // We need to clean up the resources after our test
274 await pool.destroy()
275 })
276
277 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
278 const pool = new FixedThreadPool(
279 max,
280 './tests/worker-files/thread/testWorker.js',
281 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
282 )
283 expect(pool.opts.workerChoiceStrategy).toBe(
284 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
285 )
286 // We need to clean up the resources after our test
287 await pool.destroy()
288 })
289
290 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
291 const pool = new FixedThreadPool(
292 max,
293 './tests/worker-files/thread/testWorker.js'
294 )
295 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
296 expect(pool.opts.workerChoiceStrategy).toBe(
297 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
298 )
299 // We need to clean up the resources after our test
300 await pool.destroy()
301 })
302
303 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
304 let pool = new FixedThreadPool(
305 max,
306 './tests/worker-files/thread/testWorker.js'
307 )
308 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
309 expect(
310 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
311 .requiredStatistics.runTime
312 ).toBe(true)
313 pool = new DynamicThreadPool(
314 min,
315 max,
316 './tests/worker-files/thread/testWorker.js'
317 )
318 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
319 expect(
320 pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
321 .requiredStatistics.runTime
322 ).toBe(true)
323 // We need to clean up the resources after our test
324 await pool.destroy()
325 })
326
327 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
328 const pool = new FixedThreadPool(
329 max,
330 './tests/worker-files/thread/testWorker.js',
331 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
332 )
333 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
334 const promises = []
335 for (let i = 0; i < max * 2; i++) {
336 promises.push(pool.execute())
337 }
338 await Promise.all(promises)
339 // We need to clean up the resources after our test
340 await pool.destroy()
341 })
342
343 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
344 const pool = new DynamicThreadPool(
345 min,
346 max,
347 './tests/worker-files/thread/testWorker.js',
348 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
349 )
350 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
351 const promises = []
352 for (let i = 0; i < max * 2; i++) {
353 promises.push(pool.execute())
354 }
355 await Promise.all(promises)
356 // We need to clean up the resources after our test
357 await pool.destroy()
358 })
359
360 it('Verify unknown strategies throw error', () => {
361 expect(
362 () =>
363 new DynamicThreadPool(
364 min,
365 max,
366 './tests/worker-files/thread/testWorker.js',
367 { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
368 )
369 ).toThrowError(
370 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
371 )
372 })
373 })