@@ -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' ;
@@ -50,6 +51,7 @@ export async function testScriptFactory(
50
51
uri : string ,
51
52
resourceScriptPath : string ,
52
53
func : ResourceTestFunction ,
54
+ logFn : string ,
53
55
iterations ?: number
54
56
) {
55
57
let resourceScript = await readFile ( resourceScriptPath , { encoding : 'utf8' } ) ;
@@ -59,6 +61,7 @@ export async function testScriptFactory(
59
61
resourceScript = resourceScript . replace ( 'SCRIPT_NAME_STRING' , JSON . stringify ( name ) ) ;
60
62
resourceScript = resourceScript . replace ( 'URI_STRING' , JSON . stringify ( uri ) ) ;
61
63
resourceScript = resourceScript . replace ( 'ITERATIONS_STRING' , `${ iterations } ` ) ;
64
+ resourceScript = resourceScript . replace ( 'LOG_FN' , `${ logFn . toString ( ) } ` ) ;
62
65
63
66
return resourceScript ;
64
67
}
@@ -92,6 +95,9 @@ export async function runScriptAndReturnHeapInfo(
92
95
func : HeapResourceTestFunction ,
93
96
{ iterations = 100 } = { }
94
97
) {
98
+ const log = ( ...args ) => process . stdout . write ( `${ args } \n` ) ;
99
+
100
+ log ( 'starting' ) ;
95
101
const scriptName = `${ name } .cjs` ;
96
102
const heapsnapshotFile = `${ name } .heapsnapshot.json` ;
97
103
@@ -100,36 +106,55 @@ export async function runScriptAndReturnHeapInfo(
100
106
config . url ( ) ,
101
107
HEAP_RESOURCE_SCRIPT_PATH ,
102
108
func ,
109
+ `(...args) => process.stdout.write('(subprocess): ' + args)` ,
103
110
iterations
104
111
) ;
105
112
await writeFile ( scriptName , scriptContent , { encoding : 'utf8' } ) ;
106
113
107
114
const processDiedController = new AbortController ( ) ;
108
- const script = fork ( scriptName , { execArgv : [ '--expose-gc' ] } ) ;
115
+ const script = fork ( scriptName , { execArgv : [ '--expose-gc' ] , stdio : 'inherit' } ) ;
109
116
// Interrupt our awaiting of messages if the process crashed
110
117
script . once ( 'close' , exitCode => {
111
118
if ( exitCode !== 0 ) {
119
+ log ( 'process exited with non-zero: ' , exitCode ) ;
112
120
processDiedController . abort ( new Error ( `process exited with: ${ exitCode } ` ) ) ;
113
121
}
114
122
} ) ;
115
123
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
+
116
131
const messages = on ( script , 'message' , { signal : processDiedController . signal } ) ;
117
132
const willClose = once ( script , 'close' ) ;
118
133
134
+ log ( 'fetching messages 1...' ) ;
119
135
const starting = await messages . next ( ) ;
136
+ log ( 'fetching messages 2: ' , inspect ( starting , { depth : Infinity } ) ) ;
137
+
120
138
const ending = await messages . next ( ) ;
121
139
140
+ log ( 'fetching messages 3: ' , inspect ( ending , { depth : Infinity } ) ) ;
141
+
122
142
const startingMemoryUsed = starting . value [ 0 ] . startingMemoryUsed ;
123
143
const endingMemoryUsed = ending . value [ 0 ] . endingMemoryUsed ;
124
144
125
145
// make sure the process ended
126
146
const [ exitCode ] = await willClose ;
147
+
148
+ log ( 'child process closed.' ) ;
149
+
127
150
expect ( exitCode , 'process should have exited with zero' ) . to . equal ( 0 ) ;
128
151
129
152
const heap = await readFile ( heapsnapshotFile , { encoding : 'utf8' } ) . then ( c =>
130
153
parseSnapshot ( JSON . parse ( c ) )
131
154
) ;
132
155
156
+ log ( 'done.' ) ;
157
+
133
158
// If any of the above throws we won't reach these unlinks that clean up the created files.
134
159
// This is intentional so that when debugging the file will still be present to check it for errors
135
160
await unlink ( scriptName ) ;
0 commit comments