@@ -9,6 +9,7 @@ const { expect } = require('chai');
99const { setImmediate } = require ( 'timers' ) ;
1010const { ns, isHello } = require ( '../../../src/utils' ) ;
1111const { LEGACY_HELLO_COMMAND } = require ( '../../../src/constants' ) ;
12+ const { createTimerSandbox } = require ( '../timer_sandbox' ) ;
1213
1314describe ( 'Connection Pool' , function ( ) {
1415 let server ;
@@ -128,6 +129,93 @@ describe('Connection Pool', function () {
128129 } ) ;
129130 } ) ;
130131
132+ describe ( 'minPoolSize population' , function ( ) {
133+ let clock , timerSandbox ;
134+ beforeEach ( ( ) => {
135+ timerSandbox = createTimerSandbox ( ) ;
136+ clock = sinon . useFakeTimers ( ) ;
137+ } ) ;
138+
139+ afterEach ( ( ) => {
140+ if ( clock ) {
141+ timerSandbox . restore ( ) ;
142+ clock . restore ( ) ;
143+ clock = undefined ;
144+ }
145+ } ) ;
146+
147+ it ( 'should respect the minPoolSizeCheckFrequencyMS option' , function ( ) {
148+ const pool = new ConnectionPool ( server , {
149+ minPoolSize : 2 ,
150+ minPoolSizeCheckFrequencyMS : 42 ,
151+ hostAddress : server . hostAddress ( )
152+ } ) ;
153+ const ensureSpy = sinon . spy ( pool , 'ensureMinPoolSize' ) ;
154+
155+ // return a fake connection that won't get identified as perished
156+ const createConnStub = sinon
157+ . stub ( pool , 'createConnection' )
158+ . yields ( null , { destroy : ( ) => null , generation : 0 } ) ;
159+
160+ pool . ready ( ) ;
161+
162+ // expect ensureMinPoolSize to execute immediately
163+ expect ( ensureSpy ) . to . have . been . calledOnce ;
164+ expect ( createConnStub ) . to . have . been . calledOnce ;
165+
166+ // check that the successful connection return schedules another run
167+ clock . tick ( 42 ) ;
168+ expect ( ensureSpy ) . to . have . been . calledTwice ;
169+ expect ( createConnStub ) . to . have . been . calledTwice ;
170+
171+ // check that the 2nd successful connection return schedules another run
172+ // but don't expect to get a new connection since we are at minPoolSize
173+ clock . tick ( 42 ) ;
174+ expect ( ensureSpy ) . to . have . been . calledThrice ;
175+ expect ( createConnStub ) . to . have . been . calledTwice ;
176+
177+ // check that the next scheduled check runs even after we're at minPoolSize
178+ clock . tick ( 42 ) ;
179+ expect ( ensureSpy ) . to . have . callCount ( 4 ) ;
180+ expect ( createConnStub ) . to . have . been . calledTwice ;
181+ } ) ;
182+
183+ it ( 'should default minPoolSizeCheckFrequencyMS to 100ms' , function ( ) {
184+ const pool = new ConnectionPool ( server , {
185+ minPoolSize : 2 ,
186+ hostAddress : server . hostAddress ( )
187+ } ) ;
188+ const ensureSpy = sinon . spy ( pool , 'ensureMinPoolSize' ) ;
189+
190+ // return a fake connection that won't get identified as perished
191+ const createConnStub = sinon
192+ . stub ( pool , 'createConnection' )
193+ . yields ( null , { destroy : ( ) => null , generation : 0 } ) ;
194+
195+ pool . ready ( ) ;
196+
197+ // expect ensureMinPoolSize to execute immediately
198+ expect ( ensureSpy ) . to . have . been . calledOnce ;
199+ expect ( createConnStub ) . to . have . been . calledOnce ;
200+
201+ // check that the successful connection return schedules another run
202+ clock . tick ( 100 ) ;
203+ expect ( ensureSpy ) . to . have . been . calledTwice ;
204+ expect ( createConnStub ) . to . have . been . calledTwice ;
205+
206+ // check that the 2nd successful connection return schedules another run
207+ // but don't expect to get a new connection since we are at minPoolSize
208+ clock . tick ( 100 ) ;
209+ expect ( ensureSpy ) . to . have . been . calledThrice ;
210+ expect ( createConnStub ) . to . have . been . calledTwice ;
211+
212+ // check that the next scheduled check runs even after we're at minPoolSize
213+ clock . tick ( 100 ) ;
214+ expect ( ensureSpy ) . to . have . callCount ( 4 ) ;
215+ expect ( createConnStub ) . to . have . been . calledTwice ;
216+ } ) ;
217+ } ) ;
218+
131219 describe ( 'withConnection' , function ( ) {
132220 it ( 'should manage a connection for a successful operation' , function ( done ) {
133221 server . setMessageHandler ( request => {
0 commit comments