Skip to content

Commit 5c1149f

Browse files
authored
test: try to unflake network idle tests (#4333)
I think that we are too slow to fire the second fetch during 500ms, and so network idle happens prematurely. The fix is to manually trigger the second fetch early enough.
1 parent 890add9 commit 5c1149f

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

test/assets/networkidle.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
async function sleep(delay) {
2-
return new Promise(resolve => setTimeout(resolve, delay));
3-
}
4-
51
async function main() {
62
window.ws = new WebSocket('ws://localhost:' + window.location.port + '/ws');
73
window.ws.addEventListener('message', message => {});
84

9-
const roundOne = Promise.all([
10-
fetch('fetch-request-a.js'),
11-
]);
12-
13-
await roundOne;
14-
await sleep(50);
15-
await fetch('fetch-request-d.js');
5+
fetch('fetch-request-a.js');
6+
window.top.fetchSecond = () => {
7+
// Do not return the promise here.
8+
fetch('fetch-request-b.js');
9+
};
1610
}
1711

1812
main();

test/page-network-idle.spec.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import { it, expect } from './fixtures';
19-
import type { Frame, Page } from '..';
19+
import type { Frame } from '..';
2020
import { TestServer } from '../utils/testserver';
2121

2222
it('should navigate to empty page with networkidle', async ({page, server}) => {
@@ -25,22 +25,19 @@ it('should navigate to empty page with networkidle', async ({page, server}) => {
2525
});
2626

2727
async function networkIdleTest(frame: Frame, server: TestServer, action: () => Promise<any>, isSetContent?: boolean) {
28-
const finishResponse = response => {
29-
response.statusCode = 404;
30-
response.end(`File not found`);
31-
};
32-
const waitForRequest = suffix => {
28+
const waitForRequest = (suffix: string) => {
3329
return Promise.all([
3430
server.waitForRequest(suffix),
35-
(frame['_page'] as Page).waitForRequest(server.PREFIX + suffix),
31+
frame.page().waitForRequest(server.PREFIX + suffix),
3632
]);
3733
};
38-
const responses = {};
34+
35+
let responseA, responseB;
3936
// Hold on to a bunch of requests without answering.
40-
server.setRoute('/fetch-request-a.js', (req, res) => responses['a'] = res);
37+
server.setRoute('/fetch-request-a.js', (req, res) => responseA = res);
4138
const firstFetchResourceRequested = waitForRequest('/fetch-request-a.js');
42-
server.setRoute('/fetch-request-d.js', (req, res) => responses['d'] = res);
43-
const secondFetchResourceRequested = waitForRequest('/fetch-request-d.js');
39+
server.setRoute('/fetch-request-b.js', (req, res) => responseB = res);
40+
const secondFetchResourceRequested = waitForRequest('/fetch-request-b.js');
4441

4542
const waitForLoadPromise = isSetContent ? Promise.resolve() : frame.waitForNavigation({ waitUntil: 'load' });
4643

@@ -60,17 +57,21 @@ async function networkIdleTest(frame: Frame, server: TestServer, action: () => P
6057
await firstFetchResourceRequested;
6158
expect(actionFinished).toBe(false);
6259

63-
expect(responses['a']).toBeTruthy();
64-
// Finishing response should trigger the second round.
65-
finishResponse(responses['a']);
60+
// Trigger the second request.
61+
await frame.page().evaluate(() => window['fetchSecond']());
62+
// Finish the first request.
63+
responseA.statusCode = 404;
64+
responseA.end(`File not found`);
6665

6766
// Wait for the second round to be requested.
6867
await secondFetchResourceRequested;
6968
expect(actionFinished).toBe(false);
70-
// Finishing the last response should trigger networkidle.
69+
70+
// Finishing the second response should trigger networkidle.
7171
let timerTriggered = false;
7272
const timer = setTimeout(() => timerTriggered = true, 500);
73-
finishResponse(responses['d']);
73+
responseB.statusCode = 404;
74+
responseB.end(`File not found`);
7475

7576
const response = await actionPromise;
7677
clearTimeout(timer);

0 commit comments

Comments
 (0)