aboutsummaryrefslogtreecommitdiff
path: root/test/parallel/test-worker-exit-code.js
blob: b621389b49ca6b13f9b07bedd7357f335a44d2a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Flags: --experimental-worker
'use strict';
const common = require('../common');

// This test checks that Worker has correct exit codes on parent side
// in multiple situations.

const assert = require('assert');
const worker = require('worker_threads');
const { Worker, parentPort } = worker;

// Do not use isMainThread so that this test itself can be run inside a Worker.
if (!process.env.HAS_STARTED_WORKER) {
  process.env.HAS_STARTED_WORKER = 1;
  parent();
} else {
  if (!parentPort) {
    console.error('Parent port must not be null');
    process.exit(100);
    return;
  }
  parentPort.once('message', (msg) => {
    switch (msg) {
      case 'child1':
        return child1();
      case 'child2':
        return child2();
      case 'child3':
        return child3();
      case 'child4':
        return child4();
      case 'child5':
        return child5();
      case 'child6':
        return child6();
      case 'child7':
        return child7();
      default:
        throw new Error('invalid');
    }
  });
}

function child1() {
  process.exitCode = 42;
  process.on('exit', (code) => {
    assert.strictEqual(code, 42);
  });
}

function child2() {
  process.exitCode = 99;
  process.on('exit', (code) => {
    assert.strictEqual(code, 42);
  });
  process.exit(42);
}

function child3() {
  process.exitCode = 99;
  process.on('exit', (code) => {
    assert.strictEqual(code, 0);
  });
  process.exit(0);
}

function child4() {
  process.exitCode = 99;
  process.on('exit', (code) => {
    // cannot use assert because it will be uncaughtException -> 1 exit code
    // that will render this test useless
    if (code !== 1) {
      console.error('wrong code! expected 1 for uncaughtException');
      process.exit(99);
    }
  });
  throw new Error('ok');
}

function child5() {
  process.exitCode = 95;
  process.on('exit', (code) => {
    assert.strictEqual(code, 95);
    process.exitCode = 99;
  });
}

function child6() {
  process.on('exit', (code) => {
    assert.strictEqual(code, 0);
  });
  process.on('uncaughtException', common.mustCall(() => {
    // handle
  }));
  throw new Error('ok');
}

function child7() {
  process.on('exit', (code) => {
    assert.strictEqual(code, 97);
  });
  process.on('uncaughtException', common.mustCall(() => {
    process.exitCode = 97;
  }));
  throw new Error('ok');
}

function parent() {
  const test = (arg, exit, error = null) => {
    const w = new Worker(__filename);
    w.on('exit', common.mustCall((code) => {
      assert.strictEqual(
        code, exit,
        `wrong exit for ${arg}\nexpected:${exit} but got:${code}`);
      console.log(`ok - ${arg} exited with ${exit}`);
    }));
    if (error) {
      w.on('error', common.mustCall((err) => {
        assert(error.test(err));
      }));
    }
    w.postMessage(arg);
  };

  test('child1', 42);
  test('child2', 42);
  test('child3', 0);
  test('child4', 1, /^Error: ok$/);
  test('child5', 99);
  test('child6', 0);
  test('child7', 97);
}