forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add runtime configuration property quarkus.datasource.active
- Loading branch information
Showing
54 changed files
with
1,163 additions
and
246 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
51 changes: 51 additions & 0 deletions
51
...l/deployment/src/test/java/io/quarkus/agroal/test/AgroalMetricsConfigActiveFalseTest.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,51 @@ | ||
package io.quarkus.agroal.test; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.eclipse.microprofile.metrics.Counter; | ||
import org.eclipse.microprofile.metrics.Gauge; | ||
import org.eclipse.microprofile.metrics.MetricID; | ||
import org.eclipse.microprofile.metrics.MetricRegistry; | ||
import org.eclipse.microprofile.metrics.Tag; | ||
import org.eclipse.microprofile.metrics.annotation.RegistryType; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class AgroalMetricsConfigActiveFalseTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withConfigurationResource("application-metrics-enabled.properties") | ||
.overrideConfigKey("quarkus.datasource.active", "false") | ||
.overrideConfigKey("quarkus.datasource.ds1.active", "false"); | ||
|
||
@Inject | ||
@RegistryType(type = MetricRegistry.Type.VENDOR) | ||
MetricRegistry registry; | ||
|
||
@Test | ||
public void testMetricsOfDefaultDS() { | ||
Counter acquireCount = registry.getCounters() | ||
.get(new MetricID("agroal.acquire.count", new Tag("datasource", "default"))); | ||
Gauge<?> maxUsed = registry.getGauges() | ||
.get(new MetricID("agroal.max.used.count", new Tag("datasource", "default"))); | ||
|
||
Assertions.assertNull(acquireCount, "Agroal metrics should not be registered for deactivated datasources eagerly"); | ||
Assertions.assertNull(maxUsed, "Agroal metrics should not be registered for deactivated datasources eagerly"); | ||
} | ||
|
||
@Test | ||
public void testMetricsOfDs1() { | ||
Counter acquireCount = registry.getCounters().get(new MetricID("agroal.acquire.count", | ||
new Tag("datasource", "ds1"))); | ||
Gauge<?> maxUsed = registry.getGauges().get(new MetricID("agroal.max.used.count", | ||
new Tag("datasource", "ds1"))); | ||
|
||
Assertions.assertNull(acquireCount, "Agroal metrics should not be registered for deactivated datasources eagerly"); | ||
Assertions.assertNull(maxUsed, "Agroal metrics should not be registered for deactivated datasources eagerly"); | ||
} | ||
|
||
} |
91 changes: 91 additions & 0 deletions
91
...ployment/src/test/java/io/quarkus/agroal/test/ConfigActiveFalseDefaultDatasourceTest.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,91 @@ | ||
package io.quarkus.agroal.test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.sql.SQLException; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.inject.CreationException; | ||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.agroal.api.AgroalDataSource; | ||
import io.quarkus.arc.Arc; | ||
import io.quarkus.runtime.configuration.ConfigurationException; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ConfigActiveFalseDefaultDatasourceTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.overrideConfigKey("quarkus.datasource.active", "false"); | ||
|
||
@Inject | ||
MyBean myBean; | ||
|
||
@Test | ||
public void dataSource() { | ||
DataSource ds = Arc.container().instance(DataSource.class).get(); | ||
|
||
// The bean is always available to be injected during static init | ||
// since we don't know whether the datasource will be active at runtime. | ||
// So the bean cannot be null. | ||
assertThat(ds).isNotNull(); | ||
// However, any attempt to use it at runtime will fail. | ||
assertThatThrownBy(() -> ds.getConnection()) | ||
.isInstanceOf(RuntimeException.class) | ||
.cause() | ||
.isInstanceOf(ConfigurationException.class) | ||
.hasMessageContainingAll("Datasource '<default>' was deactivated through configuration properties.", | ||
"To solve this, avoid accessing this datasource at runtime, for instance by deactivating consumers (persistence units, ...).", | ||
"Alternatively, activate the datasource by setting configuration property 'quarkus.datasource.active'" | ||
+ " to 'true' and configure datasource '<default>'", | ||
"Refer to https://quarkus.io/guides/datasource for guidance."); | ||
} | ||
|
||
@Test | ||
public void agroalDataSource() { | ||
AgroalDataSource ds = Arc.container().instance(AgroalDataSource.class).get(); | ||
|
||
// The bean is always available to be injected during static init | ||
// since we don't know whether the datasource will be active at runtime. | ||
// So the bean cannot be null. | ||
assertThat(ds).isNotNull(); | ||
// However, any attempt to use it at runtime will fail. | ||
assertThatThrownBy(() -> ds.getConnection()) | ||
.isInstanceOf(RuntimeException.class) | ||
.cause() | ||
.isInstanceOf(ConfigurationException.class) | ||
.hasMessageContainingAll("Datasource '<default>' was deactivated through configuration properties.", | ||
"To solve this, avoid accessing this datasource at runtime, for instance by deactivating consumers (persistence units, ...).", | ||
"Alternatively, activate the datasource by setting configuration property 'quarkus.datasource.active'" | ||
+ " to 'true' and configure datasource '<default>'", | ||
"Refer to https://quarkus.io/guides/datasource for guidance."); | ||
} | ||
|
||
@Test | ||
public void injectedBean() { | ||
assertThatThrownBy(() -> myBean.useDatasource()) | ||
.isInstanceOf(CreationException.class) | ||
.hasMessageContainingAll("Datasource '<default>' was deactivated through configuration properties.", | ||
"To solve this, avoid accessing this datasource at runtime, for instance by deactivating consumers (persistence units, ...).", | ||
"Alternatively, activate the datasource by setting configuration property 'quarkus.datasource.active'" | ||
+ " to 'true' and configure datasource '<default>'", | ||
"Refer to https://quarkus.io/guides/datasource for guidance."); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyBean { | ||
@Inject | ||
DataSource ds; | ||
|
||
public void useDatasource() throws SQLException { | ||
ds.getConnection(); | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...deployment/src/test/java/io/quarkus/agroal/test/ConfigActiveFalseNamedDatasourceTest.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,96 @@ | ||
package io.quarkus.agroal.test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import java.sql.SQLException; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.inject.CreationException; | ||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.quarkus.runtime.configuration.ConfigurationException; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ConfigActiveFalseNamedDatasourceTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.overrideConfigKey("quarkus.datasource.users.active", "false") | ||
// We need at least one build-time property for the datasource, | ||
// otherwise it's considered unconfigured at build time... | ||
.overrideConfigKey("quarkus.datasource.users.db-kind", "h2"); | ||
|
||
@Inject | ||
MyBean myBean; | ||
|
||
@Test | ||
public void dataSource() { | ||
DataSource ds = Arc.container().instance(DataSource.class, | ||
new io.quarkus.agroal.DataSource.DataSourceLiteral("users")).get(); | ||
|
||
// The bean is always available to be injected during static init | ||
// since we don't know whether the datasource will be active at runtime. | ||
// So the bean cannot be null. | ||
assertThat(ds).isNotNull(); | ||
// However, any attempt to use it at runtime will fail. | ||
assertThatThrownBy(() -> ds.getConnection()) | ||
.isInstanceOf(RuntimeException.class) | ||
.cause() | ||
.isInstanceOf(ConfigurationException.class) | ||
.hasMessageContainingAll("Datasource 'users' was deactivated through configuration properties.", | ||
"To solve this, avoid accessing this datasource at runtime, for instance by deactivating consumers (persistence units, ...).", | ||
"Alternatively, activate the datasource by setting configuration property 'quarkus.datasource.\"users\".active'" | ||
+ " to 'true' and configure datasource 'users'", | ||
"Refer to https://quarkus.io/guides/datasource for guidance."); | ||
} | ||
|
||
@Test | ||
public void agroalDataSource() { | ||
DataSource ds = Arc.container().instance(DataSource.class, | ||
new io.quarkus.agroal.DataSource.DataSourceLiteral("users")).get(); | ||
|
||
// The bean is always available to be injected during static init | ||
// since we don't know whether the datasource will be active at runtime. | ||
// So the bean cannot be null. | ||
assertThat(ds).isNotNull(); | ||
// However, any attempt to use it at runtime will fail. | ||
assertThatThrownBy(() -> ds.getConnection()) | ||
.isInstanceOf(RuntimeException.class) | ||
.cause() | ||
.isInstanceOf(ConfigurationException.class) | ||
.hasMessageContainingAll("Datasource 'users' was deactivated through configuration properties.", | ||
"To solve this, avoid accessing this datasource at runtime, for instance by deactivating consumers (persistence units, ...).", | ||
"Alternatively, activate the datasource by setting configuration property 'quarkus.datasource.\"users\".active'" | ||
+ " to 'true' and configure datasource 'users'", | ||
"Refer to https://quarkus.io/guides/datasource for guidance."); | ||
} | ||
|
||
@Test | ||
public void injectedBean() { | ||
assertThatThrownBy(() -> myBean.useDatasource()) | ||
.isInstanceOf(CreationException.class) | ||
.hasMessageContainingAll("Datasource 'users' was deactivated through configuration properties.", | ||
"To solve this, avoid accessing this datasource at runtime, for instance by deactivating consumers (persistence units, ...).", | ||
"Alternatively, activate the datasource by setting configuration property 'quarkus.datasource.\"users\".active'" | ||
+ " to 'true' and configure datasource 'users'", | ||
"Refer to https://quarkus.io/guides/datasource for guidance."); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyBean { | ||
@Inject | ||
@io.quarkus.agroal.DataSource("users") | ||
DataSource ds; | ||
|
||
public void useDatasource() throws SQLException { | ||
ds.getConnection(); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...deployment/src/test/java/io/quarkus/agroal/test/DataSourceHealthCheckConfigFalseTest.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,39 @@ | ||
package io.quarkus.agroal.test; | ||
|
||
import org.hamcrest.CoreMatchers; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class DataSourceHealthCheckConfigFalseTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot(jar -> jar.addAsResource(new StringAsset(""" | ||
quarkus.datasource.db-kind=h2 | ||
quarkus.datasource.username=username-default | ||
quarkus.datasource.jdbc.url=jdbc:h2:tcp://localhost/mem:default | ||
quarkus.datasource.jdbc.max-size=13 | ||
# this data source is broken, but will be deactivated, | ||
# so the overall check should pass | ||
quarkus.datasource.brokenDS.db-kind=h2 | ||
quarkus.datasource.brokenDS.jdbc.url=BROKEN | ||
quarkus.datasource.health.enabled=true | ||
quarkus.datasource.brokenDS.active=false | ||
"""), | ||
"application.properties")); | ||
|
||
@Test | ||
public void testDataSourceHealthCheckExclusion() { | ||
RestAssured.when().get("/q/health/ready") | ||
.then() | ||
.body("status", CoreMatchers.equalTo("UP")); | ||
} | ||
|
||
} |
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
Oops, something went wrong.