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

Context propagation - cleared Arc snapshot should destroy its state once the invocation ends #26968

Merged
merged 1 commit into from
Aug 3, 2022
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 @@ -79,11 +79,11 @@ public ThreadContextController begin() {
// context active, store current state, start blank context anew and restore state afterwards
// it is not necessary to deactivate the context first - just overwrite the previous state
requestContext.activate();
return new RestoreContextController(requestContext, toRestore);
return new RestoreContextController(requestContext, toRestore, true);
} else {
// context not active, activate blank one, deactivate afterwards
// context not active, activate blank one, terminate afterwards
requestContext.activate();
return requestContext::deactivate;
return requestContext::terminate;
}
}
}
Expand Down Expand Up @@ -158,14 +158,24 @@ private static final class RestoreContextController implements ThreadContextCont

private final ManagedContext requestContext;
private final InjectableContext.ContextState stateToRestore;
private final boolean destroyRequestContext;

RestoreContextController(ManagedContext requestContext, ContextState stateToRestore) {
this(requestContext, stateToRestore, false);
}

// in case of ClearContextSnapshot, we want to destroy instances of the intermediate context
RestoreContextController(ManagedContext requestContext, ContextState stateToRestore, boolean destroyRequestContext) {
this.requestContext = requestContext;
this.stateToRestore = stateToRestore;
this.destroyRequestContext = destroyRequestContext;
}

@Override
public void endContext() throws IllegalStateException {
if (destroyRequestContext) {
requestContext.destroy();
}
// it is not necessary to deactivate the context first - just overwrite the previous state
requestContext.activate(stateToRestore.isValid() ? stateToRestore : null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package io.quarkus.context.test;

import javax.annotation.PreDestroy;
import javax.enterprise.context.RequestScoped;

@RequestScoped
public class RequestBean {

public static volatile int DESTROY_INVOKED = 0;

public String callMe() {
return "Hello " + System.identityHashCode(this);
}

@PreDestroy
public void destroy() {
DESTROY_INVOKED++;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.awaitility.Awaitility;
import org.awaitility.core.ThrowingRunnable;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand Down Expand Up @@ -75,14 +76,20 @@ public void testArcTCContextPropagation() {

@Test()
public void testArcMEContextPropagationDisabled() {
// reset state
RequestBean.DESTROY_INVOKED = 0;
RestAssured.when().get("/context/noarc").then()
.statusCode(Response.Status.OK.getStatusCode());
Assertions.assertEquals(2, RequestBean.DESTROY_INVOKED);
}

@Test()
public void testArcTCContextPropagationDisabled() {
// reset state
RequestBean.DESTROY_INVOKED = 0;
RestAssured.when().get("/context/noarc-tc").then()
.statusCode(Response.Status.OK.getStatusCode());
Assertions.assertEquals(2, RequestBean.DESTROY_INVOKED);
}

@Test()
Expand Down