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