Skip to content

Commit

Permalink
Merge pull request quarkusio#45523 from geoand/quarkusio#45482
Browse files Browse the repository at this point in the history
Fix Mongo health checks
  • Loading branch information
geoand authored Jan 13, 2025
2 parents ec6bf3c + 289cf09 commit 7e76c74
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 17 deletions.
5 changes: 5 additions & 0 deletions extensions/mongodb-client/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-deployment</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.quarkus.mongodb;

import static io.restassured.RestAssured.when;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.concurrent.TimeUnit;

import jakarta.inject.Inject;

import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
Expand Down Expand Up @@ -97,4 +99,11 @@ public void testReactiveClientConfiuration() {
assertThat(clientImpl.getSettings().getReadConcern()).isEqualTo(new ReadConcern(ReadConcernLevel.SNAPSHOT));
assertThat(clientImpl.getSettings().getReadPreference()).isEqualTo(ReadPreference.primary());
}

@Test
public void healthCheck() {
when().get("/q/health/ready")
.then()
.body("status", CoreMatchers.equalTo("UP"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.quarkus.mongodb;

import static io.restassured.RestAssured.when;

import jakarta.inject.Inject;

import org.bson.Document;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.mongodb.MongoClientException;
import com.mongodb.client.MongoClient;

import io.quarkus.test.QuarkusUnitTest;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class NoConnectionHealthCheckTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.overrideConfigKey("quarkus.devservices.enabled", "false")
.overrideConfigKey("quarkus.mongodb.connection-string", "mongodb://localhost:9999")
// timeouts set to the test doesn't take too long to run
.overrideConfigKey("quarkus.mongodb.connect-timeout", "2s")
.overrideConfigKey("quarkus.mongodb.server-selection-timeout", "2s")
.overrideConfigKey("quarkus.mongodb.read-timeout", "2s");

@Inject
MongoClient mongo;

@Order(1) // done to ensure the health check runs before any application code touches the database
@Test
public void healthCheck() {
when().get("/q/health/ready")
.then()
.body("status", CoreMatchers.equalTo("DOWN"));
}

@Order(2)
@Test
public void tryConnection() {
Assertions.assertThrows(MongoClientException.class, () -> {
mongo.getDatabase("admin").runCommand(new Document("ping", 1));
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class MongoHealthCheck implements HealthCheck {

private static final Document COMMAND = new Document("ping", 1);

public void configure(MongodbConfig config) {
public MongoHealthCheck(MongodbConfig config) {
Iterable<InstanceHandle<MongoClient>> handle = Arc.container().select(MongoClient.class, Any.Literal.INSTANCE)
.handles();
Iterable<InstanceHandle<ReactiveMongoClient>> reactiveHandlers = Arc.container()
Expand All @@ -61,7 +61,7 @@ public void configure(MongodbConfig config) {
}
}

config.mongoClientConfigs.forEach(new BiConsumer<String, MongoClientConfig>() {
config.mongoClientConfigs.forEach(new BiConsumer<>() {
@Override
public void accept(String name, MongoClientConfig cfg) {
MongoClient client = getClient(handle, name);
Expand All @@ -76,7 +76,6 @@ public void accept(String name, MongoClientConfig cfg) {
}
}
});

}

private MongoClient getClient(Iterable<InstanceHandle<MongoClient>> handle, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,9 @@
import com.mongodb.event.ConnectionPoolListener;
import com.mongodb.reactivestreams.client.ReactiveContextProvider;

import io.quarkus.arc.Arc;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.credentials.CredentialsProvider;
import io.quarkus.credentials.runtime.CredentialsProviderFinder;
import io.quarkus.mongodb.MongoClientName;
import io.quarkus.mongodb.health.MongoHealthCheck;
import io.quarkus.mongodb.impl.ReactiveMongoClientImpl;
import io.quarkus.mongodb.reactive.ReactiveMongoClient;

Expand Down Expand Up @@ -111,17 +108,6 @@ public MongoClients(MongodbConfig mongodbConfig, MongoClientSupport mongoClientS
Class.forName("sun.net.ext.ExtendedSocketOptions", true, ClassLoader.getSystemClassLoader());
} catch (ClassNotFoundException ignored) {
}

try {
Class.forName("org.eclipse.microprofile.health.HealthCheck");
InstanceHandle<MongoHealthCheck> instance = Arc.container()
.instance(MongoHealthCheck.class, Any.Literal.INSTANCE);
if (instance.isAvailable()) {
instance.get().configure(mongodbConfig);
}
} catch (ClassNotFoundException e) {
// Ignored - no health check
}
}

public MongoClient createMongoClient(String clientName) throws MongoException {
Expand Down

0 comments on commit 7e76c74

Please sign in to comment.