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', () => {
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'
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
113 it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => {
114 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
117 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
118 WorkerChoiceStrategies
.LESS_RECENTLY_USED
120 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
121 LessRecentlyUsedWorkerChoiceStrategy
125 it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and dynamic pool', () => {
126 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
129 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
130 WorkerChoiceStrategies
.LESS_RECENTLY_USED
132 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
133 DynamicPoolWorkerChoiceStrategy
137 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => {
138 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
141 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
142 WorkerChoiceStrategies
.FAIR_SHARE
144 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
145 FairShareWorkerChoiceStrategy
149 it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => {
150 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
153 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
154 WorkerChoiceStrategies
.FAIR_SHARE
156 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
157 DynamicPoolWorkerChoiceStrategy
161 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => {
162 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
165 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
166 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
168 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
169 WeightedRoundRobinWorkerChoiceStrategy
173 it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => {
174 const workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
177 workerChoiceStrategyContext
.setWorkerChoiceStrategy(
178 WorkerChoiceStrategies
.WEIGHTED_ROUND_ROBIN
180 expect(workerChoiceStrategyContext
.workerChoiceStrategy
).toBeInstanceOf(
181 DynamicPoolWorkerChoiceStrategy