• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const { promisify } = require('util');
5const execFile = promisify(require('child_process').execFile);
6const { Worker, isMainThread, workerData } = require('worker_threads');
7
8const variant = process.argv[process.argv.length - 1];
9switch (true) {
10  case variant === 'main-thread': {
11    return;
12  }
13  case variant === 'main-thread-exit': {
14    return process.exit(0);
15  }
16  case variant.startsWith('worker-thread'): {
17    const worker = new Worker(__filename, { workerData: variant });
18    worker.on('error', common.mustNotCall());
19    worker.on('exit', common.mustCall((code) => {
20      assert.strictEqual(code, 0);
21    }));
22    return;
23  }
24  case !isMainThread: {
25    if (workerData === 'worker-thread-exit') {
26      process.exit(0);
27    }
28    return;
29  }
30}
31
32(async function() {
33  for (const { execArgv, variant, warnings } of [
34    { execArgv: ['--trace-exit'], variant: 'main-thread-exit', warnings: 1 },
35    { execArgv: [], variant: 'main-thread-exit', warnings: 0 },
36    { execArgv: ['--trace-exit'], variant: 'main-thread', warnings: 0 },
37    { execArgv: [], variant: 'main-thread', warnings: 0 },
38    { execArgv: ['--trace-exit'], variant: 'worker-thread-exit', warnings: 1 },
39    { execArgv: [], variant: 'worker-thread-exit', warnings: 0 },
40    { execArgv: ['--trace-exit'], variant: 'worker-thread', warnings: 0 },
41    { execArgv: [], variant: 'worker-thread', warnings: 0 },
42  ]) {
43    const { stdout, stderr } =
44      await execFile(process.execPath, [...execArgv, __filename, variant]);
45    assert.strictEqual(stdout, '');
46    const actualWarnings =
47      stderr.match(/WARNING: Exited the environment with code 0/g);
48    if (warnings === 0) {
49      assert.strictEqual(actualWarnings, null);
50      continue;
51    }
52    assert.strictEqual(actualWarnings.length, warnings);
53
54    if (variant.startsWith('worker')) {
55      const workerIds = stderr.match(/\(node:\d+, thread:\d+\)/g);
56      assert.strictEqual(workerIds.length, warnings);
57    }
58  }
59})().then(common.mustCall());
60