• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3if (common.isWindows) {
4  // No way to send CTRL_C_EVENT to processes from JS right now.
5  common.skip('platform not supported');
6}
7if (!common.isMainThread)
8  common.skip('No signal handling available in Workers');
9
10const assert = require('assert');
11const spawn = require('child_process').spawn;
12
13process.env.REPL_TEST_PPID = process.pid;
14const child = spawn(process.execPath, [ '-i' ], {
15  stdio: [null, null, 2]
16});
17
18let stdout = '';
19child.stdout.setEncoding('utf8');
20child.stdout.on('data', function(c) {
21  stdout += c;
22});
23
24child.stdout.once('data', common.mustCall(() => {
25  process.on('SIGUSR2', common.mustCall(() => {
26    process.kill(child.pid, 'SIGINT');
27    child.stdout.once('data', common.mustCall(() => {
28      // Make sure REPL still works.
29      child.stdin.end('"foobar"\n');
30    }));
31  }));
32
33  child.stdin.write('process.kill(+process.env.REPL_TEST_PPID, "SIGUSR2");' +
34                    'vm.runInThisContext("while(true){}", ' +
35                    '{ breakOnSigint: true });\n');
36}));
37
38child.on('close', function(code) {
39  const expected = 'Script execution was interrupted by `SIGINT`';
40  assert.ok(
41    stdout.includes(expected),
42    `Expected stdout to contain "${expected}", got ${stdout}`
43  );
44  assert.ok(
45    stdout.includes('foobar'),
46    `Expected stdout to contain "foobar", got ${stdout}`
47  );
48});
49