@@ -3,6 +3,7 @@ import { on, once } from 'node:events';
3
3
import { openSync } from 'node:fs' ;
4
4
import { readFile , unlink , writeFile } from 'node:fs/promises' ;
5
5
import * as path from 'node:path' ;
6
+ import { inspect } from 'node:util' ;
6
7
7
8
import { AssertionError , expect } from 'chai' ;
8
9
import type * as timers from 'timers' ;
@@ -92,6 +93,18 @@ export async function runScriptAndReturnHeapInfo(
92
93
func : HeapResourceTestFunction ,
93
94
{ iterations = 100 } = { }
94
95
) {
96
+ const log = ( ...args ) => {
97
+ const payload =
98
+ args
99
+ . map ( item =>
100
+ typeof item === 'string'
101
+ ? item
102
+ : inspect ( item , { depth : Infinity , breakLength : Infinity } )
103
+ )
104
+ . join ( ', ' ) + '\n' ;
105
+ process . stdout . write ( payload ) ;
106
+ } ;
107
+ log ( 'starting' ) ;
95
108
const scriptName = `${ name } .cjs` ;
96
109
const heapsnapshotFile = `${ name } .heapsnapshot.json` ;
97
110
@@ -105,31 +118,49 @@ export async function runScriptAndReturnHeapInfo(
105
118
await writeFile ( scriptName , scriptContent , { encoding : 'utf8' } ) ;
106
119
107
120
const processDiedController = new AbortController ( ) ;
108
- const script = fork ( scriptName , { execArgv : [ '--expose-gc' ] } ) ;
121
+ const script = fork ( scriptName , { execArgv : [ '--expose-gc' ] , stdio : 'inherit' } ) ;
109
122
// Interrupt our awaiting of messages if the process crashed
110
123
script . once ( 'close' , exitCode => {
111
124
if ( exitCode !== 0 ) {
125
+ log ( 'process exited with non-zero: ' , exitCode ) ;
112
126
processDiedController . abort ( new Error ( `process exited with: ${ exitCode } ` ) ) ;
113
127
}
114
128
} ) ;
115
129
130
+ script . once ( 'error' , error => {
131
+ log ( `processed errored: ${ error } ` ) ;
132
+ processDiedController . abort ( new Error ( `process errored: ` , { cause : error } ) ) ;
133
+ } ) ;
134
+
135
+ script . once ( 'spawn' , ( ) => log ( 'script spawned successfully.' ) ) ;
136
+
116
137
const messages = on ( script , 'message' , { signal : processDiedController . signal } ) ;
117
138
const willClose = once ( script , 'close' ) ;
118
139
140
+ log ( 'fetching messages 1...' ) ;
119
141
const starting = await messages . next ( ) ;
142
+ log ( 'fetching messages 2: ' , starting ) ;
143
+
120
144
const ending = await messages . next ( ) ;
121
145
146
+ log ( 'fetching messages 3: ' , ending ) ;
147
+
122
148
const startingMemoryUsed = starting . value [ 0 ] . startingMemoryUsed ;
123
149
const endingMemoryUsed = ending . value [ 0 ] . endingMemoryUsed ;
124
150
125
151
// make sure the process ended
126
152
const [ exitCode ] = await willClose ;
153
+
154
+ log ( 'child process closed.' ) ;
155
+
127
156
expect ( exitCode , 'process should have exited with zero' ) . to . equal ( 0 ) ;
128
157
129
158
const heap = await readFile ( heapsnapshotFile , { encoding : 'utf8' } ) . then ( c =>
130
159
parseSnapshot ( JSON . parse ( c ) )
131
160
) ;
132
161
162
+ log ( 'done.' ) ;
163
+
133
164
// If any of the above throws we won't reach these unlinks that clean up the created files.
134
165
// This is intentional so that when debugging the file will still be present to check it for errors
135
166
await unlink ( scriptName ) ;
0 commit comments