Skip to content

Commit 356b81f

Browse files
committed
Improve racy shared pool test
1 parent 27f8a74 commit 356b81f

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313

1414
import io.vertx.core.AbstractVerticle;
1515
import io.vertx.core.DeploymentOptions;
16+
import io.vertx.core.Future;
1617
import io.vertx.core.Promise;
1718
import io.vertx.core.Vertx;
1819
import io.vertx.ext.unit.Async;
1920
import io.vertx.ext.unit.TestContext;
2021
import io.vertx.ext.unit.junit.VertxUnitRunner;
2122
import io.vertx.sqlclient.PoolOptions;
23+
import io.vertx.sqlclient.SqlConnection;
2224
import org.junit.After;
2325
import org.junit.Before;
2426
import org.junit.Test;
@@ -79,14 +81,23 @@ public void start() {
7981
latch.awaitSuccess(20_000);
8082
vertx.undeploy(deployment.get())
8183
.compose(v -> PgConnection.connect(vertx, options))
82-
.compose(conn -> conn.query(COUNT_CONNECTIONS_QUERY)
83-
.execute().compose(res -> {
84-
int num = res.iterator().next().getInteger(0);
85-
return conn.close().map(num);
86-
})
87-
).onComplete(ctx.asyncAssertSuccess(num -> {
88-
ctx.assertEquals(1, num);
89-
}));
84+
.compose(conn -> waitUntilConnCountIs(conn, 10, 1)
85+
).onComplete(ctx.asyncAssertSuccess());
86+
}
87+
88+
private Future<Void> waitUntilConnCountIs(SqlConnection conn, int remaining, int expectedCount) {
89+
if (remaining > 0) {
90+
return conn.query(COUNT_CONNECTIONS_QUERY).execute().compose(res -> {
91+
int num = res.iterator().next().getInteger(0);
92+
if (num == expectedCount) {
93+
return Future.succeededFuture();
94+
} else {
95+
return waitUntilConnCountIs(conn, remaining - 1, expectedCount);
96+
}
97+
});
98+
} else {
99+
return Future.failedFuture("Could not count");
100+
}
90101
}
91102

92103
@Test

0 commit comments

Comments
 (0)