• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --debug-arraybuffer-allocations
2'use strict';
3const common = require('../common');
4const assert = require('assert');
5const { Worker } = require('worker_threads');
6
7// Regression test for https://github.com/nodejs/node/issues/28777
8// Make sure that SharedArrayBuffers and transferred ArrayBuffers created in
9// Worker threads are accessible after the creating thread ended.
10
11for (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 });
19
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}
29