Commit | Line | Data |
---|---|---|
a61a0724 | 1 | const { expect } = require('expect') |
a35560ba S |
2 | const { |
3 | WorkerChoiceStrategies, | |
4 | DynamicThreadPool, | |
5 | FixedThreadPool | |
15d56315 | 6 | } = require('../../../lib/index') |
a35560ba S |
7 | |
8 | describe('Selection strategies test suite', () => { | |
9 | it('Verify that WorkerChoiceStrategies enumeration provides string values', () => { | |
10 | expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN') | |
11 | expect(WorkerChoiceStrategies.LESS_RECENTLY_USED).toBe('LESS_RECENTLY_USED') | |
b3432a63 JB |
12 | expect(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN).toBe( |
13 | 'WEIGHTED_ROUND_ROBIN' | |
14 | ) | |
a35560ba S |
15 | }) |
16 | ||
e843b904 JB |
17 | it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => { |
18 | const min = 0 | |
19 | const max = 3 | |
20 | const pool = new DynamicThreadPool( | |
21 | min, | |
22 | max, | |
23 | './tests/worker-files/thread/testWorker.js' | |
24 | ) | |
25 | expect(pool.opts.workerChoiceStrategy).toBe( | |
26 | WorkerChoiceStrategies.ROUND_ROBIN | |
27 | ) | |
28 | // We need to clean up the resources after our test | |
29 | await pool.destroy() | |
30 | }) | |
31 | ||
32 | it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => { | |
33 | const min = 0 | |
34 | const max = 3 | |
35 | const pool = new DynamicThreadPool( | |
36 | min, | |
37 | max, | |
38 | './tests/worker-files/thread/testWorker.js' | |
39 | ) | |
40 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.ROUND_ROBIN) | |
41 | expect(pool.opts.workerChoiceStrategy).toBe( | |
42 | WorkerChoiceStrategies.ROUND_ROBIN | |
43 | ) | |
44 | // We need to clean up the resources after our test | |
45 | await pool.destroy() | |
46 | }) | |
47 | ||
bdaf31cd JB |
48 | it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => { |
49 | const max = 3 | |
50 | const pool = new FixedThreadPool( | |
51 | max, | |
52 | './tests/worker-files/thread/testWorker.js', | |
53 | { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN } | |
54 | ) | |
55 | expect(pool.opts.workerChoiceStrategy).toBe( | |
56 | WorkerChoiceStrategies.ROUND_ROBIN | |
57 | ) | |
58 | // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose` | |
59 | const promises = [] | |
60 | for (let i = 0; i < max * 2; i++) { | |
61 | promises.push(pool.execute({ test: 'test' })) | |
62 | } | |
63 | await Promise.all(promises) | |
64 | // We need to clean up the resources after our test | |
65 | await pool.destroy() | |
66 | }) | |
67 | ||
68 | it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => { | |
69 | const min = 0 | |
70 | const max = 3 | |
71 | const pool = new DynamicThreadPool( | |
72 | min, | |
73 | max, | |
74 | './tests/worker-files/thread/testWorker.js', | |
75 | { workerChoiceStrategy: WorkerChoiceStrategies.ROUND_ROBIN } | |
76 | ) | |
77 | expect(pool.opts.workerChoiceStrategy).toBe( | |
78 | WorkerChoiceStrategies.ROUND_ROBIN | |
79 | ) | |
80 | // TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose` | |
81 | const promises = [] | |
82 | for (let i = 0; i < max * 2; i++) { | |
83 | promises.push(pool.execute({ test: 'test' })) | |
84 | } | |
85 | await Promise.all(promises) | |
86 | // We need to clean up the resources after our test | |
87 | await pool.destroy() | |
88 | }) | |
89 | ||
b98ec2e6 | 90 | it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => { |
a35560ba S |
91 | const max = 3 |
92 | const pool = new FixedThreadPool( | |
93 | max, | |
94 | './tests/worker-files/thread/testWorker.js', | |
95 | { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } | |
96 | ) | |
b98ec2e6 JB |
97 | expect(pool.opts.workerChoiceStrategy).toBe( |
98 | WorkerChoiceStrategies.LESS_RECENTLY_USED | |
99 | ) | |
100 | // We need to clean up the resources after our test | |
101 | await pool.destroy() | |
102 | }) | |
a35560ba | 103 | |
b98ec2e6 JB |
104 | it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => { |
105 | const max = 3 | |
106 | const pool = new FixedThreadPool( | |
107 | max, | |
108 | './tests/worker-files/thread/testWorker.js' | |
109 | ) | |
110 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.LESS_RECENTLY_USED) | |
a35560ba S |
111 | expect(pool.opts.workerChoiceStrategy).toBe( |
112 | WorkerChoiceStrategies.LESS_RECENTLY_USED | |
113 | ) | |
b98ec2e6 JB |
114 | // We need to clean up the resources after our test |
115 | await pool.destroy() | |
116 | }) | |
a35560ba | 117 | |
ff5e76e1 | 118 | it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => { |
b98ec2e6 JB |
119 | const max = 3 |
120 | const pool = new FixedThreadPool( | |
121 | max, | |
122 | './tests/worker-files/thread/testWorker.js', | |
123 | { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } | |
124 | ) | |
a35560ba S |
125 | // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` |
126 | const promises = [] | |
127 | for (let i = 0; i < max * 2; i++) { | |
128 | promises.push(pool.execute({ test: 'test' })) | |
129 | } | |
130 | await Promise.all(promises) | |
a35560ba S |
131 | // We need to clean up the resources after our test |
132 | await pool.destroy() | |
133 | }) | |
134 | ||
ff5e76e1 JB |
135 | it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => { |
136 | const min = 0 | |
137 | const max = 3 | |
138 | const pool = new DynamicThreadPool( | |
139 | min, | |
140 | max, | |
141 | './tests/worker-files/thread/testWorker.js', | |
142 | { workerChoiceStrategy: WorkerChoiceStrategies.LESS_RECENTLY_USED } | |
143 | ) | |
144 | // TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose` | |
145 | const promises = [] | |
146 | for (let i = 0; i < max * 2; i++) { | |
147 | promises.push(pool.execute({ test: 'test' })) | |
148 | } | |
149 | await Promise.all(promises) | |
ff5e76e1 JB |
150 | // We need to clean up the resources after our test |
151 | await pool.destroy() | |
152 | }) | |
153 | ||
b3432a63 JB |
154 | it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => { |
155 | const max = 3 | |
156 | const pool = new FixedThreadPool( | |
157 | max, | |
158 | './tests/worker-files/thread/testWorker.js', | |
159 | { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } | |
160 | ) | |
161 | expect(pool.opts.workerChoiceStrategy).toBe( | |
162 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN | |
163 | ) | |
164 | // We need to clean up the resources after our test | |
165 | await pool.destroy() | |
166 | }) | |
167 | ||
168 | it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => { | |
169 | const max = 3 | |
170 | const pool = new FixedThreadPool( | |
171 | max, | |
172 | './tests/worker-files/thread/testWorker.js' | |
173 | ) | |
174 | pool.setWorkerChoiceStrategy(WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN) | |
175 | expect(pool.opts.workerChoiceStrategy).toBe( | |
176 | WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN | |
177 | ) | |
178 | // We need to clean up the resources after our test | |
179 | await pool.destroy() | |
180 | }) | |
181 | ||
182 | it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => { | |
183 | const max = 3 | |
184 | const pool = new FixedThreadPool( | |
185 | max, | |
186 | './tests/worker-files/thread/testWorker.js', | |
187 | { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } | |
188 | ) | |
189 | // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` | |
190 | const promises = [] | |
191 | for (let i = 0; i < max * 2; i++) { | |
192 | promises.push(pool.execute({ test: 'test' })) | |
193 | } | |
194 | await Promise.all(promises) | |
195 | // We need to clean up the resources after our test | |
196 | await pool.destroy() | |
197 | }) | |
198 | ||
199 | it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => { | |
200 | const min = 0 | |
201 | const max = 3 | |
202 | const pool = new DynamicThreadPool( | |
203 | min, | |
204 | max, | |
205 | './tests/worker-files/thread/testWorker.js', | |
206 | { workerChoiceStrategy: WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN } | |
207 | ) | |
208 | // TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose` | |
209 | const promises = [] | |
210 | for (let i = 0; i < max * 2; i++) { | |
211 | promises.push(pool.execute({ test: 'test' })) | |
212 | } | |
213 | await Promise.all(promises) | |
214 | // We need to clean up the resources after our test | |
215 | await pool.destroy() | |
216 | }) | |
217 | ||
a35560ba S |
218 | it('Verify unknown strategies throw error', () => { |
219 | const min = 1 | |
220 | const max = 3 | |
221 | expect( | |
222 | () => | |
223 | new DynamicThreadPool( | |
224 | min, | |
225 | max, | |
226 | './tests/worker-files/thread/testWorker.js', | |
1927ee67 | 227 | { workerChoiceStrategy: 'UNKNOWN_STRATEGY' } |
a35560ba S |
228 | ) |
229 | ).toThrowError( | |
230 | new Error("Worker choice strategy 'UNKNOWN_STRATEGY' not found") | |
231 | ) | |
232 | }) | |
233 | }) |