1
1
'use strict' ;
2
2
3
- const { ConnectionPool } = require ( '../../mongodb' ) ;
3
+ const { ConnectionPool, MongoError } = require ( '../../mongodb' ) ;
4
4
const { WaitQueueTimeoutError } = require ( '../../mongodb' ) ;
5
5
const mock = require ( '../../tools/mongodb-mock/index' ) ;
6
6
const sinon = require ( 'sinon' ) ;
@@ -69,7 +69,7 @@ describe('Connection Pool', function () {
69
69
expect ( closeEvent ) . have . property ( 'reason' ) . equal ( 'error' ) ;
70
70
} ) ;
71
71
72
- it ( 'should propagate socket timeouts to connections' , function ( done ) {
72
+ it ( 'should propagate socket timeouts to connections' , async function ( ) {
73
73
mockMongod . setMessageHandler ( request => {
74
74
const doc = request . document ;
75
75
if ( isHello ( doc ) ) {
@@ -87,18 +87,12 @@ describe('Connection Pool', function () {
87
87
88
88
pool . ready ( ) ;
89
89
90
- pool . withConnection (
91
- ( err , conn , cb ) => {
92
- expect ( err ) . to . not . exist ;
93
- conn . command ( ns ( 'admin.$cmd' ) , { ping : 1 } , undefined , ( err , result ) => {
94
- expect ( err ) . to . exist ;
95
- expect ( result ) . to . not . exist ;
96
- expect ( err ) . to . match ( / t i m e d o u t / ) ;
97
- cb ( ) ;
98
- } ) ;
99
- } ,
100
- ( ) => pool . close ( done )
101
- ) ;
90
+ const conn = await pool . checkOut ( ) ;
91
+ const maybeError = await conn . command ( ns ( 'admin.$cmd' ) , { ping : 1 } , undefined ) . catch ( e => e ) ;
92
+ expect ( maybeError ) . to . be . instanceOf ( MongoError ) ;
93
+ expect ( maybeError ) . to . match ( / t i m e d o u t / ) ;
94
+
95
+ pool . checkIn ( conn ) ;
102
96
} ) ;
103
97
104
98
it ( 'should clear timed out wait queue members if no connections are available' , function ( done ) {
@@ -222,98 +216,4 @@ describe('Connection Pool', function () {
222
216
expect ( createConnStub ) . to . have . been . calledTwice ;
223
217
} ) ;
224
218
} ) ;
225
-
226
- describe ( 'withConnection' , function ( ) {
227
- it ( 'should manage a connection for a successful operation' , function ( done ) {
228
- mockMongod . setMessageHandler ( request => {
229
- const doc = request . document ;
230
- if ( isHello ( doc ) ) {
231
- request . reply ( mock . HELLO ) ;
232
- }
233
- } ) ;
234
-
235
- const pool = new ConnectionPool ( stubServer , { hostAddress : mockMongod . hostAddress ( ) } ) ;
236
- pool . ready ( ) ;
237
-
238
- const callback = ( err , result ) => {
239
- expect ( err ) . to . not . exist ;
240
- expect ( result ) . to . exist ;
241
- pool . close ( done ) ;
242
- } ;
243
-
244
- pool . withConnection ( ( err , conn , cb ) => {
245
- expect ( err ) . to . not . exist ;
246
-
247
- conn . command (
248
- ns ( '$admin.cmd' ) ,
249
- { [ LEGACY_HELLO_COMMAND ] : 1 } ,
250
- undefined ,
251
- ( cmdErr , hello ) => {
252
- expect ( cmdErr ) . to . not . exist ;
253
- cb ( undefined , hello ) ;
254
- }
255
- ) ;
256
- } , callback ) ;
257
- } ) ;
258
-
259
- it ( 'should allow user interaction with an error' , function ( done ) {
260
- mockMongod . setMessageHandler ( request => {
261
- const doc = request . document ;
262
- if ( isHello ( doc ) ) {
263
- request . connection . destroy ( ) ;
264
- }
265
- } ) ;
266
-
267
- const pool = new ConnectionPool ( stubServer , {
268
- waitQueueTimeoutMS : 200 ,
269
- hostAddress : mockMongod . hostAddress ( )
270
- } ) ;
271
-
272
- pool . ready ( ) ;
273
-
274
- const callback = err => {
275
- expect ( err ) . to . exist ;
276
- expect ( err ) . to . match ( / c l o s e d / ) ;
277
- pool . close ( done ) ;
278
- } ;
279
-
280
- pool . withConnection (
281
- undefined ,
282
- ( err , conn , cb ) => {
283
- expect ( err ) . to . exist ;
284
- expect ( err ) . to . match ( / c l o s e d / ) ;
285
- cb ( err ) ;
286
- } ,
287
- callback
288
- ) ;
289
- } ) ;
290
-
291
- it ( 'should return an error to the original callback' , function ( done ) {
292
- mockMongod . setMessageHandler ( request => {
293
- const doc = request . document ;
294
- if ( isHello ( doc ) ) {
295
- request . reply ( mock . HELLO ) ;
296
- }
297
- } ) ;
298
-
299
- const pool = new ConnectionPool ( stubServer , { hostAddress : mockMongod . hostAddress ( ) } ) ;
300
- pool . ready ( ) ;
301
-
302
- const callback = ( err , result ) => {
303
- expect ( err ) . to . exist ;
304
- expect ( result ) . to . not . exist ;
305
- expect ( err ) . to . match ( / m y g r e a t e r r o r / ) ;
306
- pool . close ( done ) ;
307
- } ;
308
-
309
- pool . withConnection (
310
- undefined ,
311
- ( err , conn , cb ) => {
312
- expect ( err ) . to . not . exist ;
313
- cb ( new Error ( 'my great error' ) ) ;
314
- } ,
315
- callback
316
- ) ;
317
- } ) ;
318
- } ) ;
319
219
} ) ;
0 commit comments