1 const { expect
} = require('expect')
3 WorkerChoiceStrategies
,
6 } = require('../../../lib/index')
8 describe('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')
12 expect(WorkerChoiceStrategies
.FAIR_SHARE
).toBe('FAIR_SHARE')
13 expect(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
).toBe(
14 'WEIGHTED_ROUND_ROBIN'
18 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
21 const pool
= new DynamicThreadPool(
24 './tests/worker-files/thread/testWorker.js'
26 expect(pool
.opts
.workerChoiceStrategy
).toBe(
27 WorkerChoiceStrategies
.ROUND_ROBIN
29 // We need to clean up the resources after our test
33 it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
36 const pool
= new DynamicThreadPool(
39 './tests/worker-files/thread/testWorker.js'
41 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
42 expect(pool
.opts
.workerChoiceStrategy
).toBe(
43 WorkerChoiceStrategies
.ROUND_ROBIN
45 // We need to clean up the resources after our test
49 it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
52 let pool
= new FixedThreadPool(
54 './tests/worker-files/thread/testWorker.js'
56 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
58 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
59 .requiredStatistics
.runTime
61 pool
= new DynamicThreadPool(
64 './tests/worker-files/thread/testWorker.js'
66 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
68 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
69 .requiredStatistics
.runTime
71 // We need to clean up the resources after our test
75 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
77 const pool
= new FixedThreadPool(
79 './tests/worker-files/thread/testWorker.js',
80 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
82 expect(pool
.opts
.workerChoiceStrategy
).toBe(
83 WorkerChoiceStrategies
.ROUND_ROBIN
85 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
87 for (let i
= 0; i
< max
* 2; i
++) {
88 promises
.push(pool
.execute({ test
: 'test' }))
90 await Promise
.all(promises
)
91 // We need to clean up the resources after our test
95 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
98 const pool
= new DynamicThreadPool(
101 './tests/worker-files/thread/testWorker.js',
102 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
104 expect(pool
.opts
.workerChoiceStrategy
).toBe(
105 WorkerChoiceStrategies
.ROUND_ROBIN
107 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
109 for (let i
= 0; i
< max
* 2; i
++) {
110 promises
.push(pool
.execute({ test
: 'test' }))
112 await Promise
.all(promises
)
113 // We need to clean up the resources after our test
117 it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
119 const pool
= new FixedThreadPool(
121 './tests/worker-files/thread/testWorker.js',
122 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
124 expect(pool
.opts
.workerChoiceStrategy
).toBe(
125 WorkerChoiceStrategies
.LESS_RECENTLY_USED
127 // We need to clean up the resources after our test
131 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
133 const pool
= new FixedThreadPool(
135 './tests/worker-files/thread/testWorker.js'
137 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
138 expect(pool
.opts
.workerChoiceStrategy
).toBe(
139 WorkerChoiceStrategies
.LESS_RECENTLY_USED
141 // We need to clean up the resources after our test
145 it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => {
148 let pool
= new FixedThreadPool(
150 './tests/worker-files/thread/testWorker.js'
152 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
154 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
155 .requiredStatistics
.runTime
157 pool
= new DynamicThreadPool(
160 './tests/worker-files/thread/testWorker.js'
162 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
164 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
165 .requiredStatistics
.runTime
167 // We need to clean up the resources after our test
171 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
173 const pool
= new FixedThreadPool(
175 './tests/worker-files/thread/testWorker.js',
176 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
178 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
180 for (let i
= 0; i
< max
* 2; i
++) {
181 promises
.push(pool
.execute({ test
: 'test' }))
183 await Promise
.all(promises
)
184 // We need to clean up the resources after our test
188 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
191 const pool
= new DynamicThreadPool(
194 './tests/worker-files/thread/testWorker.js',
195 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
197 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
199 for (let i
= 0; i
< max
* 2; i
++) {
200 promises
.push(pool
.execute({ test
: 'test' }))
202 await Promise
.all(promises
)
203 // We need to clean up the resources after our test
207 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
209 const pool
= new FixedThreadPool(
211 './tests/worker-files/thread/testWorker.js',
212 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
214 expect(pool
.opts
.workerChoiceStrategy
).toBe(
215 WorkerChoiceStrategies
.FAIR_SHARE
217 // We need to clean up the resources after our test
221 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
223 const pool
= new FixedThreadPool(
225 './tests/worker-files/thread/testWorker.js'
227 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
228 expect(pool
.opts
.workerChoiceStrategy
).toBe(
229 WorkerChoiceStrategies
.FAIR_SHARE
231 // We need to clean up the resources after our test
235 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
238 let pool
= new FixedThreadPool(
240 './tests/worker-files/thread/testWorker.js'
242 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
244 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
245 .requiredStatistics
.runTime
247 pool
= new DynamicThreadPool(
250 './tests/worker-files/thread/testWorker.js'
252 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
254 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
255 .requiredStatistics
.runTime
257 // We need to clean up the resources after our test
261 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
263 const pool
= new FixedThreadPool(
265 './tests/worker-files/thread/testWorker.js',
266 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
268 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
270 for (let i
= 0; i
< max
* 2; i
++) {
271 promises
.push(pool
.execute({ test
: 'test' }))
273 await Promise
.all(promises
)
274 // We need to clean up the resources after our test
278 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
281 const pool
= new DynamicThreadPool(
284 './tests/worker-files/thread/testWorker.js',
285 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
287 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
289 for (let i
= 0; i
< max
* 2; i
++) {
290 promises
.push(pool
.execute({ test
: 'test' }))
292 await Promise
.all(promises
)
293 // We need to clean up the resources after our test
297 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
299 const pool
= new FixedThreadPool(
301 './tests/worker-files/thread/testWorker.js',
302 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
304 expect(pool
.opts
.workerChoiceStrategy
).toBe(
305 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
307 // We need to clean up the resources after our test
311 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
313 const pool
= new FixedThreadPool(
315 './tests/worker-files/thread/testWorker.js'
317 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
318 expect(pool
.opts
.workerChoiceStrategy
).toBe(
319 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
321 // We need to clean up the resources after our test
325 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
328 let pool
= new FixedThreadPool(
330 './tests/worker-files/thread/testWorker.js'
332 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
334 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
335 .requiredStatistics
.runTime
337 pool
= new DynamicThreadPool(
340 './tests/worker-files/thread/testWorker.js'
342 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
344 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
345 .requiredStatistics
.runTime
347 // We need to clean up the resources after our test
351 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
353 const pool
= new FixedThreadPool(
355 './tests/worker-files/thread/testWorker.js',
356 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
358 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
360 for (let i
= 0; i
< max
* 2; i
++) {
361 promises
.push(pool
.execute({ test
: 'test' }))
363 await Promise
.all(promises
)
364 // We need to clean up the resources after our test
368 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
371 const pool
= new DynamicThreadPool(
374 './tests/worker-files/thread/testWorker.js',
375 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
377 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
379 for (let i
= 0; i
< max
* 2; i
++) {
380 promises
.push(pool
.execute({ test
: 'test' }))
382 await Promise
.all(promises
)
383 // We need to clean up the resources after our test
387 it('Verify unknown strategies throw error', () => {
392 new DynamicThreadPool(
395 './tests/worker-files/thread/testWorker.js',
396 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
399 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")