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 state from before the interruption is still available. 29 child.stdin.end('a*2*3*7\n'); 30 })); 31 })); 32 33 child.stdin.write('a = 1001;' + 34 'process.kill(+process.env.REPL_TEST_PPID, "SIGUSR2");' + 35 'while(true){}\n'); 36})); 37 38child.on('close', function(code) { 39 assert.strictEqual(code, 0); 40 const expected = 'Script execution was interrupted by `SIGINT`'; 41 assert.ok( 42 stdout.includes(expected), 43 `Expected stdout to contain "${expected}", got ${stdout}` 44 ); 45 assert.ok( 46 stdout.includes('42042\n'), 47 `Expected stdout to contain "42042", got ${stdout}` 48 ); 49}); 50