11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst { promisify } = require('util');
51cb0ef41Sopenharmony_ciconst execFile = promisify(require('child_process').execFile);
61cb0ef41Sopenharmony_ciconst { Worker, isMainThread, workerData } = require('worker_threads');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst variant = process.argv[process.argv.length - 1];
91cb0ef41Sopenharmony_ciswitch (true) {
101cb0ef41Sopenharmony_ci  case variant === 'main-thread': {
111cb0ef41Sopenharmony_ci    return;
121cb0ef41Sopenharmony_ci  }
131cb0ef41Sopenharmony_ci  case variant === 'main-thread-exit': {
141cb0ef41Sopenharmony_ci    return process.exit(0);
151cb0ef41Sopenharmony_ci  }
161cb0ef41Sopenharmony_ci  case variant.startsWith('worker-thread'): {
171cb0ef41Sopenharmony_ci    const worker = new Worker(__filename, { workerData: variant });
181cb0ef41Sopenharmony_ci    worker.on('error', common.mustNotCall());
191cb0ef41Sopenharmony_ci    worker.on('exit', common.mustCall((code) => {
201cb0ef41Sopenharmony_ci      assert.strictEqual(code, 0);
211cb0ef41Sopenharmony_ci    }));
221cb0ef41Sopenharmony_ci    return;
231cb0ef41Sopenharmony_ci  }
241cb0ef41Sopenharmony_ci  case !isMainThread: {
251cb0ef41Sopenharmony_ci    if (workerData === 'worker-thread-exit') {
261cb0ef41Sopenharmony_ci      process.exit(0);
271cb0ef41Sopenharmony_ci    }
281cb0ef41Sopenharmony_ci    return;
291cb0ef41Sopenharmony_ci  }
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci(async function() {
331cb0ef41Sopenharmony_ci  for (const { execArgv, variant, warnings } of [
341cb0ef41Sopenharmony_ci    { execArgv: ['--trace-exit'], variant: 'main-thread-exit', warnings: 1 },
351cb0ef41Sopenharmony_ci    { execArgv: [], variant: 'main-thread-exit', warnings: 0 },
361cb0ef41Sopenharmony_ci    { execArgv: ['--trace-exit'], variant: 'main-thread', warnings: 0 },
371cb0ef41Sopenharmony_ci    { execArgv: [], variant: 'main-thread', warnings: 0 },
381cb0ef41Sopenharmony_ci    { execArgv: ['--trace-exit'], variant: 'worker-thread-exit', warnings: 1 },
391cb0ef41Sopenharmony_ci    { execArgv: [], variant: 'worker-thread-exit', warnings: 0 },
401cb0ef41Sopenharmony_ci    { execArgv: ['--trace-exit'], variant: 'worker-thread', warnings: 0 },
411cb0ef41Sopenharmony_ci    { execArgv: [], variant: 'worker-thread', warnings: 0 },
421cb0ef41Sopenharmony_ci  ]) {
431cb0ef41Sopenharmony_ci    const { stdout, stderr } =
441cb0ef41Sopenharmony_ci      await execFile(process.execPath, [...execArgv, __filename, variant]);
451cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, '');
461cb0ef41Sopenharmony_ci    const actualWarnings =
471cb0ef41Sopenharmony_ci      stderr.match(/WARNING: Exited the environment with code 0/g);
481cb0ef41Sopenharmony_ci    if (warnings === 0) {
491cb0ef41Sopenharmony_ci      assert.strictEqual(actualWarnings, null);
501cb0ef41Sopenharmony_ci      continue;
511cb0ef41Sopenharmony_ci    }
521cb0ef41Sopenharmony_ci    assert.strictEqual(actualWarnings.length, warnings);
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    if (variant.startsWith('worker')) {
551cb0ef41Sopenharmony_ci      const workerIds = stderr.match(/\(node:\d+, thread:\d+\)/g);
561cb0ef41Sopenharmony_ci      assert.strictEqual(workerIds.length, warnings);
571cb0ef41Sopenharmony_ci    }
581cb0ef41Sopenharmony_ci  }
591cb0ef41Sopenharmony_ci})().then(common.mustCall());
60