2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-03-03 12:22:53 -05:00
|
|
|
|
|
|
|
// Requires to be run with `--allow-net` flag
|
|
|
|
|
2021-01-06 15:31:16 -05:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2021-10-14 05:44:33 -04:00
|
|
|
assertMatch,
|
2021-01-06 15:31:16 -05:00
|
|
|
assertThrows,
|
2024-02-12 21:05:10 -05:00
|
|
|
} from "@std/assert/mod.ts";
|
2024-02-08 15:09:47 -05:00
|
|
|
|
|
|
|
function resolveWorker(worker: string): string {
|
|
|
|
return import.meta.resolve(`../testdata/workers/${worker}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: true } },
|
|
|
|
function utimeSyncFileSuccess() {
|
|
|
|
const w = new Worker(
|
|
|
|
resolveWorker("worker_types.ts"),
|
|
|
|
{ type: "module" },
|
|
|
|
);
|
|
|
|
assert(w);
|
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
);
|
2020-03-03 12:22:53 -05:00
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
Deno.test({
|
2020-04-09 18:15:17 -04:00
|
|
|
name: "worker terminate",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2020-06-09 08:33:52 -04:00
|
|
|
const jsWorker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("test_worker.js"),
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
|
|
|
const tsWorker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("test_worker.ts"),
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module", name: "tsWorker" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-03-18 19:25:55 -04:00
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const deferred1 = Promise.withResolvers<string>();
|
2021-10-14 05:44:33 -04:00
|
|
|
jsWorker.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred1.resolve(e.data);
|
2020-03-18 19:25:55 -04:00
|
|
|
};
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const deferred2 = Promise.withResolvers<string>();
|
2021-10-14 05:44:33 -04:00
|
|
|
tsWorker.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred2.resolve(e.data);
|
2020-03-18 19:25:55 -04:00
|
|
|
};
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
jsWorker.postMessage("Hello World");
|
2023-11-22 06:11:20 -05:00
|
|
|
assertEquals(await deferred1.promise, "Hello World");
|
2021-10-14 05:44:33 -04:00
|
|
|
tsWorker.postMessage("Hello World");
|
2023-11-22 06:11:20 -05:00
|
|
|
assertEquals(await deferred2.promise, "Hello World");
|
2020-04-09 18:15:17 -04:00
|
|
|
tsWorker.terminate();
|
|
|
|
jsWorker.terminate();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-02-11 04:04:59 -05:00
|
|
|
});
|
|
|
|
|
2021-01-05 21:22:38 -05:00
|
|
|
Deno.test({
|
|
|
|
name: "worker from data url",
|
|
|
|
async fn() {
|
|
|
|
const tsWorker = new Worker(
|
|
|
|
"data:application/typescript;base64,aWYgKHNlbGYubmFtZSAhPT0gInRzV29ya2VyIikgewogIHRocm93IEVycm9yKGBJbnZhbGlkIHdvcmtlciBuYW1lOiAke3NlbGYubmFtZX0sIGV4cGVjdGVkIHRzV29ya2VyYCk7Cn0KCm9ubWVzc2FnZSA9IGZ1bmN0aW9uIChlKTogdm9pZCB7CiAgcG9zdE1lc3NhZ2UoZS5kYXRhKTsKICBjbG9zZSgpOwp9Owo=",
|
|
|
|
{ type: "module", name: "tsWorker" },
|
|
|
|
);
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<string>();
|
2021-08-05 07:08:58 -04:00
|
|
|
tsWorker.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2021-01-05 21:22:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
tsWorker.postMessage("Hello World");
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(await promise, "Hello World");
|
2021-01-05 21:22:38 -05:00
|
|
|
tsWorker.terminate();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
Deno.test({
|
2020-04-09 18:15:17 -04:00
|
|
|
name: "worker nested",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2020-06-09 08:33:52 -04:00
|
|
|
const nestedWorker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("nested_worker.js"),
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module", name: "nested" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-03-18 19:25:55 -04:00
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const { promise, resolve } = Promise.withResolvers<any>();
|
2021-08-05 07:08:58 -04:00
|
|
|
nestedWorker.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2020-03-18 19:25:55 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
nestedWorker.postMessage("Hello World");
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(await promise, { type: "msg", text: "Hello World" });
|
2020-04-09 18:15:17 -04:00
|
|
|
nestedWorker.terminate();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-02-11 04:04:59 -05:00
|
|
|
});
|
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
Deno.test({
|
2020-04-09 18:15:17 -04:00
|
|
|
name: "worker throws when executing",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2020-06-09 08:33:52 -04:00
|
|
|
const throwingWorker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("throwing_worker.js"),
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-03-18 19:25:55 -04:00
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<string>();
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2021-08-05 07:08:58 -04:00
|
|
|
throwingWorker.onerror = (e: any) => {
|
2020-03-18 19:25:55 -04:00
|
|
|
e.preventDefault();
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.message);
|
2020-03-18 19:25:55 -04:00
|
|
|
};
|
|
|
|
|
2023-12-06 19:02:52 -05:00
|
|
|
assertMatch(
|
|
|
|
await promise as string,
|
|
|
|
/Uncaught \(in promise\) Error: Thrown error/,
|
|
|
|
);
|
2020-04-09 18:15:17 -04:00
|
|
|
throwingWorker.terminate();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-02-11 04:04:59 -05:00
|
|
|
});
|
2020-02-21 10:35:41 -05:00
|
|
|
|
2020-10-11 18:04:43 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "worker globals",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2021-01-18 21:26:39 -05:00
|
|
|
const workerOptions: WorkerOptions = { type: "module" };
|
2020-10-11 18:04:43 -04:00
|
|
|
const w = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("worker_globals.ts"),
|
2021-01-18 21:26:39 -05:00
|
|
|
workerOptions,
|
2020-10-11 18:04:43 -04:00
|
|
|
);
|
2021-10-14 05:44:33 -04:00
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<string>();
|
2021-08-05 07:08:58 -04:00
|
|
|
w.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2020-10-11 18:04:43 -04:00
|
|
|
};
|
2021-10-14 05:44:33 -04:00
|
|
|
|
2020-10-11 18:04:43 -04:00
|
|
|
w.postMessage("Hello, world!");
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(await promise, "true, true, true, true");
|
2020-10-11 18:04:43 -04:00
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-08-15 11:04:36 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "worker navigator",
|
|
|
|
fn: async function () {
|
|
|
|
const workerOptions: WorkerOptions = { type: "module" };
|
|
|
|
const w = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("worker_navigator.ts"),
|
2023-08-15 11:04:36 -04:00
|
|
|
workerOptions,
|
|
|
|
);
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<string>();
|
2023-08-15 11:04:36 -04:00
|
|
|
w.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2023-08-15 11:04:36 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
w.postMessage("Hello, world!");
|
|
|
|
assertEquals(await promise, "string, object, string, number");
|
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-03-18 19:25:55 -04:00
|
|
|
Deno.test({
|
2020-04-09 18:15:17 -04:00
|
|
|
name: "worker fetch API",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2020-06-09 08:33:52 -04:00
|
|
|
const fetchingWorker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("fetching_worker.js"),
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-03-18 19:25:55 -04:00
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve, reject } = Promise.withResolvers<string>();
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2021-08-05 07:08:58 -04:00
|
|
|
fetchingWorker.onerror = (e: any) => {
|
2020-03-18 19:25:55 -04:00
|
|
|
e.preventDefault();
|
2023-11-22 06:11:20 -05:00
|
|
|
reject(e.message);
|
2020-03-18 19:25:55 -04:00
|
|
|
};
|
|
|
|
// Defer promise.resolve() to allow worker to shut down
|
2021-08-05 07:08:58 -04:00
|
|
|
fetchingWorker.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2020-03-18 19:25:55 -04:00
|
|
|
};
|
|
|
|
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(await promise, "Done!");
|
2020-04-09 18:15:17 -04:00
|
|
|
fetchingWorker.terminate();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker terminate busy loop",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<number>();
|
2020-04-09 18:15:17 -04:00
|
|
|
|
2020-06-09 08:33:52 -04:00
|
|
|
const busyWorker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("busy_worker.js"),
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-04-09 18:15:17 -04:00
|
|
|
|
|
|
|
let testResult = 0;
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
busyWorker.onmessage = (e) => {
|
2020-04-09 18:15:17 -04:00
|
|
|
testResult = e.data;
|
|
|
|
if (testResult >= 10000) {
|
|
|
|
busyWorker.terminate();
|
2021-08-05 07:08:58 -04:00
|
|
|
busyWorker.onmessage = (_e) => {
|
2020-04-09 18:15:17 -04:00
|
|
|
throw new Error("unreachable");
|
|
|
|
};
|
|
|
|
setTimeout(() => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(testResult);
|
2020-04-09 18:15:17 -04:00
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
busyWorker.postMessage("ping");
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(await promise, 10000);
|
2020-04-09 18:15:17 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker race condition",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2020-04-09 18:15:17 -04:00
|
|
|
// See issue for details
|
|
|
|
// https://github.com/denoland/deno/issues/4080
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-04-09 18:15:17 -04:00
|
|
|
|
2020-06-09 08:33:52 -04:00
|
|
|
const racyWorker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("racy_worker.js"),
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-04-09 18:15:17 -04:00
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
racyWorker.onmessage = (_e) => {
|
2020-04-09 18:15:17 -04:00
|
|
|
setTimeout(() => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-04-09 18:15:17 -04:00
|
|
|
}, 100);
|
|
|
|
};
|
|
|
|
|
2021-05-11 15:09:09 -04:00
|
|
|
racyWorker.postMessage("START");
|
2020-03-18 19:25:55 -04:00
|
|
|
await promise;
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-02-21 10:35:41 -05:00
|
|
|
});
|
2020-04-13 12:34:32 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker is event listener",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2020-04-13 12:34:32 -04:00
|
|
|
let messageHandlersCalled = 0;
|
|
|
|
let errorHandlersCalled = 0;
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const deferred1 = Promise.withResolvers<void>();
|
|
|
|
const deferred2 = Promise.withResolvers<void>();
|
2020-04-13 12:34:32 -04:00
|
|
|
|
2020-06-09 08:33:52 -04:00
|
|
|
const worker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("event_worker.js"),
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-04-13 12:34:32 -04:00
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
worker.onmessage = (_e: Event) => {
|
2020-04-13 12:34:32 -04:00
|
|
|
messageHandlersCalled++;
|
|
|
|
};
|
|
|
|
worker.addEventListener("message", (_e: Event) => {
|
|
|
|
messageHandlersCalled++;
|
|
|
|
});
|
|
|
|
worker.addEventListener("message", (_e: Event) => {
|
|
|
|
messageHandlersCalled++;
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred1.resolve();
|
2020-04-13 12:34:32 -04:00
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
worker.onerror = (e) => {
|
2020-04-13 12:34:32 -04:00
|
|
|
errorHandlersCalled++;
|
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
worker.addEventListener("error", (_e: Event) => {
|
|
|
|
errorHandlersCalled++;
|
|
|
|
});
|
|
|
|
worker.addEventListener("error", (_e: Event) => {
|
|
|
|
errorHandlersCalled++;
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred2.resolve();
|
2020-04-13 12:34:32 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
worker.postMessage("ping");
|
2023-11-22 06:11:20 -05:00
|
|
|
await deferred1.promise;
|
2020-04-13 12:34:32 -04:00
|
|
|
assertEquals(messageHandlersCalled, 3);
|
|
|
|
|
|
|
|
worker.postMessage("boom");
|
2023-11-22 06:11:20 -05:00
|
|
|
await deferred2.promise;
|
2020-04-13 12:34:32 -04:00
|
|
|
assertEquals(errorHandlersCalled, 3);
|
|
|
|
worker.terminate();
|
|
|
|
},
|
|
|
|
});
|
2020-04-13 16:18:31 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker scope is event listener",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2020-06-09 08:33:52 -04:00
|
|
|
const worker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("event_worker_scope.js"),
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-04-13 16:18:31 -04:00
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const { promise, resolve } = Promise.withResolvers<any>();
|
2021-08-05 07:08:58 -04:00
|
|
|
worker.onmessage = (e: MessageEvent) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2020-04-13 16:18:31 -04:00
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
worker.onerror = (_e) => {
|
2020-04-13 16:18:31 -04:00
|
|
|
throw new Error("unreachable");
|
|
|
|
};
|
|
|
|
|
|
|
|
worker.postMessage("boom");
|
|
|
|
worker.postMessage("ping");
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(await promise, {
|
|
|
|
messageHandlersCalled: 4,
|
|
|
|
errorHandlersCalled: 4,
|
|
|
|
});
|
2020-04-13 16:18:31 -04:00
|
|
|
worker.terminate();
|
|
|
|
},
|
|
|
|
});
|
2020-04-16 17:40:29 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker with Deno namespace",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2020-06-09 08:33:52 -04:00
|
|
|
const denoWorker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("deno_worker.ts"),
|
2022-05-17 16:27:17 -04:00
|
|
|
{ type: "module", deno: { permissions: "inherit" } },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2020-04-16 17:40:29 -04:00
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<string>();
|
2021-08-05 07:08:58 -04:00
|
|
|
denoWorker.onmessage = (e) => {
|
2020-04-16 17:40:29 -04:00
|
|
|
denoWorker.terminate();
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2020-04-16 17:40:29 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
denoWorker.postMessage("Hello World");
|
2022-05-17 16:27:17 -04:00
|
|
|
assertEquals(await promise, "Hello World");
|
2020-04-16 17:40:29 -04:00
|
|
|
},
|
|
|
|
});
|
2020-05-08 08:30:53 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker with crypto in scope",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2020-06-09 08:33:52 -04:00
|
|
|
const w = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("worker_crypto.js"),
|
2020-07-14 15:24:17 -04:00
|
|
|
{ type: "module" },
|
2020-06-09 08:33:52 -04:00
|
|
|
);
|
2021-10-14 05:44:33 -04:00
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<boolean>();
|
2021-08-05 07:08:58 -04:00
|
|
|
w.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2020-05-08 08:30:53 -04:00
|
|
|
};
|
2021-10-14 05:44:33 -04:00
|
|
|
|
2020-05-08 08:30:53 -04:00
|
|
|
w.postMessage(null);
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(await promise, true);
|
2020-05-08 08:30:53 -04:00
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|
2020-11-10 07:15:42 -05:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Worker event handler order",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-11-10 07:15:42 -05:00
|
|
|
const w = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("test_worker.ts"),
|
2020-11-10 07:15:42 -05:00
|
|
|
{ type: "module", name: "tsWorker" },
|
|
|
|
);
|
|
|
|
const arr: number[] = [];
|
|
|
|
w.addEventListener("message", () => arr.push(1));
|
2021-08-05 07:08:58 -04:00
|
|
|
w.onmessage = (_e) => {
|
2020-11-10 07:15:42 -05:00
|
|
|
arr.push(2);
|
|
|
|
};
|
|
|
|
w.addEventListener("message", () => arr.push(3));
|
|
|
|
w.addEventListener("message", () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-11-10 07:15:42 -05:00
|
|
|
});
|
|
|
|
w.postMessage("Hello World");
|
|
|
|
await promise;
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(arr, [1, 2, 3]);
|
2020-11-10 07:15:42 -05:00
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|
2020-11-27 08:19:24 -05:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Worker immediate close",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2020-11-27 08:19:24 -05:00
|
|
|
const w = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("immediately_close_worker.js"),
|
2020-11-27 08:19:24 -05:00
|
|
|
{ type: "module" },
|
|
|
|
);
|
|
|
|
setTimeout(() => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2020-11-27 08:19:24 -05:00
|
|
|
}, 1000);
|
|
|
|
await promise;
|
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|
2021-01-06 15:31:16 -05:00
|
|
|
|
2021-01-08 09:44:24 -05:00
|
|
|
Deno.test({
|
|
|
|
name: "Worker post undefined",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2021-01-08 09:44:24 -05:00
|
|
|
const worker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("post_undefined.ts"),
|
2021-01-08 09:44:24 -05:00
|
|
|
{ type: "module" },
|
|
|
|
);
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
const handleWorkerMessage = (e: MessageEvent) => {
|
2021-01-08 09:44:24 -05:00
|
|
|
console.log("main <- worker:", e.data);
|
|
|
|
worker.terminate();
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2021-01-08 09:44:24 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
worker.addEventListener("messageerror", () => console.log("message error"));
|
|
|
|
worker.addEventListener("error", () => console.log("error"));
|
|
|
|
worker.addEventListener("message", handleWorkerMessage);
|
|
|
|
|
|
|
|
console.log("\npost from parent");
|
|
|
|
worker.postMessage(undefined);
|
|
|
|
await promise;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-01-06 15:31:16 -05:00
|
|
|
Deno.test("Worker inherits permissions", async function () {
|
|
|
|
const worker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("read_check_worker.js"),
|
2022-05-17 16:27:17 -04:00
|
|
|
{ type: "module", deno: { permissions: "inherit" } },
|
2021-01-06 15:31:16 -05:00
|
|
|
);
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<boolean>();
|
2021-10-14 05:44:33 -04:00
|
|
|
worker.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2021-01-06 15:31:16 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
worker.postMessage(null);
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(await promise, true);
|
2021-01-06 15:31:16 -05:00
|
|
|
worker.terminate();
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Worker limit children permissions", async function () {
|
|
|
|
const worker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("read_check_worker.js"),
|
2022-05-17 16:27:17 -04:00
|
|
|
{ type: "module", deno: { permissions: { read: false } } },
|
2021-01-06 15:31:16 -05:00
|
|
|
);
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<boolean>();
|
2021-10-14 05:44:33 -04:00
|
|
|
worker.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2021-01-06 15:31:16 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
worker.postMessage(null);
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(await promise, false);
|
2021-01-06 15:31:16 -05:00
|
|
|
worker.terminate();
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Worker limit children permissions granularly", async function () {
|
2024-02-08 15:09:47 -05:00
|
|
|
const workerUrl = resolveWorker("read_check_granular_worker.js");
|
2021-01-06 15:31:16 -05:00
|
|
|
const worker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
workerUrl,
|
2021-01-06 15:31:16 -05:00
|
|
|
{
|
|
|
|
type: "module",
|
|
|
|
deno: {
|
|
|
|
permissions: {
|
2021-10-13 13:04:44 -04:00
|
|
|
env: ["foo"],
|
|
|
|
hrtime: true,
|
|
|
|
net: ["foo", "bar:8000"],
|
2024-02-08 15:09:47 -05:00
|
|
|
ffi: [new URL("foo", workerUrl), "bar"],
|
|
|
|
read: [new URL("foo", workerUrl), "bar"],
|
|
|
|
run: [new URL("foo", workerUrl), "bar", "./baz"],
|
|
|
|
write: [new URL("foo", workerUrl), "bar"],
|
2021-01-06 15:31:16 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
2023-11-22 06:11:20 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const { promise, resolve } = Promise.withResolvers<any>();
|
|
|
|
worker.onmessage = ({ data }) => resolve(data);
|
2021-10-13 13:04:44 -04:00
|
|
|
assertEquals(await promise, {
|
|
|
|
envGlobal: "prompt",
|
|
|
|
envFoo: "granted",
|
|
|
|
envAbsent: "prompt",
|
|
|
|
hrtime: "granted",
|
|
|
|
netGlobal: "prompt",
|
|
|
|
netFoo: "granted",
|
|
|
|
netFoo8000: "granted",
|
|
|
|
netBar: "prompt",
|
|
|
|
netBar8000: "granted",
|
|
|
|
ffiGlobal: "prompt",
|
|
|
|
ffiFoo: "granted",
|
|
|
|
ffiBar: "granted",
|
|
|
|
ffiAbsent: "prompt",
|
|
|
|
readGlobal: "prompt",
|
|
|
|
readFoo: "granted",
|
|
|
|
readBar: "granted",
|
|
|
|
readAbsent: "prompt",
|
|
|
|
runGlobal: "prompt",
|
|
|
|
runFoo: "granted",
|
|
|
|
runBar: "granted",
|
|
|
|
runBaz: "granted",
|
|
|
|
runAbsent: "prompt",
|
|
|
|
writeGlobal: "prompt",
|
|
|
|
writeFoo: "granted",
|
|
|
|
writeBar: "granted",
|
|
|
|
writeAbsent: "prompt",
|
|
|
|
});
|
2021-01-06 15:31:16 -05:00
|
|
|
worker.terminate();
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Nested worker limit children permissions", async function () {
|
2021-10-13 13:04:44 -04:00
|
|
|
/** This worker has permissions but doesn't grant them to its children */
|
2021-01-06 15:31:16 -05:00
|
|
|
const worker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("parent_read_check_worker.js"),
|
2022-05-17 16:27:17 -04:00
|
|
|
{ type: "module", deno: { permissions: "inherit" } },
|
2021-01-06 15:31:16 -05:00
|
|
|
);
|
2023-11-22 06:11:20 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const { promise, resolve } = Promise.withResolvers<any>();
|
|
|
|
worker.onmessage = ({ data }) => resolve(data);
|
2021-10-13 13:04:44 -04:00
|
|
|
assertEquals(await promise, {
|
|
|
|
envGlobal: "prompt",
|
|
|
|
envFoo: "prompt",
|
|
|
|
envAbsent: "prompt",
|
|
|
|
hrtime: "prompt",
|
|
|
|
netGlobal: "prompt",
|
|
|
|
netFoo: "prompt",
|
|
|
|
netFoo8000: "prompt",
|
|
|
|
netBar: "prompt",
|
|
|
|
netBar8000: "prompt",
|
|
|
|
ffiGlobal: "prompt",
|
|
|
|
ffiFoo: "prompt",
|
|
|
|
ffiBar: "prompt",
|
|
|
|
ffiAbsent: "prompt",
|
|
|
|
readGlobal: "prompt",
|
|
|
|
readFoo: "prompt",
|
|
|
|
readBar: "prompt",
|
|
|
|
readAbsent: "prompt",
|
|
|
|
runGlobal: "prompt",
|
|
|
|
runFoo: "prompt",
|
|
|
|
runBar: "prompt",
|
|
|
|
runBaz: "prompt",
|
|
|
|
runAbsent: "prompt",
|
|
|
|
writeGlobal: "prompt",
|
|
|
|
writeFoo: "prompt",
|
|
|
|
writeBar: "prompt",
|
|
|
|
writeAbsent: "prompt",
|
|
|
|
});
|
2021-01-06 15:31:16 -05:00
|
|
|
worker.terminate();
|
|
|
|
});
|
|
|
|
|
|
|
|
// This test relies on env permissions not being granted on main thread
|
2021-10-13 13:04:44 -04:00
|
|
|
Deno.test({
|
|
|
|
name:
|
|
|
|
"Worker initialization throws on worker permissions greater than parent thread permissions",
|
|
|
|
permissions: { env: false },
|
|
|
|
fn: function () {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
const worker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("deno_worker.ts"),
|
2022-05-17 16:27:17 -04:00
|
|
|
{ type: "module", deno: { permissions: { env: true } } },
|
2021-10-13 13:04:44 -04:00
|
|
|
);
|
|
|
|
worker.terminate();
|
|
|
|
},
|
|
|
|
Deno.errors.PermissionDenied,
|
|
|
|
"Can't escalate parent thread permissions",
|
|
|
|
);
|
|
|
|
},
|
2021-01-06 15:31:16 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Worker with disabled permissions", async function () {
|
|
|
|
const worker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("no_permissions_worker.js"),
|
2022-05-17 16:27:17 -04:00
|
|
|
{ type: "module", deno: { permissions: "none" } },
|
2021-01-06 15:31:16 -05:00
|
|
|
);
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<boolean>();
|
2021-10-14 05:44:33 -04:00
|
|
|
worker.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2021-01-06 15:31:16 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
worker.postMessage(null);
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(await promise, true);
|
2021-01-06 15:31:16 -05:00
|
|
|
worker.terminate();
|
|
|
|
});
|
2021-01-07 13:06:08 -05:00
|
|
|
|
2022-03-20 17:46:39 -04:00
|
|
|
Deno.test("Worker permissions are not inherited with empty permission object", async function () {
|
|
|
|
const worker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("permission_echo.js"),
|
2022-05-17 16:27:17 -04:00
|
|
|
{ type: "module", deno: { permissions: {} } },
|
2022-03-20 17:46:39 -04:00
|
|
|
);
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const { promise, resolve } = Promise.withResolvers<any>();
|
2022-03-20 17:46:39 -04:00
|
|
|
worker.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2022-03-20 17:46:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
worker.postMessage(null);
|
|
|
|
assertEquals(await promise, {
|
|
|
|
env: "prompt",
|
|
|
|
hrtime: "prompt",
|
|
|
|
net: "prompt",
|
|
|
|
ffi: "prompt",
|
|
|
|
read: "prompt",
|
|
|
|
run: "prompt",
|
|
|
|
write: "prompt",
|
|
|
|
});
|
|
|
|
worker.terminate();
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("Worker permissions are not inherited with single specified permission", async function () {
|
|
|
|
const worker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("permission_echo.js"),
|
2022-05-17 16:27:17 -04:00
|
|
|
{ type: "module", deno: { permissions: { net: true } } },
|
2022-03-20 17:46:39 -04:00
|
|
|
);
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const { promise, resolve } = Promise.withResolvers<any>();
|
2022-03-20 17:46:39 -04:00
|
|
|
worker.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2022-03-20 17:46:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
worker.postMessage(null);
|
|
|
|
assertEquals(await promise, {
|
|
|
|
env: "prompt",
|
|
|
|
hrtime: "prompt",
|
|
|
|
net: "granted",
|
|
|
|
ffi: "prompt",
|
|
|
|
read: "prompt",
|
|
|
|
run: "prompt",
|
|
|
|
write: "prompt",
|
|
|
|
});
|
|
|
|
worker.terminate();
|
|
|
|
});
|
|
|
|
|
2021-10-13 13:04:44 -04:00
|
|
|
Deno.test("Worker with invalid permission arg", function () {
|
|
|
|
assertThrows(
|
|
|
|
() =>
|
|
|
|
new Worker(`data:,close();`, {
|
|
|
|
type: "module",
|
|
|
|
// @ts-expect-error invalid env value
|
|
|
|
deno: { permissions: { env: "foo" } },
|
|
|
|
}),
|
|
|
|
TypeError,
|
2023-09-14 17:05:18 -04:00
|
|
|
'(deno.permissions.env) invalid value: string "foo", expected "inherit" or boolean or string[]',
|
2021-10-13 13:04:44 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-01-07 13:06:08 -05:00
|
|
|
Deno.test({
|
|
|
|
name: "worker location",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<string>();
|
2024-02-08 15:09:47 -05:00
|
|
|
const workerModuleHref = resolveWorker("worker_location.ts");
|
2021-01-07 13:06:08 -05:00
|
|
|
const w = new Worker(workerModuleHref, { type: "module" });
|
2021-08-05 07:08:58 -04:00
|
|
|
w.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2021-01-07 13:06:08 -05:00
|
|
|
};
|
|
|
|
w.postMessage("Hello, world!");
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(await promise, `${workerModuleHref}, true`);
|
2021-01-07 13:06:08 -05:00
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-03-04 07:19:47 -05:00
|
|
|
Deno.test({
|
|
|
|
name: "Worker with top-level-await",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve, reject } = Promise.withResolvers<void>();
|
2021-03-04 07:19:47 -05:00
|
|
|
const worker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("worker_with_top_level_await.ts"),
|
2021-03-04 07:19:47 -05:00
|
|
|
{ type: "module" },
|
|
|
|
);
|
2021-08-05 07:08:58 -04:00
|
|
|
worker.onmessage = (e) => {
|
2021-03-04 07:19:47 -05:00
|
|
|
if (e.data == "ready") {
|
|
|
|
worker.postMessage("trigger worker handler");
|
|
|
|
} else if (e.data == "triggered worker handler") {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2021-03-04 07:19:47 -05:00
|
|
|
} else {
|
2023-11-22 06:11:20 -05:00
|
|
|
reject(new Error("Handler didn't run during top-level delay."));
|
2021-03-04 07:19:47 -05:00
|
|
|
}
|
|
|
|
};
|
2023-11-22 06:11:20 -05:00
|
|
|
await promise;
|
2021-03-05 05:32:17 -05:00
|
|
|
worker.terminate();
|
2021-03-04 07:19:47 -05:00
|
|
|
},
|
|
|
|
});
|
2021-04-20 10:26:31 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Worker with native HTTP",
|
|
|
|
fn: async function () {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2021-04-20 10:26:31 -04:00
|
|
|
const worker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("http_worker.js"),
|
2022-05-17 16:27:17 -04:00
|
|
|
{ type: "module", deno: { permissions: "inherit" } },
|
2021-04-20 10:26:31 -04:00
|
|
|
);
|
2021-04-21 22:02:11 -04:00
|
|
|
worker.onmessage = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2021-04-21 22:02:11 -04:00
|
|
|
};
|
2023-11-22 06:11:20 -05:00
|
|
|
await promise;
|
2021-04-20 10:26:31 -04:00
|
|
|
|
|
|
|
assert(worker);
|
2021-07-28 07:12:45 -04:00
|
|
|
const response = await fetch("http://localhost:4506");
|
2021-04-20 10:26:31 -04:00
|
|
|
assert(await response.arrayBuffer());
|
|
|
|
worker.terminate();
|
|
|
|
},
|
|
|
|
});
|
2021-05-11 15:09:09 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "structured cloning postMessage",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2021-05-11 15:09:09 -04:00
|
|
|
const worker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("worker_structured_cloning.ts"),
|
2021-05-11 15:09:09 -04:00
|
|
|
{ type: "module" },
|
|
|
|
);
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const { promise, resolve } = Promise.withResolvers<any>();
|
2021-08-05 07:08:58 -04:00
|
|
|
worker.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2021-05-11 15:09:09 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
worker.postMessage("START");
|
2023-11-22 06:11:20 -05:00
|
|
|
const data = await promise;
|
2021-10-14 05:44:33 -04:00
|
|
|
// self field should reference itself (circular ref)
|
|
|
|
assert(data === data.self);
|
|
|
|
// fields a and b refer to the same array
|
|
|
|
assertEquals(data.a, ["a", true, 432]);
|
|
|
|
assertEquals(data.b, ["a", true, 432]);
|
|
|
|
data.b[0] = "b";
|
|
|
|
data.a[2] += 5;
|
|
|
|
assertEquals(data.a, ["b", true, 437]);
|
|
|
|
assertEquals(data.b, ["b", true, 437]);
|
|
|
|
// c is a set
|
|
|
|
const len = data.c.size;
|
|
|
|
data.c.add(1); // This value is already in the set.
|
|
|
|
data.c.add(2);
|
|
|
|
assertEquals(len + 1, data.c.size);
|
2021-05-11 15:09:09 -04:00
|
|
|
worker.terminate();
|
|
|
|
},
|
|
|
|
});
|
2021-06-22 10:30:16 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker with relative specifier",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2021-08-11 10:20:47 -04:00
|
|
|
assertEquals(location.href, "http://127.0.0.1:4545/");
|
2021-06-22 10:30:16 -04:00
|
|
|
const w = new Worker(
|
|
|
|
"./workers/test_worker.ts",
|
|
|
|
{ type: "module", name: "tsWorker" },
|
|
|
|
);
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<string>();
|
2021-08-05 07:08:58 -04:00
|
|
|
w.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(e.data);
|
2021-06-22 10:30:16 -04:00
|
|
|
};
|
|
|
|
w.postMessage("Hello, world!");
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(await promise, "Hello, world!");
|
2021-06-22 10:30:16 -04:00
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-07-06 13:42:52 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "worker SharedArrayBuffer",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2021-07-06 13:42:52 -04:00
|
|
|
const workerOptions: WorkerOptions = { type: "module" };
|
|
|
|
const w = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("shared_array_buffer.ts"),
|
2021-07-06 13:42:52 -04:00
|
|
|
workerOptions,
|
|
|
|
);
|
|
|
|
const sab1 = new SharedArrayBuffer(1);
|
|
|
|
const sab2 = new SharedArrayBuffer(1);
|
|
|
|
const bytes1 = new Uint8Array(sab1);
|
|
|
|
const bytes2 = new Uint8Array(sab2);
|
|
|
|
assertEquals(bytes1[0], 0);
|
|
|
|
assertEquals(bytes2[0], 0);
|
2021-08-05 07:08:58 -04:00
|
|
|
w.onmessage = () => {
|
2021-07-06 13:42:52 -04:00
|
|
|
w.postMessage([sab1, sab2]);
|
2021-08-05 07:08:58 -04:00
|
|
|
w.onmessage = () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2021-07-06 13:42:52 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
await promise;
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(bytes1[0], 1);
|
|
|
|
assertEquals(bytes2[0], 2);
|
2021-07-06 13:42:52 -04:00
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-06-22 10:30:16 -04:00
|
|
|
Deno.test({
|
|
|
|
name: "Send MessagePorts from / to workers",
|
2021-08-05 07:08:58 -04:00
|
|
|
fn: async function () {
|
2021-06-22 10:30:16 -04:00
|
|
|
const worker = new Worker(
|
2024-02-08 15:09:47 -05:00
|
|
|
resolveWorker("message_port.ts"),
|
2021-06-22 10:30:16 -04:00
|
|
|
{ type: "module" },
|
|
|
|
);
|
|
|
|
const channel = new MessageChannel();
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const deferred1 = Promise.withResolvers<any>();
|
|
|
|
const deferred2 = Promise.withResolvers<boolean>();
|
|
|
|
const deferred3 = Promise.withResolvers<boolean>();
|
|
|
|
const result = Promise.withResolvers<void>();
|
2021-06-22 10:30:16 -04:00
|
|
|
worker.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred1.resolve([e.data, e.ports.length]);
|
2021-06-22 10:30:16 -04:00
|
|
|
const port1 = e.ports[0];
|
|
|
|
port1.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred2.resolve(e.data);
|
2021-06-22 10:30:16 -04:00
|
|
|
port1.close();
|
|
|
|
worker.postMessage("3", [channel.port1]);
|
|
|
|
};
|
|
|
|
port1.postMessage("2");
|
|
|
|
};
|
|
|
|
channel.port2.onmessage = (e) => {
|
2023-11-22 06:11:20 -05:00
|
|
|
deferred3.resolve(e.data);
|
2021-06-22 10:30:16 -04:00
|
|
|
channel.port2.close();
|
|
|
|
result.resolve();
|
|
|
|
};
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
assertEquals(await deferred1.promise, ["1", 1]);
|
|
|
|
assertEquals(await deferred2.promise, true);
|
|
|
|
assertEquals(await deferred3.promise, true);
|
|
|
|
await result.promise;
|
2021-06-22 10:30:16 -04:00
|
|
|
worker.terminate();
|
|
|
|
},
|
|
|
|
});
|
2022-01-09 17:42:14 -05:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "worker Deno.memoryUsage",
|
|
|
|
fn: async function () {
|
|
|
|
const w = new Worker(
|
|
|
|
/**
|
|
|
|
* Source code
|
|
|
|
* self.onmessage = function() {self.postMessage(Deno.memoryUsage())}
|
|
|
|
*/
|
|
|
|
"data:application/typescript;base64,c2VsZi5vbm1lc3NhZ2UgPSBmdW5jdGlvbigpIHtzZWxmLnBvc3RNZXNzYWdlKERlbm8ubWVtb3J5VXNhZ2UoKSl9",
|
2022-05-17 16:27:17 -04:00
|
|
|
{ type: "module", name: "tsWorker" },
|
2022-01-09 17:42:14 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
w.postMessage(null);
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const { promise, resolve } = Promise.withResolvers<any>();
|
2022-01-09 17:42:14 -05:00
|
|
|
w.onmessage = function (evt) {
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve(evt.data);
|
2022-01-09 17:42:14 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
Object.keys(
|
2023-11-22 06:11:20 -05:00
|
|
|
await promise as unknown as Record<string, number>,
|
2022-01-09 17:42:14 -05:00
|
|
|
),
|
|
|
|
["rss", "heapTotal", "heapUsed", "external"],
|
|
|
|
);
|
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|