From 7db82dab2fbaf267dad8eee62f492769a8fbdac7 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Tue, 6 Feb 2024 13:15:10 +0100 Subject: [PATCH] ArC: RequestContext - implement the activity check consistently - this is a follow-up of https://github.com/quarkusio/quarkus/pull/38107 --- .../java/io/quarkus/arc/InjectableContext.java | 4 ++-- .../java/io/quarkus/arc/impl/RequestContext.java | 16 +++++++++------- .../optimized/RequestContextInstancesTest.java | 5 ++++- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/InjectableContext.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/InjectableContext.java index 027b5c2bc5ef8..144ef3c0a339c 100644 --- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/InjectableContext.java +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/InjectableContext.java @@ -11,13 +11,13 @@ /** * A context implementing this interface makes it possible to capture and view its state via the {@link ContextState}. - * + *

* It also allows users to destroy all contextual instances within this context. */ public interface InjectableContext extends AlterableContext { /** - * Destroy all existing contextual instances. + * Destroys the current context and all existing contextual instances. */ void destroy(); 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 6ba7414ca9324..5164d84c374f3 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 @@ -64,7 +64,7 @@ public T getIfActive(Contextual contextual, Function, Creat throw Scopes.scopeDoesNotMatchException(this, bean); } RequestContextState ctxState = currentContext.get(); - if (ctxState == null) { + if (!isActive(ctxState)) { // Context is not active! return null; } @@ -102,7 +102,7 @@ public T get(Contextual contextual) { throw Scopes.scopeDoesNotMatchException(this, bean); } RequestContextState state = currentContext.get(); - if (state == null) { + if (!isActive(state)) { throw notActive(); } ContextInstanceHandle instance = (ContextInstanceHandle) state.contextInstances @@ -112,14 +112,17 @@ public T get(Contextual contextual) { @Override public boolean isActive() { - RequestContextState requestContextState = currentContext.get(); - return requestContextState == null ? false : requestContextState.isValid(); + return isActive(currentContext.get()); + } + + private boolean isActive(RequestContextState state) { + return state == null ? false : state.isValid(); } @Override public void destroy(Contextual contextual) { RequestContextState state = currentContext.get(); - if (state == null) { + if (!isActive(state)) { // Context is not active throw notActive(); } @@ -164,8 +167,7 @@ private void traceActivate(ContextState initialState) { @Override public ContextState getState() { RequestContextState state = currentContext.get(); - if (state == null) { - // Thread local not set - context is not active! + if (!isActive(state)) { throw notActive(); } return state; diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/contexts/request/optimized/RequestContextInstancesTest.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/contexts/request/optimized/RequestContextInstancesTest.java index 6ceaa360d3908..42b5269d1ce63 100644 --- a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/contexts/request/optimized/RequestContextInstancesTest.java +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/contexts/request/optimized/RequestContextInstancesTest.java @@ -2,6 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.UUID; import java.util.concurrent.ExecutionException; @@ -14,6 +15,7 @@ import jakarta.annotation.PostConstruct; import jakarta.annotation.PreDestroy; import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.context.ContextNotActiveException; import jakarta.enterprise.context.RequestScoped; import jakarta.inject.Inject; @@ -58,7 +60,8 @@ public void testContext() { InjectableContext appContext = container.getActiveContext(RequestScoped.class); // ContextInstances#removeEach() appContext.destroy(); - assertNotEquals(id2, boom.ping()); + // Request context was invalidated + assertThrows(ContextNotActiveException.class, () -> boom.ping()); container.requestContext().terminate(); }