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 | |
22 | } = require('../../../lib/pools/selection-strategies/weighted-round-robin-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 | ||
c15273f2 JB |
48 | after(() => { |
49 | fixedPool.destroy() | |
50 | dynamicPool.destroy() | |
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 | |
64 | const worker = workerChoiceStrategyContext.execute() | |
65 | expect( | |
66 | workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce | |
67 | ).toBe(true) | |
68 | expect(worker).toBe('worker') | |
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 | |
82 | const worker = workerChoiceStrategyContext.execute() | |
83 | expect( | |
84 | workerChoiceStrategyContext.workerChoiceStrategy.choose.calledOnce | |
85 | ).toBe(true) | |
86 | expect(worker).toBe('worker') | |
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 | ) | |
111 | }) | |
112 | ||
113 | it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and fixed pool', () => { | |
114 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
115 | fixedPool | |
116 | ) | |
117 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
118 | WorkerChoiceStrategies.LESS_RECENTLY_USED | |
119 | ) | |
120 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( | |
121 | LessRecentlyUsedWorkerChoiceStrategy | |
122 | ) | |
123 | }) | |
124 | ||
23ff945a | 125 | it('Verify that setWorkerChoiceStrategy() works with LESS_RECENTLY_USED and dynamic pool', () => { |
40ad1d27 JB |
126 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( |
127 | dynamicPool | |
128 | ) | |
129 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
130 | WorkerChoiceStrategies.LESS_RECENTLY_USED | |
131 | ) | |
132 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( | |
133 | DynamicPoolWorkerChoiceStrategy | |
134 | ) | |
135 | }) | |
23ff945a JB |
136 | |
137 | it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and fixed pool', () => { | |
138 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
139 | fixedPool | |
140 | ) | |
141 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
142 | WorkerChoiceStrategies.FAIR_SHARE | |
143 | ) | |
144 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( | |
145 | FairShareWorkerChoiceStrategy | |
146 | ) | |
147 | }) | |
148 | ||
149 | it('Verify that setWorkerChoiceStrategy() works with FAIR_SHARE and dynamic pool', () => { | |
150 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
151 | dynamicPool | |
152 | ) | |
153 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
154 | WorkerChoiceStrategies.FAIR_SHARE | |
155 | ) | |
156 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( | |
157 | DynamicPoolWorkerChoiceStrategy | |
158 | ) | |
159 | }) | |
160 | ||
c15273f2 JB |
161 | it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and fixed pool', () => { |
162 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
163 | fixedPool | |
164 | ) | |
165 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
166 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN | |
167 | ) | |
168 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( | |
169 | WeightedRoundRobinWorkerChoiceStrategy | |
170 | ) | |
171 | }) | |
23ff945a | 172 | |
c15273f2 JB |
173 | it('Verify that setWorkerChoiceStrategy() works with WEIGHTED_ROUND_ROBIN and dynamic pool', () => { |
174 | const workerChoiceStrategyContext = new WorkerChoiceStrategyContext( | |
175 | dynamicPool | |
176 | ) | |
177 | workerChoiceStrategyContext.setWorkerChoiceStrategy( | |
178 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN | |
179 | ) | |
180 | expect(workerChoiceStrategyContext.workerChoiceStrategy).toBeInstanceOf( | |
181 | DynamicPoolWorkerChoiceStrategy | |
182 | ) | |
183 | }) | |
40ad1d27 | 184 | }) |