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 WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
281 const pool
= new FixedThreadPool(
283 './tests/worker-files/thread/testWorker.js',
284 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
286 expect(pool
.opts
.workerChoiceStrategy
).toBe(
287 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
289 // We need to clean up the resources after our test
293 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
294 const pool
= new FixedThreadPool(
296 './tests/worker-files/thread/testWorker.js'
298 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
299 expect(pool
.opts
.workerChoiceStrategy
).toBe(
300 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
302 // We need to clean up the resources after our test
306 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
307 let pool
= new FixedThreadPool(
309 './tests/worker-files/thread/testWorker.js'
311 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
313 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
314 .requiredStatistics
.runTime
317 pool
= new DynamicThreadPool(
320 './tests/worker-files/thread/testWorker.js'
322 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
324 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
325 .requiredStatistics
.runTime
327 // We need to clean up the resources after our test
331 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
332 const pool
= new FixedThreadPool(
334 './tests/worker-files/thread/testWorker.js',
335 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
337 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
339 for (let i
= 0; i
< max
* 2; i
++) {
340 promises
.push(pool
.execute())
342 await Promise
.all(promises
)
343 // We need to clean up the resources after our test
347 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
348 const pool
= new DynamicThreadPool(
351 './tests/worker-files/thread/testWorker.js',
352 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
354 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
356 for (let i
= 0; i
< max
* 2; i
++) {
357 promises
.push(pool
.execute())
359 await Promise
.all(promises
)
360 // We need to clean up the resources after our test
364 it('Verify unknown strategies throw error', () => {
367 new DynamicThreadPool(
370 './tests/worker-files/thread/testWorker.js',
371 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
374 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")