Skip to content

Commit 454859e

Browse files
requested changes: fix exitCode
1 parent 83b5685 commit 454859e

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe.skip('MongoClient.close() Integration', () => {
2626
expect(process.getActiveResourcesInfo()).to.not.include('FSReqPromise');
2727
const err = await connectPromise.catch(e => e);
2828
expect(err).to.exist;
29-
}
29+
}
3030
);
3131
});
3232
});

test/integration/node-specific/resource_tracking_script_builder.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export async function runScriptAndGetProcessInfo(
166166
func
167167
);
168168
await writeFile(scriptName, scriptContent, { encoding: 'utf8' });
169-
const logFile = 'logs.txt';
169+
const logFile = name + '.logs.txt';
170170

171171
const script = spawn(process.argv[0], [scriptName], { stdio: ['ignore', 'ignore', 'ignore'] });
172172

@@ -187,10 +187,12 @@ export async function runScriptAndGetProcessInfo(
187187
await unlink(logFile);
188188

189189
// assertions about exit status
190-
if (exitCode) {
191-
const assertionError = new AssertionError(messages.error.message);
192-
assertionError.stack = messages.error.stack + new Error().stack.slice('Error'.length);
193-
throw assertionError;
190+
if (exitCode) {
191+
const assertionError = new AssertionError(
192+
messages.error.message + '\n\t' + JSON.stringify(messages.error.resources)
193+
);
194+
assertionError.stack = messages.error.stack + new Error().stack.slice('Error'.length);
195+
throw assertionError;
194196
}
195197

196198
// assertions about resource status

test/tools/fixtures/process_resource_script.in.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ const { expect } = require('chai');
1616
const { setTimeout } = require('timers');
1717

1818
let originalReport;
19-
const logFile = 'logs.txt';
19+
const logFile = scriptName + '.logs.txt';
2020

2121
const run = func;
2222

2323
/**
2424
*
25-
* Returns an array containing the new resources created after script started.
25+
* Returns an array containing the new libuv resources created after script started.
2626
* A new resource is something that will keep the event loop running.
2727
*
2828
* In order to be counted as a new resource, a resource MUST:
@@ -59,6 +59,27 @@ function getNewLibuvResourceArray() {
5959
return currReport;
6060
}
6161

62+
/**
63+
* Returns an object of the new resources created after script started.
64+
*
65+
*
66+
* In order to be counted as a new resource, a resource MUST either:
67+
* - Meet the criteria to be returned by our helper utility `getNewLibuvResourceArray()`
68+
* OR
69+
* - Be returned by `process.getActiveResourcesInfo()
70+
*
71+
* The reason we are using both methods to detect active resources is:
72+
* - `process.report.getReport().libuv` does not detect active requests (such as timers or file reads) accurately
73+
* - `process.getActiveResourcesInfo()` does not contain enough server information we need for our assertions
74+
*
75+
*/
76+
function getNewResources() {
77+
return {
78+
libuvResources: getNewLibuvResourceArray(),
79+
activeResources: process.getActiveResourcesInfo()
80+
};
81+
}
82+
6283
// A log function for debugging
6384
function log(message) {
6485
// remove outer parentheses for easier parsing
@@ -72,20 +93,24 @@ async function main() {
7293
log({ beforeExitHappened: true });
7394
});
7495
await run({ MongoClient, uri, log, expect, ClientEncryption, BSON });
75-
log({ newLibuvResources: getNewLibuvResourceArray() });
7696
}
7797

7898
main()
7999
.then(() => {})
80100
.catch(e => {
81-
log({ exitCode: 1, error: { message: e.message, stack: e.stack } });
101+
log({ error: { message: e.message, stack: e.stack, resources: getNewResources() } });
102+
process.exit(1);
82103
});
83104

84105
setTimeout(() => {
85106
// this means something was in the event loop such that it hung for more than 10 seconds
86107
// so we kill the process
87-
log({ newLibuvResources: getNewLibuvResourceArray() });
88-
log({ exitCode: 99, error: { message: 'Process timed out: resources remain in the event loop.' } });
108+
log({
109+
error: {
110+
message: 'Process timed out: resources remain in the event loop',
111+
resources: getNewResources()
112+
}
113+
});
89114
process.exit(99);
90115
// using `unref` will ensure this setTimeout call is not a resource / does not keep the event loop running
91116
}, 10000).unref();

0 commit comments

Comments
 (0)