Skip to content

Commit

Permalink
ArC: RequestContext - implement the activity check consistently
Browse files Browse the repository at this point in the history
- this is a follow-up of quarkusio#38107
  • Loading branch information
mkouba committed Feb 6, 2024
1 parent 99d258c commit 7db82da
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

/**
* A context implementing this interface makes it possible to capture and view its state via the {@link ContextState}.
*
* <p>
* 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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public <T> T getIfActive(Contextual<T> contextual, Function<Contextual<T>, Creat
throw Scopes.scopeDoesNotMatchException(this, bean);
}
RequestContextState ctxState = currentContext.get();
if (ctxState == null) {
if (!isActive(ctxState)) {
// Context is not active!
return null;
}
Expand Down Expand Up @@ -102,7 +102,7 @@ public <T> T get(Contextual<T> contextual) {
throw Scopes.scopeDoesNotMatchException(this, bean);
}
RequestContextState state = currentContext.get();
if (state == null) {
if (!isActive(state)) {
throw notActive();
}
ContextInstanceHandle<T> instance = (ContextInstanceHandle<T>) state.contextInstances
Expand All @@ -112,14 +112,17 @@ public <T> T get(Contextual<T> 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();
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 7db82da

Please sign in to comment.