11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst { Worker, parentPort, SHARE_ENV, workerData } = require('worker_threads');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciif (!workerData) {
71cb0ef41Sopenharmony_ci  process.env.SET_IN_PARENT = 'set';
81cb0ef41Sopenharmony_ci  assert.strictEqual(process.env.SET_IN_PARENT, 'set');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci  const w = new Worker(__filename, {
111cb0ef41Sopenharmony_ci    workerData: 'runInWorker',
121cb0ef41Sopenharmony_ci    env: SHARE_ENV
131cb0ef41Sopenharmony_ci  }).on('exit', common.mustCall(() => {
141cb0ef41Sopenharmony_ci    // Env vars from the child thread are not set globally.
151cb0ef41Sopenharmony_ci    assert.strictEqual(process.env.SET_IN_WORKER, 'set');
161cb0ef41Sopenharmony_ci  }));
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  process.env.SET_IN_PARENT_AFTER_CREATION = 'set';
191cb0ef41Sopenharmony_ci  w.postMessage({});
201cb0ef41Sopenharmony_ci} else {
211cb0ef41Sopenharmony_ci  assert.strictEqual(workerData, 'runInWorker');
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  // Env vars from the parent thread are inherited.
241cb0ef41Sopenharmony_ci  assert.strictEqual(process.env.SET_IN_PARENT, 'set');
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  process.env.SET_IN_WORKER = 'set';
271cb0ef41Sopenharmony_ci  assert.strictEqual(process.env.SET_IN_WORKER, 'set');
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  parentPort.once('message', common.mustCall(() => {
301cb0ef41Sopenharmony_ci    assert.strictEqual(process.env.SET_IN_PARENT_AFTER_CREATION, 'set');
311cb0ef41Sopenharmony_ci  }));
321cb0ef41Sopenharmony_ci}
33