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} 7 8const assert = require('assert'); 9const vm = require('vm'); 10const spawn = require('child_process').spawn; 11 12if (process.argv[2] === 'child') { 13 const method = process.argv[3]; 14 const listeners = +process.argv[4]; 15 assert.ok(method); 16 assert.ok(Number.isInteger(listeners)); 17 18 const script = `process.send('${method}'); while(true) {}`; 19 const args = method === 'runInContext' ? 20 [vm.createContext({ process })] : 21 []; 22 const options = { breakOnSigint: true }; 23 24 for (let i = 0; i < listeners; i++) 25 process.on('SIGINT', common.mustNotCall()); 26 27 assert.throws( 28 () => { vm[method](script, ...args, options); }, 29 { 30 code: 'ERR_SCRIPT_EXECUTION_INTERRUPTED', 31 message: 'Script execution was interrupted by `SIGINT`' 32 }); 33 return; 34} 35 36for (const method of ['runInThisContext', 'runInContext']) { 37 for (const listeners of [0, 1, 2]) { 38 const args = [__filename, 'child', method, listeners]; 39 const child = spawn(process.execPath, args, { 40 stdio: [null, 'pipe', 'inherit', 'ipc'] 41 }); 42 43 child.on('message', common.mustCall(() => { 44 process.kill(child.pid, 'SIGINT'); 45 })); 46 47 child.on('close', common.mustCall((code, signal) => { 48 assert.strictEqual(signal, null); 49 assert.strictEqual(code, 0); 50 })); 51 } 52} 53