Truly independent web browser
Tests/LibWeb: Test terminating workers that run script
Andreas Klingfce9a499941f
diff --git a/Tests/LibWeb/Text/expected/Workers/worker-terminate-in-nested-event-loop.txt b/Tests/LibWeb/Text/expected/Workers/worker-terminate-in-nested-event-loop.txtnew file mode 100644index 00000000000..ff9e44cb3c6--- /dev/null+++ b/Tests/LibWeb/Text/expected/Workers/worker-terminate-in-nested-event-loop.txt@@ -0,0 +1,2 @@+synchronous XMLHttpRequest: waiting+importScripts(): waitingdiff --git a/Tests/LibWeb/Text/expected/Workers/worker-terminate-in-wait.txt b/Tests/LibWeb/Text/expected/Workers/worker-terminate-in-wait.txtnew file mode 100644index 00000000000..af9e3db86b9--- /dev/null+++ b/Tests/LibWeb/Text/expected/Workers/worker-terminate-in-wait.txt@@ -0,0 +1,4 @@+control: notified 1+Atomics.wait: stopped+Atomics.wait with a timeout: stopped+memory.atomic.wait: stoppeddiff --git a/Tests/LibWeb/Text/expected/Workers/worker-terminate-running-script.txt b/Tests/LibWeb/Text/expected/Workers/worker-terminate-running-script.txtnew file mode 100644index 00000000000..458271f2d32--- /dev/null+++ b/Tests/LibWeb/Text/expected/Workers/worker-terminate-running-script.txt@@ -0,0 +1,10 @@+plain loop: stopped+compiled loop: stopped+finally: stopped+catch: stopped+generator: stopped+async function: stopped+promise jobs: stopped+event handler: stopped+regular expression: stopped+WebAssembly: stoppeddiff --git a/Tests/LibWeb/Text/input/Workers/worker-terminate-in-nested-event-loop.html b/Tests/LibWeb/Text/input/Workers/worker-terminate-in-nested-event-loop.htmlnew file mode 100644index 00000000000..ea14738be5f--- /dev/null+++ b/Tests/LibWeb/Text/input/Workers/worker-terminate-in-nested-event-loop.html@@ -0,0 +1,41 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // NB: Each of these waits in a nested event loop, which only the abort gets it out of.+ promiseTest(async () => {+ const server = httpTestServer();+ const slowURL = await server.createEcho("GET", "/worker-terminate-in-nested-event-loop", {+ status: 200,+ headers: { "Access-Control-Allow-Origin": "*" },+ body: "too late",+ delay_ms: 3000,+ });++ const SCRIPTS = {+ "synchronous XMLHttpRequest": `+ const request = new XMLHttpRequest();+ request.open("GET", ${JSON.stringify(slowURL)}, false);+ postMessage("waiting");+ request.send();+ postMessage("too late");+ `,+ "importScripts()": `+ postMessage("waiting");+ importScripts(${JSON.stringify(slowURL)});+ postMessage("too late");+ `,+ };++ for (const [name, source] of Object.entries(SCRIPTS)) {+ const worker = new Worker(URL.createObjectURL(new Blob([source], { type: "text/javascript" })));+ const messages = [];+ worker.onmessage = event => messages.push(event.data);+ worker.onerror = () => messages.push("error");+ await waitForCondition(() => messages.length >= 1);+ await timeout(50);+ worker.terminate();+ await timeout(50);+ println(`${name}: ${messages.join(", ")}`);+ }+ });+</script>diff --git a/Tests/LibWeb/Text/input/Workers/worker-terminate-in-wait.html b/Tests/LibWeb/Text/input/Workers/worker-terminate-in-wait.htmlnew file mode 100644index 00000000000..30b49bb9492--- /dev/null+++ b/Tests/LibWeb/Text/input/Workers/worker-terminate-in-wait.html@@ -0,0 +1,83 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ const ATOMICS_WAIT = `+ const memory = new SharedArrayBuffer(8);+ const words = new Int32Array(memory);+ self.postMessage(memory);+ Atomics.store(words, 1, 1);+ Atomics.wait(words, 0, 0, TIMEOUT);+ self.postMessage("woken");+ `;++ const MEMORY_ATOMIC_WAIT = `+ const memory = new WebAssembly.Memory({ initial: 1, maximum: 1, shared: true });+ // NB: The buffer of a shared memory reaches another agent as a copy, so this one tells that the wait is near.+ const words = new Int32Array(new SharedArrayBuffer(8));+ self.postMessage(words.buffer);+ const instance = new WebAssembly.Instance(+ new WebAssembly.Module(+ new Uint8Array([+ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00, 0x02, 0x09, 0x01,+ 0x01, 0x65, 0x01, 0x6d, 0x02, 0x03, 0x01, 0x01, 0x03, 0x02, 0x01, 0x00, 0x07, 0x05, 0x01, 0x01, 0x66,+ 0x00, 0x00, 0x0a, 0x0f, 0x01, 0x0d, 0x00, 0x41, 0x00, 0x41, 0x00, 0x42, 0x7f, 0xfe, 0x01, 0x02, 0x00,+ 0x1a, 0x0b,+ ])+ ),+ { e: { m: memory } }+ );+ Atomics.store(words, 1, 1);+ instance.exports.f();+ self.postMessage("woken");+ `;++ // NB: A notify finds nobody on the word once terminate() ended the wait, and the worker ran nothing past it. The worker+ // sets word 1 right before it waits, and terminate() ends a wait that starts after it as well, so that nothing here+ // depends on how long anything takes.+ async function check(name, body) {+ const script = URL.createObjectURL(new Blob([body], { type: "text/javascript" }));+ const worker = new Worker(script);+ worker.onerror = () => println(`${name}: error`);+ let words = null;+ let wasWoken = false;+ worker.onmessage = ({ data }) => {+ if (data === "woken") wasWoken = true;+ else words = new Int32Array(data);+ };+ await waitForCondition(() => words !== null && Atomics.load(words, 1) === 1);++ // NB: terminate() takes the way through the browser process, so only the end of the thread shows that it arrived.+ worker.terminate();+ await waitForCondition(() => internals.workerAgentCount() === 0);+ const notified = Atomics.notify(words, 0, 1);+ println(`${name}: ${notified === 0 && !wasWoken ? "stopped" : "still waiting"}`);+ }++ // NB: Without terminate(), the same notify wakes the worker. So this is what the checks above would see for a wait+ // that nothing ended.+ async function checkControl() {+ const script = URL.createObjectURL(new Blob([ATOMICS_WAIT.replace("TIMEOUT", "Infinity")], { type: "text/javascript" }));+ const worker = new Worker(script);+ let words = null;+ let wasWoken = false;+ worker.onmessage = ({ data }) => {+ if (data === "woken") wasWoken = true;+ else words = new Int32Array(data);+ };+ await waitForCondition(() => words !== null);+ // NB: A busy machine can take any time to get the worker to the wait.+ let notified = 0;+ await waitForCondition(() => (notified = Atomics.notify(words, 0, 1)) !== 0);+ await waitForCondition(() => wasWoken);+ worker.terminate();+ await waitForCondition(() => internals.workerAgentCount() === 0);+ println(`control: notified ${notified}`);+ }++ promiseTest(async () => {+ await checkControl();+ await check("Atomics.wait", ATOMICS_WAIT.replace("TIMEOUT", "Infinity"));+ await check("Atomics.wait with a timeout", ATOMICS_WAIT.replace("TIMEOUT", "600000"));+ await check("memory.atomic.wait", MEMORY_ATOMIC_WAIT);+ });+</script>diff --git a/Tests/LibWeb/Text/input/Workers/worker-terminate-in-wait.html.headers b/Tests/LibWeb/Text/input/Workers/worker-terminate-in-wait.html.headersnew file mode 100644index 00000000000..63b60e490f4--- /dev/null+++ b/Tests/LibWeb/Text/input/Workers/worker-terminate-in-wait.html.headers@@ -0,0 +1,2 @@+Cross-Origin-Opener-Policy: same-origin+Cross-Origin-Embedder-Policy: require-corpdiff --git a/Tests/LibWeb/Text/input/Workers/worker-terminate-running-script.html b/Tests/LibWeb/Text/input/Workers/worker-terminate-running-script.htmlnew file mode 100644index 00000000000..c943d0f5cbb--- /dev/null+++ b/Tests/LibWeb/Text/input/Workers/worker-terminate-running-script.html@@ -0,0 +1,126 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // NB: The script of a worker that terminate() aborts stops ticking long before the UI process would kill the worker.+ const TICK = `+ const channel = new BroadcastChannel(self.name);+ let nextTick = 0;+ function tick() {+ const now = performance.now();+ if (now < nextTick) return;+ nextTick = now + 5;+ channel.postMessage("tick");+ }+ `;++ const WASM_LOOP_AROUND_IMPORT = `+ new WebAssembly.Instance(+ new WebAssembly.Module(+ new Uint8Array([+ 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, 0x00, 0x00, 0x02, 0x07, 0x01,+ 0x01, 0x65, 0x01, 0x74, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x07, 0x05, 0x01, 0x01, 0x66, 0x00, 0x01,+ 0x0a, 0x0b, 0x01, 0x09, 0x00, 0x03, 0x40, 0x10, 0x00, 0x0c, 0x00, 0x0b, 0x0b,+ ])+ ),+ { e: { t: tick } }+ ).exports.f();+ `;++ const SCRIPTS = {+ "plain loop": "while (true) tick();",+ "compiled loop": `+ function hot() {+ let sum = 0;+ for (let i = 0; i < 100000; ++i) sum += i;+ return sum;+ }+ while (true) {+ hot();+ tick();+ }+ `,+ "finally": `+ try {+ while (true) tick();+ } finally {+ while (true) tick();+ }+ `,+ "catch": `+ while (true) {+ try {+ while (true) tick();+ } catch (error) {}+ }+ `,+ "generator": `+ function* forever() {+ for (;;) {+ tick();+ yield;+ }+ }+ for (const value of forever());+ `,+ "async function": `+ (async () => {+ for (;;) {+ tick();+ await 0;+ }+ })();+ `,+ "promise jobs": `+ function again() {+ tick();+ Promise.resolve().then(again);+ }+ again();+ `,+ "event handler": `+ self.onmessage = () => {+ while (true) tick();+ };+ self.postMessage("ready");+ `,+ "regular expression": `+ while (true) {+ tick();+ /(a+)+b/.test("a".repeat(25));+ }+ `,+ "WebAssembly": WASM_LOOP_AROUND_IMPORT,+ };++ async function check(name, body) {+ const channel = new BroadcastChannel(name);+ let ticks = 0;+ channel.onmessage = () => ++ticks;++ const script = URL.createObjectURL(new Blob([TICK + body], { type: "text/javascript" }));+ const worker = new Worker(script, { name });+ worker.onerror = () => println(`${name}: error`);+ worker.onmessage = () => worker.postMessage("go");+ await waitForCondition(() => ticks >= 3, 5000, 1);++ worker.terminate();+ // NB: Nothing tells when the script of a terminated worker stopped, so this waits until no tick came for 50 ms.+ // A script that keeps running ticks every 5 ms.+ let lastTicks = ticks;+ let quietSince = performance.now();+ const giveUpAt = quietSince + 5000;+ while (performance.now() - quietSince < 50 && performance.now() < giveUpAt) {+ await timeout(5);+ if (ticks !== lastTicks) {+ lastTicks = ticks;+ quietSince = performance.now();+ }+ }+ println(`${name}: ${performance.now() - quietSince >= 50 ? "stopped" : "still running"}`);+ channel.close();+ }++ promiseTest(async () => {+ for (const [name, body] of Object.entries(SCRIPTS)) await check(name, body);+ });+</script>