|
5 | 5 | longSleep, |
6 | 6 | retry, |
7 | 7 | retryInterval, |
8 | | - parallel, |
9 | 8 | asyncmap, |
10 | 9 | asyncfilter, |
11 | 10 | waitForCondition, |
@@ -173,88 +172,45 @@ describe('retry', function () { |
173 | 172 | }); |
174 | 173 | }); |
175 | 174 |
|
176 | | -describe('parallel', function () { |
177 | | - const asyncFn = async function (val: number): Promise<number> { |
178 | | - await sleep(50); |
179 | | - return val; |
180 | | - }; |
181 | | - const badAsyncFn = async function (): Promise<never> { |
182 | | - await sleep(20); |
183 | | - throw new Error('boo'); |
184 | | - }; |
185 | | - it('should perform tasks in parallel and return results', async function () { |
186 | | - const vals = [1, 2, 3]; |
187 | | - const promises: Promise<number>[] = []; |
188 | | - const start = Date.now(); |
189 | | - for (const v of vals) { |
190 | | - promises.push(asyncFn(v)); |
| 175 | +describe('waitForCondition', function () { |
| 176 | + let requestSpy: sinon.SinonSpy; |
| 177 | + beforeEach(function () { |
| 178 | + requestSpy = sinon.spy(B, 'delay'); |
| 179 | + }); |
| 180 | + afterEach(function () { |
| 181 | + requestSpy.restore(); |
| 182 | + }); |
| 183 | + it('should wait and succeed', async function () { |
| 184 | + const ref = Date.now(); |
| 185 | + function condFn (): boolean { |
| 186 | + return Date.now() - ref > 200; |
191 | 187 | } |
192 | | - const res = await parallel(promises); |
193 | | - expect(Date.now() - start).to.be.at.least(49); |
194 | | - expect(Date.now() - start).to.be.below(59); |
195 | | - expect(res.sort()).to.eql([1, 2, 3]); |
| 188 | + const result = await waitForCondition(condFn, {waitMs: 1000, intervalMs: 10}); |
| 189 | + const duration = Date.now() - ref; |
| 190 | + expect(duration).to.be.above(200); |
| 191 | + expect(duration).to.be.below(250); |
| 192 | + expect(result).to.be.true; |
196 | 193 | }); |
197 | | - it('should error with first response', async function () { |
198 | | - const vals = [1, 2, 3]; |
199 | | - const promises: Promise<number>[] = []; |
200 | | - const start = Date.now(); |
201 | | - for (const v of vals) { |
202 | | - promises.push(asyncFn(v)); |
| 194 | + it('should wait and fail', async function () { |
| 195 | + const ref = Date.now(); |
| 196 | + function condFn (): boolean { |
| 197 | + return Date.now() - ref > 200; |
203 | 198 | } |
204 | | - promises.push(badAsyncFn()); |
205 | | - let err: Error | null = null; |
206 | | - let res: number[] = []; |
207 | 199 | try { |
208 | | - res = await parallel(promises); |
209 | | - } catch (e) { |
210 | | - err = e as Error; |
| 200 | + await waitForCondition(condFn, {waitMs: 100, intervalMs: 10}); |
| 201 | + expect.fail('Should have thrown an error'); |
| 202 | + } catch (err: any) { |
| 203 | + expect(err.message).to.match(/Condition unmet/); |
211 | 204 | } |
212 | | - expect(Date.now() - start).to.be.at.least(19); |
213 | | - expect(Date.now() - start).to.be.below(49); |
214 | | - expect(err).to.exist; |
215 | | - expect(res).to.eql([]); |
216 | 205 | }); |
217 | | - |
218 | | - describe('waitForCondition', function () { |
219 | | - let requestSpy: sinon.SinonSpy; |
220 | | - beforeEach(function () { |
221 | | - requestSpy = sinon.spy(B, 'delay'); |
222 | | - }); |
223 | | - afterEach(function () { |
224 | | - requestSpy.restore(); |
225 | | - }); |
226 | | - it('should wait and succeed', async function () { |
227 | | - const ref = Date.now(); |
228 | | - function condFn (): boolean { |
229 | | - return Date.now() - ref > 200; |
230 | | - } |
231 | | - const result = await waitForCondition(condFn, {waitMs: 1000, intervalMs: 10}); |
232 | | - const duration = Date.now() - ref; |
233 | | - expect(duration).to.be.above(200); |
234 | | - expect(duration).to.be.below(250); |
235 | | - expect(result).to.be.true; |
236 | | - }); |
237 | | - it('should wait and fail', async function () { |
238 | | - const ref = Date.now(); |
239 | | - function condFn (): boolean { |
240 | | - return Date.now() - ref > 200; |
241 | | - } |
242 | | - try { |
243 | | - await waitForCondition(condFn, {waitMs: 100, intervalMs: 10}); |
244 | | - expect.fail('Should have thrown an error'); |
245 | | - } catch (err: any) { |
246 | | - expect(err.message).to.match(/Condition unmet/); |
247 | | - } |
248 | | - }); |
249 | | - it('should not exceed implicit wait timeout', async function () { |
250 | | - const ref = Date.now(); |
251 | | - function condFn (): boolean { |
252 | | - return Date.now() - ref > 15; |
253 | | - } |
254 | | - await (waitForCondition(condFn, {waitMs: 20, intervalMs: 10})); |
255 | | - const getLastCall = requestSpy.getCall(1); |
256 | | - expect(getLastCall.args[0]).to.be.at.most(10); |
257 | | - }); |
| 206 | + it('should not exceed implicit wait timeout', async function () { |
| 207 | + const ref = Date.now(); |
| 208 | + function condFn (): boolean { |
| 209 | + return Date.now() - ref > 15; |
| 210 | + } |
| 211 | + await (waitForCondition(condFn, {waitMs: 20, intervalMs: 10})); |
| 212 | + const getLastCall = requestSpy.getCall(1); |
| 213 | + expect(getLastCall.args[0]).to.be.at.most(10); |
258 | 214 | }); |
259 | 215 | }); |
260 | 216 |
|
|
0 commit comments