@@ -384,7 +384,7 @@ public void testPipeliningDistribution(TestContext ctx) {
384
384
public void testPoolIdleTimeout (TestContext ctx ) {
385
385
ProxyServer proxy = ProxyServer .create (vertx , options .getPort (), options .getHost ());
386
386
AtomicReference <ProxyServer .Connection > proxyConn = new AtomicReference <>();
387
- int pooleCleanerPeriod = 100 ;
387
+ int poolCleanerPeriod = 100 ;
388
388
int idleTimeout = 3000 ;
389
389
Async latch = ctx .async ();
390
390
proxy .proxyHandler (conn -> {
@@ -393,8 +393,8 @@ public void testPoolIdleTimeout(TestContext ctx) {
393
393
conn .clientCloseHandler (v -> {
394
394
long lifetime = System .currentTimeMillis () - now ;
395
395
int delta = 500 ;
396
- int lowerBound = idleTimeout - pooleCleanerPeriod - delta ;
397
- int upperBound = idleTimeout + pooleCleanerPeriod + delta ;
396
+ int lowerBound = idleTimeout - poolCleanerPeriod - delta ;
397
+ int upperBound = idleTimeout + poolCleanerPeriod + delta ;
398
398
ctx .assertTrue (lifetime >= lowerBound , "Was expecting connection to be closed in more than " + lowerBound + ": " + lifetime );
399
399
ctx .assertTrue (lifetime <= upperBound , "Was expecting connection to be closed in less than " + upperBound + ": " + lifetime );
400
400
latch .complete ();
@@ -408,7 +408,8 @@ public void testPoolIdleTimeout(TestContext ctx) {
408
408
listenLatch .awaitSuccess (20_000 );
409
409
410
410
poolOptions
411
- .setPoolCleanerPeriod (pooleCleanerPeriod )
411
+ .setPoolCleanerPeriod (poolCleanerPeriod )
412
+ .setMaxLifetime (0 )
412
413
.setIdleTimeout (idleTimeout )
413
414
.setIdleTimeoutUnit (TimeUnit .MILLISECONDS );
414
415
options .setPort (8080 );
@@ -422,6 +423,49 @@ public void testPoolIdleTimeout(TestContext ctx) {
422
423
.onComplete (ctx .asyncAssertSuccess ());
423
424
}
424
425
426
+ @ Test
427
+ public void testPoolMaxLifetime (TestContext ctx ) {
428
+ ProxyServer proxy = ProxyServer .create (vertx , options .getPort (), options .getHost ());
429
+ AtomicReference <ProxyServer .Connection > proxyConn = new AtomicReference <>();
430
+ int poolCleanerPeriod = 100 ;
431
+ int maxLifetime = 3000 ;
432
+ Async latch = ctx .async ();
433
+ proxy .proxyHandler (conn -> {
434
+ proxyConn .set (conn );
435
+ long now = System .currentTimeMillis ();
436
+ conn .clientCloseHandler (v -> {
437
+ long lifetime = System .currentTimeMillis () - now ;
438
+ int delta = 500 ;
439
+ int lowerBound = maxLifetime - poolCleanerPeriod - delta ;
440
+ int upperBound = maxLifetime + poolCleanerPeriod + delta ;
441
+ ctx .assertTrue (lifetime >= lowerBound , "Was expecting connection to be closed in more than " + lowerBound + ": " + lifetime );
442
+ ctx .assertTrue (lifetime <= upperBound , "Was expecting connection to be closed in less than " + upperBound + ": " + lifetime );
443
+ latch .complete ();
444
+ });
445
+ conn .connect ();
446
+ });
447
+
448
+ // Start proxy
449
+ Async listenLatch = ctx .async ();
450
+ proxy .listen (8080 , "localhost" , ctx .asyncAssertSuccess (res -> listenLatch .complete ()));
451
+ listenLatch .awaitSuccess (20_000 );
452
+
453
+ poolOptions
454
+ .setPoolCleanerPeriod (poolCleanerPeriod )
455
+ .setIdleTimeout (0 )
456
+ .setMaxLifetime (maxLifetime )
457
+ .setMaxLifetimeUnit (TimeUnit .MILLISECONDS );
458
+ options .setPort (8080 );
459
+ options .setHost ("localhost" );
460
+ PgPool pool = createPool (options , poolOptions );
461
+
462
+ // Create a connection that remains in the pool
463
+ pool
464
+ .getConnection ()
465
+ .flatMap (SqlClient ::close )
466
+ .onComplete (ctx .asyncAssertSuccess ());
467
+ }
468
+
425
469
@ Test
426
470
public void testPoolConnectTimeout (TestContext ctx ) {
427
471
Async async = ctx .async (2 );
@@ -463,9 +507,9 @@ public void testPoolConnectTimeout(TestContext ctx) {
463
507
public void testNoConnectionLeaks (TestContext ctx ) {
464
508
Async killConnections = ctx .async ();
465
509
PgConnection .connect (vertx , options ).onComplete (ctx .asyncAssertSuccess (conn -> {
466
- Collector <Row , ?, List <Integer >> collector = mapping (row -> row .getInteger (0 ), toList ());
510
+ Collector <Row , ?, List <Boolean >> collector = mapping (row -> row .getBoolean (0 ), toList ());
467
511
String sql = "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND datname = $1" ;
468
- PreparedQuery <SqlResult <List <Integer >>> preparedQuery = conn .preparedQuery (sql ).collecting (collector );
512
+ PreparedQuery <SqlResult <List <Boolean >>> preparedQuery = conn .preparedQuery (sql ).collecting (collector );
469
513
Tuple params = Tuple .of (options .getDatabase ());
470
514
preparedQuery .execute (params ).compose (cf -> conn .close ()).onComplete (ctx .asyncAssertSuccess (v -> killConnections .complete ()));
471
515
}));
0 commit comments