summaryrefslogtreecommitdiff
path: root/test/parallel/test-child-process-fork-abort-signal.js
blob: 9982444c429552c0fd1999c3c343123f5a235417 (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
'use strict';

const { mustCall, mustNotCall } = require('../common');
const { strictEqual } = require('assert');
const fixtures = require('../common/fixtures');
const { fork } = require('child_process');

{
  // Test aborting a forked child_process after calling fork
  const ac = new AbortController();
  const { signal } = ac;
  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
    signal
  });
  cp.on('exit', mustCall((code, killSignal) => {
    strictEqual(code, null);
    strictEqual(killSignal, 'SIGTERM');
  }));
  cp.on('error', mustCall((err) => {
    strictEqual(err.name, 'AbortError');
  }));
  process.nextTick(() => ac.abort());
}
{
  // Test passing an already aborted signal to a forked child_process
  const signal = AbortSignal.abort();
  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
    signal
  });
  cp.on('exit', mustCall((code, killSignal) => {
    strictEqual(code, null);
    strictEqual(killSignal, 'SIGTERM');
  }));
  cp.on('error', mustCall((err) => {
    strictEqual(err.name, 'AbortError');
  }));
}

{
  // Test passing a different kill signal
  const signal = AbortSignal.abort();
  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
    signal,
    killSignal: 'SIGKILL',
  });
  cp.on('exit', mustCall((code, killSignal) => {
    strictEqual(code, null);
    strictEqual(killSignal, 'SIGKILL');
  }));
  cp.on('error', mustCall((err) => {
    strictEqual(err.name, 'AbortError');
  }));
}

{
  // Test aborting a cp before close but after exit
  const ac = new AbortController();
  const { signal } = ac;
  const cp = fork(fixtures.path('child-process-stay-alive-forever.js'), {
    signal
  });
  cp.on('exit', mustCall(() => {
    ac.abort();
  }));
  cp.on('error', mustNotCall());

  setTimeout(() => cp.kill(), 1);
}