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(
94 WorkerChoiceStrategies
.ROUND_ROBIN
96 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
97 RoundRobinWorkerChoiceStrategy
101 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
102 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
105 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
106 WorkerChoiceStrategies
.ROUND_ROBIN
108 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
109 RoundRobinWorkerChoiceStrategy
113 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => {
114 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
117 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
118 WorkerChoiceStrategies
.LESS_USED
120 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
121 LessUsedWorkerChoiceStrategy
125 it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => {
126 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
129 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
130 WorkerChoiceStrategies
.LESS_USED
132 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
133 LessUsedWorkerChoiceStrategy
137 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => {
138 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
141 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
142 WorkerChoiceStrategies
.LESS_BUSY
144 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
145 LessBusyWorkerChoiceStrategy
149 it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => {
150 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
153 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
154 WorkerChoiceStrategies
.LESS_BUSY
156 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
157 LessBusyWorkerChoiceStrategy
161 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
162 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
165 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
166 WorkerChoiceStrategies
.FAIR_SHARE
168 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
169 FairShareWorkerChoiceStrategy
173 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic 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 WEIGHTED_ROUND_ROBIN and fixed pool', () => {
186 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
189 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
190 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
192 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
193 WeightedRoundRobinWorkerChoiceStrategy
197 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
198 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
201 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
202 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
204 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
205 WeightedRoundRobinWorkerChoiceStrategy