1
1
/* eslint-disable @typescript-eslint/no-empty-function */
2
- const { expect } = require ( 'chai' ) ;
2
+ import { expect } from 'chai' ;
3
3
import * as sinon from 'sinon' ;
4
- const mongodb = require ( '../../mongodb' ) ;
5
- const { MongoClient } = mongodb ;
4
+ import { MongoClient } from '../../mongodb' ;
6
5
import { type TestConfiguration } from '../../tools/runner/config' ;
7
6
import { runScriptAndGetProcessInfo } from './resource_tracking_script_builder' ;
8
7
import { sleep } from '../../tools/utils' ;
8
+ import { ConnectionPool , Timeout } from '../../mongodb' ;
9
9
10
10
describe . only ( 'MongoClient.close() Integration' , ( ) => {
11
11
// note: these tests are set-up in accordance of the resource ownership tree
@@ -80,7 +80,32 @@ describe.only('MongoClient.close() Integration', () => {
80
80
describe ( 'Topology' , ( ) => {
81
81
describe ( 'Node.js resource: Server Selection Timer' , ( ) => {
82
82
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
+ } ) ;
84
109
} ) ;
85
110
} ) ;
86
111
@@ -294,14 +319,14 @@ describe.only('MongoClient.close() Integration', () => {
294
319
describe ( 'after new connection pool is created' , ( ) => {
295
320
it ( 'the wait queue timer is cleaned up by client.close()' , async function ( ) {
296
321
// 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 } ) {
298
323
const waitQueueTimeoutMS = 999 ;
299
324
const client = new MongoClient ( uri , { minPoolSize : 1 , waitQueueTimeoutMS } ) ;
300
- const timeoutStartedSpy = sinon . spy ( mongodb . Timeout , 'expires' ) ;
325
+ const timeoutStartedSpy = sinon . spy ( Timeout , 'expires' ) ;
301
326
let checkoutTimeoutStarted = false ;
302
327
303
328
// 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 ( ) => {
305
330
checkoutTimeoutStarted = timeoutStartedSpy . getCalls ( ) . map ( r => r . args ) . filter ( r => r . includes ( 999 ) ) ? true : false ;
306
331
} ) ;
307
332
@@ -316,7 +341,7 @@ describe.only('MongoClient.close() Integration', () => {
316
341
} ;
317
342
318
343
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} ) ;
320
345
} ) ;
321
346
} ) ;
322
347
} ) ;
@@ -364,24 +389,39 @@ describe.only('MongoClient.close() Integration', () => {
364
389
}
365
390
} ;
366
391
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 } ) {
369
394
const dns = require ( 'dns' ) ;
370
395
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
+ }); */
373
414
374
- const srvUri = uri . replace ( 'mongodb://' , 'mongodb+srv://' ) ;
375
- const client = new MongoClient ( srvUri ) ;
415
+ const client = new MongoClient ( 'mongodb+srv://localhost' ) ;
376
416
await client . connect ( ) ;
377
-
378
-
379
417
await client . close ( ) ;
380
418
expect ( getTimerCount ( ) ) . to . equal ( 0 ) ;
419
+ sinon . restore ( ) ;
381
420
} ;
382
421
383
422
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 ) ;
385
425
} ) ;
386
426
} ) ;
387
427
} ) ;
0 commit comments