Commit | Line | Data |
---|---|---|
40ad1d27 JB |
1 | const { expect } = require('expect') |
2 | const sinon = require('sinon') | |
3 | const { | |
4 | FixedThreadPool, | |
5 | DynamicThreadPool, | |
6 | WorkerChoiceStrategies | |
cdace0e5 | 7 | } = require('../../../lib') |
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 | ) | |
d710242d | 72 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
95c83464 JB |
73 | WorkerChoiceStrategies.ROUND_ROBIN |
74 | ) | |
75 | workerChoiceStrategyContext.workerChoiceStrategies.set( | |
d710242d | 76 | workerChoiceStrategyContext.workerChoiceStrategy, |
95c83464 JB |
77 | WorkerChoiceStrategyStub |
78 | ) | |
c923ce56 | 79 | const chosenWorkerKey = workerChoiceStrategyContext.execute() |
40ad1d27 | 80 | expect( |
95c83464 | 81 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
d710242d | 82 | workerChoiceStrategyContext.workerChoiceStrategy |
95c83464 | 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 | ) | |
d710242d | 98 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
95c83464 JB |
99 | WorkerChoiceStrategies.ROUND_ROBIN |
100 | ) | |
101 | workerChoiceStrategyContext.workerChoiceStrategies.set( | |
d710242d | 102 | workerChoiceStrategyContext.workerChoiceStrategy, |
95c83464 JB |
103 | WorkerChoiceStrategyStub |
104 | ) | |
c923ce56 | 105 | const chosenWorkerKey = workerChoiceStrategyContext.execute() |
40ad1d27 | 106 | expect( |
95c83464 | 107 | workerChoiceStrategyContext.workerChoiceStrategies.get( |
d710242d | 108 | workerChoiceStrategyContext.workerChoiceStrategy |
95c83464 | 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', () => { | |
594bfb84 | 115 | const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN |
40ad1d27 JB |
116 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
117 | fixedPool | |
118 | ) | |
95c83464 JB |
119 | expect( |
120 | workerChoiceStrategyContext.workerChoiceStrategies.get( | |
594bfb84 | 121 | workerChoiceStrategy |
95c83464 JB |
122 | ) |
123 | ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) | |
d710242d | 124 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
594bfb84 | 125 | workerChoiceStrategy |
40ad1d27 | 126 | ) |
594bfb84 | 127 | workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) |
95c83464 JB |
128 | expect( |
129 | workerChoiceStrategyContext.workerChoiceStrategies.get( | |
594bfb84 | 130 | workerChoiceStrategy |
95c83464 JB |
131 | ) |
132 | ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) | |
d710242d | 133 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
594bfb84 | 134 | workerChoiceStrategy |
b2b1d84e | 135 | ) |
40ad1d27 JB |
136 | }) |
137 | ||
23ff945a | 138 | it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => { |
594bfb84 | 139 | const workerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN |
40ad1d27 JB |
140 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
141 | dynamicPool | |
142 | ) | |
95c83464 JB |
143 | expect( |
144 | workerChoiceStrategyContext.workerChoiceStrategies.get( | |
594bfb84 | 145 | workerChoiceStrategy |
95c83464 JB |
146 | ) |
147 | ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) | |
d710242d | 148 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
594bfb84 | 149 | workerChoiceStrategy |
40ad1d27 | 150 | ) |
594bfb84 | 151 | workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) |
95c83464 JB |
152 | expect( |
153 | workerChoiceStrategyContext.workerChoiceStrategies.get( | |
594bfb84 | 154 | workerChoiceStrategy |
95c83464 JB |
155 | ) |
156 | ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) | |
d710242d | 157 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
594bfb84 | 158 | workerChoiceStrategy |
b2b1d84e | 159 | ) |
40ad1d27 JB |
160 | }) |
161 | ||
737c6d97 | 162 | it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => { |
594bfb84 | 163 | const workerChoiceStrategy = WorkerChoiceStrategies.LESS_USED |
40ad1d27 JB |
164 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
165 | fixedPool | |
166 | ) | |
594bfb84 | 167 | workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) |
95c83464 JB |
168 | expect( |
169 | workerChoiceStrategyContext.workerChoiceStrategies.get( | |
594bfb84 | 170 | workerChoiceStrategy |
95c83464 JB |
171 | ) |
172 | ).toBeInstanceOf(LessUsedWorkerChoiceStrategy) | |
d710242d | 173 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
594bfb84 | 174 | workerChoiceStrategy |
b2b1d84e | 175 | ) |
40ad1d27 JB |
176 | }) |
177 | ||
737c6d97 | 178 | it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => { |
594bfb84 | 179 | const workerChoiceStrategy = WorkerChoiceStrategies.LESS_USED |
40ad1d27 JB |
180 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
181 | dynamicPool | |
182 | ) | |
594bfb84 | 183 | workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) |
95c83464 JB |
184 | expect( |
185 | workerChoiceStrategyContext.workerChoiceStrategies.get( | |
594bfb84 | 186 | workerChoiceStrategy |
95c83464 JB |
187 | ) |
188 | ).toBeInstanceOf(LessUsedWorkerChoiceStrategy) | |
d710242d | 189 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
594bfb84 | 190 | workerChoiceStrategy |
b2b1d84e | 191 | ) |
168c526f JB |
192 | }) |
193 | ||
194 | it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => { | |
594bfb84 | 195 | const workerChoiceStrategy = WorkerChoiceStrategies.LESS_BUSY |
168c526f JB |
196 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
197 | fixedPool | |
198 | ) | |
594bfb84 | 199 | workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) |
95c83464 JB |
200 | expect( |
201 | workerChoiceStrategyContext.workerChoiceStrategies.get( | |
594bfb84 | 202 | workerChoiceStrategy |
95c83464 JB |
203 | ) |
204 | ).toBeInstanceOf(LessBusyWorkerChoiceStrategy) | |
d710242d | 205 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
594bfb84 | 206 | workerChoiceStrategy |
b2b1d84e | 207 | ) |
168c526f JB |
208 | }) |
209 | ||
210 | it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => { | |
594bfb84 | 211 | const workerChoiceStrategy = WorkerChoiceStrategies.LESS_BUSY |
168c526f JB |
212 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
213 | dynamicPool | |
214 | ) | |
594bfb84 | 215 | workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) |
95c83464 JB |
216 | expect( |
217 | workerChoiceStrategyContext.workerChoiceStrategies.get( | |
594bfb84 | 218 | workerChoiceStrategy |
95c83464 JB |
219 | ) |
220 | ).toBeInstanceOf(LessBusyWorkerChoiceStrategy) | |
d710242d | 221 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
594bfb84 | 222 | workerChoiceStrategy |
b2b1d84e | 223 | ) |
40ad1d27 | 224 | }) |
23ff945a JB |
225 | |
226 | it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => { | |
594bfb84 | 227 | const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE |
23ff945a JB |
228 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
229 | fixedPool | |
230 | ) | |
594bfb84 | 231 | workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) |
95c83464 JB |
232 | expect( |
233 | workerChoiceStrategyContext.workerChoiceStrategies.get( | |
594bfb84 | 234 | workerChoiceStrategy |
95c83464 JB |
235 | ) |
236 | ).toBeInstanceOf(FairShareWorkerChoiceStrategy) | |
d710242d | 237 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
594bfb84 | 238 | workerChoiceStrategy |
b2b1d84e | 239 | ) |
23ff945a JB |
240 | }) |
241 | ||
242 | it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => { | |
594bfb84 | 243 | const workerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE |
23ff945a JB |
244 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
245 | dynamicPool | |
246 | ) | |
594bfb84 | 247 | workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) |
95c83464 JB |
248 | expect( |
249 | workerChoiceStrategyContext.workerChoiceStrategies.get( | |
594bfb84 | 250 | workerChoiceStrategy |
95c83464 JB |
251 | ) |
252 | ).toBeInstanceOf(FairShareWorkerChoiceStrategy) | |
d710242d | 253 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
594bfb84 | 254 | workerChoiceStrategy |
b2b1d84e | 255 | ) |
23ff945a JB |
256 | }) |
257 | ||
c15273f2 | 258 | it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => { |
594bfb84 | 259 | const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
c15273f2 JB |
260 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
261 | fixedPool | |
262 | ) | |
594bfb84 | 263 | workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) |
95c83464 JB |
264 | expect( |
265 | workerChoiceStrategyContext.workerChoiceStrategies.get( | |
594bfb84 | 266 | workerChoiceStrategy |
95c83464 JB |
267 | ) |
268 | ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) | |
d710242d | 269 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
594bfb84 | 270 | workerChoiceStrategy |
b2b1d84e | 271 | ) |
c15273f2 | 272 | }) |
23ff945a | 273 | |
c15273f2 | 274 | it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => { |
594bfb84 | 275 | const workerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
c15273f2 JB |
276 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
277 | dynamicPool | |
278 | ) | |
594bfb84 | 279 | workerChoiceStrategyContext.setWorkerChoiceStrategy(workerChoiceStrategy) |
95c83464 JB |
280 | expect( |
281 | workerChoiceStrategyContext.workerChoiceStrategies.get( | |
594bfb84 | 282 | workerChoiceStrategy |
95c83464 JB |
283 | ) |
284 | ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) | |
d710242d | 285 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBe( |
594bfb84 | 286 | workerChoiceStrategy |
b2b1d84e | 287 | ) |
c15273f2 | 288 | }) |
2fc5cae3 | 289 | |
73491b69 | 290 | it('Verify that worker choice strategy options enable median run time pool statistics', () => { |
2fc5cae3 JB |
291 | const wwrWorkerChoiceStrategy = WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN |
292 | let workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
293 | fixedPool, | |
294 | wwrWorkerChoiceStrategy, | |
295 | { | |
296 | medRunTime: true | |
297 | } | |
298 | ) | |
299 | expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe( | |
300 | false | |
301 | ) | |
302 | expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe( | |
303 | true | |
304 | ) | |
305 | workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
306 | dynamicPool, | |
307 | wwrWorkerChoiceStrategy, | |
308 | { | |
309 | medRunTime: true | |
310 | } | |
311 | ) | |
312 | expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe( | |
313 | false | |
314 | ) | |
315 | expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe( | |
316 | true | |
317 | ) | |
318 | const fsWorkerChoiceStrategy = WorkerChoiceStrategies.FAIR_SHARE | |
319 | workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
320 | fixedPool, | |
321 | fsWorkerChoiceStrategy, | |
322 | { | |
323 | medRunTime: true | |
324 | } | |
325 | ) | |
326 | expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe( | |
327 | false | |
328 | ) | |
329 | expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe( | |
330 | true | |
331 | ) | |
332 | workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
333 | dynamicPool, | |
334 | fsWorkerChoiceStrategy, | |
335 | { | |
336 | medRunTime: true | |
337 | } | |
338 | ) | |
339 | expect(workerChoiceStrategyContext.getRequiredStatistics().avgRunTime).toBe( | |
340 | false | |
341 | ) | |
342 | expect(workerChoiceStrategyContext.getRequiredStatistics().medRunTime).toBe( | |
343 | true | |
344 | ) | |
345 | }) | |
40ad1d27 | 346 | }) |