1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 09:10:40 -05:00
denoland-deno/cli/tests/unit_node/testdata/worker_threads.mjs
Yoshiya Hinosawa 26f42a248f
fix(ext/node): add basic node:worker_threads support (#19192)
This PR restores `node:worker_threads` implementation and test cases
from
[`std@0.175.0/node`](https://github.com/denoland/deno_std/blob/0.175.0/node/worker_threads.ts).

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
2023-05-23 20:56:29 +02:00

34 lines
912 B
JavaScript

// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import {
getEnvironmentData,
isMainThread,
parentPort,
threadId,
workerData,
} from "node:worker_threads";
import { once } from "node:events";
async function message(expectedMessage) {
const [message] = await once(parentPort, "message");
if (message !== expectedMessage) {
console.log(`Expected the message "${expectedMessage}", but got`, message);
// fail test
parentPort.close();
}
}
await message("Hello, how are you my thread?");
parentPort.postMessage("I'm fine!");
await new Promise((resolve) => setTimeout(resolve, 100));
parentPort.postMessage({
isMainThread,
threadId,
workerData: Array.isArray(workerData) &&
workerData[workerData.length - 1] instanceof MessagePort
? workerData.slice(0, -1)
: workerData,
envData: [getEnvironmentData("test"), getEnvironmentData(1)],
});