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 LessRecentlyUsedWorkerChoiceStrategy
16 } = require('../../../lib/pools/selection-strategies/less-recently-used-worker-choice-strategy')
18 FairShareWorkerChoiceStrategy
19 } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy')
21 WeightedRoundRobinWorkerChoiceStrategy
22 } = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy')
24 DynamicPoolWorkerChoiceStrategy
25 } = require('../../../lib/pools/selection-strategies/dynamic-pool-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('worker')
63 workerChoiceStrategyContext
.workerChoiceStrategy
= WorkerChoiceStrategyStub
64 const worker
= workerChoiceStrategyContext
.execute()
66 workerChoiceStrategyContext
.workerChoiceStrategy
.choose
.calledOnce
68 expect(worker
).toBe('worker')
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('worker')
81 workerChoiceStrategyContext
.workerChoiceStrategy
= WorkerChoiceStrategyStub
82 const worker
= workerChoiceStrategyContext
.execute()
84 workerChoiceStrategyContext
.workerChoiceStrategy
.choose
.calledOnce
86 expect(worker
).toBe('worker')
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 DynamicPoolWorkerChoiceStrategy
112 workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
113 ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy
)
116 it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => {
117 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
120 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
121 WorkerChoiceStrategies
.LESS_RECENTLY_USED
123 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
124 LessRecentlyUsedWorkerChoiceStrategy
128 it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and dynamic pool', () => {
129 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
132 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
133 WorkerChoiceStrategies
.LESS_RECENTLY_USED
135 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
136 DynamicPoolWorkerChoiceStrategy
139 workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
140 ).toBeInstanceOf(LessRecentlyUsedWorkerChoiceStrategy
)
143 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
144 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
147 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
148 WorkerChoiceStrategies
.FAIR_SHARE
150 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
151 FairShareWorkerChoiceStrategy
155 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
156 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
159 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
160 WorkerChoiceStrategies
.FAIR_SHARE
162 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
163 DynamicPoolWorkerChoiceStrategy
166 workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
167 ).toBeInstanceOf(FairShareWorkerChoiceStrategy
)
170 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
171 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
174 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
175 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
177 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
178 WeightedRoundRobinWorkerChoiceStrategy
182 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
183 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
186 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
187 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
189 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
190 DynamicPoolWorkerChoiceStrategy
193 workerChoiceStrategyContext
.workerChoiceStrategy
.workerChoiceStrategy
194 ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy
)