@@ -16,13 +16,13 @@ const { expect } = require('chai');
1616const { setTimeout } = require ( 'timers' ) ;
1717
1818let originalReport ;
19- const logFile = ' logs.txt';
19+ const logFile = scriptName + '. logs.txt';
2020
2121const 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
6384function 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
7898main ( )
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
84105setTimeout ( ( ) => {
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