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 DynamicPoolWorkerChoiceStrategy
28 } = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy')
30 describe('Worker choice strategy context test suite', () => {
33 let fixedPool
, dynamicPool
36 fixedPool
= new FixedThreadPool(
38 './tests/worker-files/thread/testWorker.js'
40 dynamicPool
= new DynamicThreadPool(
43 './tests/worker-files/thread/testWorker.js'
52 await fixedPool
.destroy()
53 await dynamicPool
.destroy()
56 it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
57 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
60 const WorkerChoiceStrategyStub
= sinon
.createStubInstance(
61 RoundRobinWorkerChoiceStrategy
,
63 choose
: sinon
.stub().returns(0)
66 workerChoiceStrategyContext
.workerChoiceStrategy
= WorkerChoiceStrategyStub
67 const chosenWorkerKey
= workerChoiceStrategyContext
.execute()
69 workerChoiceStrategyContext
.workerChoiceStrategy
.choose
.calledOnce
71 expect(chosenWorkerKey
).toBe(0)
74 it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => {
75 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
78 const WorkerChoiceStrategyStub
= sinon
.createStubInstance(
79 RoundRobinWorkerChoiceStrategy
,
81 choose
: sinon
.stub().returns(0)
84 workerChoiceStrategyContext
.workerChoiceStrategy
= WorkerChoiceStrategyStub
85 const chosenWorkerKey
= workerChoiceStrategyContext
.execute()
87 workerChoiceStrategyContext
.workerChoiceStrategy
.choose
.calledOnce
89 expect(chosenWorkerKey
).toBe(0)
92 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
93 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
96 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
97 WorkerChoiceStrategies
.ROUND_ROBIN
99 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
100 RoundRobinWorkerChoiceStrategy
104 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
105 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
108 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
109 WorkerChoiceStrategies
.ROUND_ROBIN
111 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
112 DynamicPoolWorkerChoiceStrategy
115 workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
116 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
119 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
120 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
123 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
124 WorkerChoiceStrategies
.LESS_USED
126 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
127 LessUsedWorkerChoiceStrategy
131 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
132 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
135 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
136 WorkerChoiceStrategies
.LESS_USED
138 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
139 DynamicPoolWorkerChoiceStrategy
142 workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
143 ).toBeInstanceOf(LessUsedWorkerChoiceStrategy
)
146 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => {
147 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
150 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
151 WorkerChoiceStrategies
.LESS_BUSY
153 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
154 LessBusyWorkerChoiceStrategy
158 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => {
159 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
162 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
163 WorkerChoiceStrategies
.LESS_BUSY
165 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
166 DynamicPoolWorkerChoiceStrategy
169 workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
170 ).toBeInstanceOf(LessBusyWorkerChoiceStrategy
)
173 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
174 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
177 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
178 WorkerChoiceStrategies
.FAIR_SHARE
180 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
181 FairShareWorkerChoiceStrategy
185 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
186 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
189 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
190 WorkerChoiceStrategies
.FAIR_SHARE
192 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
193 DynamicPoolWorkerChoiceStrategy
196 workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
197 ).toBeInstanceOf(FairShareWorkerChoiceStrategy
)
200 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
201 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
204 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
205 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
207 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
208 WeightedRoundRobinWorkerChoiceStrategy
212 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
213 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
216 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
217 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
219 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
220 DynamicPoolWorkerChoiceStrategy
223 workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
224 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy
)