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 // We need to clean up the resources after our test
209 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
210 const pool
= new FixedThreadPool(
212 './tests/worker-files/thread/testWorker.js'
214 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
215 expect(pool
.opts
.workerChoiceStrategy
).toBe(
216 WorkerChoiceStrategies
.FAIR_SHARE
218 // We need to clean up the resources after our test
222 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
223 let pool
= new FixedThreadPool(
225 './tests/worker-files/thread/testWorker.js'
227 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
229 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
230 .requiredStatistics
.runTime
233 pool
= new DynamicThreadPool(
236 './tests/worker-files/thread/testWorker.js'
238 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
240 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
241 .requiredStatistics
.runTime
243 // We need to clean up the resources after our test
247 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
248 const pool
= new FixedThreadPool(
250 './tests/worker-files/thread/testWorker.js',
251 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
253 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
255 for (let i
= 0; i
< max
* 2; i
++) {
256 promises
.push(pool
.execute())
258 await Promise
.all(promises
)
259 // We need to clean up the resources after our test
263 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
264 const pool
= new DynamicThreadPool(
267 './tests/worker-files/thread/testWorker.js',
268 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
270 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
272 for (let i
= 0; i
< max
* 2; i
++) {
273 promises
.push(pool
.execute())
275 await Promise
.all(promises
)
276 // We need to clean up the resources after our test
280 it('Verify FAIR_SHARE strategy statistics are resets after setting it', async () => {
281 const pool
= new FixedThreadPool(
283 './tests/worker-files/thread/testWorker.js'
286 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
287 .workerLastVirtualTaskTimestamp
289 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
290 for (const worker
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.keys()) {
292 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
297 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workerLastVirtualTaskTimestamp
.get(
302 // We need to clean up the resources after our test
306 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
307 const pool
= new FixedThreadPool(
309 './tests/worker-files/thread/testWorker.js',
310 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
312 expect(pool
.opts
.workerChoiceStrategy
).toBe(
313 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
315 // We need to clean up the resources after our test
319 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
320 const pool
= new FixedThreadPool(
322 './tests/worker-files/thread/testWorker.js'
324 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
325 expect(pool
.opts
.workerChoiceStrategy
).toBe(
326 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
328 // We need to clean up the resources after our test
332 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
333 let pool
= new FixedThreadPool(
335 './tests/worker-files/thread/testWorker.js'
337 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
339 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
340 .requiredStatistics
.runTime
343 pool
= new DynamicThreadPool(
346 './tests/worker-files/thread/testWorker.js'
348 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
350 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
351 .requiredStatistics
.runTime
353 // We need to clean up the resources after our test
357 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
358 const pool
= new FixedThreadPool(
360 './tests/worker-files/thread/testWorker.js',
361 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
363 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
365 for (let i
= 0; i
< max
* 2; i
++) {
366 promises
.push(pool
.execute())
368 await Promise
.all(promises
)
369 // We need to clean up the resources after our test
373 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
374 const pool
= new DynamicThreadPool(
377 './tests/worker-files/thread/testWorker.js',
378 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
380 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
382 for (let i
= 0; i
< max
* 2; i
++) {
383 promises
.push(pool
.execute())
385 await Promise
.all(promises
)
386 // We need to clean up the resources after our test
390 it('Verify WEIGHTED_ROUND_ROBIN strategy statistics are resets after setting it', async () => {
391 const pool
= new FixedThreadPool(
393 './tests/worker-files/thread/testWorker.js'
396 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
398 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
399 for (const worker
of pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.keys()) {
401 pool
.workerChoiceStrategyContext
.workerChoiceStrategy
.workersTaskRunTime
.get(
406 // We need to clean up the resources after our test
410 it('Verify unknown strategies throw error', () => {
413 new DynamicThreadPool(
416 './tests/worker-files/thread/testWorker.js',
417 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
420 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")