@@ -5,15 +5,20 @@ const vm = require('vm');
5
5
6
6
const spawn = require ( 'child_process' ) . spawn ;
7
7
8
+ const methods = [
9
+ 'runInThisContext' ,
10
+ 'runInContext'
11
+ ] ;
12
+
8
13
if ( common . isWindows ) {
9
14
// No way to send CTRL_C_EVENT to processes from JS right now.
10
15
common . skip ( 'platform not supported' ) ;
11
16
return ;
12
17
}
13
18
14
19
if ( 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 ) ;
17
22
18
23
let firstHandlerCalled = 0 ;
19
24
process . on ( 'SIGINT' , common . mustCall ( ( ) => {
@@ -27,12 +32,14 @@ if (process.argv[2] === 'child') {
27
32
// Handler attached _before_ execution.
28
33
} ) ) ;
29
34
30
- assert . throws ( ( ) => {
31
- vm . runInThisContext ( `process.kill( ${ parent } , 'SIGUSR2'); while(true) {}` , {
32
- breakOnSigint : true
33
- } ) ;
34
- } , / S c r i p t e x e c u t i o n i n t e r r u p t e d / ) ;
35
+ const script = `process.send(' ${ method } '); while(true) {}` ;
36
+ const args = method === 'runInContext' ?
37
+ [ vm . createContext ( { process } ) ] :
38
+ [ ] ;
39
+ const options = { breakOnSigint : true } ;
35
40
41
+ assert . throws ( ( ) => { vm [ method ] ( script , ...args , options ) ; } ,
42
+ / ^ E r r o r : S c r i p t e x e c u t i o n i n t e r r u p t e d \. $ / ) ;
36
43
assert . strictEqual ( firstHandlerCalled , 0 ) ;
37
44
assert . strictEqual ( onceHandlerCalled , 0 ) ;
38
45
@@ -46,7 +53,9 @@ if (process.argv[2] === 'child') {
46
53
if ( afterHandlerCalled ++ === 0 ) {
47
54
// The first time it just bounces back to check that the `once()`
48
55
// 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 ) ;
50
59
return ;
51
60
}
52
61
@@ -55,26 +64,24 @@ if (process.argv[2] === 'child') {
55
64
timeout . unref ( ) ;
56
65
} , 2 ) ) ;
57
66
58
- process . kill ( parent , 'SIGUSR2' ) ;
67
+ process . send ( method ) ;
59
68
60
69
return ;
61
70
}
62
71
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
+ } ) ;
72
76
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 ) ) ;
76
82
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