@@ -8,10 +8,12 @@ import {
88 type Collection ,
99 type MongoClient ,
1010 MongoNotConnectedError ,
11+ MongoOperationTimeoutError ,
1112 ProfilingLevel ,
1213 Topology ,
1314 TopologyType
1415} from '../../mongodb' ;
16+ import { type FailPoint } from '../../tools/utils' ;
1517
1618describe ( 'When executing an operation for the first time' , ( ) => {
1719 let client : MongoClient ;
@@ -821,4 +823,59 @@ describe('When executing an operation for the first time', () => {
821823 } ) ;
822824 } ) ;
823825 } ) ;
826+
827+ describe . only ( 'and CSOT is enabled' , function ( ) {
828+ let client : MongoClient ;
829+
830+ beforeEach ( async function ( ) {
831+ client = this . configuration . newClient ( { timeoutMS : 1000 } ) ;
832+ } ) ;
833+
834+ afterEach ( async function ( ) {
835+ await client . close ( ) ;
836+ } ) ;
837+
838+ describe ( 'and nothing is wrong' , function ( ) {
839+ it ( 'should connect the client' , async function ( ) {
840+ await client . connect ( ) ;
841+ expect ( client ) . to . have . property ( 'topology' ) . that . is . instanceOf ( Topology ) ;
842+ } ) ;
843+ } ) ;
844+
845+ describe ( 'and the server requires auth and ping is delayed' , function ( ) {
846+ beforeEach ( async function ( ) {
847+ // set failpoint to delay ping
848+ // create new util client to avoid affecting the test client
849+ const utilClient = this . configuration . newClient ( ) ;
850+ await utilClient . db ( 'admin' ) . command ( {
851+ configureFailPoint : 'failCommand' ,
852+ mode : { times : 1 } ,
853+ data : { failCommands : [ 'ping' ] , blockConnection : true , blockTimeMS : 2000 }
854+ } as FailPoint ) ;
855+ await utilClient . close ( ) ;
856+ } ) ;
857+
858+ it (
859+ 'should throw an MongoOperationTimeoutError error from connect' ,
860+ { requires : { auth : 'enabled' } } ,
861+ async function ( ) {
862+ const start = performance . now ( ) ;
863+ const error = await client . connect ( ) . catch ( error => error ) ;
864+ const end = performance . now ( ) ;
865+ expect ( error ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
866+ expect ( end - start ) . to . be . within ( 1000 , 1500 ) ;
867+ }
868+ ) ;
869+
870+ it (
871+ 'still have a pending connect promise' ,
872+ { requires : { auth : 'enabled' } } ,
873+ async function ( ) {
874+ const error = await client . connect ( ) . catch ( error => error ) ;
875+ expect ( client ) . to . have . property ( 'connectionLock' ) . that . is . instanceOf ( Promise ) ;
876+ expect ( error ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
877+ }
878+ ) ;
879+ } ) ;
880+ } ) ;
824881} ) ;
0 commit comments