Skip to content

Commit a0baa02

Browse files
author
Raphaël Droz
committed
tests: Fix a test randomly failing on Chrome and Edge when no enough time was given to warm-up upload. Do not hardcode sleep() time. (identified in the context of #361)
1 parent ee43611 commit a0baa02

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

test/asyncSpec.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ describe('upload stream', function() {
313313
expect(files[1].chunks.length).toBe(3);
314314
flow.upload();
315315
expect(files[0].isReading()).toBeTruthy();
316-
await sleep(1);
316+
await waitFor(() => xhr_server.requests.length);
317317

318318
/*
319319
[^^ ]
@@ -331,7 +331,8 @@ describe('upload stream', function() {
331331
*/
332332
expect(xhr_server.requests[0].status).toBe(200);
333333
expect(xhr_server.requests[1].status).toBe(200);
334-
await sleep(1);
334+
await waitFor(() => xhr_server.requests.length == 4);
335+
335336
/*
336337
[oo^^__]
337338
[ ]
@@ -344,19 +345,19 @@ describe('upload stream', function() {
344345
// corresponding `xhr`. They will get back to pending.
345346
// Flow should start uploading second file now
346347
files[0].pause();
347-
await sleep(1);
348+
await waitFor(() => xhr_server.requests.length == 6);
348349

349350
/*
350351
[oo____]
351352
[^^ ]
352353
*/
354+
expect(xhr_server.requests.length).toBe(6);
353355
expect(xhr_server.requests[2].aborted).toBeTruthy();
354356
expect(xhr_server.requests[3].aborted).toBeTruthy();
355357
expect(xhr_server.requests[4].aborted).toBeUndefined();
356358
expect(files[0].isUploading()).toBeFalsy();
357359

358360
flow.upload();
359-
await sleep(1);
360361
expect(files[0].isUploading()).toBeFalsy();
361362
expect(files[1].isUploading()).toBeTruthy();
362363

test/helpers.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ function hex(buff) {
2121
return [].map.call(new Uint8Array(buff), b => ('00' + b.toString(16)).slice(-2)).join('');
2222
}
2323

24+
async function waitFor(condition, milliseconds = 10, step = 1) {
25+
while(!condition() && milliseconds) {
26+
await sleep(step);
27+
milliseconds--;
28+
}
29+
}
30+
2431
function sleep(milliseconds) {
2532
return new Promise(resolve => setTimeout(resolve, milliseconds));
2633
}

0 commit comments

Comments
 (0)