11import { expect } from 'chai' ;
22import { once } from 'events' ;
3+ import * as sinon from 'sinon' ;
34
45import {
56 BSONType ,
@@ -8,12 +9,11 @@ import {
89 type Collection ,
910 type MongoClient ,
1011 MongoNotConnectedError ,
11- MongoOperationTimeoutError ,
1212 ProfilingLevel ,
1313 Topology ,
1414 TopologyType
1515} from '../../mongodb' ;
16- import { type FailPoint } from '../../tools/utils' ;
16+ import { type FailPoint , sleep } from '../../tools/utils' ;
1717
1818describe ( 'When executing an operation for the first time' , ( ) => {
1919 let client : MongoClient ;
@@ -824,7 +824,7 @@ describe('When executing an operation for the first time', () => {
824824 } ) ;
825825 } ) ;
826826
827- describe . only ( 'and CSOT is enabled' , function ( ) {
827+ describe ( 'and CSOT is enabled' , function ( ) {
828828 let client : MongoClient ;
829829
830830 beforeEach ( async function ( ) {
@@ -836,7 +836,7 @@ describe('When executing an operation for the first time', () => {
836836 } ) ;
837837
838838 describe ( 'and nothing is wrong' , function ( ) {
839- it ( 'should connect the client' , async function ( ) {
839+ it ( 'connects the client' , async function ( ) {
840840 await client . connect ( ) ;
841841 expect ( client ) . to . have . property ( 'topology' ) . that . is . instanceOf ( Topology ) ;
842842 } ) ;
@@ -856,24 +856,45 @@ describe('When executing an operation for the first time', () => {
856856 } ) ;
857857
858858 it (
859- 'should throw an MongoOperationTimeoutError error from connect ' ,
859+ 'takes as long as ping is delayed for and does not throw a timeout error ' ,
860860 { requires : { auth : 'enabled' } } ,
861861 async function ( ) {
862862 const start = performance . now ( ) ;
863- const error = await client . connect ( ) . catch ( error => error ) ;
863+ const returnedClient = await client . connect ( ) ;
864864 const end = performance . now ( ) ;
865- expect ( error ) . to . be . instanceOf ( MongoOperationTimeoutError ) ;
866- expect ( end - start ) . to . be . within ( 1000 , 1500 ) ;
865+ expect ( returnedClient ) . to . equal ( client ) ;
866+ expect ( end - start ) . to . be . within ( 2000 , 2500 ) ; // timeoutMS is 1000, did not apply.
867867 }
868868 ) ;
869+ } ) ;
870+
871+ describe ( 'when server selection takes longer than the timeout' , function ( ) {
872+ beforeEach ( async function ( ) {
873+ const selectServerStub = sinon
874+ . stub ( Topology . prototype , 'selectServer' )
875+ . callsFake ( async function ( selector , options ) {
876+ await sleep ( 2000 ) ;
877+ const result = selectServerStub . wrappedMethod . call ( this , selector , options ) ;
878+ sinon . restore ( ) ; // restore after connect selection
879+ return result ;
880+ } ) ;
881+ } ) ;
882+
883+ // restore sinon stub after test
884+ afterEach ( ( ) => {
885+ sinon . restore ( ) ;
886+ } ) ;
869887
870888 it (
871- 'still have a pending connect promise ' ,
889+ 'takes as long as selectServer is delayed for and does not throw a timeout error ' ,
872890 { requires : { auth : 'enabled' } } ,
873891 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 ) ;
892+ const start = performance . now ( ) ;
893+ expect ( client . topology ) . to . not . exist ; // make sure not connected.
894+ const res = await client . db ( ) . collection ( 'test' ) . insertOne ( { a : 1 } , { timeoutMS : 1000 } ) ; // auto-connect
895+ const end = performance . now ( ) ;
896+ expect ( res ) . to . have . property ( 'acknowledged' , true ) ;
897+ expect ( end - start ) . to . be . within ( 2000 , 2500 ) ; // timeoutMS is 1000, did not apply.
877898 }
878899 ) ;
879900 } ) ;
0 commit comments