@@ -135,6 +135,206 @@ describe('class MongoClient', function () {
135135    expect ( error ) . to . be . instanceOf ( MongoServerSelectionError ) ; 
136136  } ) ; 
137137
138+   describe ( '#connect' ,  function  ( )  { 
139+     context ( 'when keepAliveInitialDelay is provided' ,  function  ( )  { 
140+       context ( 'when the value is 0' ,  function  ( )  { 
141+         const  options  =  {  keepAliveInitialDelay : 0  } ; 
142+         let  client ; 
143+         let  spy ; 
144+ 
145+         beforeEach ( async  function  ( )  { 
146+           spy  =  sinon . spy ( net ,  'createConnection' ) ; 
147+           const  uri  =  this . configuration . url ( ) ; 
148+           client  =  new  MongoClient ( uri ,  options ) ; 
149+           await  client . connect ( ) ; 
150+         } ) ; 
151+ 
152+         afterEach ( async  function  ( )  { 
153+           await  client ?. close ( ) ; 
154+           spy . restore ( ) ; 
155+         } ) ; 
156+ 
157+         it ( 'passes through the option' ,  { 
158+           metadata : {  requires : {  apiVersion : false  }  } , 
159+           test : function  ( )  { 
160+             expect ( spy ) . to . have . been . calledWith ( 
161+               sinon . match ( { 
162+                 keepAlive : true , 
163+                 keepAliveInitialDelay : 0 
164+               } ) 
165+             ) ; 
166+           } 
167+         } ) ; 
168+       } ) ; 
169+ 
170+       context ( 'when the value is positive' ,  function  ( )  { 
171+         const  options  =  {  keepAliveInitialDelay : 100  } ; 
172+         let  client ; 
173+         let  spy ; 
174+ 
175+         beforeEach ( async  function  ( )  { 
176+           spy  =  sinon . spy ( net ,  'createConnection' ) ; 
177+           const  uri  =  this . configuration . url ( ) ; 
178+           client  =  new  MongoClient ( uri ,  options ) ; 
179+           await  client . connect ( ) ; 
180+         } ) ; 
181+ 
182+         afterEach ( async  function  ( )  { 
183+           await  client ?. close ( ) ; 
184+           spy . restore ( ) ; 
185+         } ) ; 
186+ 
187+         it ( 'passes through the option' ,  { 
188+           metadata : {  requires : {  apiVersion : false  }  } , 
189+           test : function  ( )  { 
190+             expect ( spy ) . to . have . been . calledWith ( 
191+               sinon . match ( { 
192+                 keepAlive : true , 
193+                 keepAliveInitialDelay : 100 
194+               } ) 
195+             ) ; 
196+           } 
197+         } ) ; 
198+       } ) ; 
199+ 
200+       context ( 'when the value is negative' ,  function  ( )  { 
201+         const  options  =  {  keepAliveInitialDelay : - 100  } ; 
202+         let  client ; 
203+         let  spy ; 
204+ 
205+         beforeEach ( async  function  ( )  { 
206+           spy  =  sinon . spy ( net ,  'createConnection' ) ; 
207+           const  uri  =  this . configuration . url ( ) ; 
208+           client  =  new  MongoClient ( uri ,  options ) ; 
209+           await  client . connect ( ) ; 
210+         } ) ; 
211+ 
212+         afterEach ( async  function  ( )  { 
213+           await  client ?. close ( ) ; 
214+           spy . restore ( ) ; 
215+         } ) ; 
216+ 
217+         it ( 'the Node.js runtime sets the option to 0' ,  { 
218+           metadata : {  requires : {  apiVersion : false  }  } , 
219+           test : function  ( )  { 
220+             expect ( spy ) . to . have . been . calledWith ( 
221+               sinon . match ( { 
222+                 keepAlive : true , 
223+                 keepAliveInitialDelay : 0 
224+               } ) 
225+             ) ; 
226+           } 
227+         } ) ; 
228+       } ) ; 
229+ 
230+       context ( 'when the value is mistyped' ,  function  ( )  { 
231+         // Set server selection timeout to get the error quicker. 
232+         const  options  =  {  keepAliveInitialDelay : 'test' ,  serverSelectionTimeoutMS : 1000  } ; 
233+         let  client ; 
234+         let  spy ; 
235+ 
236+         beforeEach ( async  function  ( )  { 
237+           spy  =  sinon . spy ( net ,  'createConnection' ) ; 
238+           const  uri  =  this . configuration . url ( ) ; 
239+           client  =  new  MongoClient ( uri ,  options ) ; 
240+         } ) ; 
241+ 
242+         afterEach ( async  function  ( )  { 
243+           await  client ?. close ( ) ; 
244+           spy . restore ( ) ; 
245+         } ) ; 
246+ 
247+         it ( 'throws an error' ,  { 
248+           metadata : {  requires : {  apiVersion : false  }  } , 
249+           test : async  function  ( )  { 
250+             const  error  =  await  client . connect ( ) . catch ( error  =>  error ) ; 
251+             expect ( error . message ) . to . include ( 
252+               'property must be of type number. Received type string' 
253+             ) ; 
254+           } 
255+         } ) ; 
256+       } ) ; 
257+     } ) ; 
258+ 
259+     context ( 'when keepAliveInitialDelay is not provided' ,  function  ( )  { 
260+       let  client ; 
261+       let  spy ; 
262+ 
263+       beforeEach ( async  function  ( )  { 
264+         spy  =  sinon . spy ( net ,  'createConnection' ) ; 
265+         client  =  this . configuration . newClient ( ) ; 
266+         await  client . connect ( ) ; 
267+       } ) ; 
268+ 
269+       afterEach ( async  function  ( )  { 
270+         await  client ?. close ( ) ; 
271+         spy . restore ( ) ; 
272+       } ) ; 
273+ 
274+       it ( 'sets keepalive to 120000' ,  function  ( )  { 
275+         expect ( spy ) . to . have . been . calledWith ( 
276+           sinon . match ( { 
277+             keepAlive : true , 
278+             keepAliveInitialDelay : 120000 
279+           } ) 
280+         ) ; 
281+       } ) ; 
282+     } ) ; 
283+ 
284+     context ( 'when noDelay is not provided' ,  function  ( )  { 
285+       let  client ; 
286+       let  spy ; 
287+ 
288+       beforeEach ( async  function  ( )  { 
289+         spy  =  sinon . spy ( net ,  'createConnection' ) ; 
290+         client  =  this . configuration . newClient ( ) ; 
291+         await  client . connect ( ) ; 
292+       } ) ; 
293+ 
294+       afterEach ( async  function  ( )  { 
295+         await  client ?. close ( ) ; 
296+         spy . restore ( ) ; 
297+       } ) ; 
298+ 
299+       it ( 'sets noDelay to true' ,  function  ( )  { 
300+         expect ( spy ) . to . have . been . calledWith ( 
301+           sinon . match ( { 
302+             noDelay : true 
303+           } ) 
304+         ) ; 
305+       } ) ; 
306+     } ) ; 
307+ 
308+     context ( 'when noDelay is provided' ,  function  ( )  { 
309+       let  client ; 
310+       let  spy ; 
311+ 
312+       beforeEach ( async  function  ( )  { 
313+         const  options  =  {  noDelay : false  } ; 
314+         spy  =  sinon . spy ( net ,  'createConnection' ) ; 
315+         const  uri  =  this . configuration . url ( ) ; 
316+         client  =  new  MongoClient ( uri ,  options ) ; 
317+         await  client . connect ( ) ; 
318+       } ) ; 
319+ 
320+       afterEach ( async  function  ( )  { 
321+         await  client ?. close ( ) ; 
322+         spy . restore ( ) ; 
323+       } ) ; 
324+ 
325+       it ( 'sets noDelay' ,  { 
326+         metadata : {  requires : {  apiVersion : false  }  } , 
327+         test : function  ( )  { 
328+           expect ( spy ) . to . have . been . calledWith ( 
329+             sinon . match ( { 
330+               noDelay : false 
331+             } ) 
332+           ) ; 
333+         } 
334+       } ) ; 
335+     } ) ; 
336+   } ) ; 
337+ 
138338  it ( 'Should correctly pass through appname' ,  { 
139339    metadata : { 
140340      requires : { 
@@ -889,12 +1089,12 @@ describe('class MongoClient', function () {
8891089        metadata : {  requires : {  topology : [ 'single' ]  }  } , 
8901090        test : async  function  ( )  { 
8911091          await  client . connect ( ) ; 
892-           expect ( netSpy ) . to . have . been . calledWith ( { 
893-             autoSelectFamily :  false , 
894-             autoSelectFamilyAttemptTimeout :  100 , 
895-             host :  'localhost' , 
896-             port :  27017 
897-           } ) ; 
1092+           expect ( netSpy ) . to . have . been . calledWith ( 
1093+             sinon . match ( { 
1094+                autoSelectFamily :  false , 
1095+                autoSelectFamilyAttemptTimeout :  100 
1096+             } ) 
1097+           ) ; 
8981098        } 
8991099      } ) ; 
9001100    } ) ; 
@@ -908,11 +1108,11 @@ describe('class MongoClient', function () {
9081108        metadata : {  requires : {  topology : [ 'single' ]  }  } , 
9091109        test : async  function  ( )  { 
9101110          await  client . connect ( ) ; 
911-           expect ( netSpy ) . to . have . been . calledWith ( { 
912-             autoSelectFamily :  true , 
913-             host :  'localhost' , 
914-             port :  27017 
915-           } ) ; 
1111+           expect ( netSpy ) . to . have . been . calledWith ( 
1112+             sinon . match ( { 
1113+                autoSelectFamily :  true 
1114+             } ) 
1115+           ) ; 
9161116        } 
9171117      } ) ; 
9181118    } ) ; 
0 commit comments