Skip to content

Commit

Permalink
test: expand Worker test for non-shared ArrayBuffer
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
addaleax authored and targos committed Nov 10, 2019
1 parent 94d6719 commit 4f5d414
Showing 1 changed file with 19 additions and 14 deletions.
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]));
}));
}

0 comments on commit 4f5d414

Please sign in to comment.