Commit | Line | Data |
---|---|---|
40ad1d27 JB |
1 | const { expect } = require('expect') |
2 | const sinon = require('sinon') | |
3 | const { | |
4 | FixedThreadPool, | |
5 | DynamicThreadPool, | |
6 | WorkerChoiceStrategies | |
7 | } = require('../../../lib/index') | |
23ff945a JB |
8 | const { |
9 | WorkerChoiceStrategyContext | |
10 | } = require('../../../lib/pools/selection-strategies/worker-choice-strategy-context') | |
40ad1d27 JB |
11 | const { |
12 | RoundRobinWorkerChoiceStrategy | |
13 | } = require('../../../lib/pools/selection-strategies/round-robin-worker-choice-strategy') | |
14 | const { | |
737c6d97 JB |
15 | LessUsedWorkerChoiceStrategy |
16 | } = require('../../../lib/pools/selection-strategies/less-used-worker-choice-strategy') | |
168c526f JB |
17 | const { |
18 | LessBusyWorkerChoiceStrategy | |
19 | } = require('../../../lib/pools/selection-strategies/less-busy-worker-choice-strategy') | |
40ad1d27 | 20 | const { |
23ff945a JB |
21 | FairShareWorkerChoiceStrategy |
22 | } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy') | |
c15273f2 JB |
23 | const { |
24 | WeightedRoundRobinWorkerChoiceStrategy | |
fa6f1296 | 25 | } = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy') |
40ad1d27 JB |
26 | const { |
27 | DynamicPoolWorkerChoiceStrategy | |
28 | } = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy') | |
29 | ||
30 | describe('Worker choice strategy context test suite', () => { | |
c15273f2 JB |
31 | const min = 1 |
32 | const max = 3 | |
40ad1d27 | 33 | let fixedPool, dynamicPool |
c15273f2 JB |
34 | |
35 | before(() => { | |
36 | fixedPool = new FixedThreadPool( | |
37 | max, | |
38 | './tests/worker-files/thread/testWorker.js' | |
39 | ) | |
40 | dynamicPool = new DynamicThreadPool( | |
41 | min, | |
42 | max, | |
43 | './tests/worker-files/thread/testWorker.js' | |
44 | ) | |
40ad1d27 JB |
45 | }) |
46 | ||
47 | afterEach(() => { | |
48 | sinon.restore() | |
49 | }) | |
50 | ||
fd7ebd49 JB |
51 | after(async () => { |
52 | await fixedPool.destroy() | |
53 | await dynamicPool.destroy() | |
c15273f2 JB |
54 | }) |
55 | ||
40ad1d27 JB |
56 | it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => { |
57 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
58 | fixedPool | |
59 | ) | |
60 | const WorkerChoiceStrategyStub = sinon.createStubInstance( | |
61 | RoundRobinWorkerChoiceStrategy, | |
62 | { | |
c923ce56 | 63 | choose: sinon.stub().returns(0) |
40ad1d27 JB |
64 | } |
65 | ) | |
66 | workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub | |
c923ce56 | 67 | const chosenWorkerKey = workerChoiceStrategyContext.execute() |
40ad1d27 | 68 | expect( |
6cdd998c | 69 | workerChoiceStrategyContext.getWorkerChoiceStrategy().choose.calledOnce |
40ad1d27 | 70 | ).toBe(true) |
c923ce56 | 71 | expect(chosenWorkerKey).toBe(0) |
40ad1d27 JB |
72 | }) |
73 | ||
74 | it('Verify that execute() return the worker chosen by the strategy with dynamic pool', () => { | |
75 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
76 | dynamicPool | |
77 | ) | |
78 | const WorkerChoiceStrategyStub = sinon.createStubInstance( | |
79 | RoundRobinWorkerChoiceStrategy, | |
80 | { | |
c923ce56 | 81 | choose: sinon.stub().returns(0) |
40ad1d27 JB |
82 | } |
83 | ) | |
84 | workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub | |
c923ce56 | 85 | const chosenWorkerKey = workerChoiceStrategyContext.execute() |
40ad1d27 | 86 | expect( |
6cdd998c | 87 | workerChoiceStrategyContext.getWorkerChoiceStrategy().choose.calledOnce |
40ad1d27 | 88 | ).toBe(true) |
c923ce56 | 89 | expect(chosenWorkerKey).toBe(0) |
40ad1d27 JB |
90 | }) |
91 | ||
92 | it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and fixed pool', () => { | |
93 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
94 | fixedPool | |
95 | ) | |
96 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
97 | WorkerChoiceStrategies.ROUND_ROBIN | |
98 | ) | |
b0afa8e5 JB |
99 | expect( |
100 | workerChoiceStrategyContext.getWorkerChoiceStrategy() | |
101 | ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) | |
40ad1d27 JB |
102 | }) |
103 | ||
23ff945a | 104 | it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => { |
40ad1d27 JB |
105 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
106 | dynamicPool | |
107 | ) | |
108 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
109 | WorkerChoiceStrategies.ROUND_ROBIN | |
110 | ) | |
87c65328 | 111 | expect( |
b0afa8e5 JB |
112 | workerChoiceStrategyContext.getWorkerChoiceStrategy() |
113 | ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy) | |
114 | expect( | |
115 | workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy | |
87c65328 | 116 | ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) |
40ad1d27 JB |
117 | }) |
118 | ||
737c6d97 | 119 | it('Verify that setWorkerChoiceStrategy() works with LESS_USED and fixed pool', () => { |
40ad1d27 JB |
120 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
121 | fixedPool | |
122 | ) | |
123 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
737c6d97 | 124 | WorkerChoiceStrategies.LESS_USED |
40ad1d27 | 125 | ) |
b0afa8e5 JB |
126 | expect( |
127 | workerChoiceStrategyContext.getWorkerChoiceStrategy() | |
737c6d97 | 128 | ).toBeInstanceOf(LessUsedWorkerChoiceStrategy) |
40ad1d27 JB |
129 | }) |
130 | ||
737c6d97 | 131 | it('Verify that setWorkerChoiceStrategy() works with LESS_USED and dynamic pool', () => { |
40ad1d27 JB |
132 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
133 | dynamicPool | |
134 | ) | |
135 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
737c6d97 | 136 | WorkerChoiceStrategies.LESS_USED |
40ad1d27 | 137 | ) |
87c65328 | 138 | expect( |
b0afa8e5 JB |
139 | workerChoiceStrategyContext.getWorkerChoiceStrategy() |
140 | ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy) | |
141 | expect( | |
142 | workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy | |
737c6d97 | 143 | ).toBeInstanceOf(LessUsedWorkerChoiceStrategy) |
168c526f JB |
144 | }) |
145 | ||
146 | it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and fixed pool', () => { | |
147 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
148 | fixedPool | |
149 | ) | |
150 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
151 | WorkerChoiceStrategies.LESS_BUSY | |
152 | ) | |
153 | expect( | |
154 | workerChoiceStrategyContext.getWorkerChoiceStrategy() | |
155 | ).toBeInstanceOf(LessBusyWorkerChoiceStrategy) | |
156 | }) | |
157 | ||
158 | it('Verify that setWorkerChoiceStrategy() works with LESS_BUSY and dynamic pool', () => { | |
159 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
160 | dynamicPool | |
161 | ) | |
162 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
163 | WorkerChoiceStrategies.LESS_BUSY | |
164 | ) | |
165 | expect( | |
166 | workerChoiceStrategyContext.getWorkerChoiceStrategy() | |
167 | ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy) | |
168 | expect( | |
169 | workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy | |
170 | ).toBeInstanceOf(LessBusyWorkerChoiceStrategy) | |
40ad1d27 | 171 | }) |
23ff945a JB |
172 | |
173 | it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => { | |
174 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
175 | fixedPool | |
176 | ) | |
177 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
178 | WorkerChoiceStrategies.FAIR_SHARE | |
179 | ) | |
b0afa8e5 JB |
180 | expect( |
181 | workerChoiceStrategyContext.getWorkerChoiceStrategy() | |
182 | ).toBeInstanceOf(FairShareWorkerChoiceStrategy) | |
23ff945a JB |
183 | }) |
184 | ||
185 | it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => { | |
186 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
187 | dynamicPool | |
188 | ) | |
189 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
190 | WorkerChoiceStrategies.FAIR_SHARE | |
191 | ) | |
87c65328 | 192 | expect( |
b0afa8e5 JB |
193 | workerChoiceStrategyContext.getWorkerChoiceStrategy() |
194 | ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy) | |
195 | expect( | |
196 | workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy | |
87c65328 | 197 | ).toBeInstanceOf(FairShareWorkerChoiceStrategy) |
23ff945a JB |
198 | }) |
199 | ||
c15273f2 JB |
200 | it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => { |
201 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
202 | fixedPool | |
203 | ) | |
204 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
205 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN | |
206 | ) | |
b0afa8e5 JB |
207 | expect( |
208 | workerChoiceStrategyContext.getWorkerChoiceStrategy() | |
209 | ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) | |
c15273f2 | 210 | }) |
23ff945a | 211 | |
c15273f2 JB |
212 | it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => { |
213 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
214 | dynamicPool | |
215 | ) | |
216 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
217 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN | |
218 | ) | |
87c65328 | 219 | expect( |
b0afa8e5 JB |
220 | workerChoiceStrategyContext.getWorkerChoiceStrategy() |
221 | ).toBeInstanceOf(DynamicPoolWorkerChoiceStrategy) | |
222 | expect( | |
223 | workerChoiceStrategyContext.getWorkerChoiceStrategy().workerChoiceStrategy | |
87c65328 | 224 | ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) |
c15273f2 | 225 | }) |
40ad1d27 | 226 | }) |