@@ -29,6 +29,8 @@ import {
2929 findLast ,
3030 sleep
3131} from '../../tools/utils' ;
32+ import { Topology } from '../../../lib/sdam/topology' ;
33+ import { MongoServerSelectionError } from '../../../lib/error' ;
3234
3335const failPointMetadata = { requires : { mongodb : '>=4.4' } } ;
3436
@@ -611,35 +613,22 @@ describe('AbortSignal support', () => {
611613 let client : MongoClient ;
612614 let db : Db ;
613615 let collection : Collection < { a : number ; ssn : string } > ;
614- const logs : Log [ ] = [ ] ;
615616 let connectStarted ;
616617 let controller : AbortController ;
617618 let signal : AbortSignal ;
618619 let cursor : AbstractCursor < { a : number } > ;
619620
620621 describe ( 'when connect succeeds' , ( ) => {
621622 beforeEach ( async function ( ) {
622- logs . length = 0 ;
623-
624623 const promise = promiseWithResolvers < void > ( ) ;
625624 connectStarted = promise . promise ;
626625
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- ) ;
626+ client = this . configuration . newClient ( { } , { serverSelectionTimeoutMS : 1000 } ) ;
627+
628+ client . once ( 'open' , ( ) => {
629+ controller . abort ( ) ;
630+ promise . resolve ( ) ;
631+ } ) ;
643632 db = client . db ( 'abortSignal' ) ;
644633 collection = db . collection ( 'support' ) ;
645634
@@ -650,7 +639,6 @@ describe('AbortSignal support', () => {
650639 } ) ;
651640
652641 afterEach ( async function ( ) {
653- logs . length = 0 ;
654642 await client ?. close ( ) ;
655643 } ) ;
656644
@@ -666,22 +654,18 @@ describe('AbortSignal support', () => {
666654
667655 describe ( 'when connect fails' , ( ) => {
668656 beforeEach ( async function ( ) {
669- logs . length = 0 ;
670-
671657 const promise = promiseWithResolvers < void > ( ) ;
672658 connectStarted = promise . promise ;
673659
660+ const selectServerStub = sinon
661+ . stub ( Topology . prototype , 'selectServer' )
662+ . callsFake ( async function ( ...args ) {
663+ controller . abort ( ) ;
664+ promise . resolve ( ) ;
665+ return selectServerStub . wrappedMethod . call ( this , ...args ) ;
666+ } ) ;
667+
674668 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- } ,
685669 serverSelectionTimeoutMS : 200 ,
686670 maxPoolSize : 1
687671 } ) ;
@@ -695,18 +679,23 @@ describe('AbortSignal support', () => {
695679 } ) ;
696680
697681 afterEach ( async function ( ) {
698- logs . length = 0 ;
682+ sinon . restore ( ) ;
699683 await client ?. close ( ) ;
700684 } ) ;
701685
702- it ( 'escapes auto connect without interrupting it ' , async ( ) => {
686+ it ( 'server selection error is thrown before reaching signal abort state check ' , async ( ) => {
703687 const toArray = cursor . toArray ( ) . catch ( error => error ) ;
704688 await connectStarted ;
705- expect ( await toArray ) . to . be . instanceOf ( DOMException ) ;
689+ const findError = await toArray ;
690+ expect ( findError ) . to . be . instanceOf ( MongoServerSelectionError ) ;
691+ if ( process . platform !== 'win32' ) {
692+ // linux / mac, unix in general will have this errno set,
693+ // which is generally helpful if this is kept elevated in the error message
694+ expect ( findError ) . to . match ( / E N O T F O U N D / ) ;
695+ }
706696 await sleep ( 500 ) ;
707697 expect ( client . topology ) . to . exist ;
708698 expect ( client . topology . description ) . to . have . property ( 'type' , 'Unknown' ) ;
709- expect ( findLast ( logs , l => l . message . includes ( 'Server selection failed' ) ) ) . to . exist ;
710699 } ) ;
711700 } ) ;
712701 } ) ;
0 commit comments