-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unblock SmallRye Health exposed routes
- Loading branch information
Showing
12 changed files
with
155 additions
and
55 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
106 changes: 106 additions & 0 deletions
106
...lth/deployment/src/test/java/io/quarkus/smallrye/health/test/BlockingNonBlockingTest.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,106 @@ | ||
package io.quarkus.smallrye.health.test; | ||
|
||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
||
import java.util.logging.Level; | ||
import java.util.logging.LogRecord; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
|
||
import org.eclipse.microprofile.health.HealthCheck; | ||
import org.eclipse.microprofile.health.HealthCheckResponse; | ||
import org.eclipse.microprofile.health.Liveness; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.MethodOrderer; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestMethodOrder; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.test.InMemoryLogHandler; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
import io.restassured.parsing.Parser; | ||
import io.smallrye.health.SmallRyeHealthReporter; | ||
|
||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
public class BlockingNonBlockingTest { | ||
|
||
private static final java.util.logging.Logger rootLogger = java.util.logging.LogManager.getLogManager() | ||
.getLogger("io.vertx.core"); | ||
private static final InMemoryLogHandler inMemoryLogHandler = new InMemoryLogHandler( | ||
record -> record.getLevel().intValue() >= java.util.logging.Level.WARNING.intValue()); | ||
|
||
@BeforeEach | ||
public void setLogHandler() { | ||
inMemoryLogHandler.getRecords().clear(); | ||
rootLogger.addHandler(inMemoryLogHandler); | ||
} | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(BlockingHealthCheck.class, SchedulerBean.class) | ||
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")); | ||
|
||
@Test | ||
public void testRegisterHealthOnBlockingThreadStep1() { | ||
// wait for the initial scheduler call to finish | ||
try { | ||
Thread.sleep(5000); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
try { | ||
RestAssured.defaultParser = Parser.JSON; | ||
// repeat the call a few times since the block isn't always logged | ||
for (int i = 0; i < 3; i++) { | ||
RestAssured.when().get("/q/health").then() | ||
.body("status", is("UP"), | ||
"checks.status", contains("UP"), | ||
"checks.name", contains("blocking")); | ||
} | ||
} finally { | ||
RestAssured.reset(); | ||
} | ||
|
||
if (!inMemoryLogHandler.getRecords().isEmpty()) { | ||
LogRecord logRecord = inMemoryLogHandler.getRecords().get(0); | ||
assertEquals(Level.WARNING, logRecord.getLevel()); | ||
assertFalse(logRecord.getMessage().contains("has been blocked for"), | ||
"The blocking health check ran on eventloop thread"); | ||
} | ||
} | ||
|
||
@Liveness | ||
static final class BlockingHealthCheck implements HealthCheck { | ||
@Override | ||
public HealthCheckResponse call() { | ||
// block for 3s which is more than allowed default blocking duration of eventloop (2s) | ||
try { | ||
Thread.sleep(3000); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return HealthCheckResponse.up("blocking"); | ||
} | ||
} | ||
|
||
@ApplicationScoped | ||
static final class SchedulerBean { | ||
|
||
@Inject | ||
SmallRyeHealthReporter smallRyeHealthReporter; | ||
|
||
@Scheduled(every = "20s") | ||
public void registerHealth() { | ||
smallRyeHealthReporter.getHealth(); | ||
} | ||
} | ||
} |
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
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
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