Skip to content

Commit

Permalink
GraphQL: Make sure the context terminate
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
(cherry picked from commit 48787de)
  • Loading branch information
phillip-kruger authored and gsmet committed Jul 19, 2022
1 parent c28193b commit f23ca08
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.quarkus.smallrye.graphql.deployment;

import static io.quarkus.smallrye.graphql.deployment.AbstractGraphQLTest.MEDIATYPE_JSON;

import javax.inject.Inject;

import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Query;
import org.hamcrest.Matchers;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.smallrye.mutiny.Uni;
import io.vertx.ext.web.RoutingContext;

/**
* Testing that the context terminate
*/
public class GraphQLTerminateContextTest extends AbstractGraphQLTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot((JavaArchive jar) -> jar
.addClasses(TestTerminateContextResource.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"));

@Test
public void testWhoAmI() {
runTestWith("user1");
runTestWith("user2");
runTestWith("user2");
}

public void runTestWith(String expectedUser) {
String fooRequest = getPayload("{\n" +
" whoami {\n" +
" name\n" +
" }\n" +
"}");

RestAssured.given().when()
.accept(MEDIATYPE_JSON)
.contentType(MEDIATYPE_JSON)
.body(fooRequest)
.with().header("X-Test", expectedUser)
.post("/graphql")
.then()
.assertThat()
.statusCode(200)
.and()
.log().body().and()
.body("data.whoami.name", Matchers.equalTo(expectedUser));
}

@GraphQLApi
public static class TestTerminateContextResource {

@Inject
RoutingContext ctx;

@Query
public Uni<YouAre> whoami() {
return Uni.createFrom().item(() -> {
YouAre youAre = new YouAre();
youAre.name = ctx.request().headers().get("X-Test");
return youAre;
});
}
}

public static class YouAre {
public String name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public abstract class SmallRyeGraphQLAbstractHandler implements Handler<RoutingC
private final CurrentIdentityAssociation currentIdentityAssociation;
private final CurrentVertxRequest currentVertxRequest;
private final ManagedContext currentManagedContext;

private final Handler currentManagedContextTerminationHandler;
private final boolean runBlocking;

private volatile ExecutionService executionService;
Expand All @@ -46,6 +46,13 @@ public SmallRyeGraphQLAbstractHandler(
this.currentVertxRequest = currentVertxRequest;
this.currentManagedContext = Arc.container().requestContext();
this.runBlocking = runBlocking;
this.currentManagedContextTerminationHandler = new Handler() {
@Override
public void handle(Object e) {
currentManagedContext.terminate();
}

};
}

@Override
Expand All @@ -56,11 +63,16 @@ public void handle(final RoutingContext ctx) {
} else {

currentManagedContext.activate();
handleWithIdentity(ctx);

ctx.response().bodyEndHandler((e) -> {
ctx.response()
.endHandler(currentManagedContextTerminationHandler)
.exceptionHandler(currentManagedContextTerminationHandler)
.closeHandler(currentManagedContextTerminationHandler);

try {
handleWithIdentity(ctx);
} catch (Throwable t) {
currentManagedContext.terminate();
});
}
}
}

Expand Down

0 comments on commit f23ca08

Please sign in to comment.