11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst { Worker, isMainThread, parentPort } = require('worker_threads');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// Verify that cwd changes from the main thread are handled correctly in
71cb0ef41Sopenharmony_ci// workers.
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// Do not use isMainThread directly, otherwise the test would time out in case
101cb0ef41Sopenharmony_ci// it's started inside of another worker thread.
111cb0ef41Sopenharmony_ciif (!process.env.HAS_STARTED_WORKER) {
121cb0ef41Sopenharmony_ci  process.env.HAS_STARTED_WORKER = '1';
131cb0ef41Sopenharmony_ci  if (!isMainThread) {
141cb0ef41Sopenharmony_ci    common.skip('This test can only run as main thread');
151cb0ef41Sopenharmony_ci  }
161cb0ef41Sopenharmony_ci  // Normalize the current working dir to also work with the root folder.
171cb0ef41Sopenharmony_ci  process.chdir(__dirname);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  assert(!process.cwd.toString().includes('Atomics.load'));
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  // 1. Start the first worker.
221cb0ef41Sopenharmony_ci  const w = new Worker(__filename);
231cb0ef41Sopenharmony_ci  w.once('message', common.mustCall((message) => {
241cb0ef41Sopenharmony_ci    // 5. Change the cwd and send that to the spawned worker.
251cb0ef41Sopenharmony_ci    assert.strictEqual(message, process.cwd());
261cb0ef41Sopenharmony_ci    process.chdir('..');
271cb0ef41Sopenharmony_ci    w.postMessage(process.cwd());
281cb0ef41Sopenharmony_ci  }));
291cb0ef41Sopenharmony_ci} else if (!process.env.SECOND_WORKER) {
301cb0ef41Sopenharmony_ci  process.env.SECOND_WORKER = '1';
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  // 2. Save the current cwd and verify that `process.cwd` includes the
331cb0ef41Sopenharmony_ci  //    Atomics.load call and spawn a new worker.
341cb0ef41Sopenharmony_ci  const cwd = process.cwd();
351cb0ef41Sopenharmony_ci  assert(process.cwd.toString().includes('Atomics.load'));
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  const w = new Worker(__filename);
381cb0ef41Sopenharmony_ci  w.once('message', common.mustCall((message) => {
391cb0ef41Sopenharmony_ci    // 4. Verify at the current cwd is identical to the received and the
401cb0ef41Sopenharmony_ci    //    original one.
411cb0ef41Sopenharmony_ci    assert.strictEqual(process.cwd(), message);
421cb0ef41Sopenharmony_ci    assert.strictEqual(message, cwd);
431cb0ef41Sopenharmony_ci    parentPort.postMessage(cwd);
441cb0ef41Sopenharmony_ci  }));
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  parentPort.once('message', common.mustCall((message) => {
471cb0ef41Sopenharmony_ci    // 6. Verify that the current cwd is identical to the received one but not
481cb0ef41Sopenharmony_ci    //    with the original one.
491cb0ef41Sopenharmony_ci    assert.strictEqual(process.cwd(), message);
501cb0ef41Sopenharmony_ci    assert.notStrictEqual(message, cwd);
511cb0ef41Sopenharmony_ci    w.postMessage(message);
521cb0ef41Sopenharmony_ci  }));
531cb0ef41Sopenharmony_ci} else {
541cb0ef41Sopenharmony_ci  // 3. Save the current cwd, post back to the "main" thread and verify that
551cb0ef41Sopenharmony_ci  //    `process.cwd` includes the Atomics.load call.
561cb0ef41Sopenharmony_ci  const cwd = process.cwd();
571cb0ef41Sopenharmony_ci  // Send the current cwd to the parent.
581cb0ef41Sopenharmony_ci  parentPort.postMessage(cwd);
591cb0ef41Sopenharmony_ci  assert(process.cwd.toString().includes('Atomics.load'));
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  parentPort.once('message', common.mustCall((message) => {
621cb0ef41Sopenharmony_ci    // 7. Verify that the current cwd is identical to the received one but
631cb0ef41Sopenharmony_ci    //    not with the original one.
641cb0ef41Sopenharmony_ci    assert.strictEqual(process.cwd(), message);
651cb0ef41Sopenharmony_ci    assert.notStrictEqual(message, cwd);
661cb0ef41Sopenharmony_ci  }));
671cb0ef41Sopenharmony_ci}
68