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 { | |
15 | LessRecentlyUsedWorkerChoiceStrategy | |
16 | } = require('../../../lib/pools/selection-strategies/less-recently-used-worker-choice-strategy') | |
17 | const { | |
23ff945a JB |
18 | FairShareWorkerChoiceStrategy |
19 | } = require('../../../lib/pools/selection-strategies/fair-share-worker-choice-strategy') | |
c15273f2 JB |
20 | const { |
21 | WeightedRoundRobinWorkerChoiceStrategy | |
fa6f1296 | 22 | } = require('../../../lib/pools/selection-strategies/weighted-round-robin-worker-choice-strategy') |
40ad1d27 JB |
23 | const { |
24 | DynamicPoolWorkerChoiceStrategy | |
25 | } = require('../../../lib/pools/selection-strategies/dynamic-pool-worker-choice-strategy') | |
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 | { | |
60 | choose: sinon.stub().returns('worker') | |
61 | } | |
62 | ) | |
63 | workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub | |
0220f124 | 64 | const chosenWorker = workerChoiceStrategyContext.execute() |
40ad1d27 | 65 | expect( |
6cdd998c | 66 | workerChoiceStrategyContext.getWorkerChoiceStrategy().choose.calledOnce |
40ad1d27 | 67 | ).toBe(true) |
0220f124 | 68 | expect(chosenWorker).toBe('worker') |
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 | { | |
78 | choose: sinon.stub().returns('worker') | |
79 | } | |
80 | ) | |
81 | workerChoiceStrategyContext.workerChoiceStrategy = WorkerChoiceStrategyStub | |
0220f124 | 82 | const chosenWorker = workerChoiceStrategyContext.execute() |
40ad1d27 | 83 | expect( |
6cdd998c | 84 | workerChoiceStrategyContext.getWorkerChoiceStrategy().choose.calledOnce |
40ad1d27 | 85 | ).toBe(true) |
0220f124 | 86 | expect(chosenWorker).toBe('worker') |
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( | |
94 | WorkerChoiceStrategies.ROUND_ROBIN | |
95 | ) | |
96 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( | |
97 | RoundRobinWorkerChoiceStrategy | |
98 | ) | |
99 | }) | |
100 | ||
23ff945a | 101 | it('Verify that setWorkerChoiceStrategy() works with ROUND_ROBIN and dynamic pool', () => { |
40ad1d27 JB |
102 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
103 | dynamicPool | |
104 | ) | |
105 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
106 | WorkerChoiceStrategies.ROUND_ROBIN | |
107 | ) | |
108 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( | |
109 | DynamicPoolWorkerChoiceStrategy | |
110 | ) | |
87c65328 JB |
111 | expect( |
112 | workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy | |
113 | ).toBeInstanceOf(RoundRobinWorkerChoiceStrategy) | |
40ad1d27 JB |
114 | }) |
115 | ||
116 | it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => { | |
117 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
118 | fixedPool | |
119 | ) | |
120 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
121 | WorkerChoiceStrategies.LESS_RECENTLY_USED | |
122 | ) | |
123 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( | |
124 | LessRecentlyUsedWorkerChoiceStrategy | |
125 | ) | |
126 | }) | |
127 | ||
23ff945a | 128 | it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and dynamic pool', () => { |
40ad1d27 JB |
129 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
130 | dynamicPool | |
131 | ) | |
132 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
133 | WorkerChoiceStrategies.LESS_RECENTLY_USED | |
134 | ) | |
135 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( | |
136 | DynamicPoolWorkerChoiceStrategy | |
137 | ) | |
87c65328 JB |
138 | expect( |
139 | workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy | |
140 | ).toBeInstanceOf(LessRecentlyUsedWorkerChoiceStrategy) | |
40ad1d27 | 141 | }) |
23ff945a JB |
142 | |
143 | it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => { | |
144 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
145 | fixedPool | |
146 | ) | |
147 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
148 | WorkerChoiceStrategies.FAIR_SHARE | |
149 | ) | |
150 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( | |
151 | FairShareWorkerChoiceStrategy | |
152 | ) | |
153 | }) | |
154 | ||
155 | it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => { | |
156 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
157 | dynamicPool | |
158 | ) | |
159 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
160 | WorkerChoiceStrategies.FAIR_SHARE | |
161 | ) | |
162 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( | |
163 | DynamicPoolWorkerChoiceStrategy | |
164 | ) | |
87c65328 JB |
165 | expect( |
166 | workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy | |
167 | ).toBeInstanceOf(FairShareWorkerChoiceStrategy) | |
23ff945a JB |
168 | }) |
169 | ||
c15273f2 JB |
170 | it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => { |
171 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
172 | fixedPool | |
173 | ) | |
174 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
175 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN | |
176 | ) | |
177 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( | |
178 | WeightedRoundRobinWorkerChoiceStrategy | |
179 | ) | |
180 | }) | |
23ff945a | 181 | |
c15273f2 JB |
182 | it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => { |
183 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
184 | dynamicPool | |
185 | ) | |
186 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
187 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN | |
188 | ) | |
189 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( | |
190 | DynamicPoolWorkerChoiceStrategy | |
191 | ) | |
87c65328 JB |
192 | expect( |
193 | workerChoiceStrategyContext.workerChoiceStrategy.workerChoiceStrategy | |
194 | ).toBeInstanceOf(WeightedRoundRobinWorkerChoiceStrategy) | |
c15273f2 | 195 | }) |
40ad1d27 | 196 | }) |