11import { expect } from 'chai' ;
22import { once } from 'events' ;
33import * as net from 'net' ;
4- import { Socket } from 'net' ;
54import * as sinon from 'sinon' ;
65
76import {
@@ -144,8 +143,9 @@ describe('class MongoClient', function () {
144143 let spy ;
145144
146145 beforeEach ( async function ( ) {
147- spy = sinon . spy ( Socket . prototype , 'setKeepAlive' ) ;
148- client = this . configuration . newClient ( options ) ;
146+ spy = sinon . spy ( net , 'createConnection' ) ;
147+ const uri = this . configuration . url ( ) ;
148+ client = new MongoClient ( uri , options ) ;
149149 await client . connect ( ) ;
150150 } ) ;
151151
@@ -155,7 +155,12 @@ describe('class MongoClient', function () {
155155 } ) ;
156156
157157 it ( 'passes through the option' , function ( ) {
158- expect ( spy ) . to . have . been . calledWith ( true , 0 ) ;
158+ expect ( spy ) . to . have . been . calledWith (
159+ sinon . match ( {
160+ keepAlive : true ,
161+ keepAliveInitialDelay : 0
162+ } )
163+ ) ;
159164 } ) ;
160165 } ) ;
161166
@@ -165,8 +170,9 @@ describe('class MongoClient', function () {
165170 let spy ;
166171
167172 beforeEach ( async function ( ) {
168- spy = sinon . spy ( Socket . prototype , 'setKeepAlive' ) ;
169- client = this . configuration . newClient ( options ) ;
173+ spy = sinon . spy ( net , 'createConnection' ) ;
174+ const uri = this . configuration . url ( ) ;
175+ client = new MongoClient ( uri , options ) ;
170176 await client . connect ( ) ;
171177 } ) ;
172178
@@ -176,17 +182,39 @@ describe('class MongoClient', function () {
176182 } ) ;
177183
178184 it ( 'passes through the option' , function ( ) {
179- expect ( spy ) . to . have . been . calledWith ( true , 100 ) ;
185+ expect ( spy ) . to . have . been . calledWith (
186+ sinon . match ( {
187+ keepAlive : true ,
188+ keepAliveInitialDelay : 100
189+ } )
190+ ) ;
180191 } ) ;
181192 } ) ;
182193
183194 context ( 'when the value is negative' , function ( ) {
184195 const options = { keepAliveInitialDelay : - 100 } ;
196+ let client ;
197+ let spy ;
198+
199+ beforeEach ( async function ( ) {
200+ spy = sinon . spy ( net , 'createConnection' ) ;
201+ const uri = this . configuration . url ( ) ;
202+ client = new MongoClient ( uri , options ) ;
203+ await client . connect ( ) ;
204+ } ) ;
185205
186- it ( 'raises an error' , function ( ) {
187- expect ( ( ) => {
188- this . configuration . newClient ( options ) ;
189- } ) . to . throw ( / k e e p A l i v e I n i t i a l D e l a y c a n o n l y b e a p o s i t i v e i n t v a l u e / ) ;
206+ afterEach ( async function ( ) {
207+ await client ?. close ( ) ;
208+ spy . restore ( ) ;
209+ } ) ;
210+
211+ it ( 'sets the option to 0' , function ( ) {
212+ expect ( spy ) . to . have . been . calledWith (
213+ sinon . match ( {
214+ keepAlive : true ,
215+ keepAliveInitialDelay : 0
216+ } )
217+ ) ;
190218 } ) ;
191219 } ) ;
192220 } ) ;
@@ -196,7 +224,7 @@ describe('class MongoClient', function () {
196224 let spy ;
197225
198226 beforeEach ( async function ( ) {
199- spy = sinon . spy ( Socket . prototype , 'setKeepAlive ' ) ;
227+ spy = sinon . spy ( net , 'createConnection ' ) ;
200228 client = this . configuration . newClient ( ) ;
201229 await client . connect ( ) ;
202230 } ) ;
@@ -207,7 +235,12 @@ describe('class MongoClient', function () {
207235 } ) ;
208236
209237 it ( 'sets keepalive to 120000' , function ( ) {
210- expect ( spy ) . to . have . been . calledWith ( true , 120000 ) ;
238+ expect ( spy ) . to . have . been . calledWith (
239+ sinon . match ( {
240+ keepAlive : true ,
241+ keepAliveInitialDelay : 120000
242+ } )
243+ ) ;
211244 } ) ;
212245 } ) ;
213246 } ) ;
0 commit comments