Skip to content

Commit 6e27003

Browse files
committed
chore: undefined error fix
1 parent 645784d commit 6e27003

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ describe('MongoClient.close() Integration', () => {
6565
authMechanism: 'MONGODB-OIDC'
6666
};
6767
const client = new MongoClient(uri, options);
68-
client.connect();
68+
const connectPromise = client.connect();
6969
expect(process.getActiveResourcesInfo()).to.include('FSReqPromise');
7070
await client.close();
7171
expect(process.getActiveResourcesInfo()).to.not.include('FSReqPromise');
72+
await connectPromise;
7273
}
7374
);
7475
});
@@ -80,6 +81,7 @@ describe('MongoClient.close() Integration', () => {
8081
describe('Node.js resource: Server Selection Timer', () => {
8182
describe('after a Topology is created through client.connect()', () => {
8283
const metadata: MongoDBMetadataUI = { requires: { topology: 'replicaset' } };
84+
8385
it.skip('server selection timers are cleaned up by client.close()', metadata, async () => {
8486
const run = async function ({ MongoClient, uri, expect, sleep, mongodb, getTimerCount }) {
8587
const serverSelectionTimeoutMS = 2222;
@@ -160,7 +162,7 @@ describe('MongoClient.close() Integration', () => {
160162
const heartbeatFrequencyMS = 2000;
161163
const client = new MongoClient('mongodb://fakeUri', { heartbeatFrequencyMS });
162164
const willBeHeartbeatFailed = once(client, 'serverHeartbeatFailed');
163-
client.connect();
165+
const connectPromise = client.connect();
164166
await willBeHeartbeatFailed;
165167
function getMonitorTimer(servers) {
166168
for (const [, server] of servers) {
@@ -174,6 +176,8 @@ describe('MongoClient.close() Integration', () => {
174176
expect(getMonitorTimer(servers)).to.not.exist;
175177

176178
expect(getTimerCount()).to.equal(0);
179+
180+
await connectPromise;
177181
};
178182
await runScriptAndGetProcessInfo('timer-heartbeat-failed-monitor', config, run);
179183
}

test/integration/node-specific/resource_tracking_script_builder.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,27 +174,32 @@ export async function runScriptAndGetProcessInfo(
174174
REPORT_RESOURCE_SCRIPT_PATH,
175175
func
176176
);
177-
await writeFile(scriptName, scriptContent, { encoding: 'utf8' });
178-
const logFile = name + '.logs.txt';
179177

178+
const logFile = name + '.logs.txt';
180179
const stdErrFile = 'err.out';
180+
181+
await unlink(scriptName).catch(() => null);
182+
await unlink(logFile).catch(() => null);
183+
await unlink(stdErrFile).catch(() => null);
184+
185+
await writeFile(scriptName, scriptContent, { encoding: 'utf8' });
186+
181187
const script = spawn(process.execPath, [scriptName], {
182188
stdio: ['ignore', 'ignore', openSync(stdErrFile, 'w')]
183189
});
184190

185191
const willClose = once(script, 'close');
186192

187193
// make sure the process ended
188-
const [exitCode] = await willClose;
194+
const [exitCode] = (await willClose) as [number];
189195

190196
// format messages from child process as an object
191197
const messages = (await readFile(logFile, 'utf-8'))
192198
.trim()
193199
.split('\n')
194-
.map(line => JSON.parse(line))
195-
.reduce((acc, curr) => ({ ...acc, ...curr }), {});
200+
.map(line => JSON.parse(line));
196201

197-
const stdErrSize = await readFile(stdErrFile, { encoding: 'utf8' });
202+
const stdErr = await readFile(stdErrFile, { encoding: 'utf8' });
198203

199204
// delete temporary files
200205
await unlink(scriptName);
@@ -203,18 +208,23 @@ export async function runScriptAndGetProcessInfo(
203208

204209
// assertions about exit status
205210
if (exitCode) {
211+
const { error } = messages.find(m => m.error != null);
212+
expect(error).to.exist;
206213
const assertionError = new AssertionError(
207-
messages.error?.message + '\n\t' + JSON.stringify(messages.error?.resources, undefined, 2)
214+
error.message + '\n\t' + JSON.stringify(error.resources, undefined, 2)
208215
);
209-
assertionError.stack = messages.error?.stack + new Error().stack.slice('Error'.length);
216+
assertionError.stack = error.stack + new Error().stack.slice('Error'.length);
210217
throw assertionError;
211218
}
212219

213220
// assertions about resource status
214-
expect(messages.beforeExitHappened).to.be.true;
215-
expect(messages.newResources.libuvResources).to.be.empty;
216-
expect(messages.newResources.activeResources).to.be.empty;
221+
const { beforeExitHappened } = messages.find(m => 'beforeExitHappened' in m);
222+
const { newResources } = messages.find(m => 'newResources' in m);
223+
224+
expect(beforeExitHappened).to.be.true;
225+
expect(newResources.libuvResources).to.be.empty;
226+
expect(newResources.activeResources).to.be.empty;
217227

218228
// assertion about error output
219-
expect(stdErrSize).to.be.empty;
229+
expect(stdErr).to.be.empty;
220230
}

test/tools/fixtures/process_resource_script.in.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
/* eslint-disable no-undef */
4-
/* eslint-disable no-unused-vars */
4+
55
const driverPath = DRIVER_SOURCE_PATH;
66
const func = FUNCTION_STRING;
77
const scriptName = SCRIPT_NAME_STRING;
@@ -48,7 +48,6 @@ function getNewLibuvResourceArray() {
4848
* @param {LibuvResource} resource
4949
*/
5050
function isNewLibuvResource(resource) {
51-
const serverType = ['tcp', 'udp'];
5251
return (
5352
!originalReportAddresses.includes(resource.address) && resource.is_referenced // if a resource is unreferenced, it's not keeping the event loop open
5453
);
@@ -128,10 +127,10 @@ async function main() {
128127
}
129128

130129
main()
131-
.then(() => {})
130+
.then(() => null)
132131
.catch(e => {
133132
log({ error: { message: e.message, stack: e.stack, resources: getNewResources() } });
134-
process.exit(1);
133+
process.exit(2);
135134
});
136135

137136
setTimeout(() => {

0 commit comments

Comments
 (0)