@@ -611,35 +611,22 @@ describe('AbortSignal support', () => {
611611 let client : MongoClient ;
612612 let db : Db ;
613613 let collection : Collection < { a : number ; ssn : string } > ;
614- const logs : Log [ ] = [ ] ;
615614 let connectStarted ;
616615 let controller : AbortController ;
617616 let signal : AbortSignal ;
618617 let cursor : AbstractCursor < { a : number } > ;
619618
620619 describe ( 'when connect succeeds' , ( ) => {
621620 beforeEach ( async function ( ) {
622- logs . length = 0 ;
623-
624621 const promise = promiseWithResolvers < void > ( ) ;
625622 connectStarted = promise . promise ;
626623
627- client = this . configuration . newClient (
628- { } ,
629- {
630- mongodbLogComponentSeverities : { serverSelection : 'debug' } ,
631- mongodbLogPath : {
632- write : log => {
633- if ( log . c === 'serverSelection' && log . operation === 'handshake' ) {
634- controller . abort ( ) ;
635- promise . resolve ( ) ;
636- }
637- logs . push ( log ) ;
638- }
639- } ,
640- serverSelectionTimeoutMS : 1000
641- }
642- ) ;
624+ client = this . configuration . newClient ( { } , { serverSelectionTimeoutMS : 1000 } ) ;
625+
626+ client . once ( 'open' , ( ) => {
627+ controller . abort ( ) ;
628+ promise . resolve ( ) ;
629+ } ) ;
643630 db = client . db ( 'abortSignal' ) ;
644631 collection = db . collection ( 'support' ) ;
645632
@@ -650,7 +637,6 @@ describe('AbortSignal support', () => {
650637 } ) ;
651638
652639 afterEach ( async function ( ) {
653- logs . length = 0 ;
654640 await client ?. close ( ) ;
655641 } ) ;
656642
@@ -666,22 +652,18 @@ describe('AbortSignal support', () => {
666652
667653 describe ( 'when connect fails' , ( ) => {
668654 beforeEach ( async function ( ) {
669- logs . length = 0 ;
670-
671655 const promise = promiseWithResolvers < void > ( ) ;
672656 connectStarted = promise . promise ;
673657
658+ const selectServerStub = sinon
659+ . stub ( Topology . prototype , 'selectServer' )
660+ . callsFake ( async function ( ...args ) {
661+ controller . abort ( ) ;
662+ promise . resolve ( ) ;
663+ return selectServerStub . wrappedMethod . call ( this , ...args ) ;
664+ } ) ;
665+
674666 client = this . configuration . newClient ( 'mongodb://iLoveJavaScript' , {
675- mongodbLogComponentSeverities : { serverSelection : 'debug' } ,
676- mongodbLogPath : {
677- write : log => {
678- if ( log . c === 'serverSelection' && log . operation === 'handshake' ) {
679- controller . abort ( ) ;
680- promise . resolve ( ) ;
681- }
682- logs . push ( log ) ;
683- }
684- } ,
685667 serverSelectionTimeoutMS : 200 ,
686668 maxPoolSize : 1
687669 } ) ;
@@ -695,18 +677,23 @@ describe('AbortSignal support', () => {
695677 } ) ;
696678
697679 afterEach ( async function ( ) {
698- logs . length = 0 ;
680+ sinon . restore ( ) ;
699681 await client ?. close ( ) ;
700682 } ) ;
701683
702- it ( 'escapes auto connect without interrupting it ' , async ( ) => {
684+ it ( 'server selection error is thrown before reaching signal abort state check ' , async ( ) => {
703685 const toArray = cursor . toArray ( ) . catch ( error => error ) ;
704686 await connectStarted ;
705- expect ( await toArray ) . to . be . instanceOf ( DOMException ) ;
687+ const findError = await toArray ;
688+ expect ( findError ) . to . be . instanceOf ( MongoServerSelectionError ) ;
689+ if ( process . platform !== 'win32' ) {
690+ // linux / mac, unix in general will have this errno set,
691+ // which is generally helpful if this is kept elevated in the error message
692+ expect ( findError ) . to . match ( / E N O T F O U N D / ) ;
693+ }
706694 await sleep ( 500 ) ;
707695 expect ( client . topology ) . to . exist ;
708696 expect ( client . topology . description ) . to . have . property ( 'type' , 'Unknown' ) ;
709- expect ( findLast ( logs , l => l . message . includes ( 'Server selection failed' ) ) ) . to . exist ;
710697 } ) ;
711698 } ) ;
712699 } ) ;
0 commit comments