Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(NODE-6631): Implement integration tests for improved client.close() - sockets + timers #4361

Draft
wants to merge 50 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
0af55e9
skeleton
aditi-khare-mongoDB Dec 16, 2024
4798bc2
skeleton updates
aditi-khare-mongoDB Dec 16, 2024
8adca00
refactor for table
aditi-khare-mongoDB Dec 17, 2024
3412bb1
boilerplate execution
aditi-khare-mongoDB Dec 17, 2024
1531bec
fix
aditi-khare-mongoDB Dec 17, 2024
4200f3f
preliminary tests finished
aditi-khare-mongoDB Dec 18, 2024
321ef71
remove misc file
aditi-khare-mongoDB Dec 18, 2024
1503084
temp for screen-share
aditi-khare-mongoDB Dec 19, 2024
a128974
lint
aditi-khare-mongoDB Dec 19, 2024
4e55dee
boilerplate fully working
aditi-khare-mongoDB Dec 19, 2024
f560088
TLS test case running
aditi-khare-mongoDB Dec 20, 2024
ef9cc90
clea up lint
aditi-khare-mongoDB Dec 20, 2024
9ed1528
clean up
aditi-khare-mongoDB Dec 20, 2024
1c8e20f
most of tree reformatting done
aditi-khare-mongoDB Dec 20, 2024
1de9809
reorganized tests and added in most of neal's suggestions
aditi-khare-mongoDB Dec 20, 2024
e77405f
TLS test cases and socket test cases
aditi-khare-mongoDB Dec 30, 2024
11aa73c
TLS test cases
aditi-khare-mongoDB Dec 30, 2024
c99579a
fix message formatting
aditi-khare-mongoDB Dec 30, 2024
1aeb046
Merge branch 'main' into NODE-6615/integration-client-close
aditi-khare-mongoDB Dec 30, 2024
47c20db
fix message formatting + test cases naming
aditi-khare-mongoDB Dec 30, 2024
133b20d
Delete logs.txt
aditi-khare-mongoDB Dec 30, 2024
8fd53a4
requested changes: remove log calls, change chai to expect, clarify s…
aditi-khare-mongoDB Dec 30, 2024
e60a42b
requested changes: additional expectation
aditi-khare-mongoDB Dec 30, 2024
13a7f27
temp
aditi-khare-mongoDB Dec 30, 2024
2f97c7b
Merge branch 'NODE-6615/integration-client-close' into NODE-6620/sockets
aditi-khare-mongoDB Dec 30, 2024
bbdd8fd
rttPinger done
aditi-khare-mongoDB Dec 31, 2024
958835d
initial commit
aditi-khare-mongoDB Jan 2, 2025
d9ed22a
removed resources that we are no longer integration testing - connect…
aditi-khare-mongoDB Jan 2, 2025
d1361c7
Merge branch 'NODE-6615/integration-client-close' into NODE-6620/sockets
aditi-khare-mongoDB Jan 2, 2025
425cf3f
temp
aditi-khare-mongoDB Jan 2, 2025
83b5685
requested changes: add in exitCode message, skip unimplemented tests,…
aditi-khare-mongoDB Jan 2, 2025
a82a7fd
temp
aditi-khare-mongoDB Jan 2, 2025
454859e
requested changes: fix exitCode
aditi-khare-mongoDB Jan 2, 2025
24fe48f
Merge branch 'NODE-6615/integration-client-close' into NODE-6620/sockets
aditi-khare-mongoDB Jan 2, 2025
56662b8
remove extra file
aditi-khare-mongoDB Jan 2, 2025
9ec5b00
temp
aditi-khare-mongoDB Jan 3, 2025
5eb92ee
neal's requested changes
aditi-khare-mongoDB Jan 3, 2025
78f787f
remove extra files
aditi-khare-mongoDB Jan 5, 2025
125305a
Merge branch 'NODE-6615/integration-client-close' into NODE-6620/sockets
aditi-khare-mongoDB Jan 5, 2025
77fadbb
re-add assertion
aditi-khare-mongoDB Jan 5, 2025
aefa1cc
Update test/integration/node-specific/resource_tracking_script_builde…
aditi-khare-mongoDB Jan 6, 2025
2a43e4d
temp
aditi-khare-mongoDB Jan 6, 2025
e9a3108
make stderr inherit
aditi-khare-mongoDB Jan 6, 2025
e96a94c
sockets working
aditi-khare-mongoDB Jan 6, 2025
d828529
timers
aditi-khare-mongoDB Jan 7, 2025
1f9f0ba
Merge branch 'NODE-6615/integration-client-close' into NODE-6620/sockets
aditi-khare-mongoDB Jan 7, 2025
24ebcde
eod commit
aditi-khare-mongoDB Jan 8, 2025
f003454
rebase
aditi-khare-mongoDB Jan 9, 2025
4901502
Merge branch 'main' into NODE-6620/sockets
aditi-khare-mongoDB Jan 9, 2025
1f321ec
push for virtual office
aditi-khare-mongoDB Jan 9, 2025
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
141 changes: 141 additions & 0 deletions heartbeat-failed-monitor-timer.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
'use strict';

/* eslint-disable no-undef */
/* eslint-disable no-unused-vars */
const driverPath = "/Users/aditi.khare/Desktop/node-mongodb-native/lib";
const func = (async function ({ MongoClient, uri, expect, sleep, getTimerCount }) {
const heartbeatFrequencyMS = 2000;
const client = new MongoClient('mongodb://fakeUri', { heartbeatFrequencyMS });
let heartbeatHappened = false;
client.on('serverHeartbeatFailed', () => heartbeatHappened = true);
client.connect();
await sleep(heartbeatFrequencyMS * 2.5);
expect(heartbeatHappened).to.be.true;
function getMonitorTimer(servers) {
for (const server of servers) {
return server[1]?.monitor.monitorId.timerId;
}
}
;
const servers = client.topology.s.servers;
expect(getMonitorTimer(servers)).to.exist;
await client.close();
expect(getMonitorTimer(servers)).to.not.exist;
expect(getTimerCount()).to.equal(0);
});
const scriptName = "heartbeat-failed-monitor-timer";
const uri = "mongodb://localhost:27017/integration_tests?authSource=admin";

const mongodb = require(driverPath);
const { MongoClient } = mongodb;
const process = require('node:process');
const util = require('node:util');
const timers = require('node:timers');
const fs = require('node:fs');
const sinon = require('sinon');
const { expect } = require('chai');
const { setTimeout } = require('timers');

let originalReport;
const logFile = scriptName + '.logs.txt';
const sleep = util.promisify(setTimeout);

const run = func;

/**
*
* Returns an array containing the new libuv resources created after script started.
* A new resource is something that will keep the event loop running.
*
* In order to be counted as a new resource, a resource MUST:
* - Must NOT share an address with a libuv resource that existed at the start of script
* - Must be referenced. See [here](https://nodejs.org/api/timers.html#timeoutref) for more context.
*
* We're using the following tool to track resources: `process.report.getReport().libuv`
* For more context, see documentation for [process.report.getReport()](https://nodejs.org/api/report.html), and [libuv](https://docs.libuv.org/en/v1.x/handle.html).
*
*/
function getNewLibuvResourceArray() {
let currReport = process.report.getReport().libuv;
const originalReportAddresses = originalReport.map(resource => resource.address);

/**
* @typedef {Object} LibuvResource
* @property {string} type What is the resource type? For example, 'tcp' | 'timer' | 'udp' | 'tty'... (See more in [docs](https://docs.libuv.org/en/v1.x/handle.html)).
* @property {boolean} is_referenced Is the resource keeping the JS event loop active?
*
* @param {LibuvResource} resource
*/
function isNewLibuvResource(resource) {
const serverType = ['tcp', 'udp'];
return (
!originalReportAddresses.includes(resource.address) && resource.is_referenced // if a resource is unreferenced, it's not keeping the event loop open
);
}

currReport = currReport.filter(resource => isNewLibuvResource(resource));
return currReport;
}

/**
* Returns an object of the new resources created after script started.
*
*
* In order to be counted as a new resource, a resource MUST either:
* - Meet the criteria to be returned by our helper utility `getNewLibuvResourceArray()`
* OR
* - Be returned by `process.getActiveResourcesInfo() and is not 'TTYWrap'
*
* The reason we are using both methods to detect active resources is:
* - `process.report.getReport().libuv` does not detect active requests (such as timers or file reads) accurately
* - `process.getActiveResourcesInfo()` does not contain enough server information we need for our assertions
*
*/
function getNewResources() {

return {
libuvResources: getNewLibuvResourceArray(),
activeResources: process.getActiveResourcesInfo()
};
}

/**
* @returns Number of active timers in event loop
*/
const getTimerCount = () => process.getActiveResourcesInfo().filter(r => r === 'Timeout').length;

// A log function for debugging
function log(message) {
// remove outer parentheses for easier parsing
const messageToLog = JSON.stringify(message) + ' \n';
fs.writeFileSync(logFile, messageToLog, { flag: 'a' });
}

async function main() {
originalReport = process.report.getReport().libuv;
process.on('beforeExit', () => {
log({ beforeExitHappened: true });
});
await run({ MongoClient, uri, log, expect, mongodb, sleep, sinon, getTimerCount });
log({ newResources: getNewResources() });
}

main()
.then(() => {})
.catch(e => {
log({ error: { message: e.message, stack: e.stack, resources: getNewResources() } });
process.exit(1);
});

setTimeout(() => {
// this means something was in the event loop such that it hung for more than 10 seconds
// so we kill the process
log({
error: {
message: 'Process timed out: resources remain in the event loop',
resources: getNewResources()
}
});
process.exit(99);
// using `unref` will ensure this setTimeout call is not a resource / does not keep the event loop running
}, 10000).unref();
2 changes: 2 additions & 0 deletions heartbeat-failed-monitor-timer.logs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"error":{"message":"expected 1 to equal +0","stack":"AssertionError: expected 1 to equal +0\n at func (/Users/aditi.khare/Desktop/node-mongodb-native/heartbeat-failed-monitor-timer.cjs:24:64)\n at async main (/Users/aditi.khare/Desktop/node-mongodb-native/heartbeat-failed-monitor-timer.cjs:119:3)","resources":{"libuvResources":[],"activeResources":[]}}}
{"error":{"message":"expected 1 to equal +0","stack":"AssertionError: expected 1 to equal +0\n at func (/Users/aditi.khare/Desktop/node-mongodb-native/heartbeat-failed-monitor-timer.cjs:24:64)\n at async main (/Users/aditi.khare/Desktop/node-mongodb-native/heartbeat-failed-monitor-timer.cjs:119:3)","resources":{"libuvResources":[],"activeResources":["TTYWrap"]}}}
Loading
Loading