Add fair sharing worker choice strategy
[poolifier.git] / tests / pools / selection-strategies / selection-strategies.test.js
CommitLineData
a61a0724 1const { expect } = require('expect')
a35560ba
S
2const {
3 WorkerChoiceStrategies,
4 DynamicThreadPool,
5 FixedThreadPool
15d56315 6} = require('../../../lib/index')
a35560ba
S
7
8describe('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')
23ff945a 12 expect(WorkerChoiceStrategies.FAIR_SHARE).toBe('FAIR_SHARE')
b3432a63
JB
13 expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe(
14 'WEIGHTED_ROUND_ROBIN'
15 )
a35560ba
S
16 })
17
e843b904
JB
18 it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
19 const min = 0
20 const max = 3
21 const pool = new DynamicThreadPool(
22 min,
23 max,
24 './tests/worker-files/thread/testWorker.js'
25 )
26 expect(pool.opts.workerChoiceStrategy).toBe(
27 WorkerChoiceStrategies.ROUND_ROBIN
28 )
29 // We need to clean up the resources after our test
30 await pool.destroy()
31 })
32
33 it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
34 const min = 0
35 const max = 3
36 const pool = new DynamicThreadPool(
37 min,
38 max,
39 './tests/worker-files/thread/testWorker.js'
40 )
41 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN)
42 expect(pool.opts.workerChoiceStrategy).toBe(
43 WorkerChoiceStrategies.ROUND_ROBIN
44 )
45 // We need to clean up the resources after our test
46 await pool.destroy()
47 })
48
bdaf31cd
JB
49 it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
50 const max = 3
51 const pool = new FixedThreadPool(
52 max,
53 './tests/worker-files/thread/testWorker.js',
54 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
55 )
56 expect(pool.opts.workerChoiceStrategy).toBe(
57 WorkerChoiceStrategies.ROUND_ROBIN
58 )
59 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
60 const promises = []
61 for (let i = 0; i < max * 2; i++) {
62 promises.push(pool.execute({ test: 'test' }))
63 }
64 await Promise.all(promises)
65 // We need to clean up the resources after our test
66 await pool.destroy()
67 })
68
69 it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
70 const min = 0
71 const max = 3
72 const pool = new DynamicThreadPool(
73 min,
74 max,
75 './tests/worker-files/thread/testWorker.js',
76 { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN }
77 )
78 expect(pool.opts.workerChoiceStrategy).toBe(
79 WorkerChoiceStrategies.ROUND_ROBIN
80 )
81 // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
82 const promises = []
83 for (let i = 0; i < max * 2; i++) {
84 promises.push(pool.execute({ test: 'test' }))
85 }
86 await Promise.all(promises)
87 // We need to clean up the resources after our test
88 await pool.destroy()
89 })
90
b98ec2e6 91 it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
a35560ba
S
92 const max = 3
93 const pool = new FixedThreadPool(
94 max,
95 './tests/worker-files/thread/testWorker.js',
96 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
97 )
b98ec2e6
JB
98 expect(pool.opts.workerChoiceStrategy).toBe(
99 WorkerChoiceStrategies.LESS_RECENTLY_USED
100 )
101 // We need to clean up the resources after our test
102 await pool.destroy()
103 })
a35560ba 104
b98ec2e6
JB
105 it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
106 const max = 3
107 const pool = new FixedThreadPool(
108 max,
109 './tests/worker-files/thread/testWorker.js'
110 )
111 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED)
a35560ba
S
112 expect(pool.opts.workerChoiceStrategy).toBe(
113 WorkerChoiceStrategies.LESS_RECENTLY_USED
114 )
b98ec2e6
JB
115 // We need to clean up the resources after our test
116 await pool.destroy()
117 })
a35560ba 118
ff5e76e1 119 it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
b98ec2e6
JB
120 const max = 3
121 const pool = new FixedThreadPool(
122 max,
123 './tests/worker-files/thread/testWorker.js',
124 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
125 )
a35560ba
S
126 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
127 const promises = []
128 for (let i = 0; i < max * 2; i++) {
129 promises.push(pool.execute({ test: 'test' }))
130 }
131 await Promise.all(promises)
a35560ba
S
132 // We need to clean up the resources after our test
133 await pool.destroy()
134 })
135
ff5e76e1
JB
136 it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
137 const min = 0
138 const max = 3
139 const pool = new DynamicThreadPool(
140 min,
141 max,
142 './tests/worker-files/thread/testWorker.js',
143 { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED }
144 )
145 // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
146 const promises = []
147 for (let i = 0; i < max * 2; i++) {
148 promises.push(pool.execute({ test: 'test' }))
149 }
150 await Promise.all(promises)
ff5e76e1
JB
151 // We need to clean up the resources after our test
152 await pool.destroy()
153 })
154
23ff945a
JB
155 it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
156 const max = 3
157 const pool = new FixedThreadPool(
158 max,
159 './tests/worker-files/thread/testWorker.js',
160 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
161 )
162 expect(pool.opts.workerChoiceStrategy).toBe(
163 WorkerChoiceStrategies.FAIR_SHARE
164 )
165 // We need to clean up the resources after our test
166 await pool.destroy()
167 })
168
169 it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
170 const max = 3
171 const pool = new FixedThreadPool(
172 max,
173 './tests/worker-files/thread/testWorker.js'
174 )
175 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.FAIR_SHARE)
176 expect(pool.opts.workerChoiceStrategy).toBe(
177 WorkerChoiceStrategies.FAIR_SHARE
178 )
179 // We need to clean up the resources after our test
180 await pool.destroy()
181 })
182
183 it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
184 const max = 3
185 const pool = new FixedThreadPool(
186 max,
187 './tests/worker-files/thread/testWorker.js',
188 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
189 )
190 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
191 const promises = []
192 for (let i = 0; i < max * 2; i++) {
193 promises.push(pool.execute({ test: 'test' }))
194 }
195 await Promise.all(promises)
196 // We need to clean up the resources after our test
197 await pool.destroy()
198 })
199
200 it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
201 const min = 0
202 const max = 3
203 const pool = new DynamicThreadPool(
204 min,
205 max,
206 './tests/worker-files/thread/testWorker.js',
207 { workerChoiceStrategy: WorkerChoiceStrategies.FAIR_SHARE }
208 )
209 // TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
210 const promises = []
211 for (let i = 0; i < max * 2; i++) {
212 promises.push(pool.execute({ test: 'test' }))
213 }
214 await Promise.all(promises)
215 // We need to clean up the resources after our test
216 await pool.destroy()
217 })
218
b3432a63
JB
219 it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
220 const max = 3
221 const pool = new FixedThreadPool(
222 max,
223 './tests/worker-files/thread/testWorker.js',
224 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
225 )
226 expect(pool.opts.workerChoiceStrategy).toBe(
227 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
228 )
229 // We need to clean up the resources after our test
230 await pool.destroy()
231 })
232
233 it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
234 const max = 3
235 const pool = new FixedThreadPool(
236 max,
237 './tests/worker-files/thread/testWorker.js'
238 )
239 pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN)
240 expect(pool.opts.workerChoiceStrategy).toBe(
241 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
242 )
243 // We need to clean up the resources after our test
244 await pool.destroy()
245 })
246
247 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
248 const max = 3
249 const pool = new FixedThreadPool(
250 max,
251 './tests/worker-files/thread/testWorker.js',
252 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
253 )
254 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
255 const promises = []
256 for (let i = 0; i < max * 2; i++) {
257 promises.push(pool.execute({ test: 'test' }))
258 }
259 await Promise.all(promises)
260 // We need to clean up the resources after our test
261 await pool.destroy()
262 })
263
264 it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
265 const min = 0
266 const max = 3
267 const pool = new DynamicThreadPool(
268 min,
269 max,
270 './tests/worker-files/thread/testWorker.js',
271 { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN }
272 )
273 // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
274 const promises = []
275 for (let i = 0; i < max * 2; i++) {
276 promises.push(pool.execute({ test: 'test' }))
277 }
278 await Promise.all(promises)
279 // We need to clean up the resources after our test
280 await pool.destroy()
281 })
282
a35560ba
S
283 it('Verify unknown strategies throw error', () => {
284 const min = 1
285 const max = 3
286 expect(
287 () =>
288 new DynamicThreadPool(
289 min,
290 max,
291 './tests/worker-files/thread/testWorker.js',
1927ee67 292 { workerChoiceStrategy: 'UNKNOWN_STRATEGY' }
a35560ba
S
293 )
294 ).toThrowError(
295 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
296 )
297 })
298})