summaryrefslogtreecommitdiff
path: root/tests/specs/node/worker_threads/auto_exits.mjs
diff options
context:
space:
mode:
authorSatya Rohith <me@satyarohith.com>2024-05-03 11:22:47 +0530
committerGitHub <noreply@github.com>2024-05-03 05:52:47 +0000
commit02d0ff58d2bab24ebbd1e61afe6b571ff4c415af (patch)
tree78b7fa4e7cf0265d688e34ddaa422ec344273e06 /tests/specs/node/worker_threads/auto_exits.mjs
parent3e98ea4e69732d8a659ca0ca61747fe3887ab673 (diff)
refactor(tests): move worker_threads itests to spec tests (#23648)
Diffstat (limited to 'tests/specs/node/worker_threads/auto_exits.mjs')
-rw-r--r--tests/specs/node/worker_threads/auto_exits.mjs19
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/specs/node/worker_threads/auto_exits.mjs b/tests/specs/node/worker_threads/auto_exits.mjs
new file mode 100644
index 000000000..e434f59f7
--- /dev/null
+++ b/tests/specs/node/worker_threads/auto_exits.mjs
@@ -0,0 +1,19 @@
+import { isMainThread, parentPort, Worker } from "node:worker_threads";
+
+function onMessageOneshot() {
+ console.log("Got message from main thread!");
+ parentPort.off("message", onMessageOneshot);
+}
+
+if (isMainThread) {
+ // This re-loads the current file inside a Worker instance.
+ const w = new Worker(import.meta.filename);
+
+ setTimeout(() => {
+ w.postMessage("Hello! I am from the main thread.");
+ }, 500);
+} else {
+ console.log("Inside Worker!");
+ console.log(isMainThread); // Prints 'false'.
+ parentPort.on("message", onMessageOneshot);
+}