Skip to content

Commit fb23a84

Browse files
clean up logs per suggestions
1 parent bbc2113 commit fb23a84

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

test/integration/node-specific/resource_clean_up.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('Driver Resources', () => {
3232
}
3333
});
3434

35-
context('on MongoClient.close()', () => {
35+
context.only('on MongoClient.close()', () => {
3636
before('create leak reproduction script', async function () {
3737
if (globalThis.AbortController == null || typeof this.configuration.serverApi === 'string') {
3838
return;

test/integration/node-specific/resource_tracking_script_builder.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export async function testScriptFactory(
5151
uri: string,
5252
resourceScriptPath: string,
5353
func: ResourceTestFunction,
54-
logFn: string,
5554
iterations?: number
5655
) {
5756
let resourceScript = await readFile(resourceScriptPath, { encoding: 'utf8' });
@@ -61,7 +60,6 @@ export async function testScriptFactory(
6160
resourceScript = resourceScript.replace('SCRIPT_NAME_STRING', JSON.stringify(name));
6261
resourceScript = resourceScript.replace('URI_STRING', JSON.stringify(uri));
6362
resourceScript = resourceScript.replace('ITERATIONS_STRING', `${iterations}`);
64-
resourceScript = resourceScript.replace('LOG_FN', `${logFn.toString()}`);
6563

6664
return resourceScript;
6765
}
@@ -95,8 +93,17 @@ export async function runScriptAndReturnHeapInfo(
9593
func: HeapResourceTestFunction,
9694
{ iterations = 100 } = {}
9795
) {
98-
const log = (...args) => process.stdout.write(`${args}\n`);
99-
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+
};
100107
log('starting');
101108
const scriptName = `${name}.cjs`;
102109
const heapsnapshotFile = `${name}.heapsnapshot.json`;
@@ -106,7 +113,6 @@ export async function runScriptAndReturnHeapInfo(
106113
config.url(),
107114
HEAP_RESOURCE_SCRIPT_PATH,
108115
func,
109-
`(...args) => process.stdout.write('(subprocess): ' + args)`,
110116
iterations
111117
);
112118
await writeFile(scriptName, scriptContent, { encoding: 'utf8' });
@@ -133,11 +139,11 @@ export async function runScriptAndReturnHeapInfo(
133139

134140
log('fetching messages 1...');
135141
const starting = await messages.next();
136-
log('fetching messages 2: ', inspect(starting, { depth: Infinity }));
142+
log('fetching messages 2: ', starting);
137143

138144
const ending = await messages.next();
139145

140-
log('fetching messages 3: ', inspect(ending, { depth: Infinity }));
146+
log('fetching messages 3: ', ending);
141147

142148
const startingMemoryUsed = starting.value[0].startingMemoryUsed;
143149
const endingMemoryUsed = ending.value[0].endingMemoryUsed;

test/tools/fixtures/heap_resource_script.in.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const func = FUNCTION_STRING;
77
const name = SCRIPT_NAME_STRING;
88
const uri = URI_STRING;
99
const iterations = ITERATIONS_STRING;
10-
const log = LOG_FN;
10+
const { inspect } = require('util');
1111

1212
const { MongoClient } = require(driverPath);
1313
const process = require('node:process');
@@ -22,20 +22,30 @@ const run = func;
2222

2323
const MB = (2 ** 10) ** 2;
2424

25+
const log = (...args) => {
26+
const payload =
27+
args
28+
.map(item =>
29+
typeof item === 'string' ? item : inspect(item, { depth: Infinity, breakLength: Infinity })
30+
)
31+
.join(', ') + '\n';
32+
process.stdout.write('(subprocess): ' + payload);
33+
};
34+
2535
async function main() {
26-
log('starting execution' + '\n');
36+
log('starting execution');
2737
const startingMemoryUsed = process.memoryUsage().heapUsed / MB;
2838
process.send({ startingMemoryUsed });
2939

30-
log('sent first message' + '\n');
40+
log('sent first message');
3141

3242
for (let iteration = 0; iteration < iterations; iteration++) {
3343
await run({ MongoClient, uri, iteration });
34-
iteration % 20 === 0 && log(`iteration ${iteration} complete\n`);
44+
iteration % 20 === 0 && log(`iteration ${iteration} complete`);
3545
global.gc();
3646
}
3747

38-
log('script executed' + '\n');
48+
log('script executed');
3949

4050
global.gc();
4151
// Sleep b/c maybe gc will run
@@ -44,16 +54,16 @@ async function main() {
4454

4555
const endingMemoryUsed = process.memoryUsage().heapUsed / MB;
4656

47-
log('sending second message' + '\n');
57+
log('sending second message');
4858

4959
process.send({ endingMemoryUsed });
50-
log('second message sent.' + '\n');
60+
log('second message sent.');
5161

5262
const start = now();
5363
v8.writeHeapSnapshot(`${name}.heapsnapshot.json`);
5464
const end = now();
5565

56-
log(`heap snapshot written in ${end - start}ms. script exiting` + '\n');
66+
log(`heap snapshot written in ${end - start}ms. script exiting`);
5767
}
5868

5969
main()

0 commit comments

Comments
 (0)