• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const { mustCall, mustNotCall } = require('../common');
4const { strictEqual } = require('assert');
5const fixtures = require('../common/fixtures');
6const { fork } = require('child_process');
7
8{
9  // Test aborting a forked child_process after calling fork
10  const ac = new AbortController();
11  const { signal } = ac;
12  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
13    signal
14  });
15  cp.on('exit', mustCall((code, killSignal) => {
16    strictEqual(code, null);
17    strictEqual(killSignal, 'SIGTERM');
18  }));
19  cp.on('error', mustCall((err) => {
20    strictEqual(err.name, 'AbortError');
21  }));
22  process.nextTick(() => ac.abort());
23}
24
25{
26  // Test aborting with custom error
27  const ac = new AbortController();
28  const { signal } = ac;
29  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
30    signal
31  });
32  cp.on('exit', mustCall((code, killSignal) => {
33    strictEqual(code, null);
34    strictEqual(killSignal, 'SIGTERM');
35  }));
36  cp.on('error', mustCall((err) => {
37    strictEqual(err.name, 'AbortError');
38    strictEqual(err.cause.name, 'Error');
39    strictEqual(err.cause.message, 'boom');
40  }));
41  process.nextTick(() => ac.abort(new Error('boom')));
42}
43
44{
45  // Test passing an already aborted signal to a forked child_process
46  const signal = AbortSignal.abort();
47  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
48    signal
49  });
50  cp.on('exit', mustCall((code, killSignal) => {
51    strictEqual(code, null);
52    strictEqual(killSignal, 'SIGTERM');
53  }));
54  cp.on('error', mustCall((err) => {
55    strictEqual(err.name, 'AbortError');
56  }));
57}
58
59{
60  // Test passing an aborted signal with custom error to a forked child_process
61  const signal = AbortSignal.abort(new Error('boom'));
62  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
63    signal
64  });
65  cp.on('exit', mustCall((code, killSignal) => {
66    strictEqual(code, null);
67    strictEqual(killSignal, 'SIGTERM');
68  }));
69  cp.on('error', mustCall((err) => {
70    strictEqual(err.name, 'AbortError');
71    strictEqual(err.cause.name, 'Error');
72    strictEqual(err.cause.message, 'boom');
73  }));
74}
75
76{
77  // Test passing a different kill signal
78  const signal = AbortSignal.abort();
79  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
80    signal,
81    killSignal: 'SIGKILL',
82  });
83  cp.on('exit', mustCall((code, killSignal) => {
84    strictEqual(code, null);
85    strictEqual(killSignal, 'SIGKILL');
86  }));
87  cp.on('error', mustCall((err) => {
88    strictEqual(err.name, 'AbortError');
89  }));
90}
91
92{
93  // Test aborting a cp before close but after exit
94  const ac = new AbortController();
95  const { signal } = ac;
96  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
97    signal
98  });
99  cp.on('exit', mustCall(() => {
100    ac.abort();
101  }));
102  cp.on('error', mustNotCall());
103
104  setTimeout(() => cp.kill(), 1);
105}
106