1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05: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:
Bartek Iwańczuk 2024-03-11 22:59:42 +00:00 committed by GitHub
parent a77b2987bc
commit 47f318230f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 37 additions and 0 deletions

View 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");

View file

@ -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,
});