1 const { expect
} = require('expect')
2 const sinon
= require('sinon')
7 } = require('../../../lib/index')
9 WorkerChoiceStrategyContext
10 } = require('../../../lib/pools/selection-strategies/worker-choice-strategy-context')
12 RoundRobinWorkerChoiceStrategy
13 } = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy')
15 LessUsedWorkerChoiceStrategy
16 } = require('../../../lib/pools/selection-strategies/less-used-worker-choice-strategy')
18 LessBusyWorkerChoiceStrategy
19 } = require('../../../lib/pools/selection-strategies/less-busy-worker-choice-strategy')
21 FairShareWorkerChoiceStrategy
22 } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy')
24 WeightedRoundRobinWorkerChoiceStrategy
25 } = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy')
27 describe('Worker choice strategy context test suite', () => {
30 let fixedPool
, dynamicPool
33 fixedPool
= new FixedThreadPool(
35 './tests/worker-files/thread/testWorker.js'
37 dynamicPool
= new DynamicThreadPool(
40 './tests/worker-files/thread/testWorker.js'
49 await fixedPool
.destroy()
50 await dynamicPool
.destroy()
53 it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
54 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
57 const WorkerChoiceStrategyStub
= sinon
.createStubInstance(
58 RoundRobinWorkerChoiceStrategy
,
60 choose
: sinon
.stub().returns(0)
63 workerChoiceStrategyContext
.workerChoiceStrategy
= WorkerChoiceStrategyStub
64 const chosenWorkerKey
= workerChoiceStrategyContext
.execute()
66 workerChoiceStrategyContext
.workerChoiceStrategy
.choose
.calledOnce
68 expect(chosenWorkerKey
).toBe(0)
71 it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
72 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
75 const WorkerChoiceStrategyStub
= sinon
.createStubInstance(
76 RoundRobinWorkerChoiceStrategy
,
78 choose
: sinon
.stub().returns(0)
81 workerChoiceStrategyContext
.workerChoiceStrategy
= WorkerChoiceStrategyStub
82 const chosenWorkerKey
= workerChoiceStrategyContext
.execute()
84 workerChoiceStrategyContext
.workerChoiceStrategy
.choose
.calledOnce
86 expect(chosenWorkerKey
).toBe(0)
89 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
90 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
93 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
94 RoundRobinWorkerChoiceStrategy
96 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
97 WorkerChoiceStrategies
.ROUND_ROBIN
99 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
101 WorkerChoiceStrategies
.ROUND_ROBIN
103 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
104 RoundRobinWorkerChoiceStrategy
106 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
107 WorkerChoiceStrategies
.ROUND_ROBIN
111 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
112 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
115 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
116 RoundRobinWorkerChoiceStrategy
118 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
119 WorkerChoiceStrategies
.ROUND_ROBIN
121 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
123 WorkerChoiceStrategies
.ROUND_ROBIN
125 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
126 RoundRobinWorkerChoiceStrategy
128 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
129 WorkerChoiceStrategies
.ROUND_ROBIN
133 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
134 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
137 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
139 WorkerChoiceStrategies
.LESS_USED
141 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
142 LessUsedWorkerChoiceStrategy
144 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
145 WorkerChoiceStrategies
.LESS_USED
149 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
150 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
153 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
155 WorkerChoiceStrategies
.LESS_USED
157 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
158 LessUsedWorkerChoiceStrategy
160 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
161 WorkerChoiceStrategies
.LESS_USED
165 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => {
166 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
169 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
171 WorkerChoiceStrategies
.LESS_BUSY
173 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
174 LessBusyWorkerChoiceStrategy
176 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
177 WorkerChoiceStrategies
.LESS_BUSY
181 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => {
182 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
185 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
187 WorkerChoiceStrategies
.LESS_BUSY
189 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
190 LessBusyWorkerChoiceStrategy
192 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
193 WorkerChoiceStrategies
.LESS_BUSY
197 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
198 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
201 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
203 WorkerChoiceStrategies
.FAIR_SHARE
205 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
206 FairShareWorkerChoiceStrategy
208 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
209 WorkerChoiceStrategies
.FAIR_SHARE
213 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
214 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
217 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
219 WorkerChoiceStrategies
.FAIR_SHARE
221 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
222 FairShareWorkerChoiceStrategy
224 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
225 WorkerChoiceStrategies
.FAIR_SHARE
229 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
230 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
233 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
235 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
237 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
238 WeightedRoundRobinWorkerChoiceStrategy
240 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
241 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
245 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
246 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
249 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
251 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
253 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
254 WeightedRoundRobinWorkerChoiceStrategy
256 expect(workerChoiceStrategyContext
.workerChoiceStrategyType
).toBe(
257 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
261 it('Verify that getWorkerChoiceStrategy() default return ROUND_ROBIN strategy', () => {
262 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
266 workerChoiceStrategyContext
.getWorkerChoiceStrategy(fixedPool
)
267 expect(strategy
).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
270 it('Verify that getWorkerChoiceStrategy() can return ROUND_ROBIN strategy', () => {
271 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
274 const strategy
= workerChoiceStrategyContext
.getWorkerChoiceStrategy(
276 WorkerChoiceStrategies
.ROUND_ROBIN
278 expect(strategy
).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
281 it('Verify that getWorkerChoiceStrategy() can return LESS_USED strategy', () => {
282 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
285 const strategy
= workerChoiceStrategyContext
.getWorkerChoiceStrategy(
287 WorkerChoiceStrategies
.LESS_USED
289 expect(strategy
).toBeInstanceOf(LessUsedWorkerChoiceStrategy
)
292 it('Verify that getWorkerChoiceStrategy() can return LESS_BUSY strategy', () => {
293 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
296 const strategy
= workerChoiceStrategyContext
.getWorkerChoiceStrategy(
298 WorkerChoiceStrategies
.LESS_BUSY
300 expect(strategy
).toBeInstanceOf(LessBusyWorkerChoiceStrategy
)
303 it('Verify that getWorkerChoiceStrategy() can return FAIR_SHARE strategy', () => {
304 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
307 const strategy
= workerChoiceStrategyContext
.getWorkerChoiceStrategy(
309 WorkerChoiceStrategies
.FAIR_SHARE
311 expect(strategy
).toBeInstanceOf(FairShareWorkerChoiceStrategy
)
314 it('Verify that getWorkerChoiceStrategy() can return WEIGHTED_ROUND_ROBIN strategy', () => {
315 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
318 const strategy
= workerChoiceStrategyContext
.getWorkerChoiceStrategy(
320 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
322 expect(strategy
).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy
)
325 it('Verify that getWorkerChoiceStrategy() throw error on unknown strategy', () => {
326 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
330 workerChoiceStrategyContext
.getWorkerChoiceStrategy(
335 new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found")