1 const { expect
} = require('expect')
3 WorkerChoiceStrategies
,
6 } = require('../../../lib/index')
8 describe('Selection strategies test suite', () => {
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'
21 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
22 const pool
= new DynamicThreadPool(
25 './tests/worker-files/thread/testWorker.js'
27 expect(pool
.opts
.workerChoiceStrategy
).toBe(
28 WorkerChoiceStrategies
.ROUND_ROBIN
30 // We need to clean up the resources after our test
34 it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
35 const pool
= new DynamicThreadPool(
38 './tests/worker-files/thread/testWorker.js'
40 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
41 expect(pool
.opts
.workerChoiceStrategy
).toBe(
42 WorkerChoiceStrategies
.ROUND_ROBIN
44 // We need to clean up the resources after our test
48 it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
49 let pool
= new FixedThreadPool(
51 './tests/worker-files/thread/testWorker.js'
53 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
55 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
56 .requiredStatistics
.runTime
59 pool
= new DynamicThreadPool(
62 './tests/worker-files/thread/testWorker.js'
64 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
66 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
67 .requiredStatistics
.runTime
69 // We need to clean up the resources after our test
73 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
74 const pool
= new FixedThreadPool(
76 './tests/worker-files/thread/testWorker.js',
77 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
79 expect(pool
.opts
.workerChoiceStrategy
).toBe(
80 WorkerChoiceStrategies
.ROUND_ROBIN
82 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
84 for (let i
= 0; i
< max
* 2; i
++) {
85 promises
.push(pool
.execute())
87 await Promise
.all(promises
)
88 // We need to clean up the resources after our test
92 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
93 const pool
= new DynamicThreadPool(
96 './tests/worker-files/thread/testWorker.js',
97 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
99 expect(pool
.opts
.workerChoiceStrategy
).toBe(
100 WorkerChoiceStrategies
.ROUND_ROBIN
102 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
104 for (let i
= 0; i
< max
* 2; i
++) {
105 promises
.push(pool
.execute())
107 await Promise
.all(promises
)
108 // We need to clean up the resources after our test
112 it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
113 const pool
= new FixedThreadPool(
115 './tests/worker-files/thread/testWorker.js',
116 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
118 expect(pool
.opts
.workerChoiceStrategy
).toBe(
119 WorkerChoiceStrategies
.LESS_RECENTLY_USED
121 // We need to clean up the resources after our test
125 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
126 const pool
= new FixedThreadPool(
128 './tests/worker-files/thread/testWorker.js'
130 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
131 expect(pool
.opts
.workerChoiceStrategy
).toBe(
132 WorkerChoiceStrategies
.LESS_RECENTLY_USED
134 // We need to clean up the resources after our test
138 it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => {
139 let pool
= new FixedThreadPool(
141 './tests/worker-files/thread/testWorker.js'
143 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
145 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
146 .requiredStatistics
.runTime
149 pool
= new DynamicThreadPool(
152 './tests/worker-files/thread/testWorker.js'
154 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
156 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
157 .requiredStatistics
.runTime
159 // We need to clean up the resources after our test
163 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
164 const pool
= new FixedThreadPool(
166 './tests/worker-files/thread/testWorker.js',
167 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
169 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
171 for (let i
= 0; i
< max
* 2; i
++) {
172 promises
.push(pool
.execute())
174 await Promise
.all(promises
)
175 // We need to clean up the resources after our test
179 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
180 const pool
= new DynamicThreadPool(
183 './tests/worker-files/thread/testWorker.js',
184 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
186 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
188 for (let i
= 0; i
< max
* 2; i
++) {
189 promises
.push(pool
.execute())
191 await Promise
.all(promises
)
192 // We need to clean up the resources after our test
196 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
197 const pool
= new FixedThreadPool(
199 './tests/worker-files/thread/testWorker.js',
200 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
202 expect(pool
.opts
.workerChoiceStrategy
).toBe(
203 WorkerChoiceStrategies
.FAIR_SHARE
205 for (const worker
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.keys()) {
207 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
212 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
217 // We need to clean up the resources after our test
221 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
222 const pool
= new FixedThreadPool(
224 './tests/worker-files/thread/testWorker.js'
226 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
227 expect(pool
.opts
.workerChoiceStrategy
).toBe(
228 WorkerChoiceStrategies
.FAIR_SHARE
230 // We need to clean up the resources after our test
234 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
235 let pool
= new FixedThreadPool(
237 './tests/worker-files/thread/testWorker.js'
239 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
241 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
242 .requiredStatistics
.runTime
245 pool
= new DynamicThreadPool(
248 './tests/worker-files/thread/testWorker.js'
250 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
252 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
253 .requiredStatistics
.runTime
255 // We need to clean up the resources after our test
259 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
260 const pool
= new FixedThreadPool(
262 './tests/worker-files/thread/testWorker.js',
263 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
265 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
267 for (let i
= 0; i
< max
* 2; i
++) {
268 promises
.push(pool
.execute())
270 await Promise
.all(promises
)
271 // We need to clean up the resources after our test
275 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
276 const pool
= new DynamicThreadPool(
279 './tests/worker-files/thread/testWorker.js',
280 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
282 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
284 for (let i
= 0; i
< max
* 2; i
++) {
285 promises
.push(pool
.execute())
287 await Promise
.all(promises
)
288 // We need to clean up the resources after our test
292 it('Verify FAIR_SHARE strategy statistics are resets after setting it', async () => {
293 let pool
= new FixedThreadPool(
295 './tests/worker-files/thread/testWorker.js'
298 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
299 .workerLastVirtualTaskTimestamp
301 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
302 for (const worker
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.keys()) {
304 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
309 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
315 pool
= new DynamicThreadPool(
318 './tests/worker-files/thread/testWorker.js'
321 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
322 .workerLastVirtualTaskTimestamp
324 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
325 for (const worker
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.keys()) {
327 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
332 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
337 // We need to clean up the resources after our test
341 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
342 const pool
= new FixedThreadPool(
344 './tests/worker-files/thread/testWorker.js',
345 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
347 expect(pool
.opts
.workerChoiceStrategy
).toBe(
348 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
350 for (const worker
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.keys()) {
352 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.get(
357 // We need to clean up the resources after our test
361 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
362 const pool
= new FixedThreadPool(
364 './tests/worker-files/thread/testWorker.js'
366 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
367 expect(pool
.opts
.workerChoiceStrategy
).toBe(
368 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
370 // We need to clean up the resources after our test
374 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
375 let pool
= new FixedThreadPool(
377 './tests/worker-files/thread/testWorker.js'
379 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
381 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
382 .requiredStatistics
.runTime
385 pool
= new DynamicThreadPool(
388 './tests/worker-files/thread/testWorker.js'
390 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
392 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
393 .requiredStatistics
.runTime
395 // We need to clean up the resources after our test
399 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
400 const pool
= new FixedThreadPool(
402 './tests/worker-files/thread/testWorker.js',
403 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
405 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
407 for (let i
= 0; i
< max
* 2; i
++) {
408 promises
.push(pool
.execute())
410 await Promise
.all(promises
)
411 // We need to clean up the resources after our test
415 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
416 const pool
= new DynamicThreadPool(
419 './tests/worker-files/thread/testWorker.js',
420 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
422 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
424 for (let i
= 0; i
< max
* 2; i
++) {
425 promises
.push(pool
.execute())
427 await Promise
.all(promises
)
428 // We need to clean up the resources after our test
432 it('Verify WEIGHTED_ROUND_ROBIN strategy statistics are resets after setting it', async () => {
433 let pool
= new FixedThreadPool(
435 './tests/worker-files/thread/testWorker.js'
438 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
440 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
441 for (const worker
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.keys()) {
443 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.get(
449 pool
= new DynamicThreadPool(
452 './tests/worker-files/thread/testWorker.js'
455 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
458 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
459 for (const worker
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
.workersTaskRunTime
.keys()) {
461 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
.workersTaskRunTime
.get(
466 // We need to clean up the resources after our test
470 it('Verify unknown strategies throw error', () => {
473 new DynamicThreadPool(
476 './tests/worker-files/thread/testWorker.js',
477 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
480 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")