Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArC: RequestContext - implement the activity check consistently #38605

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading