summaryrefslogtreecommitdiff
path: root/test/parallel/test-child-process-spawnsync-kill-signal.js
blob: 6d14f24724bb2b3c161a673ef273676bf6ba8f7e (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
// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const cp = require('child_process');

if (process.argv[2] === 'child') {
  setInterval(() => {}, 1000);
} else {
  const internalCp = require('internal/child_process');
  const oldSpawnSync = internalCp.spawnSync;
  const { SIGKILL } = require('os').constants.signals;

  function spawn(killSignal, beforeSpawn) {
    if (beforeSpawn) {
      internalCp.spawnSync = common.mustCall(function(opts) {
        beforeSpawn(opts);
        return oldSpawnSync(opts);
      });
    }
    const child = cp.spawnSync(process.execPath,
                               [__filename, 'child'],
                               { killSignal, timeout: 100 });
    if (beforeSpawn)
      internalCp.spawnSync = oldSpawnSync;
    assert.strictEqual(child.status, null);
    assert.strictEqual(child.error.code, 'ETIMEDOUT');
    return child;
  }

  // Verify that an error is thrown for unknown signals.
  assert.throws(() => {
    spawn('SIG_NOT_A_REAL_SIGNAL');
  }, { code: 'ERR_UNKNOWN_SIGNAL', name: 'TypeError' });

  // Verify that the default kill signal is SIGTERM.
  {
    const child = spawn(undefined, (opts) => {
      assert.strictEqual(opts.killSignal, undefined);
    });

    assert.strictEqual(child.signal, 'SIGTERM');
  }

  // Verify that a string signal name is handled properly.
  {
    const child = spawn('SIGKILL', (opts) => {
      assert.strictEqual(opts.killSignal, SIGKILL);
    });

    assert.strictEqual(child.signal, 'SIGKILL');
  }

  // Verify that a numeric signal is handled properly.
  {
    assert.strictEqual(typeof SIGKILL, 'number');

    const child = spawn(SIGKILL, (opts) => {
      assert.strictEqual(opts.killSignal, SIGKILL);
    });

    assert.strictEqual(child.signal, 'SIGKILL');
  }
}