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 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
95 WorkerChoiceStrategies
.ROUND_ROBIN
97 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
98 RoundRobinWorkerChoiceStrategy
102 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
103 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
106 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
108 WorkerChoiceStrategies
.ROUND_ROBIN
110 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
111 RoundRobinWorkerChoiceStrategy
115 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
116 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
119 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
121 WorkerChoiceStrategies
.LESS_USED
123 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
124 LessUsedWorkerChoiceStrategy
128 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
129 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
132 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
134 WorkerChoiceStrategies
.LESS_USED
136 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
137 LessUsedWorkerChoiceStrategy
141 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => {
142 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
145 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
147 WorkerChoiceStrategies
.LESS_BUSY
149 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
150 LessBusyWorkerChoiceStrategy
154 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => {
155 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
158 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
160 WorkerChoiceStrategies
.LESS_BUSY
162 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
163 LessBusyWorkerChoiceStrategy
167 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
168 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
171 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
173 WorkerChoiceStrategies
.FAIR_SHARE
175 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
176 FairShareWorkerChoiceStrategy
180 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
181 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
184 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
186 WorkerChoiceStrategies
.FAIR_SHARE
188 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
189 FairShareWorkerChoiceStrategy
193 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
194 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
197 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
199 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
201 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
202 WeightedRoundRobinWorkerChoiceStrategy
206 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
207 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
210 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
212 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
214 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
215 WeightedRoundRobinWorkerChoiceStrategy