Skip to content

Commit ecf6ae8

Browse files
addaleaxtargos
authored andcommitted
test: expand Worker test for non-shared ArrayBuffer
This test would be broken by V8 7.9 due to the changed `ArrayBuffer` backing store management (the same way that V8 7.8 broke this for `SharedArrayBuffer`s). While working on a solution, it would be good to already have this test in Node.js to avoid unnecessary accidental breakage. Refs: nodejs/node-v8#115 PR-URL: #30044 Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 4b57088 commit ecf6ae8

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

test/parallel/test-worker-sharedarraybuffer-from-worker-thread.js

+19-14
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@ const assert = require('assert');
55
const { Worker } = require('worker_threads');
66

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

11-
const w = new Worker(`
12-
const { parentPort } = require('worker_threads');
13-
const sharedArrayBuffer = new SharedArrayBuffer(4);
14-
parentPort.postMessage(sharedArrayBuffer);
15-
`, { eval: true });
11+
for (const ctor of ['ArrayBuffer', 'SharedArrayBuffer']) {
12+
const w = new Worker(`
13+
const { parentPort } = require('worker_threads');
14+
const arrayBuffer = new ${ctor}(4);
15+
parentPort.postMessage(
16+
arrayBuffer,
17+
'${ctor}' === 'SharedArrayBuffer' ? [] : [arrayBuffer]);
18+
`, { eval: true });
1619

17-
let sharedArrayBuffer;
18-
w.once('message', common.mustCall((message) => sharedArrayBuffer = message));
19-
w.once('exit', common.mustCall(() => {
20-
const uint8array = new Uint8Array(sharedArrayBuffer);
21-
uint8array[0] = 42;
22-
assert.deepStrictEqual(uint8array, new Uint8Array([42, 0, 0, 0]));
23-
}));
20+
let arrayBuffer;
21+
w.once('message', common.mustCall((message) => arrayBuffer = message));
22+
w.once('exit', common.mustCall(() => {
23+
assert.strictEqual(arrayBuffer.constructor.name, ctor);
24+
const uint8array = new Uint8Array(arrayBuffer);
25+
uint8array[0] = 42;
26+
assert.deepStrictEqual(uint8array, new Uint8Array([42, 0, 0, 0]));
27+
}));
28+
}

0 commit comments

Comments
 (0)