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