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-choice-strategy')
24 DynamicPoolWorkerChoiceStrategy
25 } = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy')
27 describe('Worker choice strategy context test suite', () => {
28 let fixedPool
, dynamicPool
30 fixedPool
= sinon
.createStubInstance(FixedThreadPool
)
31 dynamicPool
= sinon
.createStubInstance(DynamicThreadPool
)
38 it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
39 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
42 const WorkerChoiceStrategyStub
= sinon
.createStubInstance(
43 RoundRobinWorkerChoiceStrategy
,
45 choose
: sinon
.stub().returns('worker')
48 workerChoiceStrategyContext
.workerChoiceStrategy
= WorkerChoiceStrategyStub
49 const worker
= workerChoiceStrategyContext
.execute()
51 workerChoiceStrategyContext
.workerChoiceStrategy
.choose
.calledOnce
53 expect(worker
).toBe('worker')
56 it('Verify that execute() return the worker chosen by the strategy with dynamic 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 worker
= workerChoiceStrategyContext
.execute()
69 workerChoiceStrategyContext
.workerChoiceStrategy
.choose
.calledOnce
71 expect(worker
).toBe('worker')
74 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => {
75 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
78 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
79 WorkerChoiceStrategies
.ROUND_ROBIN
81 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
82 RoundRobinWorkerChoiceStrategy
86 it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => {
87 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
90 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
91 WorkerChoiceStrategies
.ROUND_ROBIN
93 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
94 DynamicPoolWorkerChoiceStrategy
98 it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => {
99 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
102 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
103 WorkerChoiceStrategies
.LESS_RECENTLY_USED
105 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
106 LessRecentlyUsedWorkerChoiceStrategy
110 it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and dynamic pool', () => {
111 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
114 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
115 WorkerChoiceStrategies
.LESS_RECENTLY_USED
117 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
118 DynamicPoolWorkerChoiceStrategy
122 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
123 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
126 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
127 WorkerChoiceStrategies
.FAIR_SHARE
129 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
130 FairShareWorkerChoiceStrategy
134 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
135 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
138 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
139 WorkerChoiceStrategies
.FAIR_SHARE
141 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
142 DynamicPoolWorkerChoiceStrategy
146 // it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
147 // const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
150 // workerChoiceStrategyContext.setWorkerChoiceStrategy(
151 // WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
153 // expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
154 // WeightedRoundRobinWorkerChoiceStrategy
158 // it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
159 // const workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
162 // workerChoiceStrategyContext.setWorkerChoiceStrategy(
163 // WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN
165 // expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf(
166 // DynamicPoolWorkerChoiceStrategy