@@ -8,20 +8,65 @@ if (common.isWindows) {
88 common . skip ( 'Not applicable on Windows' ) ;
99}
1010
11- const child = spawn ( process . execPath , [
12- '-e' ,
13- 'require("fs").closeSync(0); setTimeout(() => {}, 2000)' ,
14- ] , { stdio : [ 'pipe' , 'ignore' , 'ignore' ] } ) ;
15-
16- const timeout = setTimeout ( ( ) => {
17- assert . fail ( 'stdin close event was not emitted' ) ;
18- } , 1000 ) ;
19-
20- child . stdin . on ( 'close' , common . mustCall ( ( ) => {
21- clearTimeout ( timeout ) ;
22- child . kill ( ) ;
23- } ) ) ;
11+ function spawnChild ( script ) {
12+ return spawn ( process . execPath , [ '-e' , script ] , {
13+ stdio : [ 'pipe' , 'ignore' , 'ignore' ] ,
14+ } ) ;
15+ }
16+
17+ function runTest ( { script, setup } , done ) {
18+ const child = spawnChild ( script ) ;
19+ const timeout = setTimeout ( ( ) => {
20+ assert . fail ( 'stdin close event was not emitted' ) ;
21+ } , 2000 ) ;
22+
23+ let closed = false ;
24+ let exited = false ;
25+ function maybeDone ( ) {
26+ if ( ! closed || ! exited ) return ;
27+ clearTimeout ( timeout ) ;
28+ done ( ) ;
29+ }
30+
31+ child . stdin . once ( 'close' , common . mustCall ( ( ) => {
32+ closed = true ;
33+ child . kill ( ) ;
34+ maybeDone ( ) ;
35+ } ) ) ;
36+
37+ child . once ( 'exit' , common . mustCall ( ( ) => {
38+ exited = true ;
39+ maybeDone ( ) ;
40+ } ) ) ;
41+
42+ setup ( child ) ;
43+ }
44+
45+ runTest ( {
46+ script : 'setTimeout(() => require("fs").closeSync(0), 50); setTimeout(() => {}, 2000)' ,
47+ setup : ( ) => { } ,
48+ } , common . mustCall ( ( ) => {
49+ runTest ( {
50+ script : 'setTimeout(() => require("fs").closeSync(0), 200); setTimeout(() => {}, 2000)' ,
51+ setup : ( child ) => {
52+ const handle = child . stdin ?. _handle ;
53+ assert . strictEqual ( typeof handle ?. watchPeerClose , 'function' ) ;
54+ handle . watchPeerClose ( true , common . mustNotCall ( ) ) ;
55+ } ,
56+ } , common . mustCall ( ( ) => {
57+ runTest ( {
58+ script : 'setTimeout(() => {}, 2000)' ,
59+ setup : ( child ) => {
60+ const handle = child . stdin ?. _handle ;
61+ assert . strictEqual ( typeof handle ?. watchPeerClose , 'function' ) ;
2462
25- child . on ( 'exit' , common . mustCall ( ( ) => {
26- clearTimeout ( timeout ) ;
63+ child . stdin . once ( 'close' , common . mustCall ( ( ) => {
64+ assert . doesNotThrow ( ( ) => {
65+ handle . watchPeerClose ( true , common . mustNotCall ( ) ) ;
66+ } ) ;
67+ } ) ) ;
68+ child . stdin . destroy ( ) ;
69+ } ,
70+ } , common . mustCall ( ( ) => { } ) ) ;
71+ } ) ) ;
2772} ) ) ;
0 commit comments