Commit | Line | Data |
---|---|---|
a61a0724 | 1 | const { expect } = require('expect') |
a35560ba S |
2 | const { |
3 | WorkerChoiceStrategies, | |
4 | DynamicThreadPool, | |
5 | FixedThreadPool | |
15d56315 | 6 | } = require('../../../lib/index') |
a35560ba S |
7 | |
8 | describe('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 | ||
b3432a63 | 280 | it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => { |
b3432a63 JB |
281 | const pool = new FixedThreadPool( |
282 | max, | |
283 | './tests/worker-files/thread/testWorker.js', | |
284 | { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } | |
285 | ) | |
286 | expect(pool.opts.workerChoiceStrategy).toBe( | |
287 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN | |
288 | ) | |
289 | // We need to clean up the resources after our test | |
290 | await pool.destroy() | |
291 | }) | |
292 | ||
293 | it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => { | |
b3432a63 JB |
294 | const pool = new FixedThreadPool( |
295 | max, | |
296 | './tests/worker-files/thread/testWorker.js' | |
297 | ) | |
298 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) | |
299 | expect(pool.opts.workerChoiceStrategy).toBe( | |
300 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN | |
301 | ) | |
302 | // We need to clean up the resources after our test | |
303 | await pool.destroy() | |
304 | }) | |
305 | ||
10fcfaf4 | 306 | it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => { |
10fcfaf4 JB |
307 | let pool = new FixedThreadPool( |
308 | max, | |
309 | './tests/worker-files/thread/testWorker.js' | |
310 | ) | |
311 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) | |
312 | expect( | |
313 | pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() | |
314 | .requiredStatistics.runTime | |
315 | ).toBe(true) | |
fd7ebd49 | 316 | await pool.destroy() |
10fcfaf4 JB |
317 | pool = new DynamicThreadPool( |
318 | min, | |
319 | max, | |
320 | './tests/worker-files/thread/testWorker.js' | |
321 | ) | |
322 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) | |
323 | expect( | |
324 | pool.workerChoiceStrategyContext.getWorkerChoiceStrategy() | |
325 | .requiredStatistics.runTime | |
326 | ).toBe(true) | |
327 | // We need to clean up the resources after our test | |
328 | await pool.destroy() | |
329 | }) | |
330 | ||
b3432a63 | 331 | it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => { |
b3432a63 JB |
332 | const pool = new FixedThreadPool( |
333 | max, | |
334 | './tests/worker-files/thread/testWorker.js', | |
335 | { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } | |
336 | ) | |
337 | // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` | |
338 | const promises = [] | |
339 | for (let i = 0; i < max * 2; i++) { | |
6db75ad9 | 340 | promises.push(pool.execute()) |
b3432a63 JB |
341 | } |
342 | await Promise.all(promises) | |
343 | // We need to clean up the resources after our test | |
344 | await pool.destroy() | |
345 | }) | |
346 | ||
347 | it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => { | |
b3432a63 JB |
348 | const pool = new DynamicThreadPool( |
349 | min, | |
350 | max, | |
351 | './tests/worker-files/thread/testWorker.js', | |
352 | { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } | |
353 | ) | |
354 | // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` | |
355 | const promises = [] | |
356 | for (let i = 0; i < max * 2; i++) { | |
6db75ad9 | 357 | promises.push(pool.execute()) |
b3432a63 JB |
358 | } |
359 | await Promise.all(promises) | |
360 | // We need to clean up the resources after our test | |
361 | await pool.destroy() | |
362 | }) | |
363 | ||
a35560ba | 364 | it('Verify unknown strategies throw error', () => { |
a35560ba S |
365 | expect( |
366 | () => | |
367 | new DynamicThreadPool( | |
368 | min, | |
369 | max, | |
370 | './tests/worker-files/thread/testWorker.js', | |
1927ee67 | 371 | { workerChoiceStrategy: 'UNKNOWN_STRATEGY' } |
a35560ba S |
372 | ) |
373 | ).toThrowError( | |
374 | new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found") | |
375 | ) | |
376 | }) | |
377 | }) |