Skip to content

Commit

Permalink
Bug 1751518 [wpt PR 32504] - App history: tweak and test event/promis…
Browse files Browse the repository at this point in the history
…e ordering, a=testonly

Automatic update from web-platform-tests
App history: tweak and test event/promise ordering

By adding new exhaustive tests under ordering/, it was revealed that the ordering between navigatesuccess/navigateerror and the committed/finished promises was not always consistent:

1. Simply adding a currentchange event handler would cause microtasks to run during commit, which changed some ordering.

2. Calling transitionWhile() would take us from the zero-promise case to the 1+-promise case in ScriptPromise::All(). As the new comment explains, both the spec and implementation have an observably-different fast path for the 0-promise case which caused changes in ordering.

In the course of fixing this, I found out that the did_finish_before_commit_ code in app_history_api_navigation.{h,cc} was actually not a mitigation for the case it stated, where promises passed to transitionWhile() would settle faster than the browser-process roundtrip for same-document traversals. That is in fact impossible, since we only fire the navigate event after the browser-process roundtrip has completed. Instead, they were a mitigation for (1).

This commit then ensures consistent ordering, tested with new rather-exhaustive tests, in the following manner:

* We move the firing of currentchange to before resolving the committed promise. This eliminates (1) and allows us to delete the did_finish_before_commit_ tracking.

* We always ensure we pass 1+ promises to ScriptPromise::All(). This eliminates (2).

A consequence of this is that we are now more likely to get rejected finished promises, in cases like

    await appHistory.navigate("#1").committed;
    await appHistory.navigate("#2").committed;

Before, the finished promise for the #1 navigation would go through the fast path per (2), and fulfill before the navigation to #2 canceled it. Now that does not happen, so code like the above will give an unhandled promise rejection for #1's finished promise.

To avoid this, we unconditionally mark finished promises as handled. This follows some web platform precedent, e.g. stream closed promises, where the promise is one of several information channels (in this case the developer might also find out via the AbortSignal or the navigateerror event). We do *not* do this for the committed promise though, as if a commit fails, that's probably something more deeply wrong, and cannot be ignored.

All of these changes will require spec updates.

For the tests, we introduce a new ordering/ directory which contains cross-cutting ordering tests, and we consolidate a few tests into the newly-introduced variant-driven exhaustive ones. A couple of other tests were affected by these changes too or fixed as a drive-by.

Change-Id: I8a50ca28d009e0a8a2c94331cd17f29b0a8dc463
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3405377
Reviewed-by: Nate Chapin <japhet@chromium.org>
Commit-Queue: Domenic Denicola <domenic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#963772}

--

wpt-commits: 3638c168c6db3f12a8d72f1e70e2d66ced788528
wpt-pr: 32504
  • Loading branch information
domenic authored and moz-wptsync-bot committed Mar 7, 2022
1 parent 120dca5 commit dd5cb8c
Show file tree
Hide file tree
Showing 16 changed files with 210 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Wait for after the load event so that the navigation doesn't get converted
// into a replace navigation.
await new Promise(resolve => window.onload = t.step_timeout(resolve, 0));
await appHistory.navigate("#foo");
await appHistory.navigate("#foo").committed;
assert_equals(appHistory.entries().length, 2);

let oncurrentchange_back_called = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
assert_equals(appHistory.entries().length, 2);
appHistory.navigate("#2");
}
}
};
let back_result = appHistory.back();
await promise_rejects_dom(t, "AbortError", back_result.committed);
await promise_rejects_dom(t, "AbortError", back_result.finished);
await back_result.committed;
assert_equals(location.hash, "#2");
await promise_rejects_dom(t, "AbortError", back_result.finished);
assert_equals(appHistory.current.index, 1);
assert_equals(appHistory.entries().length, 2);
}, "Using transitionWhile then navigate() in the ensuing currentchange should abort the transitionWhile");
}, "Using transitionWhile() then navigate() in the ensuing currentchange should abort the finished promise (but not the committed promise)");
</script>

This file was deleted.

This file was deleted.

24 changes: 24 additions & 0 deletions testing/web-platform/tests/app-history/ordering/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# App history ordering tests

These are meant to test the ordering between various events and promises; they
don't fit into any particular sibling directory.

Some of them test simple cases rather-exhaustively, and others test tricky cases
(e.g. reentrancy or navigations aborting previous navigations) in a more focused
way.

Note:

* Variants specifically exist for `currentchange` because an event listener
existing for `currentchange` causes code to run, and thus microtasks to run,
at a very specific point in the navigation-commit lifecycle. We want to test
that it doesn't impact the ordering.
* Similarly we test that `transitionWhile(Promise.resolve())` does not change
the ordering compared to no `transitionWhile()` call, for same-document
navigations.

TODOs:

* Also test `appHistory.transition.finished` when it is implemented.
* Also test `popstate` and `hashchange` once
<https://github.com/whatwg/html/issues/1792> is fixed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<meta name="variant" content="?">
<meta name="variant" content="?transitionWhile">
<meta name="variant" content="?currentchange">
<meta name="variant" content="?transitionWhile&currentChange">

<script>
const variants = new Set((new URLSearchParams(location.search)).keys());

promise_test(async t => {
// Wait for after the load event so that the navigation doesn't get converted
// into a replace navigation.
await new Promise(resolve => window.onload = () => t.step_timeout(resolve, 0));

await appHistory.navigate("#1").finished;

const events = [];
appHistory.addEventListener("navigatesuccess", () => events.push(`navigatesuccess ${location.hash}`));
appHistory.addEventListener("navigateerror", () => events.push(`navigateerror ${location.hash}`));
if (variants.has("currentchange")) {
appHistory.addEventListener("currentchange", () => events.push(`currentchange ${location.hash}`));
}

if (variants.has("transitionWhile")) {
appHistory.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve());
});
}

const { committed, finished } = appHistory.back();
committed.then(
() => events.push(`committed fulfilled ${location.hash}`),
() => events.push(`committed rejected ${location.hash}`)
);
finished.then(
() => events.push(`finished fulfilled ${location.hash}`),
() => events.push(`finished rejected ${location.hash}`)
);

Promise.resolve().then(() => events.push(`promise microtask ${location.hash}`));

await finished;

if (variants.has("currentchange")) {
assert_array_equals(events, [
"promise microtask #1",
"currentchange ",
"committed fulfilled ",
"navigatesuccess ",
"finished fulfilled "
]);
} else {
assert_array_equals(events, [
"promise microtask #1",
"committed fulfilled ",
"navigatesuccess ",
"finished fulfilled "
]);
}
}, "event and promise ordering for same-document appHistory.back()");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<meta name="variant" content="?">
<meta name="variant" content="?currentchange">

<script>
const variants = new Set((new URLSearchParams(location.search)).keys());

promise_test(async t => {
// Wait for after the load event so that the navigation doesn't get converted
// into a replace navigation.
await new Promise(resolve => window.onload = () => t.step_timeout(resolve, 0));

const events = [];
appHistory.addEventListener("navigatesuccess", () => events.push(`navigatesuccess ${location.hash}`));
appHistory.addEventListener("navigateerror", () => events.push(`navigateerror ${location.hash}`));
if (variants.has("currentchange")) {
appHistory.addEventListener("currentchange", () => events.push(`currentchange ${location.hash}`));
}

appHistory.addEventListener("navigate", e => {
e.transitionWhile(Promise.reject());
});

const { committed, finished } = appHistory.navigate("#1");
committed.then(
() => events.push(`committed fulfilled ${location.hash}`),
() => events.push(`committed rejected ${location.hash}`)
);
finished.then(
() => events.push(`finished fulfilled ${location.hash}`),
() => events.push(`finished rejected ${location.hash}`)
);

Promise.resolve().then(() => events.push(`promise microtask ${location.hash}`));

await finished.catch(() => {});

if (variants.has("currentchange")) {
assert_array_equals(events, [
"currentchange #1",
"committed fulfilled #1",
"promise microtask #1",
"navigateerror #1",
"finished rejected #1"
]);
} else {
assert_array_equals(events, [
"committed fulfilled #1",
"promise microtask #1",
"navigateerror #1",
"finished rejected #1"
]);
}
}, "event and promise ordering for appHistory.navigate() with a rejected promise passed to transitionWhile()");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<meta name="variant" content="?">
<meta name="variant" content="?transitionWhile">
<meta name="variant" content="?currentchange">
<meta name="variant" content="?transitionWhile&currentChange">

<script>
const variants = new Set((new URLSearchParams(location.search)).keys());

promise_test(async t => {
// Wait for after the load event so that the navigation doesn't get converted
// into a replace navigation.
await new Promise(resolve => window.onload = () => t.step_timeout(resolve, 0));

const events = [];
appHistory.addEventListener("navigatesuccess", () => events.push(`navigatesuccess ${location.hash}`));
appHistory.addEventListener("navigateerror", () => events.push(`navigateerror ${location.hash}`));
if (variants.has("currentchange")) {
appHistory.addEventListener("currentchange", () => events.push(`currentchange ${location.hash}`));
}

if (variants.has("transitionWhile")) {
appHistory.addEventListener("navigate", e => {
e.transitionWhile(Promise.resolve());
});
}

const { committed, finished } = appHistory.navigate("#1");
committed.then(
() => events.push(`committed fulfilled ${location.hash}`),
() => events.push(`committed rejected ${location.hash}`)
);
finished.then(
() => events.push(`finished fulfilled ${location.hash}`),
() => events.push(`finished rejected ${location.hash}`)
);

Promise.resolve().then(() => events.push(`promise microtask ${location.hash}`));

await finished;

if (variants.has("currentchange")) {
assert_array_equals(events, [
"currentchange #1",
"committed fulfilled #1",
"promise microtask #1",
"navigatesuccess #1",
"finished fulfilled #1"
]);
} else {
assert_array_equals(events, [
"committed fulfilled #1",
"promise microtask #1",
"navigatesuccess #1",
"finished fulfilled #1"
]);
}
}, "event and promise ordering for same-document appHistory.navigate()");
</script>

0 comments on commit dd5cb8c

Please sign in to comment.