@@ -9,6 +9,7 @@ const { expect } = require('chai');
9
9
const { setImmediate } = require ( 'timers' ) ;
10
10
const { ns, isHello } = require ( '../../../src/utils' ) ;
11
11
const { LEGACY_HELLO_COMMAND } = require ( '../../../src/constants' ) ;
12
+ const { createTimerSandbox } = require ( '../timer_sandbox' ) ;
12
13
13
14
describe ( 'Connection Pool' , function ( ) {
14
15
let server ;
@@ -128,6 +129,93 @@ describe('Connection Pool', function () {
128
129
} ) ;
129
130
} ) ;
130
131
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
+
131
219
describe ( 'withConnection' , function ( ) {
132
220
it ( 'should manage a connection for a successful operation' , function ( done ) {
133
221
server . setMessageHandler ( request => {
0 commit comments