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