-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Fix for deactivating managed context in GraphQL server #27040
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
189 changes: 189 additions & 0 deletions
189
...l/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/ConcurrentAuthTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
package io.quarkus.smallrye.graphql.deployment; | ||
|
||
import static io.quarkus.smallrye.graphql.deployment.AbstractGraphQLTest.getPropertyAsString; | ||
import static io.restassured.RestAssured.given; | ||
|
||
import java.time.LocalDate; | ||
import java.time.Month; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.eclipse.microprofile.graphql.GraphQLApi; | ||
import org.eclipse.microprofile.graphql.Query; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.security.Authenticated; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.response.Response; | ||
|
||
public class ConcurrentAuthTest extends AbstractGraphQLTest { | ||
|
||
static Map<String, String> PROPERTIES = new HashMap<>(); | ||
static { | ||
|
||
PROPERTIES.put("quarkus.smallrye-graphql.error-extension-fields", "classification,code"); | ||
PROPERTIES.put("quarkus.smallrye-graphql.show-runtime-exception-message", "java.lang.SecurityException"); | ||
|
||
PROPERTIES.put("quarkus.http.auth.basic", "true"); | ||
PROPERTIES.put("quarkus.security.users.embedded.enabled", "true"); | ||
PROPERTIES.put("quarkus.security.users.embedded.plain-text", "true"); | ||
PROPERTIES.put("quarkus.security.users.embedded.users.scott", "jb0ss"); | ||
} | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(FilmResource.class, Film.class, GalaxyService.class) | ||
.addAsResource(new StringAsset(getPropertyAsString(PROPERTIES)), "application.properties") | ||
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")); | ||
|
||
private int iterations = 5000; | ||
|
||
@Test | ||
public void concurrentAllFilmsOnly() throws InterruptedException, ExecutionException { | ||
ExecutorService executor = Executors.newFixedThreadPool(50); | ||
|
||
var futures = new ArrayList<CompletableFuture<Boolean>>(iterations); | ||
for (int i = 0; i < iterations; i++) { | ||
futures.add(CompletableFuture.supplyAsync(this::allFilmsRequestWithAuth, executor) | ||
.thenApply(r -> !r.getBody().asString().contains("unauthorized"))); | ||
} | ||
Optional<Boolean> success = getTestResult(futures); | ||
Assertions.assertTrue(success.orElse(false), "Unauthorized response codes were found"); | ||
executor.shutdown(); | ||
} | ||
|
||
private static Optional<Boolean> getTestResult(ArrayList<CompletableFuture<Boolean>> futures) | ||
throws InterruptedException, ExecutionException { | ||
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) | ||
.thenApply(v -> futures.stream() | ||
.map(CompletableFuture::join) | ||
.reduce(Boolean::logicalAnd)) | ||
.get(); | ||
} | ||
|
||
private Response allFilmsRequestWithAuth() { | ||
String requestBody = "{\"query\":" + | ||
"\"" + | ||
"{" + | ||
" allFilmsSecured {" + | ||
" title" + | ||
" director" + | ||
" releaseDate" + | ||
" episodeID" + | ||
"}" + | ||
"}" + | ||
"\"" + | ||
"}"; | ||
|
||
return given() | ||
.body(requestBody) | ||
.auth() | ||
.preemptive() | ||
.basic("scott", "jb0ss") | ||
.post("/graphql/"); | ||
} | ||
|
||
@GraphQLApi | ||
public static class FilmResource { | ||
|
||
@Inject | ||
GalaxyService service; | ||
|
||
@Query("allFilmsSecured") | ||
@Authenticated | ||
public List<Film> getAllFilmsSecured() { | ||
return service.getAllFilms(); | ||
} | ||
} | ||
|
||
public static class Film { | ||
|
||
private String title; | ||
private Integer episodeID; | ||
private String director; | ||
private LocalDate releaseDate; | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public Integer getEpisodeID() { | ||
return episodeID; | ||
} | ||
|
||
public void setEpisodeID(Integer episodeID) { | ||
this.episodeID = episodeID; | ||
} | ||
|
||
public String getDirector() { | ||
return director; | ||
} | ||
|
||
public void setDirector(String director) { | ||
this.director = director; | ||
} | ||
|
||
public LocalDate getReleaseDate() { | ||
return releaseDate; | ||
} | ||
|
||
public void setReleaseDate(LocalDate releaseDate) { | ||
this.releaseDate = releaseDate; | ||
} | ||
|
||
} | ||
|
||
@ApplicationScoped | ||
public static class GalaxyService { | ||
|
||
private List<Film> films = new ArrayList<>(); | ||
|
||
public GalaxyService() { | ||
|
||
Film aNewHope = new Film(); | ||
aNewHope.setTitle("A New Hope"); | ||
aNewHope.setReleaseDate(LocalDate.of(1977, Month.MAY, 25)); | ||
aNewHope.setEpisodeID(4); | ||
aNewHope.setDirector("George Lucas"); | ||
|
||
Film theEmpireStrikesBack = new Film(); | ||
theEmpireStrikesBack.setTitle("The Empire Strikes Back"); | ||
theEmpireStrikesBack.setReleaseDate(LocalDate.of(1980, Month.MAY, 21)); | ||
theEmpireStrikesBack.setEpisodeID(5); | ||
theEmpireStrikesBack.setDirector("George Lucas"); | ||
|
||
Film returnOfTheJedi = new Film(); | ||
returnOfTheJedi.setTitle("Return Of The Jedi"); | ||
returnOfTheJedi.setReleaseDate(LocalDate.of(1983, Month.MAY, 25)); | ||
returnOfTheJedi.setEpisodeID(6); | ||
returnOfTheJedi.setDirector("George Lucas"); | ||
|
||
films.add(aNewHope); | ||
films.add(theEmpireStrikesBack); | ||
films.add(returnOfTheJedi); | ||
} | ||
|
||
public List<Film> getAllFilms() { | ||
return films; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't this one in a finally? Why deactivate in one case and terminate in the case of an exception?
Also what's the purpose of this.currentManagedContextTerminationHandler now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.currentManagedContextTerminationHandler
will terminate (so deactivate and destroy) once we are done, while the deactivate after thehandleWithIdentity
will only deactivate (so that it does not leak)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not need to be in a finally as the catch with terminate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can understand that but my concern was more that we are not consistent. Typically here: https://github.com/quarkusio/quarkus/pull/27040/files#diff-5b5cda47e6a5418c9345a577d468f058e05c412dc9b64fce6a6ef5aec496023bR75-R85 where we simply deactivate. Or maybe the exception from the latter is caught below?
Also what do you mean by leaking? What I don't understand is how we can leak something between this and the end of the response? Or is it that the thread is somehow reused between the end of this and the end of the response?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thread could be reused, in a high traffic scenario.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But you're sure that all the response handlers we added earlier are always executed on the right thread at the right time?
That's what gets me a bit worried.
Note that I have no idea how this all works so I'm asking naive questions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes sure. Maybe @stuartwdouglas can explain better. By leaking I mean that a new request that use the same thread can get the context that still float around (because it's not been deactivated)