Skip to content
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

Add tests for interaction between import() and microtask queue #35949

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// META: global=window,dedicatedworker,sharedworker
// META: script=ticker.js

promise_test(async t => {
const getCount = ticker(1000);

const importP = import("<invalid>");
await promise_rejects_js(t, TypeError, importP, 'import() should reject');

assert_less_than(getCount(), 1000);
}, "import() should not drain the microtask queue if it fails during specifier resolution");

promise_test(async t => {
// Use Date.now() to ensure that the module is not in the module map
const specifier = "./empty-module.js?" + Date.now();

await import(specifier);

const getCount = ticker(1000);
await import(specifier);
assert_less_than(getCount(), 1000);
}, "import() should not drain the microtask queue when loading an already loaded module");

promise_test(async t => {
// Use Date.now() to ensure that the module is not in the module map
const specifier = "./empty-module.js?" + Date.now();

const getCount = ticker(1e7);
await import(specifier);
assert_equals(getCount(), 1e7);
}, "import() should drain the microtask queue when fetching a new module");

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// META: global=dedicatedworker,sharedworker
// META: script=ticker.js

promise_test(async t => {
// Use Date.now() to ensure that the module is not in the module map
const specifier = "./empty-module.css?" + Date.now();

const getCount = ticker(1000);

const importP = import(specifier, { assert: { type: "css" } });
await promise_rejects_js(t, TypeError, importP, 'import() should reject');

assert_less_than(getCount(), 1000);
}, "import() should not drain the microtask queue if it fails because of the 'type: css' assertion in a worker");
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
This file is empty, because all it matters is if the
dynamic import that loads it fails or succedes.
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
This file is empty, because all it matters is if the
dynamic import that loads it fails or succedes.
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// META: global=serviceworker
// META: script=ticker.js

promise_test(async t => {
// Use Date.now() to ensure that the module is not in the module map
const specifier = "./empty-module.js?" + Date.now();

const getCount = ticker(1000);

const importP = import(specifier);
await promise_rejects_js(t, TypeError, importP, 'import() should reject');

assert_less_than(getCount(), 1000);
}, "import() should not drain the microtask queue if it fails because it's used in a ServiceWorker");
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
globalThis.ticker = function ticker(max) {
let i = 0;
let stop = false;
Promise.resolve().then(function loop() {
if (stop || i >= max) return;
i++;
Promise.resolve().then(loop);
});
return () => {
stop = true;
return i;
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// META: global=window,dedicatedworker,sharedworker
// META: script=ticker.js

promise_test(async t => {
// Use Date.now() to ensure that the module is not in the module map
const specifier = "./empty-module.js?" + Date.now();

const getCount = ticker(1000);

const importP = import(specifier, { assert: { type: "<invalid>" } });
await promise_rejects_js(t, TypeError, importP, 'import() should reject');

assert_less_than(getCount(), 1000);
}, "import() should not drain the microtask queue if it fails while validating the 'type' assertion");

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<canvas id ="output" width="100" height="100" style="background: blue;"></canvas>
<script>
"use strict";
const canvas = document.getElementById('output');
const ctx = canvas.getContext('2d');

ctx.fillStyle = 'green';
ctx.fillRect(0, 0, 100, 100);
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html class="reftest-wait">
<link rel="help" href="https://html.spec.whatwg.org/#hostimportmoduledynamically(referencingscriptormodule,-specifier,-promisecapability)">
<link rel="match" href="worklet-ref.html">
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved
<style>
#output {
width: 100px;
height: 100px;
background-image: paint(rects);
background-color: blue;
}
</style>
<script src="/common/reftest-wait.js"></script>
<script src="/common/worklet-reftest.js"></script>
<body>
<div id="output"></div>

<script id="code" type="text/worklet">
registerPaint('rects', class {
async paint(ctx, geom) {
ctx.fillStyle = 'red';

const getCount = ticker(1000);

try {
// Use Date.now() to ensure that the module is not in the module map
await import("./empty-module.js?" + Date.now());
} catch (e) {
if (getCount() < 1000) {
ctx.fillStyle = 'green';
}
}
ctx.fillRect(0, 0, geom.width, geom.height);
}
});
</script>

<script>
"use strict";
CSS.paintWorklet.addModule("./ticker.js").then(() =>
importWorkletAndTerminateTestAfterAsyncPaint(CSS.paintWorklet, document.getElementById('code').textContent)
);
</script>