33
44const common = require ( '../common' ) ;
55const assert = require ( 'assert' ) ;
6- const cp = require ( 'child_process' ) ;
6+ const { spawn } = require ( 'child_process' ) ;
7+ const fixtures = require ( '../common/fixtures' ) ;
78
9+ const aliveScript = fixtures . path ( 'child-process-stay-alive-forever.js' ) ;
810{
911 // Verify that passing an AbortSignal works
1012 const controller = new AbortController ( ) ;
1113 const { signal } = controller ;
1214
13- const echo = cp . spawn ( 'echo' , [ 'fun' ] , {
14- encoding : 'utf8' ,
15- shell : true ,
16- signal
15+ const cp = spawn ( process . execPath , [ aliveScript ] , {
16+ signal,
1717 } ) ;
1818
19- echo . on ( 'error' , common . mustCall ( ( e ) => {
19+ cp . on ( 'exit' , common . mustCall ( ( code , killSignal ) => {
20+ assert . strictEqual ( code , null ) ;
21+ assert . strictEqual ( killSignal , 'SIGTERM' ) ;
22+ } ) ) ;
23+
24+ cp . on ( 'error' , common . mustCall ( ( e ) => {
2025 assert . strictEqual ( e . name , 'AbortError' ) ;
2126 } ) ) ;
2227
@@ -30,13 +35,76 @@ const cp = require('child_process');
3035
3136 controller . abort ( ) ;
3237
33- const echo = cp . spawn ( 'echo' , [ 'fun' ] , {
34- encoding : 'utf8' ,
35- shell : true ,
36- signal
38+ const cp = spawn ( process . execPath , [ aliveScript ] , {
39+ signal,
40+ } ) ;
41+ cp . on ( 'exit' , common . mustCall ( ( code , killSignal ) => {
42+ assert . strictEqual ( code , null ) ;
43+ assert . strictEqual ( killSignal , 'SIGTERM' ) ;
44+ } ) ) ;
45+
46+ cp . on ( 'error' , common . mustCall ( ( e ) => {
47+ assert . strictEqual ( e . name , 'AbortError' ) ;
48+ } ) ) ;
49+ }
50+
51+ {
52+ // Verify that waiting a bit and closing works
53+ const controller = new AbortController ( ) ;
54+ const { signal } = controller ;
55+
56+ const cp = spawn ( process . execPath , [ aliveScript ] , {
57+ signal,
58+ } ) ;
59+
60+ cp . on ( 'exit' , common . mustCall ( ( code , killSignal ) => {
61+ assert . strictEqual ( code , null ) ;
62+ assert . strictEqual ( killSignal , 'SIGTERM' ) ;
63+ } ) ) ;
64+
65+ cp . on ( 'error' , common . mustCall ( ( e ) => {
66+ assert . strictEqual ( e . name , 'AbortError' ) ;
67+ } ) ) ;
68+
69+ setTimeout ( ( ) => controller . abort ( ) , 1 ) ;
70+ }
71+
72+ {
73+ // Test passing a different killSignal
74+ const controller = new AbortController ( ) ;
75+ const { signal } = controller ;
76+
77+ const cp = spawn ( process . execPath , [ aliveScript ] , {
78+ signal,
79+ killSignal : 'SIGKILL' ,
3780 } ) ;
3881
39- echo . on ( 'error' , common . mustCall ( ( e ) => {
82+ cp . on ( 'exit' , common . mustCall ( ( code , killSignal ) => {
83+ assert . strictEqual ( code , null ) ;
84+ assert . strictEqual ( killSignal , 'SIGKILL' ) ;
85+ } ) ) ;
86+
87+ cp . on ( 'error' , common . mustCall ( ( e ) => {
4088 assert . strictEqual ( e . name , 'AbortError' ) ;
4189 } ) ) ;
90+
91+ setTimeout ( ( ) => controller . abort ( ) , 1 ) ;
92+ }
93+
94+ {
95+ // Test aborting a cp before close but after exit
96+ const controller = new AbortController ( ) ;
97+ const { signal } = controller ;
98+
99+ const cp = spawn ( process . execPath , [ aliveScript ] , {
100+ signal,
101+ } ) ;
102+
103+ cp . on ( 'exit' , common . mustCall ( ( ) => {
104+ controller . abort ( ) ;
105+ } ) ) ;
106+
107+ cp . on ( 'error' , common . mustNotCall ( ) ) ;
108+
109+ setTimeout ( ( ) => cp . kill ( ) , 1 ) ;
42110}
0 commit comments