Skip to content

Commit

Permalink
App history: tweak and test event/promise ordering
Browse files Browse the repository at this point in the history
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}
  • Loading branch information
domenic authored and chromium-wpt-export-bot committed Jan 27, 2022
1 parent ed1f565 commit 3638c16
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
8 changes: 4 additions & 4 deletions app-history/navigate-event/transitionWhile-and-navigate.html
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>
30 changes: 0 additions & 30 deletions app-history/navigate/navigate-same-document-event-order.html

This file was deleted.

This file was deleted.

24 changes: 24 additions & 0 deletions 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.
63 changes: 63 additions & 0 deletions app-history/ordering/back-same-document.html
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>
61 changes: 61 additions & 0 deletions app-history/ordering/navigate-same-document.html
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 3638c16

Please sign in to comment.