@@ -3,6 +3,7 @@ import { on, once } from 'node:events';
33import { openSync } from 'node:fs' ;
44import { readFile , unlink , writeFile } from 'node:fs/promises' ;
55import * as path from 'node:path' ;
6+ import { inspect } from 'node:util' ;
67
78import { AssertionError , expect } from 'chai' ;
89import type * as timers from 'timers' ;
@@ -50,6 +51,7 @@ export async function testScriptFactory(
5051 uri : string ,
5152 resourceScriptPath : string ,
5253 func : ResourceTestFunction ,
54+ logFn : string ,
5355 iterations ?: number
5456) {
5557 let resourceScript = await readFile ( resourceScriptPath , { encoding : 'utf8' } ) ;
@@ -59,6 +61,7 @@ export async function testScriptFactory(
5961 resourceScript = resourceScript . replace ( 'SCRIPT_NAME_STRING' , JSON . stringify ( name ) ) ;
6062 resourceScript = resourceScript . replace ( 'URI_STRING' , JSON . stringify ( uri ) ) ;
6163 resourceScript = resourceScript . replace ( 'ITERATIONS_STRING' , `${ iterations } ` ) ;
64+ resourceScript = resourceScript . replace ( 'LOG_FN' , `${ logFn . toString ( ) } ` ) ;
6265
6366 return resourceScript ;
6467}
@@ -92,6 +95,9 @@ export async function runScriptAndReturnHeapInfo(
9295 func : HeapResourceTestFunction ,
9396 { iterations = 100 } = { }
9497) {
98+ const log = ( ...args ) => process . stdout . write ( `${ args } \n` ) ;
99+
100+ log ( 'starting' ) ;
95101 const scriptName = `${ name } .cjs` ;
96102 const heapsnapshotFile = `${ name } .heapsnapshot.json` ;
97103
@@ -100,36 +106,55 @@ export async function runScriptAndReturnHeapInfo(
100106 config . url ( ) ,
101107 HEAP_RESOURCE_SCRIPT_PATH ,
102108 func ,
109+ `(...args) => process.stdout.write('(subprocess): ' + args)` ,
103110 iterations
104111 ) ;
105112 await writeFile ( scriptName , scriptContent , { encoding : 'utf8' } ) ;
106113
107114 const processDiedController = new AbortController ( ) ;
108- const script = fork ( scriptName , { execArgv : [ '--expose-gc' ] } ) ;
115+ const script = fork ( scriptName , { execArgv : [ '--expose-gc' ] , stdio : 'inherit' } ) ;
109116 // Interrupt our awaiting of messages if the process crashed
110117 script . once ( 'close' , exitCode => {
111118 if ( exitCode !== 0 ) {
119+ log ( 'process exited with non-zero: ' , exitCode ) ;
112120 processDiedController . abort ( new Error ( `process exited with: ${ exitCode } ` ) ) ;
113121 }
114122 } ) ;
115123
124+ script . once ( 'error' , error => {
125+ log ( `processed errored: ${ error } ` ) ;
126+ processDiedController . abort ( new Error ( `process errored: ` , { cause : error } ) ) ;
127+ } ) ;
128+
129+ script . once ( 'spawn' , ( ) => log ( 'script spawned successfully.' ) ) ;
130+
116131 const messages = on ( script , 'message' , { signal : processDiedController . signal } ) ;
117132 const willClose = once ( script , 'close' ) ;
118133
134+ log ( 'fetching messages 1...' ) ;
119135 const starting = await messages . next ( ) ;
136+ log ( 'fetching messages 2: ' , inspect ( starting , { depth : Infinity } ) ) ;
137+
120138 const ending = await messages . next ( ) ;
121139
140+ log ( 'fetching messages 3: ' , inspect ( ending , { depth : Infinity } ) ) ;
141+
122142 const startingMemoryUsed = starting . value [ 0 ] . startingMemoryUsed ;
123143 const endingMemoryUsed = ending . value [ 0 ] . endingMemoryUsed ;
124144
125145 // make sure the process ended
126146 const [ exitCode ] = await willClose ;
147+
148+ log ( 'child process closed.' ) ;
149+
127150 expect ( exitCode , 'process should have exited with zero' ) . to . equal ( 0 ) ;
128151
129152 const heap = await readFile ( heapsnapshotFile , { encoding : 'utf8' } ) . then ( c =>
130153 parseSnapshot ( JSON . parse ( c ) )
131154 ) ;
132155
156+ log ( 'done.' ) ;
157+
133158 // If any of the above throws we won't reach these unlinks that clean up the created files.
134159 // This is intentional so that when debugging the file will still be present to check it for errors
135160 await unlink ( scriptName ) ;
0 commit comments