mirror of
https://github.com/denoland/deno
synced 2024-08-25 02:57:22 +00:00
test(ext/node): add worker_threads test for SharedArrayBuffer (#22850)
Follow up to https://github.com/denoland/deno/pull/22815
This commit is contained in:
parent
a77b2987bc
commit
47f318230f
17
tests/unit_node/testdata/worker_threads2.mjs
vendored
Normal file
17
tests/unit_node/testdata/worker_threads2.mjs
vendored
Normal file
|
@ -0,0 +1,17 @@
|
|||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
import { parentPort, 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();
|
||||
}
|
||||
}
|
||||
|
||||
const uint = new Uint8Array(workerData.sharedArrayBuffer);
|
||||
uint[0]++;
|
||||
await message("Hello");
|
||||
parentPort.postMessage("Hello");
|
|
@ -218,3 +218,23 @@ Deno.test({
|
|||
clearTimeout(timeout);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "[worker_threads] SharedArrayBuffer",
|
||||
async fn() {
|
||||
const sab = new SharedArrayBuffer(Uint8Array.BYTES_PER_ELEMENT);
|
||||
const uint = new Uint8Array(sab);
|
||||
const worker = new workerThreads.Worker(
|
||||
new URL("./testdata/worker_threads2.mjs", import.meta.url),
|
||||
{
|
||||
workerData: { sharedArrayBuffer: sab },
|
||||
},
|
||||
);
|
||||
worker.postMessage("Hello");
|
||||
if ((await once(worker, "message"))[0] != "Hello") throw new Error();
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
worker.terminate();
|
||||
if (uint[0] != 1) throw new Error();
|
||||
},
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue