Skip to content

Commit ffc21b5

Browse files
Some improvements:
- Found a pool test I could modify (but couldn't validate locally yet) - Changed the code to return the release the connection, which wasn't happening
1 parent 67b5982 commit ffc21b5

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

vertx-pg-client/src/test/java/io/vertx/pgclient/PgPoolTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.vertx.core.Handler;
2323
import io.vertx.core.VertxOptions;
2424
import io.vertx.core.impl.ContextInternal;
25+
import io.vertx.core.impl.NoStackTraceThrowable;
2526
import io.vertx.ext.unit.Async;
2627
import io.vertx.ext.unit.TestContext;
2728
import io.vertx.ext.unit.junit.Repeat;
@@ -592,4 +593,45 @@ private void testConnectionClosedInProvider(TestContext ctx, boolean immediately
592593
}));
593594
}));
594595
}
596+
597+
@Test
598+
public void testConnectionTimeoutWhenExecutingDirectly(TestContext ctx) {
599+
PgPool pool = createPool(options, new PoolOptions().setConnectionTimeout(2).setMaxSize(2));
600+
final Async latch = ctx.async(2);
601+
pool.getConnection(ctx.asyncAssertSuccess(conn -> {
602+
conn
603+
.query("SELECT id, message from immutable")
604+
.execute(ctx.asyncAssertSuccess(rows -> {
605+
ctx.assertEquals(12, rows.size());
606+
latch.countDown();
607+
}));
608+
}));
609+
610+
pool.getConnection(ctx.asyncAssertSuccess(conn -> {
611+
conn
612+
.query("SELECT id, message from immutable")
613+
.execute(ctx.asyncAssertSuccess(rows -> {
614+
ctx.assertEquals(12, rows.size());
615+
latch.countDown();
616+
}));
617+
}));
618+
619+
latch.awaitSuccess();
620+
final long timerId = vertx.setTimer(10000L, id -> {
621+
ctx.fail("Timeout exceeded without completing");
622+
});
623+
//Used both connections
624+
Async async = ctx.async(10);
625+
for (int i = 0; i < 10; i++) {
626+
pool
627+
.query("SELECT id, message from immutable")
628+
.execute(ctx.asyncAssertFailure(t -> {
629+
ctx.assertTrue(t instanceof NoStackTraceThrowable);
630+
ctx.assertEquals("Timeout", t.getMessage());
631+
async.countDown();
632+
}));
633+
}
634+
635+
async.handler(v -> vertx.cancelTimer(timerId));
636+
}
595637
}

vertx-sql-client/src/main/java/io/vertx/sqlclient/impl/PoolImpl.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,20 @@ public Future<SqlConnection> getConnection() {
170170
@Override
171171
public <R> Future<R> schedule(ContextInternal context, CommandBase<R> cmd) {
172172
PromiseInternal<SqlConnectionPool.PooledConnection> promise = context.promise();
173+
//Acquires the connection honoring the pool's connection timeout
173174
acquire(context, connectionTimeout, promise);
174-
return promise.future().compose(pooled -> pooled.schedule(context, cmd));
175+
return promise.future().compose(pooled -> {
176+
//We need to 'init' the connection of close will fail.
177+
pooled.init(pooled);
178+
return pooled.schedule(context, cmd)
179+
.eventually(v -> {
180+
Promise<Void> p = Promise.promise();
181+
pooled.close(pooled, p);
182+
return p.future();
183+
}
184+
);
185+
}
186+
);
175187
}
176188

177189
private void acquire(ContextInternal context, long timeout, Handler<AsyncResult<SqlConnectionPool.PooledConnection>> completionHandler) {

0 commit comments

Comments
 (0)