2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 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,
|
2021-08-11 10:20:47 -04:00
|
|
|
} from "../../../../test_util/std/testing/asserts.ts";
|
|
|
|
import { deferred } from "../../../../test_util/std/async/deferred.ts";
|
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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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
|
|
|
|
2021-10-14 05:44:33 -04:00
|
|
|
const promise1 = deferred();
|
|
|
|
jsWorker.onmessage = (e) => {
|
|
|
|
promise1.resolve(e.data);
|
2020-03-18 19:25:55 -04:00
|
|
|
};
|
|
|
|
|
2021-10-14 05:44:33 -04:00
|
|
|
const promise2 = deferred();
|
|
|
|
tsWorker.onmessage = (e) => {
|
|
|
|
promise2.resolve(e.data);
|
2020-03-18 19:25:55 -04:00
|
|
|
};
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
jsWorker.postMessage("Hello World");
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(await promise1, "Hello World");
|
|
|
|
tsWorker.postMessage("Hello World");
|
|
|
|
assertEquals(await promise2, "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" },
|
|
|
|
);
|
|
|
|
|
2021-10-14 05:44:33 -04:00
|
|
|
const promise = deferred();
|
2021-08-05 07:08:58 -04:00
|
|
|
tsWorker.onmessage = (e) => {
|
2021-10-14 05:44:33 -04:00
|
|
|
promise.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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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
|
|
|
|
2021-10-14 05:44:33 -04:00
|
|
|
const promise = deferred();
|
2021-08-05 07:08:58 -04:00
|
|
|
nestedWorker.onmessage = (e) => {
|
2021-10-14 05:44:33 -04:00
|
|
|
promise.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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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
|
|
|
|
2021-10-14 05:44:33 -04:00
|
|
|
const promise = deferred();
|
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();
|
2021-10-14 05:44:33 -04:00
|
|
|
promise.resolve(e.message);
|
2020-03-18 19:25:55 -04:00
|
|
|
};
|
|
|
|
|
2021-10-14 05:44:33 -04:00
|
|
|
assertMatch(await promise as string, /Uncaught 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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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
|
|
|
|
|
|
|
const promise = deferred();
|
2021-08-05 07:08:58 -04:00
|
|
|
w.onmessage = (e) => {
|
2021-10-14 05:44:33 -04:00
|
|
|
promise.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(
|
|
|
|
import.meta.resolve("./worker_navigator.ts"),
|
|
|
|
workerOptions,
|
|
|
|
);
|
|
|
|
|
|
|
|
const promise = deferred();
|
|
|
|
w.onmessage = (e) => {
|
|
|
|
promise.resolve(e.data);
|
|
|
|
};
|
|
|
|
|
|
|
|
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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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
|
|
|
|
2021-10-14 05:44:33 -04:00
|
|
|
const promise = deferred();
|
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();
|
|
|
|
promise.reject(e.message);
|
|
|
|
};
|
|
|
|
// Defer promise.resolve() to allow worker to shut down
|
2021-08-05 07:08:58 -04:00
|
|
|
fetchingWorker.onmessage = (e) => {
|
2021-10-14 05:44:33 -04:00
|
|
|
promise.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 () {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2020-04-09 18:15:17 -04:00
|
|
|
|
2020-06-09 08:33:52 -04:00
|
|
|
const busyWorker = new Worker(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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(() => {
|
2021-10-14 05:44:33 -04:00
|
|
|
promise.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
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2020-04-09 18:15:17 -04:00
|
|
|
|
2020-06-09 08:33:52 -04:00
|
|
|
const racyWorker = new Worker(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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(() => {
|
|
|
|
promise.resolve();
|
|
|
|
}, 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;
|
|
|
|
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise1 = deferred();
|
|
|
|
const promise2 = deferred();
|
2020-04-13 12:34:32 -04:00
|
|
|
|
2020-06-09 08:33:52 -04:00
|
|
|
const worker = new Worker(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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++;
|
|
|
|
promise1.resolve();
|
|
|
|
});
|
|
|
|
|
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++;
|
|
|
|
promise2.resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
worker.postMessage("ping");
|
|
|
|
await promise1;
|
|
|
|
assertEquals(messageHandlersCalled, 3);
|
|
|
|
|
|
|
|
worker.postMessage("boom");
|
|
|
|
await promise2;
|
|
|
|
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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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
|
|
|
|
2021-10-14 05:44:33 -04:00
|
|
|
const promise = deferred();
|
2021-08-05 07:08:58 -04:00
|
|
|
worker.onmessage = (e: MessageEvent) => {
|
2021-10-14 05:44:33 -04:00
|
|
|
promise.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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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
|
|
|
|
2022-05-17 16:27:17 -04:00
|
|
|
const promise = deferred();
|
2021-08-05 07:08:58 -04:00
|
|
|
denoWorker.onmessage = (e) => {
|
2020-04-16 17:40:29 -04:00
|
|
|
denoWorker.terminate();
|
2022-05-17 16:27:17 -04:00
|
|
|
promise.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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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
|
|
|
|
|
|
|
const promise = deferred();
|
2021-08-05 07:08:58 -04:00
|
|
|
w.onmessage = (e) => {
|
2021-10-14 05:44:33 -04:00
|
|
|
promise.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 () {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2020-11-10 07:15:42 -05:00
|
|
|
const w = new Worker(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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", () => {
|
|
|
|
promise.resolve();
|
|
|
|
});
|
|
|
|
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 () {
|
2020-11-27 08:19:24 -05:00
|
|
|
const promise = deferred();
|
|
|
|
const w = new Worker(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./immediately_close_worker.js"),
|
2020-11-27 08:19:24 -05:00
|
|
|
{ type: "module" },
|
|
|
|
);
|
|
|
|
setTimeout(() => {
|
|
|
|
promise.resolve();
|
|
|
|
}, 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 () {
|
2021-01-08 09:44:24 -05:00
|
|
|
const promise = deferred();
|
|
|
|
const worker = new Worker(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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();
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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
|
|
|
);
|
|
|
|
|
2021-10-14 05:44:33 -04:00
|
|
|
const promise = deferred();
|
|
|
|
worker.onmessage = (e) => {
|
|
|
|
promise.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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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
|
|
|
);
|
|
|
|
|
2021-10-14 05:44:33 -04:00
|
|
|
const promise = deferred();
|
|
|
|
worker.onmessage = (e) => {
|
|
|
|
promise.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 () {
|
|
|
|
const worker = new Worker(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./read_check_granular_worker.js"),
|
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"],
|
|
|
|
ffi: [new URL("foo", import.meta.url), "bar"],
|
|
|
|
read: [new URL("foo", import.meta.url), "bar"],
|
|
|
|
run: [new URL("foo", import.meta.url), "bar", "./baz"],
|
|
|
|
write: [new URL("foo", import.meta.url), "bar"],
|
2021-01-06 15:31:16 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
2021-10-13 13:04:44 -04:00
|
|
|
const promise = deferred();
|
|
|
|
worker.onmessage = ({ data }) => promise.resolve(data);
|
|
|
|
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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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
|
|
|
);
|
|
|
|
const promise = deferred();
|
2021-10-13 13:04:44 -04:00
|
|
|
worker.onmessage = ({ data }) => promise.resolve(data);
|
|
|
|
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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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
|
|
|
);
|
|
|
|
|
2021-10-14 05:44:33 -04:00
|
|
|
const promise = deferred();
|
|
|
|
worker.onmessage = (e) => {
|
|
|
|
promise.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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./permission_echo.js"),
|
2022-05-17 16:27:17 -04:00
|
|
|
{ type: "module", deno: { permissions: {} } },
|
2022-03-20 17:46:39 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
const promise = deferred();
|
|
|
|
worker.onmessage = (e) => {
|
|
|
|
promise.resolve(e.data);
|
|
|
|
};
|
|
|
|
|
|
|
|
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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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
|
|
|
);
|
|
|
|
|
|
|
|
const promise = deferred();
|
|
|
|
worker.onmessage = (e) => {
|
|
|
|
promise.resolve(e.data);
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
2022-04-08 04:32:48 -04:00
|
|
|
'Error parsing args at position 0: (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 () {
|
2021-01-07 13:06:08 -05:00
|
|
|
const promise = deferred();
|
2022-07-20 10:56:53 -04:00
|
|
|
const workerModuleHref = import.meta.resolve("./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) => {
|
2021-10-14 05:44:33 -04:00
|
|
|
promise.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();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
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-01-07 13:06:08 -05:00
|
|
|
const w = new Worker(
|
|
|
|
"./workers/test_worker.ts",
|
|
|
|
{ type: "module", name: "tsWorker" },
|
|
|
|
);
|
2021-10-14 05:44:33 -04:00
|
|
|
const promise = deferred();
|
2021-08-05 07:08:58 -04:00
|
|
|
w.onmessage = (e) => {
|
2021-10-14 05:44:33 -04:00
|
|
|
promise.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, "Hello, world!");
|
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 () {
|
2021-03-04 07:19:47 -05:00
|
|
|
const result = deferred();
|
|
|
|
const worker = new Worker(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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") {
|
|
|
|
result.resolve();
|
|
|
|
} else {
|
|
|
|
result.reject(new Error("Handler didn't run during top-level delay."));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
await result;
|
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 () {
|
2021-04-21 22:02:11 -04:00
|
|
|
const result = deferred();
|
2021-04-20 10:26:31 -04:00
|
|
|
const worker = new Worker(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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 = () => {
|
|
|
|
result.resolve();
|
|
|
|
};
|
|
|
|
await result;
|
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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./worker_structured_cloning.ts"),
|
2021-05-11 15:09:09 -04:00
|
|
|
{ type: "module" },
|
|
|
|
);
|
|
|
|
|
2021-10-14 05:44:33 -04:00
|
|
|
const result = deferred();
|
2021-08-05 07:08:58 -04:00
|
|
|
worker.onmessage = (e) => {
|
2021-10-14 05:44:33 -04:00
|
|
|
result.resolve(e.data);
|
2021-05-11 15:09:09 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
worker.postMessage("START");
|
2021-10-14 05:44:33 -04:00
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const data = await result as any;
|
|
|
|
// 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" },
|
|
|
|
);
|
2021-10-14 05:44:33 -04:00
|
|
|
const promise = deferred();
|
2021-08-05 07:08:58 -04:00
|
|
|
w.onmessage = (e) => {
|
2021-10-14 05:44:33 -04:00
|
|
|
promise.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 () {
|
2021-07-06 13:42:52 -04:00
|
|
|
const promise = deferred();
|
|
|
|
const workerOptions: WorkerOptions = { type: "module" };
|
|
|
|
const w = new Worker(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./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 = () => {
|
2021-07-06 13:42:52 -04:00
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
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(
|
2022-07-20 10:56:53 -04:00
|
|
|
import.meta.resolve("./message_port.ts"),
|
2021-06-22 10:30:16 -04:00
|
|
|
{ type: "module" },
|
|
|
|
);
|
|
|
|
const channel = new MessageChannel();
|
|
|
|
|
2021-10-14 05:44:33 -04:00
|
|
|
const promise1 = deferred();
|
|
|
|
const promise2 = deferred();
|
|
|
|
const promise3 = deferred();
|
|
|
|
const result = deferred();
|
2021-06-22 10:30:16 -04:00
|
|
|
worker.onmessage = (e) => {
|
2021-10-14 05:44:33 -04:00
|
|
|
promise1.resolve([e.data, e.ports.length]);
|
2021-06-22 10:30:16 -04:00
|
|
|
const port1 = e.ports[0];
|
|
|
|
port1.onmessage = (e) => {
|
2021-10-14 05:44:33 -04:00
|
|
|
promise2.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) => {
|
2021-10-14 05:44:33 -04:00
|
|
|
promise3.resolve(e.data);
|
2021-06-22 10:30:16 -04:00
|
|
|
channel.port2.close();
|
|
|
|
result.resolve();
|
|
|
|
};
|
|
|
|
|
2021-10-14 05:44:33 -04:00
|
|
|
assertEquals(await promise1, ["1", 1]);
|
|
|
|
assertEquals(await promise2, true);
|
|
|
|
assertEquals(await promise3, true);
|
2021-06-22 10:30:16 -04:00
|
|
|
await result;
|
|
|
|
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);
|
|
|
|
|
|
|
|
const memoryUsagePromise = deferred();
|
|
|
|
w.onmessage = function (evt) {
|
|
|
|
memoryUsagePromise.resolve(evt.data);
|
|
|
|
};
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
Object.keys(
|
|
|
|
await memoryUsagePromise as unknown as Record<string, number>,
|
|
|
|
),
|
|
|
|
["rss", "heapTotal", "heapUsed", "external"],
|
|
|
|
);
|
|
|
|
w.terminate();
|
|
|
|
},
|
|
|
|
});
|