11/* eslint-disable @typescript-eslint/no-empty-function */
2- const { expect } = require ( 'chai' ) ;
2+ import { expect } from 'chai' ;
33import * as sinon from 'sinon' ;
4- const mongodb = require ( '../../mongodb' ) ;
5- const { MongoClient } = mongodb ;
4+ import { MongoClient } from '../../mongodb' ;
65import { type TestConfiguration } from '../../tools/runner/config' ;
76import { runScriptAndGetProcessInfo } from './resource_tracking_script_builder' ;
87import { sleep } from '../../tools/utils' ;
8+ import { ConnectionPool , Timeout } from '../../mongodb' ;
99
1010describe . only ( 'MongoClient.close() Integration' , ( ) => {
1111 // note: these tests are set-up in accordance of the resource ownership tree
@@ -80,7 +80,32 @@ describe.only('MongoClient.close() Integration', () => {
8080 describe ( 'Topology' , ( ) => {
8181 describe ( 'Node.js resource: Server Selection Timer' , ( ) => {
8282 describe ( 'after a Topology is created through client.connect()' , ( ) => {
83- it . skip ( 'server selection timers are cleaned up by client.close()' , async ( ) => { } ) ;
83+ it . only ( 'server selection timers are cleaned up by client.close()' , async ( ) => {
84+ // note: this test is not called in a separate process since it requires stubbing internal class: Timeout
85+ const run = async function ( { MongoClient, uri, expect, sinon, sleep, getTimerCount } ) {
86+ const serverSelectionTimeoutMS = 777 ;
87+ const client = new MongoClient ( uri , { minPoolSize : 1 , serverSelectionTimeoutMS } ) ;
88+ const timeoutStartedSpy = sinon . spy ( Timeout , 'expires' ) ;
89+ let serverSelectionTimeoutStarted = false ;
90+
91+ // make server selection hang so check out timer isn't cleared and check that the timeout has started
92+ sinon . stub ( Promise , 'race' ) . callsFake ( ( ) => {
93+ serverSelectionTimeoutStarted = timeoutStartedSpy . getCalls ( ) . filter ( r => r . args . includes ( 777 ) ) . flat ( ) . length > 0 ;
94+ } ) ;
95+
96+ client . db ( 'db' ) . collection ( 'collection' ) . insertOne ( { x : 1 } ) . catch ( e => e ) ;
97+
98+ // don't allow entire checkout timer to elapse to ensure close is called mid-timeout
99+ await sleep ( serverSelectionTimeoutMS / 2 ) ;
100+ expect ( serverSelectionTimeoutStarted ) . to . be . true ;
101+
102+ await client . close ( ) ;
103+ expect ( getTimerCount ( ) ) . to . equal ( 0 ) ;
104+ } ;
105+
106+ const getTimerCount = ( ) => process . getActiveResourcesInfo ( ) . filter ( r => r === 'Timeout' ) . length ;
107+ await run ( { MongoClient, uri : config . uri , sleep, sinon, expect, getTimerCount} ) ;
108+ } ) ;
84109 } ) ;
85110 } ) ;
86111
@@ -294,14 +319,14 @@ describe.only('MongoClient.close() Integration', () => {
294319 describe ( 'after new connection pool is created' , ( ) => {
295320 it ( 'the wait queue timer is cleaned up by client.close()' , async function ( ) {
296321 // note: this test is not called in a separate process since it requires stubbing internal function
297- const run = async function ( { MongoClient, uri, expect, sinon, sleep, mongodb , getTimerCount } ) {
322+ const run = async function ( { MongoClient, uri, expect, sinon, sleep, getTimerCount } ) {
298323 const waitQueueTimeoutMS = 999 ;
299324 const client = new MongoClient ( uri , { minPoolSize : 1 , waitQueueTimeoutMS } ) ;
300- const timeoutStartedSpy = sinon . spy ( mongodb . Timeout , 'expires' ) ;
325+ const timeoutStartedSpy = sinon . spy ( Timeout , 'expires' ) ;
301326 let checkoutTimeoutStarted = false ;
302327
303328 // make waitQueue hang so check out timer isn't cleared and check that the timeout has started
304- sinon . stub ( mongodb . ConnectionPool . prototype , 'processWaitQueue' ) . callsFake ( async ( ) => {
329+ sinon . stub ( ConnectionPool . prototype , 'processWaitQueue' ) . callsFake ( async ( ) => {
305330 checkoutTimeoutStarted = timeoutStartedSpy . getCalls ( ) . map ( r => r . args ) . filter ( r => r . includes ( 999 ) ) ? true : false ;
306331 } ) ;
307332
@@ -316,7 +341,7 @@ describe.only('MongoClient.close() Integration', () => {
316341 } ;
317342
318343 const getTimerCount = ( ) => process . getActiveResourcesInfo ( ) . filter ( r => r === 'Timeout' ) . length ;
319- await run ( { MongoClient, uri : config . uri , sleep, sinon, expect, mongodb , getTimerCount} ) ;
344+ await run ( { MongoClient, uri : config . uri , sleep, sinon, expect, getTimerCount} ) ;
320345 } ) ;
321346 } ) ;
322347 } ) ;
@@ -364,24 +389,39 @@ describe.only('MongoClient.close() Integration', () => {
364389 }
365390 } ;
366391 describe ( 'after SRVPoller is created' , ( ) => {
367- it . only ( 'timers are cleaned up by client.close()' , metadata , async ( ) => {
368- const run = async function ( { MongoClient, uri, expect, sinon, getTimerCount } ) {
392+ it . skip ( 'timers are cleaned up by client.close()' , metadata , async ( ) => {
393+ const run = async function ( { MongoClient, uri, expect, log , sinon, mongodb , getTimerCount } ) {
369394 const dns = require ( 'dns' ) ;
370395
371- sinon . stub ( dns . promises , 'resolveTxt' ) . callsFake ( async ( ) => uri ) ;
372- sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => uri ) ;
396+ sinon . stub ( dns . promises , 'resolveTxt' ) . callsFake ( async ( ) => {
397+ throw { code : 'ENODATA' } ;
398+ } ) ;
399+ sinon . stub ( dns . promises , 'resolveSrv' ) . callsFake ( async ( ) => {
400+ const formattedUri = mongodb . HostAddress . fromString ( uri . split ( '//' ) [ 1 ] ) ;
401+ return [
402+ {
403+ name : formattedUri . host ,
404+ port : formattedUri . port ,
405+ weight : 0 ,
406+ priority : 0 ,
407+ protocol : formattedUri . host . isIPv6 ? 'IPv6' : 'IPv4'
408+ }
409+ ] ;
410+ } ) ;
411+ /* sinon.stub(mongodb, 'checkParentDomainMatch').callsFake(async () => {
412+ console.log('in here!!!');
413+ }); */
373414
374- const srvUri = uri . replace ( 'mongodb://' , 'mongodb+srv://' ) ;
375- const client = new MongoClient ( srvUri ) ;
415+ const client = new MongoClient ( 'mongodb+srv://localhost' ) ;
376416 await client . connect ( ) ;
377-
378-
379417 await client . close ( ) ;
380418 expect ( getTimerCount ( ) ) . to . equal ( 0 ) ;
419+ sinon . restore ( ) ;
381420 } ;
382421
383422 const getTimerCount = ( ) => process . getActiveResourcesInfo ( ) . filter ( r => r === 'Timeout' ) . length ;
384- await run ( { MongoClient, uri : config . uri , sleep, sinon, expect, mongodb, getTimerCount} ) ;
423+ // await run({ MongoClient, uri: config.uri, sleep, sinon, expect, mongodb, getTimerCount});
424+ await runScriptAndGetProcessInfo ( 'srv-poller-timer' , config , run ) ;
385425 } ) ;
386426 } ) ;
387427 } ) ;
0 commit comments