Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions test/integration/node-specific/resource_clean_up.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const MB_PERMITTED_OFFSET = 5;
describe('Driver Resources', () => {
let startingMemoryUsed;
let endingMemoryUsed;
let heap;
let clientsInMemory;

beforeEach(function () {
if (globalThis.AbortController == null) {
Expand All @@ -34,12 +34,6 @@ describe('Driver Resources', () => {

context('on MongoClient.close()', () => {
before('create leak reproduction script', async function () {
if (process.version.includes('v24')) {
if (this.test) {
this.test.skipReason = 'TODO(NODE-6945): Fix v24 heap snapshot parsing';
}
this.test?.skip();
}
if (globalThis.AbortController == null || typeof this.configuration.serverApi === 'string') {
return;
}
Expand All @@ -58,7 +52,7 @@ describe('Driver Resources', () => {

startingMemoryUsed = res.startingMemoryUsed;
endingMemoryUsed = res.endingMemoryUsed;
heap = res.heap;
clientsInMemory = res.clientsInMemory;
});

describe('ending memory usage', () => {
Expand All @@ -77,11 +71,10 @@ describe('Driver Resources', () => {

describe('ending heap snapshot', () => {
it('has 0 MongoClients in memory', async () => {
const clients = heap.nodes.filter(n => n.name === 'MongoClient' && n.type === 'object');
// lengthOf crashes chai b/c it tries to print out a gigantic diff
expect(
clients.length,
`expected no MongoClients in the heapsnapshot, found ${clients.length}`
clientsInMemory,
`expected no MongoClients in the heapsnapshot, found ${clientsInMemory}`
).to.equal(0);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { inspect } from 'node:util';

import { AssertionError, expect } from 'chai';
import type * as timers from 'timers';
import { parseSnapshot } from 'v8-heapsnapshot';

import type * as mongodb from '../../mongodb';
import { type MongoClient } from '../../mongodb';
Expand Down Expand Up @@ -106,7 +105,6 @@ export async function runScriptAndReturnHeapInfo(
};
log('starting');
const scriptName = `${name}.cjs`;
const heapsnapshotFile = `${name}.heapsnapshot.json`;

const scriptContent = await testScriptFactory(
name,
Expand Down Expand Up @@ -145,8 +143,16 @@ export async function runScriptAndReturnHeapInfo(

log('fetching messages 3: ', ending);

const startingMemoryUsed = starting.value[0].startingMemoryUsed;
const endingMemoryUsed = ending.value[0].endingMemoryUsed;
const {
value: [{ startingMemoryUsed }]
} = starting;
const {
value: [{ endingMemoryUsed }]
} = ending;

const {
value: [{ clientsInMemory }]
} = await messages.next();

// make sure the process ended
const [exitCode] = await willClose;
Expand All @@ -155,21 +161,16 @@ export async function runScriptAndReturnHeapInfo(

expect(exitCode, 'process should have exited with zero').to.equal(0);

const heap = await readFile(heapsnapshotFile, { encoding: 'utf8' }).then(c =>
parseSnapshot(JSON.parse(c))
);

log('done.');

// If any of the above throws we won't reach these unlinks that clean up the created files.
// This is intentional so that when debugging the file will still be present to check it for errors
await unlink(scriptName);
await unlink(heapsnapshotFile);

return {
startingMemoryUsed,
endingMemoryUsed,
heap
clientsInMemory
};
}

Expand Down
10 changes: 4 additions & 6 deletions test/tools/fixtures/heap_resource_script.in.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

const driverPath = DRIVER_SOURCE_PATH;
const func = FUNCTION_STRING;
const name = SCRIPT_NAME_STRING;
const uri = URI_STRING;
const iterations = ITERATIONS_STRING;
const { inspect } = require('util');
Expand All @@ -15,7 +14,6 @@ const v8 = require('node:v8');
const util = require('node:util');
const timers = require('node:timers');

const now = performance.now.bind(performance);
const sleep = util.promisify(timers.setTimeout);

const run = func;
Expand Down Expand Up @@ -59,11 +57,11 @@ async function main() {
process.send({ endingMemoryUsed });
log('second message sent.');

const start = now();
v8.writeHeapSnapshot(`${name}.heapsnapshot.json`);
const end = now();
const clientsInMemory = v8.queryObjects(MongoClient);

log(`heap snapshot written in ${end - start}ms. script exiting`);
process.send({ clientsInMemory });

log('clients instances in memory sent.');
}

main()
Expand Down