Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reproducer for quarkus/issues/15025: Quarkus keeps dead database connect
- Loading branch information
Showing
6 changed files
with
134 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...-and-HV/src/test/java/io/quarkus/qe/hibernate/connections/ConnectionPoolHandlingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package io.quarkus.qe.hibernate.connections; | ||
|
||
import static io.restassured.RestAssured.when; | ||
import static org.hamcrest.Matchers.containsString; | ||
|
||
import java.time.Duration; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.awaitility.Awaitility; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.qe.hibernate.resources.CustomH2DatabaseTestResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
/** | ||
* Reproducer for https://github.com/quarkusio/quarkus/issues/15025. | ||
*/ | ||
@QuarkusTest | ||
public class ConnectionPoolHandlingTest { | ||
|
||
private static final String EXPECTED_COUNTER = "vendor_agroal_available_count{datasource=\"default\"} %s.0"; | ||
|
||
@ConfigProperty(name = "quarkus.datasource.jdbc.min-size") | ||
int minConnections; | ||
|
||
CustomH2DatabaseTestResource database; | ||
Thread restartDatabaseJob; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
restartDatabaseJob = new Thread(this::restartDatabaseIndefinitely); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
if (restartDatabaseJob == null) { | ||
restartDatabaseJob.interrupt(); | ||
} | ||
} | ||
|
||
@Test | ||
public void connectionPoolShouldRemoveDeadDatabaseConnections() throws InterruptedException { | ||
whenRestartDatabaseManyTimes(); | ||
thenVendorAgroalAvailableCountShouldNeverBeUpToMinConnections(); | ||
} | ||
|
||
private void whenRestartDatabaseManyTimes() { | ||
restartDatabaseJob.start(); | ||
} | ||
|
||
private void thenVendorAgroalAvailableCountShouldNeverBeUpToMinConnections() { | ||
// Wait 4 seconds to start checking and up to 15 seconds to verify the assertion (as the counter can increase over time) | ||
Awaitility.await().pollDelay(Duration.ofSeconds(8)).atMost(Duration.ofSeconds(30)) | ||
.untilAsserted(() -> { | ||
when().get("/q/metrics").then().statusCode(HttpStatus.SC_OK) | ||
.and().body(containsString(String.format(EXPECTED_COUNTER, minConnections))); | ||
}); | ||
|
||
} | ||
|
||
private void restartDatabaseIndefinitely() { | ||
try { | ||
while (true) { | ||
database.stop(); | ||
database.start(); | ||
Thread.sleep(1000); | ||
// call health check to acquire the new connection | ||
when().get("/q/health"); | ||
} | ||
|
||
} catch (InterruptedException e) { | ||
// stopped in tearDown | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...-and-HV/src/test/java/io/quarkus/qe/hibernate/resources/CustomH2DatabaseTestResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.quarkus.qe.hibernate.resources; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
import io.quarkus.test.h2.H2DatabaseTestResource; | ||
|
||
public class CustomH2DatabaseTestResource extends H2DatabaseTestResource { | ||
|
||
@Override | ||
public void inject(Object testInstance) { | ||
super.inject(testInstance); | ||
|
||
Class<?> c = testInstance.getClass(); | ||
while (c != Object.class) { | ||
for (Field f : c.getDeclaredFields()) { | ||
if (f.getType().isAssignableFrom(CustomH2DatabaseTestResource.class)) { | ||
setFieldValue(f, testInstance, this); | ||
} | ||
} | ||
|
||
c = c.getSuperclass(); | ||
} | ||
} | ||
|
||
private void setFieldValue(Field f, Object testInstance, Object value) { | ||
try { | ||
f.setAccessible(true); | ||
f.set(testInstance, value); | ||
return; | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
004-quarkus-HHH-and-HV/src/test/java/io/quarkus/qe/hibernate/validator/TestResources.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package io.quarkus.qe.hibernate.validator; | ||
|
||
import io.quarkus.qe.hibernate.resources.CustomH2DatabaseTestResource; | ||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.h2.H2DatabaseTestResource; | ||
|
||
@QuarkusTestResource(H2DatabaseTestResource.class) | ||
@QuarkusTestResource(CustomH2DatabaseTestResource.class) | ||
public class TestResources { | ||
} |