From 9ff2f52e9ef59493204c19bbb8f5f2cd0bdece21 Mon Sep 17 00:00:00 2001 From: xstefank Date: Fri, 2 Aug 2024 12:22:36 +0200 Subject: [PATCH] Fix a race condition in ReactiveDatasourceHealthCheck data field population --- .../ReactiveDatasourceHealthCheck.java | 3 +- .../DataSourceHealthCheckPayloadTest.java | 28 +++++++++++++++++++ .../DataSourceHealthCheckPayloadTest.java | 28 +++++++++++++++++++ .../DataSourceHealthCheckPayloadTest.java | 28 +++++++++++++++++++ .../DataSourceHealthCheckPayloadTest.java | 28 +++++++++++++++++++ 5 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 extensions/reactive-mssql-client/deployment/src/test/java/io/quarkus/reactive/mssql/client/DataSourceHealthCheckPayloadTest.java create mode 100644 extensions/reactive-mysql-client/deployment/src/test/java/io/quarkus/reactive/mysql/client/DataSourceHealthCheckPayloadTest.java create mode 100644 extensions/reactive-oracle-client/deployment/src/test/java/io/quarkus/reactive/oracle/client/DataSourceHealthCheckPayloadTest.java create mode 100644 extensions/reactive-pg-client/deployment/src/test/java/io/quarkus/reactive/pg/client/DataSourceHealthCheckPayloadTest.java diff --git a/extensions/reactive-datasource/runtime/src/main/java/io/quarkus/reactive/datasource/runtime/ReactiveDatasourceHealthCheck.java b/extensions/reactive-datasource/runtime/src/main/java/io/quarkus/reactive/datasource/runtime/ReactiveDatasourceHealthCheck.java index b0d804266c96b9..840dec97bd7e7b 100644 --- a/extensions/reactive-datasource/runtime/src/main/java/io/quarkus/reactive/datasource/runtime/ReactiveDatasourceHealthCheck.java +++ b/extensions/reactive-datasource/runtime/src/main/java/io/quarkus/reactive/datasource/runtime/ReactiveDatasourceHealthCheck.java @@ -76,7 +76,6 @@ public HealthCheckResponse call() { //20 seconds is rather high, but using just 10 is often not enough on slow CI //systems, especially if the connections have to be established for the first time. databaseConnectionAttempt.get(20, TimeUnit.SECONDS); - builder.withData(dataSourceName, "UP"); } catch (RuntimeException | ExecutionException exception) { operationsError(dataSourceName, exception); builder.down(); @@ -105,6 +104,8 @@ private void checkFailure(AsyncResult> ar, HealthCheckResponseBuilde operationsError(dataSourceName, ar.cause()); builder.down(); builder.withData(dataSourceName, "down - connection failed: " + ar.cause().getMessage()); + } else { + builder.withData(dataSourceName, "UP"); } } diff --git a/extensions/reactive-mssql-client/deployment/src/test/java/io/quarkus/reactive/mssql/client/DataSourceHealthCheckPayloadTest.java b/extensions/reactive-mssql-client/deployment/src/test/java/io/quarkus/reactive/mssql/client/DataSourceHealthCheckPayloadTest.java new file mode 100644 index 00000000000000..048fef6f85bd73 --- /dev/null +++ b/extensions/reactive-mssql-client/deployment/src/test/java/io/quarkus/reactive/mssql/client/DataSourceHealthCheckPayloadTest.java @@ -0,0 +1,28 @@ +package io.quarkus.reactive.mssql.client; + +import org.hamcrest.CoreMatchers; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class DataSourceHealthCheckPayloadTest { + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest() + .withEmptyApplication() + .withConfigurationResource("application-default-datasource.properties") + .overrideConfigKey("quarkus.datasource.health.enabled", "true"); + + @Test + public void testDataSourceHealthCheckExclusion() { + RestAssured.when().get("/q/health/ready") + .then() + .body("status", CoreMatchers.equalTo("DOWN")) + .body("checks.data", CoreMatchers + .hasItem(Matchers.hasValue(CoreMatchers.containsString("down - connection failed")))); + } + +} diff --git a/extensions/reactive-mysql-client/deployment/src/test/java/io/quarkus/reactive/mysql/client/DataSourceHealthCheckPayloadTest.java b/extensions/reactive-mysql-client/deployment/src/test/java/io/quarkus/reactive/mysql/client/DataSourceHealthCheckPayloadTest.java new file mode 100644 index 00000000000000..df0864fa2f9069 --- /dev/null +++ b/extensions/reactive-mysql-client/deployment/src/test/java/io/quarkus/reactive/mysql/client/DataSourceHealthCheckPayloadTest.java @@ -0,0 +1,28 @@ +package io.quarkus.reactive.mysql.client; + +import org.hamcrest.CoreMatchers; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class DataSourceHealthCheckPayloadTest { + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest() + .withEmptyApplication() + .withConfigurationResource("application-default-datasource.properties") + .overrideConfigKey("quarkus.datasource.health.enabled", "true"); + + @Test + public void testDataSourceHealthCheckExclusion() { + RestAssured.when().get("/q/health/ready") + .then() + .body("status", CoreMatchers.equalTo("DOWN")) + .body("checks.data", CoreMatchers + .hasItem(Matchers.hasValue(CoreMatchers.containsString("down - connection failed")))); + } + +} diff --git a/extensions/reactive-oracle-client/deployment/src/test/java/io/quarkus/reactive/oracle/client/DataSourceHealthCheckPayloadTest.java b/extensions/reactive-oracle-client/deployment/src/test/java/io/quarkus/reactive/oracle/client/DataSourceHealthCheckPayloadTest.java new file mode 100644 index 00000000000000..dda4f0775334cb --- /dev/null +++ b/extensions/reactive-oracle-client/deployment/src/test/java/io/quarkus/reactive/oracle/client/DataSourceHealthCheckPayloadTest.java @@ -0,0 +1,28 @@ +package io.quarkus.reactive.oracle.client; + +import org.hamcrest.CoreMatchers; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class DataSourceHealthCheckPayloadTest { + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest() + .withEmptyApplication() + .withConfigurationResource("application-default-datasource.properties") + .overrideConfigKey("quarkus.datasource.health.enabled", "true"); + + @Test + public void testDataSourceHealthCheckExclusion() { + RestAssured.when().get("/q/health/ready") + .then() + .body("status", CoreMatchers.equalTo("DOWN")) + .body("checks.data", CoreMatchers + .hasItem(Matchers.hasValue(CoreMatchers.containsString("down - connection failed")))); + } + +} diff --git a/extensions/reactive-pg-client/deployment/src/test/java/io/quarkus/reactive/pg/client/DataSourceHealthCheckPayloadTest.java b/extensions/reactive-pg-client/deployment/src/test/java/io/quarkus/reactive/pg/client/DataSourceHealthCheckPayloadTest.java new file mode 100644 index 00000000000000..de4cadb6307b26 --- /dev/null +++ b/extensions/reactive-pg-client/deployment/src/test/java/io/quarkus/reactive/pg/client/DataSourceHealthCheckPayloadTest.java @@ -0,0 +1,28 @@ +package io.quarkus.reactive.pg.client; + +import org.hamcrest.CoreMatchers; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class DataSourceHealthCheckPayloadTest { + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest() + .withEmptyApplication() + .withConfigurationResource("application-default-datasource.properties") + .overrideConfigKey("quarkus.datasource.health.enabled", "true"); + + @Test + public void testDataSourceHealthCheckExclusion() { + RestAssured.when().get("/q/health/ready") + .then() + .body("status", CoreMatchers.equalTo("DOWN")) + .body("checks.data", CoreMatchers + .hasItem(Matchers.hasValue(CoreMatchers.containsString("down - connection failed")))); + } + +}