1
1
import { expect } from 'chai' ;
2
2
import { once } from 'events' ;
3
+ import * as sinon from 'sinon' ;
3
4
4
5
import {
5
6
BSONType ,
@@ -8,12 +9,11 @@ import {
8
9
type Collection ,
9
10
type MongoClient ,
10
11
MongoNotConnectedError ,
11
- MongoOperationTimeoutError ,
12
12
ProfilingLevel ,
13
13
Topology ,
14
14
TopologyType
15
15
} from '../../mongodb' ;
16
- import { type FailPoint } from '../../tools/utils' ;
16
+ import { type FailPoint , sleep } from '../../tools/utils' ;
17
17
18
18
describe ( 'When executing an operation for the first time' , ( ) => {
19
19
let client : MongoClient ;
@@ -824,7 +824,7 @@ describe('When executing an operation for the first time', () => {
824
824
} ) ;
825
825
} ) ;
826
826
827
- describe . only ( 'and CSOT is enabled' , function ( ) {
827
+ describe ( 'and CSOT is enabled' , function ( ) {
828
828
let client : MongoClient ;
829
829
830
830
beforeEach ( async function ( ) {
@@ -836,7 +836,7 @@ describe('When executing an operation for the first time', () => {
836
836
} ) ;
837
837
838
838
describe ( 'and nothing is wrong' , function ( ) {
839
- it ( 'should connect the client' , async function ( ) {
839
+ it ( 'connects the client' , async function ( ) {
840
840
await client . connect ( ) ;
841
841
expect ( client ) . to . have . property ( 'topology' ) . that . is . instanceOf ( Topology ) ;
842
842
} ) ;
@@ -856,24 +856,45 @@ describe('When executing an operation for the first time', () => {
856
856
} ) ;
857
857
858
858
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 ' ,
860
860
{ requires : { auth : 'enabled' } } ,
861
861
async function ( ) {
862
862
const start = performance . now ( ) ;
863
- const error = await client . connect ( ) . catch ( error => error ) ;
863
+ const returnedClient = await client . connect ( ) ;
864
864
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.
867
867
}
868
868
) ;
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
+ } ) ;
869
887
870
888
it (
871
- 'still have a pending connect promise ' ,
889
+ 'takes as long as selectServer is delayed for and does not throw a timeout error ' ,
872
890
{ requires : { auth : 'enabled' } } ,
873
891
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.
877
898
}
878
899
) ;
879
900
} ) ;
0 commit comments