-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #508 from HerrDerb/improve-context-based-healthchecks
Improve context based healthchecks
- Loading branch information
Showing
8 changed files
with
180 additions
and
29 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
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
23 changes: 23 additions & 0 deletions
23
testsuite/experimental/src/test/java/io/smallrye/health/deployment/ContextHealthCheck.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,23 @@ | ||
package io.smallrye.health.deployment; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
|
||
import org.eclipse.microprofile.health.HealthCheck; | ||
import org.eclipse.microprofile.health.HealthCheckResponse; | ||
|
||
import io.smallrye.health.api.Wellness; | ||
|
||
@Wellness | ||
@ApplicationScoped | ||
public class ContextHealthCheck implements HealthCheck { | ||
|
||
@Inject | ||
ContextInfo context; | ||
|
||
@Override | ||
public HealthCheckResponse call() { | ||
return HealthCheckResponse.up(context.getValue()); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
testsuite/experimental/src/test/java/io/smallrye/health/deployment/ContextInfo.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,17 @@ | ||
package io.smallrye.health.deployment; | ||
|
||
import jakarta.enterprise.context.RequestScoped; | ||
|
||
@RequestScoped | ||
public class ContextInfo { | ||
|
||
private String value; | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
testsuite/experimental/src/test/java/io/smallrye/health/deployment/RequestFilter.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,29 @@ | ||
package io.smallrye.health.deployment; | ||
|
||
import java.io.IOException; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.annotation.WebFilter; | ||
import jakarta.servlet.http.HttpFilter; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
@WebFilter(urlPatterns = "*", filterName = "ContextInfoFilter") | ||
public class RequestFilter extends HttpFilter { | ||
|
||
public static final String TEST_CONTEXT_HEADER = "X-CONTEXT"; | ||
|
||
@Inject | ||
ContextInfo contextInfo; | ||
|
||
@Override | ||
protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) | ||
throws IOException, ServletException { | ||
var value = req.getHeader(TEST_CONTEXT_HEADER); | ||
contextInfo.setValue(value); | ||
super.doFilter(req, res, chain); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...suite/experimental/src/test/java/io/smallrye/health/test/ContextPropagatedHealthTest.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.smallrye.health.test; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.container.test.api.RunAsClient; | ||
import org.jboss.shrinkwrap.api.Archive; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import io.smallrye.health.deployment.ContextHealthCheck; | ||
import io.smallrye.health.deployment.ContextInfo; | ||
import io.smallrye.health.deployment.RequestFilter; | ||
import io.smallrye.mutiny.Uni; | ||
import io.smallrye.mutiny.infrastructure.Infrastructure; | ||
|
||
@RunAsClient | ||
class ContextPropagatedHealthTest extends TCKBase { | ||
|
||
@Deployment | ||
public static Archive<WebArchive> getDeployment() { | ||
return DeploymentUtils | ||
.createWarFileWithClasses(ContextPropagatedHealthTest.class.getSimpleName(), | ||
ContextHealthCheck.class, ContextInfo.class, RequestFilter.class, TCKBase.class) | ||
.addAsManifestResource( | ||
new StringAsset("io.smallrye.health.context.propagation=true"), | ||
"microprofile-config.properties"); | ||
} | ||
|
||
// got to repeat this test a couple of times to trigger potential race condition issue at least once. | ||
@Test(invocationCount = 20) | ||
void twoParallelRequests_shouldNotMixupContext() { | ||
String contextValue = "test-value"; | ||
String otherContextValue = "other-test-value"; | ||
Uni<Response> response = Uni.createFrom().voidItem().emitOn(Infrastructure.getDefaultExecutor()).onItem() | ||
.transform(v -> getUrlHealthContents(Map.of(RequestFilter.TEST_CONTEXT_HEADER, contextValue))); | ||
Uni<Response> otherResponse = Uni.createFrom().voidItem().emitOn(Infrastructure.getDefaultExecutor()).onItem() | ||
.transform(v -> getUrlHealthContents(Map.of(RequestFilter.TEST_CONTEXT_HEADER, otherContextValue))); | ||
|
||
List<Response> responses = Uni.join().all(response, otherResponse).andCollectFailures().await().indefinitely(); | ||
|
||
String responseBody = responses.get(0).getBody().orElseThrow(); | ||
String otherResponseBody = responses.get(1).getBody().orElseThrow(); | ||
Assert.assertNotEquals(responseBody, otherResponseBody); | ||
Assert.assertTrue(responseBody.contains(contextValue), responseBody); | ||
Assert.assertTrue(otherResponseBody.contains(otherContextValue), otherResponseBody); | ||
} | ||
} |
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