xref: /third_party/node/test/parallel/test-worker-message-channel-sharedarraybuffer.js
  • Home
  • History
  • Annotate Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
  • only in /third_party/node/test/parallel/
11cb0ef41Sopenharmony_ci// Flags: --expose-gc
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst { Worker } = require('worker_threads');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci{
91cb0ef41Sopenharmony_ci  const sharedArrayBuffer = new SharedArrayBuffer(12);
101cb0ef41Sopenharmony_ci  const local = Buffer.from(sharedArrayBuffer);
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci  const w = new Worker(`
131cb0ef41Sopenharmony_ci    const { parentPort } = require('worker_threads');
141cb0ef41Sopenharmony_ci    parentPort.on('message', ({ sharedArrayBuffer }) => {
151cb0ef41Sopenharmony_ci      const local = Buffer.from(sharedArrayBuffer);
161cb0ef41Sopenharmony_ci      local.write('world!', 6);
171cb0ef41Sopenharmony_ci      parentPort.postMessage('written!');
181cb0ef41Sopenharmony_ci    });
191cb0ef41Sopenharmony_ci  `, { eval: true });
201cb0ef41Sopenharmony_ci  w.on('message', common.mustCall(() => {
211cb0ef41Sopenharmony_ci    assert.strictEqual(local.toString(), 'Hello world!');
221cb0ef41Sopenharmony_ci    global.gc();
231cb0ef41Sopenharmony_ci    w.terminate();
241cb0ef41Sopenharmony_ci  }));
251cb0ef41Sopenharmony_ci  w.postMessage({ sharedArrayBuffer });
261cb0ef41Sopenharmony_ci  // This would be a race condition if the memory regions were overlapping
271cb0ef41Sopenharmony_ci  local.write('Hello ');
281cb0ef41Sopenharmony_ci}
29

Indexes created Thu Nov 07 10:32:03 CST 2024