@@ -8,19 +8,42 @@ const scripts = {
88 child : path . join ( __dirname , 'fixtures' , 'child.js' ) ,
99} ;
1010
11+ // Helper to poll for child processes
12+ async function waitForChildren ( pid : number , maxWaitMs = 3000 ) : Promise < any [ ] > {
13+ const startTime = Date . now ( ) ;
14+ while ( Date . now ( ) - startTime < maxWaitMs ) {
15+ const children = await psTree ( pid ) ;
16+ if ( children . length > 0 ) {
17+ return children ;
18+ }
19+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
20+ }
21+ return psTree ( pid ) ; // Final attempt
22+ }
23+
24+ // Helper to poll for processes to terminate
25+ async function waitForTermination ( pid : number , maxWaitMs = 10000 ) : Promise < void > {
26+ const startTime = Date . now ( ) ;
27+ while ( Date . now ( ) - startTime < maxWaitMs ) {
28+ const children = await psTree ( pid ) ;
29+ if ( children . length === 0 ) {
30+ return ;
31+ }
32+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
33+ }
34+ }
35+
1136describe ( 'psTree()' , ( ) => {
1237 test ( 'Spawn a Parent process which has Ten Child Processes' , async ( ) => {
1338 const parent = exec ( `node ${ scripts . parent } ` ) ;
1439
15- // Wait for the child process(es) to spawn.
16- await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
17-
18- const children = await psTree ( parent . pid ! ) ;
40+ // Poll until child processes are detected
41+ const children = await waitForChildren ( parent . pid ! ) ;
1942
2043 expect ( children . length ) . toBeGreaterThan ( 0 ) ;
2144
22- // Allow time for the processes to be terminated.
23- await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ;
45+ // Poll until processes terminate
46+ await waitForTermination ( parent . pid ! ) ;
2447
2548 const postKillChildren = await psTree ( parent . pid ! ) ;
2649
@@ -30,17 +53,27 @@ describe('psTree()', () => {
3053 test ( 'Includes itself if includeRoot is true' , async ( ) => {
3154 const parent = exec ( `node ${ scripts . parent } ` ) ;
3255
33- // Wait for the child process(es) to spawn.
34- await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
35-
36- const processes = await psTree ( parent . pid ! , true ) ;
56+ // Poll until processes are detected (parent + children, so > 1)
57+ let processes : any [ ] = [ ] ;
58+ const startTime = Date . now ( ) ;
59+ while ( Date . now ( ) - startTime < 3000 ) {
60+ processes = await psTree ( parent . pid ! , true ) ;
61+ // Wait for parent + at least one child (length > 1)
62+ if ( processes . length > 1 ) {
63+ break ;
64+ }
65+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
66+ }
67+ if ( processes . length <= 1 ) {
68+ processes = await psTree ( parent . pid ! , true ) ; // Final attempt
69+ }
3770
3871 const parentProcess = processes . find ( ( process ) => Number . parseInt ( process . PID , 10 ) === parent . pid ! ) ;
3972
4073 expect ( parentProcess ) . toBeDefined ( ) ;
4174
42- // Allow time for the processes to be terminated.
43- await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) ) ;
75+ // Poll until processes terminate
76+ await waitForTermination ( parent . pid ! ) ;
4477
4578 const postKillChildren = await psTree ( parent . pid ! ) ;
4679
0 commit comments