Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: expand Worker test for non-shared ArrayBuffer #30044

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions test/parallel/test-worker-sharedarraybuffer-from-worker-thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@ const assert = require('assert');
const { Worker } = require('worker_threads');

// Regression test for https://github.com/nodejs/node/issues/28777
// Make sure that SharedArrayBuffers created in Worker threads are accessible
// after the creating thread ended.
// Make sure that SharedArrayBuffers and transferred ArrayBuffers created in
// Worker threads are accessible after the creating thread ended.

const w = new Worker(`
const { parentPort } = require('worker_threads');
const sharedArrayBuffer = new SharedArrayBuffer(4);
parentPort.postMessage(sharedArrayBuffer);
`, { eval: true });
for (const ctor of ['ArrayBuffer', 'SharedArrayBuffer']) {
const w = new Worker(`
const { parentPort } = require('worker_threads');
const arrayBuffer = new ${ctor}(4);
parentPort.postMessage(
arrayBuffer,
'${ctor}' === 'SharedArrayBuffer' ? [] : [arrayBuffer]);
`, { eval: true });

let sharedArrayBuffer;
w.once('message', common.mustCall((message) => sharedArrayBuffer = message));
w.once('exit', common.mustCall(() => {
const uint8array = new Uint8Array(sharedArrayBuffer);
uint8array[0] = 42;
assert.deepStrictEqual(uint8array, new Uint8Array([42, 0, 0, 0]));
}));
let arrayBuffer;
w.once('message', common.mustCall((message) => arrayBuffer = message));
w.once('exit', common.mustCall(() => {
assert.strictEqual(arrayBuffer.constructor.name, ctor);
const uint8array = new Uint8Array(arrayBuffer);
uint8array[0] = 42;
assert.deepStrictEqual(uint8array, new Uint8Array([42, 0, 0, 0]));
}));
}