From 79b3bc05d6de520f1df73face1744ae3d8be0bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 11 Feb 2020 10:04:59 +0100 Subject: workers: basic event loop (#3828) * establish basic event loop for workers * make "self.close()" inside worker * remove "runWorkerMessageLoop() - instead manually call global function in Rust when message arrives. This is done in preparation for structured clone * refactor "WorkerChannel" and use distinct structs for internal and external channels; "WorkerChannelsInternal" and "WorkerHandle" * move "State.worker_channels_internal" to "Worker.internal_channels" * add "WorkerEvent" enum for child->host communication; currently "Message(Buf)" and "Error(ErrBox)" variants are supported * add tests for nested workers * add tests for worker throwing error on startup --- cli/tests/subdir/nested_worker.js | 19 +++++++++++++++++++ cli/tests/subdir/sibling_worker.js | 4 ++++ cli/tests/subdir/test_worker.js | 2 -- cli/tests/subdir/test_worker.ts | 2 -- cli/tests/subdir/throwing_worker.js | 2 ++ 5 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 cli/tests/subdir/nested_worker.js create mode 100644 cli/tests/subdir/sibling_worker.js create mode 100644 cli/tests/subdir/throwing_worker.js (limited to 'cli/tests/subdir') diff --git a/cli/tests/subdir/nested_worker.js b/cli/tests/subdir/nested_worker.js new file mode 100644 index 000000000..b0acd70d7 --- /dev/null +++ b/cli/tests/subdir/nested_worker.js @@ -0,0 +1,19 @@ +// Specifier should be resolved relative to current file +const jsWorker = new Worker("./sibling_worker.js", { + type: "module", + name: "sibling" +}); + +jsWorker.onerror = _e => { + postMessage({ type: "error" }); +}; + +jsWorker.onmessage = e => { + console.log("js worker on message"); + postMessage({ type: "msg", text: e }); + close(); +}; + +onmessage = function(e) { + jsWorker.postMessage(e.data); +}; diff --git a/cli/tests/subdir/sibling_worker.js b/cli/tests/subdir/sibling_worker.js new file mode 100644 index 000000000..0e91141ce --- /dev/null +++ b/cli/tests/subdir/sibling_worker.js @@ -0,0 +1,4 @@ +onmessage = e => { + postMessage(e.data); + close(); +}; diff --git a/cli/tests/subdir/test_worker.js b/cli/tests/subdir/test_worker.js index f0d9fbed6..70e1d8b73 100644 --- a/cli/tests/subdir/test_worker.js +++ b/cli/tests/subdir/test_worker.js @@ -1,6 +1,5 @@ let thrown = false; -// TODO(bartlomieju): add test for throwing in web worker if (self.name !== "jsWorker") { throw Error(`Bad worker name: ${self.name}, expected jsWorker`); } @@ -14,7 +13,6 @@ onmessage = function(e) { } postMessage(e.data); - close(); }; diff --git a/cli/tests/subdir/test_worker.ts b/cli/tests/subdir/test_worker.ts index bc3f358f8..2ea8f9214 100644 --- a/cli/tests/subdir/test_worker.ts +++ b/cli/tests/subdir/test_worker.ts @@ -4,8 +4,6 @@ if (self.name !== "tsWorker") { onmessage = function(e): void { console.log(e.data); - postMessage(e.data); - close(); }; diff --git a/cli/tests/subdir/throwing_worker.js b/cli/tests/subdir/throwing_worker.js new file mode 100644 index 000000000..56ee4ff88 --- /dev/null +++ b/cli/tests/subdir/throwing_worker.js @@ -0,0 +1,2 @@ +// This worker just throws error when it's being executed +throw Error("Thrown error"); -- cgit v1.2.3