@@ -12,13 +12,14 @@ import {
1212 Db ,
1313 getTopology ,
1414 MongoClient ,
15+ MongoClientClosedError ,
1516 MongoNotConnectedError ,
1617 MongoServerSelectionError ,
1718 ReadPreference ,
1819 ServerDescription ,
1920 Topology
2021} from '../../mongodb' ;
21- import { runLater } from '../../tools/utils' ;
22+ import { clearFailPoint , configureFailPoint , runLater } from '../../tools/utils' ;
2223import { setupDatabase } from '../shared' ;
2324
2425describe ( 'class MongoClient' , function ( ) {
@@ -1064,6 +1065,51 @@ describe('class MongoClient', function () {
10641065 expect ( client . s . activeCursors ) . to . have . lengthOf ( 1 ) ;
10651066 } ) ;
10661067 } ) ;
1068+
1069+ describe ( 'maxPoolSize is not fully used when running clean up operations' , function ( ) {
1070+ let client ;
1071+
1072+ beforeEach ( async function ( ) {
1073+ await configureFailPoint ( this . configuration , {
1074+ configureFailPoint : 'failCommand' ,
1075+ mode : 'alwaysOn' ,
1076+ data : {
1077+ failCommands : [ 'insert' ] ,
1078+ blockConnection : true ,
1079+ blockTimeMS : 500
1080+ }
1081+ } ) ;
1082+
1083+ client = this . configuration . newClient ( { } , { maxPoolSize : 1 , monitorCommands : true } ) ;
1084+ } ) ;
1085+
1086+ afterEach ( async function ( ) {
1087+ await clearFailPoint ( this . configuration ) ;
1088+ await client . close ( ) ;
1089+ } ) ;
1090+
1091+ it (
1092+ 'closes in-use connections before running clean up operations avoiding a deadlock' ,
1093+ { requires : { topology : '!load-balanced' } } ,
1094+ async ( ) => {
1095+ const inserted = client
1096+ . db ( 't' )
1097+ . collection ( 't' )
1098+ . insertOne ( { a : 1 } )
1099+ . catch ( error => error ) ;
1100+
1101+ await once ( client , 'commandStarted' ) ;
1102+
1103+ const start = performance . now ( ) ;
1104+ await client . close ( ) ;
1105+ const error = await inserted ;
1106+ const end = performance . now ( ) ;
1107+
1108+ expect ( end - start ) . to . be . lessThan ( 100 ) ;
1109+ expect ( error ) . to . be . instanceOf ( MongoClientClosedError ) ;
1110+ }
1111+ ) ;
1112+ } ) ;
10671113 } ) ;
10681114
10691115 context ( 'when connecting' , function ( ) {
0 commit comments