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('worker')
66 workerChoiceStrategyContext
.workerChoiceStrategy
= WorkerChoiceStrategyStub
67 const chosenWorker
= workerChoiceStrategyContext
.execute()
69 workerChoiceStrategyContext
.getWorkerChoiceStrategy().choose
.calledOnce
71 expect(chosenWorker
).toBe('worker')
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('worker')
84 workerChoiceStrategyContext
.workerChoiceStrategy
= WorkerChoiceStrategyStub
85 const chosenWorker
= workerChoiceStrategyContext
.execute()
87 workerChoiceStrategyContext
.getWorkerChoiceStrategy().choose
.calledOnce
89 expect(chosenWorker
).toBe('worker')
92 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
93 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
96 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
97 WorkerChoiceStrategies
.ROUND_ROBIN
100 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
101 ).toBeInstanceOf(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
112 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
113 ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy
)
115 workerChoiceStrategyContext
.getWorkerChoiceStrategy().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
127 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
128 ).toBeInstanceOf(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
139 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
140 ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy
)
142 workerChoiceStrategyContext
.getWorkerChoiceStrategy().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
154 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
155 ).toBeInstanceOf(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
166 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
167 ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy
)
169 workerChoiceStrategyContext
.getWorkerChoiceStrategy().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
181 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
182 ).toBeInstanceOf(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
193 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
194 ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy
)
196 workerChoiceStrategyContext
.getWorkerChoiceStrategy().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
208 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
209 ).toBeInstanceOf(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
220 workerChoiceStrategyContext
.getWorkerChoiceStrategy()
221 ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy
)
223 workerChoiceStrategyContext
.getWorkerChoiceStrategy().workerChoiceStrategy
224 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy
)