0f02c363c0b2e3a90a5234faa2f4be4669b5959c
[poolifier.git] / tests / pools / selection-strategies / worker-choice-strategy-context.test.js
1 const { expect } = require('expect')
2 const sinon = require('sinon')
3 const {
4 FixedThreadPool,
5 DynamicThreadPool,
6 WorkerChoiceStrategies
7 } = require('../../../lib/index')
8 const {
9 WorkerChoiceStrategyContext
10 } = require('../../../lib/pools/selection-strategies/worker-choice-strategy-context')
11 const {
12 RoundRobinWorkerChoiceStrategy
13 } = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy')
14 const {
15 LessUsedWorkerChoiceStrategy
16 } = require('../../../lib/pools/selection-strategies/less-used-worker-choice-strategy')
17 const {
18 LessBusyWorkerChoiceStrategy
19 } = require('../../../lib/pools/selection-strategies/less-busy-worker-choice-strategy')
20 const {
21 FairShareWorkerChoiceStrategy
22 } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy')
23 const {
24 WeightedRoundRobinWorkerChoiceStrategy
25 } = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy')
26
27 describe('Worker choice strategy context test suite', () => {
28 const min = 1
29 const max = 3
30 let fixedPool, dynamicPool
31
32 before(() => {
33 fixedPool = new FixedThreadPool(
34 max,
35 './tests/worker-files/thread/testWorker.js'
36 )
37 dynamicPool = new DynamicThreadPool(
38 min,
39 max,
40 './tests/worker-files/thread/testWorker.js'
41 )
42 })
43
44 afterEach(() => {
45 sinon.restore()
46 })
47
48 after(async () => {
49 await fixedPool.destroy()
50 await dynamicPool.destroy()
51 })
52
53 it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
54 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
55 fixedPool
56 )
57 const WorkerChoiceStrategyStub = sinon.createStubInstance(
58 RoundRobinWorkerChoiceStrategy,
59 {
60 choose: sinon.stub().returns(0)
61 }
62 )
63 expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
64 WorkerChoiceStrategies.ROUND_ROBIN
65 )
66 workerChoiceStrategyContext.workerChoiceStrategies.set(
67 workerChoiceStrategyContext.workerChoiceStrategyType,
68 WorkerChoiceStrategyStub
69 )
70 const chosenWorkerKey = workerChoiceStrategyContext.execute()
71 expect(
72 workerChoiceStrategyContext.workerChoiceStrategies.get(
73 workerChoiceStrategyContext.workerChoiceStrategyType
74 ).choose.calledOnce
75 ).toBe(true)
76 expect(chosenWorkerKey).toBe(0)
77 })
78
79 it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
80 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
81 dynamicPool
82 )
83 const WorkerChoiceStrategyStub = sinon.createStubInstance(
84 RoundRobinWorkerChoiceStrategy,
85 {
86 choose: sinon.stub().returns(0)
87 }
88 )
89 expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
90 WorkerChoiceStrategies.ROUND_ROBIN
91 )
92 workerChoiceStrategyContext.workerChoiceStrategies.set(
93 workerChoiceStrategyContext.workerChoiceStrategyType,
94 WorkerChoiceStrategyStub
95 )
96 const chosenWorkerKey = workerChoiceStrategyContext.execute()
97 expect(
98 workerChoiceStrategyContext.workerChoiceStrategies.get(
99 workerChoiceStrategyContext.workerChoiceStrategyType
100 ).choose.calledOnce
101 ).toBe(true)
102 expect(chosenWorkerKey).toBe(0)
103 })
104
105 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
106 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
107 fixedPool
108 )
109 expect(
110 workerChoiceStrategyContext.workerChoiceStrategies.get(
111 WorkerChoiceStrategies.ROUND_ROBIN
112 )
113 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
114 expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
115 WorkerChoiceStrategies.ROUND_ROBIN
116 )
117 workerChoiceStrategyContext.setWorkerChoiceStrategy(
118 fixedPool,
119 WorkerChoiceStrategies.ROUND_ROBIN
120 )
121 expect(
122 workerChoiceStrategyContext.workerChoiceStrategies.get(
123 WorkerChoiceStrategies.ROUND_ROBIN
124 )
125 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
126 expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
127 WorkerChoiceStrategies.ROUND_ROBIN
128 )
129 })
130
131 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
132 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
133 dynamicPool
134 )
135 expect(
136 workerChoiceStrategyContext.workerChoiceStrategies.get(
137 WorkerChoiceStrategies.ROUND_ROBIN
138 )
139 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
140 expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
141 WorkerChoiceStrategies.ROUND_ROBIN
142 )
143 workerChoiceStrategyContext.setWorkerChoiceStrategy(
144 dynamicPool,
145 WorkerChoiceStrategies.ROUND_ROBIN
146 )
147 expect(
148 workerChoiceStrategyContext.workerChoiceStrategies.get(
149 WorkerChoiceStrategies.ROUND_ROBIN
150 )
151 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
152 expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
153 WorkerChoiceStrategies.ROUND_ROBIN
154 )
155 })
156
157 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
158 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
159 fixedPool
160 )
161 workerChoiceStrategyContext.setWorkerChoiceStrategy(
162 fixedPool,
163 WorkerChoiceStrategies.LESS_USED
164 )
165 expect(
166 workerChoiceStrategyContext.workerChoiceStrategies.get(
167 WorkerChoiceStrategies.LESS_USED
168 )
169 ).toBeInstanceOf(LessUsedWorkerChoiceStrategy)
170 expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
171 WorkerChoiceStrategies.LESS_USED
172 )
173 })
174
175 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
176 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
177 dynamicPool
178 )
179 workerChoiceStrategyContext.setWorkerChoiceStrategy(
180 dynamicPool,
181 WorkerChoiceStrategies.LESS_USED
182 )
183 expect(
184 workerChoiceStrategyContext.workerChoiceStrategies.get(
185 WorkerChoiceStrategies.LESS_USED
186 )
187 ).toBeInstanceOf(LessUsedWorkerChoiceStrategy)
188 expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
189 WorkerChoiceStrategies.LESS_USED
190 )
191 })
192
193 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => {
194 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
195 fixedPool
196 )
197 workerChoiceStrategyContext.setWorkerChoiceStrategy(
198 fixedPool,
199 WorkerChoiceStrategies.LESS_BUSY
200 )
201 expect(
202 workerChoiceStrategyContext.workerChoiceStrategies.get(
203 WorkerChoiceStrategies.LESS_BUSY
204 )
205 ).toBeInstanceOf(LessBusyWorkerChoiceStrategy)
206 expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
207 WorkerChoiceStrategies.LESS_BUSY
208 )
209 })
210
211 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => {
212 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
213 dynamicPool
214 )
215 workerChoiceStrategyContext.setWorkerChoiceStrategy(
216 dynamicPool,
217 WorkerChoiceStrategies.LESS_BUSY
218 )
219 expect(
220 workerChoiceStrategyContext.workerChoiceStrategies.get(
221 WorkerChoiceStrategies.LESS_BUSY
222 )
223 ).toBeInstanceOf(LessBusyWorkerChoiceStrategy)
224 expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
225 WorkerChoiceStrategies.LESS_BUSY
226 )
227 })
228
229 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
230 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
231 fixedPool
232 )
233 workerChoiceStrategyContext.setWorkerChoiceStrategy(
234 fixedPool,
235 WorkerChoiceStrategies.FAIR_SHARE
236 )
237 expect(
238 workerChoiceStrategyContext.workerChoiceStrategies.get(
239 WorkerChoiceStrategies.FAIR_SHARE
240 )
241 ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
242 expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
243 WorkerChoiceStrategies.FAIR_SHARE
244 )
245 })
246
247 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
248 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
249 dynamicPool
250 )
251 workerChoiceStrategyContext.setWorkerChoiceStrategy(
252 dynamicPool,
253 WorkerChoiceStrategies.FAIR_SHARE
254 )
255 expect(
256 workerChoiceStrategyContext.workerChoiceStrategies.get(
257 WorkerChoiceStrategies.FAIR_SHARE
258 )
259 ).toBeInstanceOf(FairShareWorkerChoiceStrategy)
260 expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
261 WorkerChoiceStrategies.FAIR_SHARE
262 )
263 })
264
265 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
266 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
267 fixedPool
268 )
269 workerChoiceStrategyContext.setWorkerChoiceStrategy(
270 fixedPool,
271 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
272 )
273 expect(
274 workerChoiceStrategyContext.workerChoiceStrategies.get(
275 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
276 )
277 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
278 expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
279 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
280 )
281 })
282
283 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
284 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
285 dynamicPool
286 )
287 workerChoiceStrategyContext.setWorkerChoiceStrategy(
288 dynamicPool,
289 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
290 )
291 expect(
292 workerChoiceStrategyContext.workerChoiceStrategies.get(
293 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
294 )
295 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
296 expect(workerChoiceStrategyContext.workerChoiceStrategyType).toBe(
297 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
298 )
299 })
300
301 it('Verify that getWorkerChoiceStrategy() default return ROUND_ROBIN strategy', () => {
302 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
303 fixedPool
304 )
305 const strategy =
306 workerChoiceStrategyContext.getWorkerChoiceStrategy(fixedPool)
307 expect(strategy).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
308 })
309
310 it('Verify that getWorkerChoiceStrategy() can return ROUND_ROBIN strategy', () => {
311 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
312 fixedPool
313 )
314 const strategy = workerChoiceStrategyContext.getWorkerChoiceStrategy(
315 fixedPool,
316 WorkerChoiceStrategies.ROUND_ROBIN
317 )
318 expect(strategy).toBeInstanceOf(RoundRobinWorkerChoiceStrategy)
319 })
320
321 it('Verify that getWorkerChoiceStrategy() can return LESS_USED strategy', () => {
322 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
323 fixedPool
324 )
325 const strategy = workerChoiceStrategyContext.getWorkerChoiceStrategy(
326 fixedPool,
327 WorkerChoiceStrategies.LESS_USED
328 )
329 expect(strategy).toBeInstanceOf(LessUsedWorkerChoiceStrategy)
330 })
331
332 it('Verify that getWorkerChoiceStrategy() can return LESS_BUSY strategy', () => {
333 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
334 fixedPool
335 )
336 const strategy = workerChoiceStrategyContext.getWorkerChoiceStrategy(
337 fixedPool,
338 WorkerChoiceStrategies.LESS_BUSY
339 )
340 expect(strategy).toBeInstanceOf(LessBusyWorkerChoiceStrategy)
341 })
342
343 it('Verify that getWorkerChoiceStrategy() can return FAIR_SHARE strategy', () => {
344 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
345 fixedPool
346 )
347 const strategy = workerChoiceStrategyContext.getWorkerChoiceStrategy(
348 fixedPool,
349 WorkerChoiceStrategies.FAIR_SHARE
350 )
351 expect(strategy).toBeInstanceOf(FairShareWorkerChoiceStrategy)
352 })
353
354 it('Verify that getWorkerChoiceStrategy() can return WEIGHTED_ROUND_ROBIN strategy', () => {
355 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
356 fixedPool
357 )
358 const strategy = workerChoiceStrategyContext.getWorkerChoiceStrategy(
359 fixedPool,
360 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
361 )
362 expect(strategy).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy)
363 })
364
365 it('Verify that getWorkerChoiceStrategy() throw error on unknown strategy', () => {
366 const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
367 fixedPool
368 )
369 expect(() => {
370 workerChoiceStrategyContext.getWorkerChoiceStrategy(
371 fixedPool,
372 'UNKNOWN_STRATEGY'
373 )
374 }).toThrowError(
375 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")
376 )
377 })
378 })