Skip to content

Commit

Permalink
Improve flakiness service-workers/service-worker/claim-fetch.https.html
Browse files Browse the repository at this point in the history
This CL improves the flakiness in WPT claim-fetch.https.html by adding
a controllerchange event to wait until the controller is actually
updated.

Before this change, running the test with 1000 iterations on Linux got
the following result:
> "808 tests ran as expected, 192 didn't"
(100+ text diff / 50+ timeout / 1 crash / others pass)

After this change, running the test with 1000 iterations on Linux gets
the following result:
> "999 tests ran as expected, 1 didn't"
(1 timeout / 999 pass)

The WPT rarely causes the timeout but let's keep watching the result.

Bug: 1180076
Change-Id: Ia71921dda3b5bc6a22405a6363101466a9d83a0a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2721387
Commit-Queue: Asami Doi <[email protected]>
Reviewed-by: Kenichi Ishibashi <[email protected]>
Cr-Commit-Position: refs/heads/master@{#858869}
  • Loading branch information
d0iasm authored and chromium-wpt-export-bot committed Mar 2, 2021
1 parent b8fa2d5 commit 75b1ece
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions service-workers/service-worker/claim-fetch.https.html
Original file line number Diff line number Diff line change
@@ -1,80 +1,84 @@
<!doctype html>
<meta charset=utf-8>
<title></title>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<body>
<script>

async function try_fetch(fetch_func, path) {
async function tryFetch(fetchFunc, path) {
let response;
try {
response = await fetch_func(path);
response = await fetchFunc(path);
} catch (err) {
throw (`fetch() threw: ${err}`);
}

let response_text;
let responseText;
try {
response_text = await response.text();
responseText = await response.text();
} catch (err) {
throw (`text() threw: ${err}`);
}

return response_text;
return responseText;
}

promise_test(async function(t) {
let frame;
const scope = 'resources/';
const script = 'resources/claim-worker.js';
t.add_cleanup(async () => {
if (frame)
frame.remove();
return service_worker_unregister(t, scope);
});

const resource = 'simple.txt';

// Create the test frame.
await service_worker_unregister(t, scope);
frame = await with_iframe('resources/blank.html');
const frame = await with_iframe('resources/blank.html');
t.add_cleanup(() => frame.remove());

// Check the controller and test with fetch.
assert_equals(frame.contentWindow.navigator.controller, undefined,
'Should have no controller.');
let response;
try {
response = await try_fetch(frame.contentWindow.fetch, resource);
response = await tryFetch(frame.contentWindow.fetch, resource);
} catch (err) {
assert_unreached(`uncontrolled fetch failed: ${err}`);
}
assert_equals(response, 'a simple text file\n',
'fetch() should not be intercepted.');

// Register a service worker.
const registration = await navigator.serviceWorker.register(script, {scope});
const registration =
await service_worker_unregister_and_register(t, script, scope);
t.add_cleanup(() => registration.unregister());
const worker = registration.installing;
await wait_for_state(t, worker, 'activated');

// Register a controllerchange event to wait until the controller is updated
// and check if the frame is controlled by a service worker.
const controllerChanged = new Promise((resolve) => {
frame.contentWindow.navigator.serviceWorker.oncontrollerchange = () => {
resolve(frame.contentWindow.navigator.serviceWorker.controller);
};
});

// Tell the service worker to claim the iframe.
const saw_message = new Promise((resolve) => {
const sawMessage = new Promise((resolve) => {
const channel = new MessageChannel();
channel.port1.onmessage = t.step_func((event) => {
resolve(event.data);
});
worker.postMessage({port: channel.port2}, [channel.port2]);
});
const data = await saw_message;
const data = await sawMessage;
assert_equals(data, 'PASS', 'Worker call to claim() should fulfill.');

// Check the controller and test with fetch.
const controller = frame.contentWindow.navigator.serviceWorker.controller;
// Check if the controller is updated after claim() and test with fetch.
const controller = await controllerChanged;
assert_true(controller instanceof frame.contentWindow.ServiceWorker,
'iframe should be controlled.');
try {
response = await try_fetch(frame.contentWindow.fetch, resource);
response = await tryFetch(frame.contentWindow.fetch, resource);
} catch (err) {
assert_unreached(`controlled fetch failed: ${err}`);
}
Expand Down

0 comments on commit 75b1ece

Please sign in to comment.