@@ -5,15 +5,20 @@ const vm = require('vm');
55
66const spawn = require('child_process').spawn;
77
8+ const methods = [
9+ 'runInThisContext',
10+ 'runInContext'
11+ ];
12+
813if (common.isWindows) {
914 // No way to send CTRL_C_EVENT to processes from JS right now.
1015 common.skip('platform not supported');
1116 return;
1217}
1318
1419if (process.argv[2] === 'child') {
15- const parent = + process.env.REPL_TEST_PPID ;
16- assert.ok(parent );
20+ const method = process.argv[3] ;
21+ assert.ok(method );
1722
1823 let firstHandlerCalled = 0;
1924 process.on('SIGINT', common.mustCall(() => {
@@ -27,12 +32,14 @@ if (process.argv[2] === 'child') {
2732 // Handler attached _before_ execution.
2833 }));
2934
30- assert.throws(() => {
31- vm.runInThisContext(`process.kill(${parent}, 'SIGUSR2'); while(true) {}`, {
32- breakOnSigint: true
33- }) ;
34- }, /Script execution interrupted/) ;
35+ const script = `process.send('${method}'); while(true) {}`;
36+ const args = method === 'runInContext' ?
37+ [vm.createContext({ process })] :
38+ [] ;
39+ const options = { breakOnSigint: true } ;
3540
41+ assert.throws(() => { vm[method](script, ...args, options); },
42+ /^Error: Script execution interrupted\.$/);
3643 assert.strictEqual(firstHandlerCalled, 0);
3744 assert.strictEqual(onceHandlerCalled, 0);
3845
@@ -46,7 +53,9 @@ if (process.argv[2] === 'child') {
4653 if (afterHandlerCalled++ === 0) {
4754 // The first time it just bounces back to check that the `once()`
4855 // handler is not called the second time.
49- process.kill(parent, 'SIGUSR2');
56+ assert.strictEqual(firstHandlerCalled, 1);
57+ assert.strictEqual(onceHandlerCalled, 1);
58+ process.send(method);
5059 return;
5160 }
5261
@@ -55,26 +64,24 @@ if (process.argv[2] === 'child') {
5564 timeout.unref();
5665 }, 2));
5766
58- process.kill(parent, 'SIGUSR2' );
67+ process.send(method );
5968
6069 return;
6170}
6271
63- process.env.REPL_TEST_PPID = process.pid;
64-
65- // Set the `SIGUSR2` handler before spawning the child process to make sure
66- // the signal is always handled.
67- process.on('SIGUSR2', common.mustCall(() => {
68- // First kill() breaks the while(true) loop, second one invokes the real
69- // signal handlers.
70- process.kill(child.pid, 'SIGINT');
71- }, 3));
72+ for (const method of methods) {
73+ const child = spawn(process.execPath, [__filename, 'child', method], {
74+ stdio: [null, 'inherit', 'inherit', 'ipc']
75+ });
7276
73- const child = spawn(process.execPath, [__filename, 'child'], {
74- stdio: [null, 'inherit', 'inherit']
75- });
77+ child.on('message', common.mustCall(() => {
78+ // First kill() breaks the while(true) loop, second one invokes the real
79+ // signal handlers.
80+ process.kill(child.pid, 'SIGINT');
81+ }, 3));
7682
77- child.on('close', function(code, signal) {
78- assert.strictEqual(signal, null);
79- assert.strictEqual(code, 0);
80- });
83+ child.on('close', common.mustCall((code, signal) => {
84+ assert.strictEqual(signal, null);
85+ assert.strictEqual(code, 0);
86+ }));
87+ }
0 commit comments