-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test(navigation): fix flaky networkidle tests #1058
Merged
JoelEinbinder
merged 2 commits into
microsoft:master
from
JoelEinbinder:flaky_networkidle_test
Feb 19, 2020
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ | |
*/ | ||
|
||
const utils = require('./utils'); | ||
const { performance } = require('perf_hooks'); | ||
|
||
/** | ||
* @type {PageTestSuite} | ||
|
@@ -396,29 +395,40 @@ module.exports.describe = function({testRunner, expect, playwright, MAC, WIN, FF | |
expect(response.status()).toBe(200); | ||
}); | ||
|
||
/** | ||
* @param {import('../src/frames').Frame} frame | ||
* @param {TestServer} server | ||
* @param {'networkidle0'|'networkidle2'} signal | ||
* @param {() => Promise<void>} action | ||
* @param {boolean} isSetContent | ||
*/ | ||
async function networkIdleTest(frame, server, signal, action, isSetContent) { | ||
let lastResponseFinished; | ||
const finishResponse = response => { | ||
lastResponseFinished = performance.now(); | ||
response.statusCode = 404; | ||
response.end(`File not found`); | ||
}; | ||
|
||
const waitForRequest = suffix => { | ||
return Promise.all([ | ||
server.waitForRequest(suffix), | ||
frame._page.waitForRequest(server.PREFIX + suffix), | ||
]) | ||
} | ||
frame.setContent | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stray change |
||
let responses = {}; | ||
// Hold on to a bunch of requests without answering. | ||
server.setRoute('/fetch-request-a.js', (req, res) => responses.a = res); | ||
server.setRoute('/fetch-request-b.js', (req, res) => responses.b = res); | ||
server.setRoute('/fetch-request-c.js', (req, res) => responses.c = res); | ||
const initialFetchResourcesRequested = Promise.all([ | ||
server.waitForRequest('/fetch-request-a.js'), | ||
server.waitForRequest('/fetch-request-b.js'), | ||
server.waitForRequest('/fetch-request-c.js'), | ||
waitForRequest('/fetch-request-a.js'), | ||
waitForRequest('/fetch-request-b.js'), | ||
waitForRequest('/fetch-request-c.js') | ||
]); | ||
|
||
let secondFetchResourceRequested; | ||
if (signal === 'networkidle0') { | ||
server.setRoute('/fetch-request-d.js', (req, res) => responses.d = res); | ||
secondFetchResourceRequested = server.waitForRequest('/fetch-request-d.js'); | ||
secondFetchResourceRequested = waitForRequest('/fetch-request-d.js'); | ||
} | ||
|
||
const waitForLoadPromise = isSetContent ? Promise.resolve() : frame.waitForNavigation({ waitUntil: 'load' }); | ||
|
@@ -442,10 +452,11 @@ module.exports.describe = function({testRunner, expect, playwright, MAC, WIN, FF | |
expect(responses.a).toBeTruthy(); | ||
expect(responses.b).toBeTruthy(); | ||
expect(responses.c).toBeTruthy(); | ||
// Finishing first response should leave 2 requests alive and trigger networkidle2. | ||
finishResponse(responses.a); | ||
|
||
let timer; | ||
let timerTriggered = false; | ||
if (signal === 'networkidle0') { | ||
// Finishing first response should leave 2 requests alive and trigger networkidle2. | ||
finishResponse(responses.a); | ||
// Finishing two more responses should trigger the second round. | ||
finishResponse(responses.b); | ||
finishResponse(responses.c); | ||
|
@@ -454,11 +465,16 @@ module.exports.describe = function({testRunner, expect, playwright, MAC, WIN, FF | |
await secondFetchResourceRequested; | ||
expect(actionFinished).toBe(false); | ||
// Finishing the last response should trigger networkidle0. | ||
timer = setTimeout(() => timerTriggered = true, 500); | ||
finishResponse(responses.d); | ||
} else { | ||
timer = setTimeout(() => timerTriggered = true, 500); | ||
// Finishing first response should leave 2 requests alive and trigger networkidle2. | ||
finishResponse(responses.a); | ||
} | ||
|
||
const response = await actionPromise; | ||
expect(performance.now() - lastResponseFinished).not.toBeLessThan(450); | ||
clearTimeout(timer); | ||
expect(timerTriggered).toBe(true); | ||
if (!isSetContent) | ||
expect(response.ok()).toBe(true); | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am curious why do we need to waitForRequest for the test to pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember, but it is definitely required. I'll look at it again tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The protocol events for firefox come in a strange order if we dont await the request events on the frontend:
Because A appears to be resolved before B and C are sent, networkidle2 is never trigged, and the timer resolves 500ms after the initial index.html request, not the responseRecieved A as was intended.