Skip to content

Commit

Permalink
Improve Mongo health checks
Browse files Browse the repository at this point in the history
- use parallel composition to avoid cumulative timeouts
- allows the user to set the checked database
- only configure health checks if the health is on the classpath
  • Loading branch information
cescoffier committed Mar 30, 2021
1 parent 7a3176d commit 82863e7
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 32 deletions.
5 changes: 5 additions & 0 deletions extensions/mongodb-client/deployment/pom.xml
Expand Up @@ -39,6 +39,11 @@
<artifactId>quarkus-smallrye-metrics-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-health-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
Expand Down
@@ -1,11 +1,14 @@
package io.quarkus.mongodb;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

import javax.enterprise.inject.Any;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.literal.NamedLiteral;
import javax.inject.Inject;

import org.eclipse.microprofile.health.HealthCheckResponse;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -17,6 +20,7 @@

import io.quarkus.arc.Arc;
import io.quarkus.arc.runtime.ClientProxyUnwrapper;
import io.quarkus.mongodb.health.MongoHealthCheck;
import io.quarkus.mongodb.runtime.MongoClientName;
import io.quarkus.test.QuarkusUnitTest;

Expand All @@ -34,6 +38,10 @@ public class DefaultAndNamedMongoClientConfigTest extends MongoWithReplicasTestB
@MongoClientName("cluster2")
MongoClient client2;

@Inject
@Any
MongoHealthCheck health;

private final ClientProxyUnwrapper unwrapper = new ClientProxyUnwrapper();

@AfterEach
Expand All @@ -58,6 +66,14 @@ public void testNamedDataSourceInjection() {
assertThat(Arc.container().instance(MongoClient.class, Default.Literal.INSTANCE).get()).isNotNull();
assertThat(Arc.container().instance(MongoClient.class, NamedLiteral.of("cluster2")).get()).isNotNull();
assertThat(Arc.container().instance(MongoClient.class, NamedLiteral.of("cluster3")).get()).isNull();

org.eclipse.microprofile.health.HealthCheckResponse response = health.call();
assertThat(response.getState()).isEqualTo(HealthCheckResponse.State.UP);
assertThat(response.getData()).isNotEmpty();
assertThat(response.getData().get()).hasSize(2).contains(
entry("<default>", "OK"),
entry("cluster2", "OK"));

}

private void assertProperConnection(MongoClient client, int expectedPort) {
Expand Down
@@ -1,9 +1,14 @@
package io.quarkus.mongodb;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

import java.util.function.BiConsumer;

import javax.enterprise.inject.Any;
import javax.inject.Inject;

import org.eclipse.microprofile.health.HealthCheckResponse;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -12,6 +17,7 @@

import com.mongodb.client.MongoClient;

import io.quarkus.mongodb.health.MongoHealthCheck;
import io.quarkus.mongodb.reactive.ReactiveMongoClient;
import io.quarkus.test.QuarkusUnitTest;

Expand All @@ -28,6 +34,10 @@ public class DefaultMongoClientConfigTest extends MongoWithReplicasTestBase {
@Inject
ReactiveMongoClient reactiveClient;

@Inject
@Any
MongoHealthCheck health;

@AfterEach
void cleanup() {
if (reactiveClient != null) {
Expand All @@ -42,5 +52,25 @@ void cleanup() {
public void testClientInjection() {
assertThat(client.listDatabaseNames().first()).isNotEmpty();
assertThat(reactiveClient.listDatabases().collectItems().first().await().indefinitely()).isNotEmpty();

org.eclipse.microprofile.health.HealthCheckResponse response = health.call();
assertThat(response.getState()).isEqualTo(HealthCheckResponse.State.UP);
assertThat(response.getData()).isNotEmpty();
assertThat(response.getData().get()).hasSize(2).contains(entry("<default-reactive>", "OK"),
entry("<default>", "OK"));

// Stop the database and recheck the health
stopMongoDatabase();

response = health.call();
assertThat(response.getState()).isEqualTo(HealthCheckResponse.State.DOWN);
assertThat(response.getData()).isNotEmpty();
assertThat(response.getData().get()).hasSize(2)
.allSatisfy(new BiConsumer<String, Object>() {
@Override
public void accept(String s, Object o) {
assertThat(o.toString()).startsWith("KO, reason:");
}
});
}
}
@@ -1,12 +1,15 @@
package io.quarkus.mongodb;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

import java.lang.annotation.Annotation;

import javax.enterprise.inject.Any;
import javax.enterprise.inject.Default;
import javax.inject.Inject;

import org.eclipse.microprofile.health.HealthCheckResponse;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -19,6 +22,7 @@
import io.quarkus.arc.Arc;
import io.quarkus.arc.InjectableBean;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.mongodb.health.MongoHealthCheck;
import io.quarkus.test.QuarkusUnitTest;

public class NamedMongoClientConfigTest extends MongoWithReplicasTestBase {
Expand All @@ -36,6 +40,10 @@ public class NamedMongoClientConfigTest extends MongoWithReplicasTestBase {
@MongoClientName("cluster2")
MongoClient client2;

@Inject
@Any
MongoHealthCheck health;

@AfterEach
void cleanup() {
if (client != null) {
Expand All @@ -52,6 +60,15 @@ public void testNamedDataSourceInjection() {
assertThat(client2.listDatabases().first()).isNotEmpty();

assertNoDefaultClient();

checkHealth();
}

public void checkHealth() {
org.eclipse.microprofile.health.HealthCheckResponse response = health.call();
assertThat(response.getState()).isEqualTo(HealthCheckResponse.State.UP);
assertThat(response.getData()).isNotEmpty();
assertThat(response.getData().get()).hasSize(2).contains(entry("cluster1", "OK"), entry("cluster2", "OK"));
}

private void assertNoDefaultClient() {
Expand Down
@@ -1,13 +1,16 @@
package io.quarkus.mongodb;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import javax.enterprise.inject.Any;
import javax.enterprise.inject.Default;
import javax.inject.Inject;

import org.eclipse.microprofile.health.HealthCheckResponse;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -22,6 +25,7 @@
import io.quarkus.arc.InjectableBean;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.arc.runtime.ClientProxyUnwrapper;
import io.quarkus.mongodb.health.MongoHealthCheck;
import io.quarkus.mongodb.impl.ReactiveMongoClientImpl;
import io.quarkus.mongodb.reactive.ReactiveMongoClient;
import io.quarkus.test.QuarkusUnitTest;
Expand All @@ -41,6 +45,10 @@ public class NamedReactiveMongoClientConfigTest extends MongoWithReplicasTestBas
@MongoClientName("cluster2")
ReactiveMongoClient client2;

@Inject
@Any
MongoHealthCheck health;

private final ClientProxyUnwrapper unwrapper = new ClientProxyUnwrapper();

@AfterEach
Expand All @@ -62,6 +70,15 @@ public void testNamedDataSourceInjection() {
assertThat(client2.listDatabases().collectItems().first().await().indefinitely()).isNotEmpty();

assertNoDefaultClient();

checkHealth();
}

public void checkHealth() {
org.eclipse.microprofile.health.HealthCheckResponse response = health.call();
assertThat(response.getState()).isEqualTo(HealthCheckResponse.State.UP);
assertThat(response.getData()).isNotEmpty();
assertThat(response.getData().get()).hasSize(2).contains(entry("cluster1", "OK"), entry("cluster2", "OK"));
}

private void assertProperConnection(ReactiveMongoClient client, int expectedPort) {
Expand Down

0 comments on commit 82863e7

Please sign in to comment.