Skip to content

Commit

Permalink
Merge pull request #38107 from manovotn/issue37741
Browse files Browse the repository at this point in the history
Arc - Decide whether req. context is active based on validity of its ContextState
  • Loading branch information
mkouba authored Jan 12, 2024
2 parents 96a7361 + 6d8c398 commit b776e87
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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");
});
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public <T> T get(Contextual<T> contextual) {

@Override
public boolean isActive() {
return currentContext.get() != null;
RequestContextState requestContextState = currentContext.get();
return requestContextState == null ? false : requestContextState.isValid();
}

@Override
Expand Down

0 comments on commit b776e87

Please sign in to comment.