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, workerData } = require('worker_threads');
141cb0ef41Sopenharmony_ci    const local = Buffer.from(workerData.sharedArrayBuffer);
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci    parentPort.on('message', () => {
171cb0ef41Sopenharmony_ci      local.write('world!', 6);
181cb0ef41Sopenharmony_ci      parentPort.postMessage('written!');
191cb0ef41Sopenharmony_ci    });
201cb0ef41Sopenharmony_ci  `, {
211cb0ef41Sopenharmony_ci    eval: true,
221cb0ef41Sopenharmony_ci    workerData: { sharedArrayBuffer }
231cb0ef41Sopenharmony_ci  });
241cb0ef41Sopenharmony_ci  w.on('message', common.mustCall(() => {
251cb0ef41Sopenharmony_ci    assert.strictEqual(local.toString(), 'Hello world!');
261cb0ef41Sopenharmony_ci    global.gc();
271cb0ef41Sopenharmony_ci    w.terminate();
281cb0ef41Sopenharmony_ci  }));
291cb0ef41Sopenharmony_ci  w.postMessage({});
301cb0ef41Sopenharmony_ci  // This would be a race condition if the memory regions were overlapping
311cb0ef41Sopenharmony_ci  local.write('Hello ');
321cb0ef41Sopenharmony_ci}
33