1
1
import { expect } from 'chai' ;
2
2
import { once } from 'events' ;
3
3
import * as net from 'net' ;
4
- import { Socket } from 'net' ;
5
4
import * as sinon from 'sinon' ;
6
5
7
6
import {
@@ -144,8 +143,9 @@ describe('class MongoClient', function () {
144
143
let spy ;
145
144
146
145
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 ) ;
149
149
await client . connect ( ) ;
150
150
} ) ;
151
151
@@ -155,7 +155,12 @@ describe('class MongoClient', function () {
155
155
} ) ;
156
156
157
157
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
+ ) ;
159
164
} ) ;
160
165
} ) ;
161
166
@@ -165,8 +170,9 @@ describe('class MongoClient', function () {
165
170
let spy ;
166
171
167
172
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 ) ;
170
176
await client . connect ( ) ;
171
177
} ) ;
172
178
@@ -176,17 +182,39 @@ describe('class MongoClient', function () {
176
182
} ) ;
177
183
178
184
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
+ ) ;
180
191
} ) ;
181
192
} ) ;
182
193
183
194
context ( 'when the value is negative' , function ( ) {
184
195
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
+ } ) ;
185
205
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
+ ) ;
190
218
} ) ;
191
219
} ) ;
192
220
} ) ;
@@ -196,7 +224,7 @@ describe('class MongoClient', function () {
196
224
let spy ;
197
225
198
226
beforeEach ( async function ( ) {
199
- spy = sinon . spy ( Socket . prototype , 'setKeepAlive ' ) ;
227
+ spy = sinon . spy ( net , 'createConnection ' ) ;
200
228
client = this . configuration . newClient ( ) ;
201
229
await client . connect ( ) ;
202
230
} ) ;
@@ -207,7 +235,12 @@ describe('class MongoClient', function () {
207
235
} ) ;
208
236
209
237
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
+ ) ;
211
244
} ) ;
212
245
} ) ;
213
246
} ) ;
0 commit comments