58
58
import java .util .concurrent .locks .ReentrantLock ;
59
59
import java .util .stream .Stream ;
60
60
61
- import static com .mongodb .internal .connection .DefaultConnectionPool .MAX_CONNECTING ;
62
61
import static java .lang .Long .MAX_VALUE ;
63
62
import static java .util .concurrent .TimeUnit .MILLISECONDS ;
64
63
import static java .util .concurrent .TimeUnit .MINUTES ;
81
80
public class DefaultConnectionPoolTest {
82
81
private static final ServerId SERVER_ID = new ServerId (new ClusterId (), new ServerAddress ());
83
82
private static final long TEST_WAIT_TIMEOUT_MILLIS = SECONDS .toMillis (5 );
83
+ private static final int DEFAULT_MAX_CONNECTING = ConnectionPoolSettings .builder ().build ().getMaxConnecting ();
84
84
85
85
private TestInternalConnectionFactory connectionFactory ;
86
86
@@ -331,11 +331,15 @@ private static Stream<Arguments> concurrentUsageArguments() {
331
331
return Stream .of (// variants marked with (*) have proved their usefulness by detecting bugs
332
332
Arguments .of (0 , 1 , true , 8 , true , false , 0.02f , 0 , 0 ),
333
333
Arguments .of (0 , 1 , false , 8 , false , true , 0.02f , 0 , 0 ), // (*)
334
- Arguments .of (MAX_CONNECTING , MAX_CONNECTING , true , 8 , true , true , 0 , 0 , 0 ),
335
- Arguments .of (MAX_CONNECTING + 1 , MAX_CONNECTING + 5 , true , 2 * (MAX_CONNECTING + 5 ), true , true , 0.02f , 0 , 0 ),
336
- Arguments .of (MAX_CONNECTING + 5 , MAX_CONNECTING + 5 , false , 2 * (MAX_CONNECTING + 5 ), true , true , 0.02f , 0 , 0 ), // (*)
337
- Arguments .of (MAX_CONNECTING + 1 , MAX_CONNECTING + 5 , false , 2 * (MAX_CONNECTING + 5 ), true , true , 0.3f , 0.1f , 0.1f ),
338
- Arguments .of (MAX_CONNECTING + 1 , MAX_CONNECTING + 5 , true , 2 * (MAX_CONNECTING + 5 ), true , true , 0 , 0.5f , 0.05f ));
334
+ Arguments .of (DEFAULT_MAX_CONNECTING , DEFAULT_MAX_CONNECTING , true , 8 , true , true , 0 , 0 , 0 ),
335
+ Arguments .of (DEFAULT_MAX_CONNECTING + 1 , DEFAULT_MAX_CONNECTING + 5 , true , 2 * (DEFAULT_MAX_CONNECTING + 5 ),
336
+ true , true , 0.02f , 0 , 0 ),
337
+ Arguments .of (DEFAULT_MAX_CONNECTING + 5 , DEFAULT_MAX_CONNECTING + 5 , false , 2 * (DEFAULT_MAX_CONNECTING + 5 ),
338
+ true , true , 0.02f , 0 , 0 ), // (*)
339
+ Arguments .of (DEFAULT_MAX_CONNECTING + 1 , DEFAULT_MAX_CONNECTING + 5 , false , 2 * (DEFAULT_MAX_CONNECTING + 5 ),
340
+ true , true , 0.3f , 0.1f , 0.1f ),
341
+ Arguments .of (DEFAULT_MAX_CONNECTING + 1 , DEFAULT_MAX_CONNECTING + 5 , true , 2 * (DEFAULT_MAX_CONNECTING + 5 ),
342
+ true , true , 0 , 0.5f , 0.05f ));
339
343
}
340
344
341
345
@ Test
@@ -346,14 +350,15 @@ public void callbackShouldNotBlockCheckoutIfOpenAsyncWorksNotInCurrentThread() t
346
350
TestConnectionPoolListener listener = new TestConnectionPoolListener ();
347
351
provider = new DefaultConnectionPool (SERVER_ID , controllableConnFactory .factory ,
348
352
ConnectionPoolSettings .builder ()
349
- .maxSize (MAX_CONNECTING + maxAvailableConnections )
353
+ .maxSize (DEFAULT_MAX_CONNECTING + maxAvailableConnections )
350
354
.addConnectionPoolListener (listener )
351
355
.maxWaitTime (TEST_WAIT_TIMEOUT_MILLIS , MILLISECONDS )
352
356
.maintenanceInitialDelay (MAX_VALUE , NANOSECONDS )
353
357
.build (),
354
358
mockSdamProvider ());
355
359
provider .ready ();
356
- acquireOpenPermits (provider , MAX_CONNECTING , InfiniteCheckoutEmulation .INFINITE_CALLBACK , controllableConnFactory , listener );
360
+ acquireOpenPermits (provider , DEFAULT_MAX_CONNECTING , InfiniteCheckoutEmulation .INFINITE_CALLBACK ,
361
+ controllableConnFactory , listener );
357
362
assertUseConcurrently (provider , 2 * maxAvailableConnections ,
358
363
true , true ,
359
364
0.02f , 0 , 0 ,
@@ -364,7 +369,7 @@ public void callbackShouldNotBlockCheckoutIfOpenAsyncWorksNotInCurrentThread() t
364
369
* The idea of this test is as follows:
365
370
* <ol>
366
371
* <li>Check out some connections from the pool
367
- * ({@link DefaultConnectionPool#MAX_CONNECTING } connections must not be checked out to make the next step possible).</li>
372
+ * ({@link #DEFAULT_MAX_CONNECTING } connections must not be checked out to make the next step possible).</li>
368
373
* <li>Acquire all permits to open a connection and leave them acquired.</li>
369
374
* <li>Check in the checked out connections and concurrently check them out again.</li>
370
375
* </ol>
@@ -380,7 +385,7 @@ public void checkoutHandOverMechanism() throws InterruptedException, TimeoutExce
380
385
TestConnectionPoolListener listener = new TestConnectionPoolListener ();
381
386
provider = new DefaultConnectionPool (SERVER_ID , controllableConnFactory .factory ,
382
387
ConnectionPoolSettings .builder ()
383
- .maxSize (MAX_CONNECTING
388
+ .maxSize (DEFAULT_MAX_CONNECTING
384
389
+ openConnectionsCount
385
390
/* This wiggle room is needed to open opportunities to create new connections from the standpoint of
386
391
* the max pool size, and then check that no connections were created nonetheless. */
@@ -395,7 +400,7 @@ public void checkoutHandOverMechanism() throws InterruptedException, TimeoutExce
395
400
for (int i = 0 ; i < openConnectionsCount ; i ++) {
396
401
connections .add (provider .get (0 , NANOSECONDS ));
397
402
}
398
- acquireOpenPermits (provider , MAX_CONNECTING , InfiniteCheckoutEmulation .INFINITE_OPEN , controllableConnFactory , listener );
403
+ acquireOpenPermits (provider , DEFAULT_MAX_CONNECTING , InfiniteCheckoutEmulation .INFINITE_OPEN , controllableConnFactory , listener );
399
404
int previousIdx = 0 ;
400
405
// concurrently check in / check out and assert the hand-over mechanism works
401
406
for (int idx = 0 ; idx < connections .size (); idx += maxConcurrentlyHandedOver ) {
@@ -542,7 +547,7 @@ private static void acquireOpenPermits(final DefaultConnectionPool pool, final i
542
547
final InfiniteCheckoutEmulation infiniteEmulation ,
543
548
final ControllableConnectionFactory controllableConnFactory ,
544
549
final TestConnectionPoolListener listener ) throws TimeoutException , InterruptedException {
545
- assertTrue (openPermitsCount <= MAX_CONNECTING );
550
+ assertTrue (openPermitsCount <= DEFAULT_MAX_CONNECTING );
546
551
int initialCreatedEventCount = listener .countEvents (ConnectionCreatedEvent .class );
547
552
switch (infiniteEmulation ) {
548
553
case INFINITE_CALLBACK : {
0 commit comments