From 6d8c398ba560392ae2933193635d07a0051753cd Mon Sep 17 00:00:00 2001 From: Matej Novotny Date: Tue, 9 Jan 2024 16:46:48 +0100 Subject: [PATCH] Arc - Decide whether req. context is active based on validity of its ContextState --- .../ReqContextActivationTerminationTest.java | 51 +++++++++++++++++++ .../io/quarkus/arc/impl/RequestContext.java | 3 +- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 extensions/vertx-http/deployment/src/test/java/io/quarkus/vertx/http/router/ReqContextActivationTerminationTest.java diff --git a/extensions/vertx-http/deployment/src/test/java/io/quarkus/vertx/http/router/ReqContextActivationTerminationTest.java b/extensions/vertx-http/deployment/src/test/java/io/quarkus/vertx/http/router/ReqContextActivationTerminationTest.java new file mode 100644 index 0000000000000..fbacffd72cead --- /dev/null +++ b/extensions/vertx-http/deployment/src/test/java/io/quarkus/vertx/http/router/ReqContextActivationTerminationTest.java @@ -0,0 +1,51 @@ +package io.quarkus.vertx.http.router; + +import static org.hamcrest.Matchers.is; + +import jakarta.enterprise.event.Observes; +import jakarta.inject.Singleton; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.arc.Arc; +import io.quarkus.runtime.StartupEvent; +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; +import io.vertx.ext.web.Router; + +/** + * Test is located here so that {@code VertxCurrentContextFactory} is used within req. context implementation. + * See also https://github.com/quarkusio/quarkus/issues/37741 + */ +public class ReqContextActivationTerminationTest { + + @RegisterExtension + static final QuarkusUnitTest config = new QuarkusUnitTest() + .withApplicationRoot((jar) -> jar.addClasses(BeanWithObserver.class)); + + @Test + public void testRoute() { + RestAssured.when().get("/boom").then().statusCode(200).body(is("ok")); + } + + @Singleton + public static class BeanWithObserver { + + private static int counter; + + void observeRouter(@Observes StartupEvent startup, Router router) { + router.get("/boom").handler(ctx -> { + // context starts as inactive; we perform manual activation/termination and assert + Assertions.assertEquals(false, Arc.container().requestContext().isActive()); + Arc.container().requestContext().activate(); + Assertions.assertEquals(true, Arc.container().requestContext().isActive()); + Arc.container().requestContext().terminate(); + Assertions.assertEquals(false, Arc.container().requestContext().isActive()); + ctx.response().setStatusCode(200).end("ok"); + }); + } + + } +} diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/RequestContext.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/RequestContext.java index 762663007603f..0e81d5b5865b4 100644 --- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/RequestContext.java +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/RequestContext.java @@ -112,7 +112,8 @@ public T get(Contextual contextual) { @Override public boolean isActive() { - return currentContext.get() != null; + RequestContextState requestContextState = currentContext.get(); + return requestContextState == null ? false : requestContextState.isValid(); } @Override