b12a8df4b726438ba173f867ea59e19123f00fb1
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 can be run in a fixed pool', async () => {
51 const pool
= new FixedThreadPool(
53 './tests/worker-files/thread/testWorker.js',
54 { workerChoiceStrategy
: WorkerChoiceStrategies
.ROUND_ROBIN
}
56 expect(pool
.opts
.workerChoiceStrategy
).toBe(
57 WorkerChoiceStrategies
.ROUND_ROBIN
59 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
61 for (let i
= 0; i
< max
* 2; i
++) {
62 promises
.push(pool
.execute({ test
: 'test' }))
64 await Promise
.all(promises
)
65 // We need to clean up the resources after our test
69 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
72 const pool
= new DynamicThreadPool(
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({ test
: 'test' }))
86 await Promise
.all(promises
)
87 // We need to clean up the resources after our test
91 it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
93 const pool
= new FixedThreadPool(
95 './tests/worker-files/thread/testWorker.js',
96 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
98 expect(pool
.opts
.workerChoiceStrategy
).toBe(
99 WorkerChoiceStrategies
.LESS_RECENTLY_USED
101 // We need to clean up the resources after our test
105 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
107 const pool
= new FixedThreadPool(
109 './tests/worker-files/thread/testWorker.js'
111 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.LESS_RECENTLY_USED
)
112 expect(pool
.opts
.workerChoiceStrategy
).toBe(
113 WorkerChoiceStrategies
.LESS_RECENTLY_USED
115 // We need to clean up the resources after our test
119 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
121 const pool
= new FixedThreadPool(
123 './tests/worker-files/thread/testWorker.js',
124 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
126 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
128 for (let i
= 0; i
< max
* 2; i
++) {
129 promises
.push(pool
.execute({ test
: 'test' }))
131 await Promise
.all(promises
)
132 // We need to clean up the resources after our test
136 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
139 const pool
= new DynamicThreadPool(
142 './tests/worker-files/thread/testWorker.js',
143 { workerChoiceStrategy
: WorkerChoiceStrategies
.LESS_RECENTLY_USED
}
145 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
147 for (let i
= 0; i
< max
* 2; i
++) {
148 promises
.push(pool
.execute({ test
: 'test' }))
150 await Promise
.all(promises
)
151 // We need to clean up the resources after our test
155 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
157 const pool
= new FixedThreadPool(
159 './tests/worker-files/thread/testWorker.js',
160 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
162 expect(pool
.opts
.workerChoiceStrategy
).toBe(
163 WorkerChoiceStrategies
.FAIR_SHARE
165 // We need to clean up the resources after our test
169 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
171 const pool
= new FixedThreadPool(
173 './tests/worker-files/thread/testWorker.js'
175 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.FAIR_SHARE
)
176 expect(pool
.opts
.workerChoiceStrategy
).toBe(
177 WorkerChoiceStrategies
.FAIR_SHARE
179 // We need to clean up the resources after our test
183 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
185 const pool
= new FixedThreadPool(
187 './tests/worker-files/thread/testWorker.js',
188 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
190 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
192 for (let i
= 0; i
< max
* 2; i
++) {
193 promises
.push(pool
.execute({ test
: 'test' }))
195 await Promise
.all(promises
)
196 // We need to clean up the resources after our test
200 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
203 const pool
= new DynamicThreadPool(
206 './tests/worker-files/thread/testWorker.js',
207 { workerChoiceStrategy
: WorkerChoiceStrategies
.FAIR_SHARE
}
209 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
211 for (let i
= 0; i
< max
* 2; i
++) {
212 promises
.push(pool
.execute({ test
: 'test' }))
214 await Promise
.all(promises
)
215 // We need to clean up the resources after our test
219 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
221 const pool
= new FixedThreadPool(
223 './tests/worker-files/thread/testWorker.js',
224 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
226 expect(pool
.opts
.workerChoiceStrategy
).toBe(
227 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
229 // We need to clean up the resources after our test
233 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
235 const pool
= new FixedThreadPool(
237 './tests/worker-files/thread/testWorker.js'
239 pool
.setWorkerChoiceStrategy(WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
)
240 expect(pool
.opts
.workerChoiceStrategy
).toBe(
241 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
243 // We need to clean up the resources after our test
247 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
249 const pool
= new FixedThreadPool(
251 './tests/worker-files/thread/testWorker.js',
252 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
254 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
256 for (let i
= 0; i
< max
* 2; i
++) {
257 promises
.push(pool
.execute({ test
: 'test' }))
259 await Promise
.all(promises
)
260 // We need to clean up the resources after our test
264 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
267 const pool
= new DynamicThreadPool(
270 './tests/worker-files/thread/testWorker.js',
271 { workerChoiceStrategy
: WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
}
273 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
275 for (let i
= 0; i
< max
* 2; i
++) {
276 promises
.push(pool
.execute({ test
: 'test' }))
278 await Promise
.all(promises
)
279 // We need to clean up the resources after our test
283 it('Verify unknown strategies throw error', () => {
288 new DynamicThreadPool(
291 './tests/worker-files/thread/testWorker.js',
292 { workerChoiceStrategy
: 'UNKNOWN_STRATEGY' }
295 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")