Skip to content

Commit 3638c16

Browse files
domenicchromium-wpt-export-bot
authored andcommitted
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}
1 parent ed1f565 commit 3638c16

16 files changed

+210
-75
lines changed

app-history/currentchange-event/currentchange-app-history-back-forward-same-doc.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Wait for after the load event so that the navigation doesn't get converted
77
// into a replace navigation.
88
await new Promise(resolve => window.onload = t.step_timeout(resolve, 0));
9-
await appHistory.navigate("#foo");
9+
await appHistory.navigate("#foo").committed;
1010
assert_equals(appHistory.entries().length, 2);
1111

1212
let oncurrentchange_back_called = false;

app-history/navigate-event/transitionWhile-and-navigate.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
assert_equals(appHistory.entries().length, 2);
1717
appHistory.navigate("#2");
1818
}
19-
}
19+
};
2020
let back_result = appHistory.back();
21-
await promise_rejects_dom(t, "AbortError", back_result.committed);
22-
await promise_rejects_dom(t, "AbortError", back_result.finished);
21+
await back_result.committed;
2322
assert_equals(location.hash, "#2");
23+
await promise_rejects_dom(t, "AbortError", back_result.finished);
2424
assert_equals(appHistory.current.index, 1);
2525
assert_equals(appHistory.entries().length, 2);
26-
}, "Using transitionWhile then navigate() in the ensuing currentchange should abort the transitionWhile");
26+
}, "Using transitionWhile() then navigate() in the ensuing currentchange should abort the finished promise (but not the committed promise)");
2727
</script>

app-history/navigate/navigate-same-document-event-order.html

-30
This file was deleted.

app-history/navigate/navigate-transitionWhile-reject-event-order.html

-40
This file was deleted.

app-history/ordering/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# App history ordering tests
2+
3+
These are meant to test the ordering between various events and promises; they
4+
don't fit into any particular sibling directory.
5+
6+
Some of them test simple cases rather-exhaustively, and others test tricky cases
7+
(e.g. reentrancy or navigations aborting previous navigations) in a more focused
8+
way.
9+
10+
Note:
11+
12+
* Variants specifically exist for `currentchange` because an event listener
13+
existing for `currentchange` causes code to run, and thus microtasks to run,
14+
at a very specific point in the navigation-commit lifecycle. We want to test
15+
that it doesn't impact the ordering.
16+
* Similarly we test that `transitionWhile(Promise.resolve())` does not change
17+
the ordering compared to no `transitionWhile()` call, for same-document
18+
navigations.
19+
20+
TODOs:
21+
22+
* Also test `appHistory.transition.finished` when it is implemented.
23+
* Also test `popstate` and `hashchange` once
24+
<https://github.com/whatwg/html/issues/1792> is fixed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!doctype html>
2+
<script src="/resources/testharness.js"></script>
3+
<script src="/resources/testharnessreport.js"></script>
4+
<meta name="variant" content="?">
5+
<meta name="variant" content="?transitionWhile">
6+
<meta name="variant" content="?currentchange">
7+
<meta name="variant" content="?transitionWhile&currentChange">
8+
9+
<script>
10+
const variants = new Set((new URLSearchParams(location.search)).keys());
11+
12+
promise_test(async t => {
13+
// Wait for after the load event so that the navigation doesn't get converted
14+
// into a replace navigation.
15+
await new Promise(resolve => window.onload = () => t.step_timeout(resolve, 0));
16+
17+
await appHistory.navigate("#1").finished;
18+
19+
const events = [];
20+
appHistory.addEventListener("navigatesuccess", () => events.push(`navigatesuccess ${location.hash}`));
21+
appHistory.addEventListener("navigateerror", () => events.push(`navigateerror ${location.hash}`));
22+
if (variants.has("currentchange")) {
23+
appHistory.addEventListener("currentchange", () => events.push(`currentchange ${location.hash}`));
24+
}
25+
26+
if (variants.has("transitionWhile")) {
27+
appHistory.addEventListener("navigate", e => {
28+
e.transitionWhile(Promise.resolve());
29+
});
30+
}
31+
32+
const { committed, finished } = appHistory.back();
33+
committed.then(
34+
() => events.push(`committed fulfilled ${location.hash}`),
35+
() => events.push(`committed rejected ${location.hash}`)
36+
);
37+
finished.then(
38+
() => events.push(`finished fulfilled ${location.hash}`),
39+
() => events.push(`finished rejected ${location.hash}`)
40+
);
41+
42+
Promise.resolve().then(() => events.push(`promise microtask ${location.hash}`));
43+
44+
await finished;
45+
46+
if (variants.has("currentchange")) {
47+
assert_array_equals(events, [
48+
"promise microtask #1",
49+
"currentchange ",
50+
"committed fulfilled ",
51+
"navigatesuccess ",
52+
"finished fulfilled "
53+
]);
54+
} else {
55+
assert_array_equals(events, [
56+
"promise microtask #1",
57+
"committed fulfilled ",
58+
"navigatesuccess ",
59+
"finished fulfilled "
60+
]);
61+
}
62+
}, "event and promise ordering for same-document appHistory.back()");
63+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!doctype html>
2+
<script src="/resources/testharness.js"></script>
3+
<script src="/resources/testharnessreport.js"></script>
4+
<meta name="variant" content="?">
5+
<meta name="variant" content="?currentchange">
6+
7+
<script>
8+
const variants = new Set((new URLSearchParams(location.search)).keys());
9+
10+
promise_test(async t => {
11+
// Wait for after the load event so that the navigation doesn't get converted
12+
// into a replace navigation.
13+
await new Promise(resolve => window.onload = () => t.step_timeout(resolve, 0));
14+
15+
const events = [];
16+
appHistory.addEventListener("navigatesuccess", () => events.push(`navigatesuccess ${location.hash}`));
17+
appHistory.addEventListener("navigateerror", () => events.push(`navigateerror ${location.hash}`));
18+
if (variants.has("currentchange")) {
19+
appHistory.addEventListener("currentchange", () => events.push(`currentchange ${location.hash}`));
20+
}
21+
22+
appHistory.addEventListener("navigate", e => {
23+
e.transitionWhile(Promise.reject());
24+
});
25+
26+
const { committed, finished } = appHistory.navigate("#1");
27+
committed.then(
28+
() => events.push(`committed fulfilled ${location.hash}`),
29+
() => events.push(`committed rejected ${location.hash}`)
30+
);
31+
finished.then(
32+
() => events.push(`finished fulfilled ${location.hash}`),
33+
() => events.push(`finished rejected ${location.hash}`)
34+
);
35+
36+
Promise.resolve().then(() => events.push(`promise microtask ${location.hash}`));
37+
38+
await finished.catch(() => {});
39+
40+
if (variants.has("currentchange")) {
41+
assert_array_equals(events, [
42+
"currentchange #1",
43+
"committed fulfilled #1",
44+
"promise microtask #1",
45+
"navigateerror #1",
46+
"finished rejected #1"
47+
]);
48+
} else {
49+
assert_array_equals(events, [
50+
"committed fulfilled #1",
51+
"promise microtask #1",
52+
"navigateerror #1",
53+
"finished rejected #1"
54+
]);
55+
}
56+
}, "event and promise ordering for appHistory.navigate() with a rejected promise passed to transitionWhile()");
57+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!doctype html>
2+
<script src="/resources/testharness.js"></script>
3+
<script src="/resources/testharnessreport.js"></script>
4+
<meta name="variant" content="?">
5+
<meta name="variant" content="?transitionWhile">
6+
<meta name="variant" content="?currentchange">
7+
<meta name="variant" content="?transitionWhile&currentChange">
8+
9+
<script>
10+
const variants = new Set((new URLSearchParams(location.search)).keys());
11+
12+
promise_test(async t => {
13+
// Wait for after the load event so that the navigation doesn't get converted
14+
// into a replace navigation.
15+
await new Promise(resolve => window.onload = () => t.step_timeout(resolve, 0));
16+
17+
const events = [];
18+
appHistory.addEventListener("navigatesuccess", () => events.push(`navigatesuccess ${location.hash}`));
19+
appHistory.addEventListener("navigateerror", () => events.push(`navigateerror ${location.hash}`));
20+
if (variants.has("currentchange")) {
21+
appHistory.addEventListener("currentchange", () => events.push(`currentchange ${location.hash}`));
22+
}
23+
24+
if (variants.has("transitionWhile")) {
25+
appHistory.addEventListener("navigate", e => {
26+
e.transitionWhile(Promise.resolve());
27+
});
28+
}
29+
30+
const { committed, finished } = appHistory.navigate("#1");
31+
committed.then(
32+
() => events.push(`committed fulfilled ${location.hash}`),
33+
() => events.push(`committed rejected ${location.hash}`)
34+
);
35+
finished.then(
36+
() => events.push(`finished fulfilled ${location.hash}`),
37+
() => events.push(`finished rejected ${location.hash}`)
38+
);
39+
40+
Promise.resolve().then(() => events.push(`promise microtask ${location.hash}`));
41+
42+
await finished;
43+
44+
if (variants.has("currentchange")) {
45+
assert_array_equals(events, [
46+
"currentchange #1",
47+
"committed fulfilled #1",
48+
"promise microtask #1",
49+
"navigatesuccess #1",
50+
"finished fulfilled #1"
51+
]);
52+
} else {
53+
assert_array_equals(events, [
54+
"committed fulfilled #1",
55+
"promise microtask #1",
56+
"navigatesuccess #1",
57+
"finished fulfilled #1"
58+
]);
59+
}
60+
}, "event and promise ordering for same-document appHistory.navigate()");
61+
</script>

0 commit comments

Comments
 (0)