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
58 pool
= new DynamicThreadPool(
61 './tests/worker-files/thread/testWorker.js'
63 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.ROUND_ROBIN
)
65 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
66 .requiredStatistics
.runTime
68 // We need to clean up the resources after our test
72 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
73 const pool
= new FixedThreadPool(
75 './tests/worker-files/thread/testWorker.js',
76 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
78 expect(pool
.opts
.workerChoiceStrategy
).toBe(
79 WorkerChoiceStrategies
.ROUND_ROBIN
81 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
83 for (let i
= 0; i
< max
* 2; i
++) {
84 promises
.push(pool
.execute())
86 await Promise
.all(promises
)
87 // We need to clean up the resources after our test
91 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
92 const pool
= new DynamicThreadPool(
95 './tests/worker-files/thread/testWorker.js',
96 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
98 expect(pool
.opts
.workerChoiceStrategy
).toBe(
99 WorkerChoiceStrategies
.ROUND_ROBIN
101 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
103 for (let i
= 0; i
< max
* 2; i
++) {
104 promises
.push(pool
.execute())
106 await Promise
.all(promises
)
107 // We need to clean up the resources after our test
111 it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
112 const pool
= new FixedThreadPool(
114 './tests/worker-files/thread/testWorker.js',
115 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
117 expect(pool
.opts
.workerChoiceStrategy
).toBe(
118 WorkerChoiceStrategies
.LESS_RECENTLY_USED
120 // We need to clean up the resources after our test
124 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
125 const pool
= new FixedThreadPool(
127 './tests/worker-files/thread/testWorker.js'
129 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
130 expect(pool
.opts
.workerChoiceStrategy
).toBe(
131 WorkerChoiceStrategies
.LESS_RECENTLY_USED
133 // We need to clean up the resources after our test
137 it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => {
138 let pool
= new FixedThreadPool(
140 './tests/worker-files/thread/testWorker.js'
142 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
144 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
145 .requiredStatistics
.runTime
147 pool
= new DynamicThreadPool(
150 './tests/worker-files/thread/testWorker.js'
152 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
154 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
155 .requiredStatistics
.runTime
157 // We need to clean up the resources after our test
161 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
162 const pool
= new FixedThreadPool(
164 './tests/worker-files/thread/testWorker.js',
165 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
167 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
169 for (let i
= 0; i
< max
* 2; i
++) {
170 promises
.push(pool
.execute())
172 await Promise
.all(promises
)
173 // We need to clean up the resources after our test
177 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
178 const pool
= new DynamicThreadPool(
181 './tests/worker-files/thread/testWorker.js',
182 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
184 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
186 for (let i
= 0; i
< max
* 2; i
++) {
187 promises
.push(pool
.execute())
189 await Promise
.all(promises
)
190 // We need to clean up the resources after our test
194 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
195 const pool
= new FixedThreadPool(
197 './tests/worker-files/thread/testWorker.js',
198 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
200 expect(pool
.opts
.workerChoiceStrategy
).toBe(
201 WorkerChoiceStrategies
.FAIR_SHARE
203 // We need to clean up the resources after our test
207 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
208 const pool
= new FixedThreadPool(
210 './tests/worker-files/thread/testWorker.js'
212 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
213 expect(pool
.opts
.workerChoiceStrategy
).toBe(
214 WorkerChoiceStrategies
.FAIR_SHARE
216 // We need to clean up the resources after our test
220 it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
221 let pool
= new FixedThreadPool(
223 './tests/worker-files/thread/testWorker.js'
225 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
227 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
228 .requiredStatistics
.runTime
230 pool
= new DynamicThreadPool(
233 './tests/worker-files/thread/testWorker.js'
235 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
237 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
238 .requiredStatistics
.runTime
240 // We need to clean up the resources after our test
244 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
245 const pool
= new FixedThreadPool(
247 './tests/worker-files/thread/testWorker.js',
248 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
250 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
252 for (let i
= 0; i
< max
* 2; i
++) {
253 promises
.push(pool
.execute())
255 await Promise
.all(promises
)
256 // We need to clean up the resources after our test
260 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
261 const pool
= new DynamicThreadPool(
264 './tests/worker-files/thread/testWorker.js',
265 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
267 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
269 for (let i
= 0; i
< max
* 2; i
++) {
270 promises
.push(pool
.execute())
272 await Promise
.all(promises
)
273 // We need to clean up the resources after our test
277 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
278 const pool
= new FixedThreadPool(
280 './tests/worker-files/thread/testWorker.js',
281 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
283 expect(pool
.opts
.workerChoiceStrategy
).toBe(
284 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
286 // We need to clean up the resources after our test
290 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
291 const pool
= new FixedThreadPool(
293 './tests/worker-files/thread/testWorker.js'
295 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
296 expect(pool
.opts
.workerChoiceStrategy
).toBe(
297 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
299 // We need to clean up the resources after our test
303 it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
304 let pool
= new FixedThreadPool(
306 './tests/worker-files/thread/testWorker.js'
308 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
310 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
311 .requiredStatistics
.runTime
313 pool
= new DynamicThreadPool(
316 './tests/worker-files/thread/testWorker.js'
318 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
320 pool
.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
321 .requiredStatistics
.runTime
323 // We need to clean up the resources after our test
327 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
328 const pool
= new FixedThreadPool(
330 './tests/worker-files/thread/testWorker.js',
331 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
333 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
335 for (let i
= 0; i
< max
* 2; i
++) {
336 promises
.push(pool
.execute())
338 await Promise
.all(promises
)
339 // We need to clean up the resources after our test
343 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
344 const pool
= new DynamicThreadPool(
347 './tests/worker-files/thread/testWorker.js',
348 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
350 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
352 for (let i
= 0; i
< max
* 2; i
++) {
353 promises
.push(pool
.execute())
355 await Promise
.all(promises
)
356 // We need to clean up the resources after our test
360 it('Verify unknown strategies throw error', () => {
363 new DynamicThreadPool(
366 './tests/worker-files/thread/testWorker.js',
367 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
370 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")