-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Add support for duplicated context for reactive routes and @ConsumeEvent #23669
Merged
cescoffier
merged 2 commits into
quarkusio:main
from
cescoffier:duplicated-context-checks
Feb 15, 2022
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
116 changes: 116 additions & 0 deletions
116
...e-routes/deployment/src/test/java/io/quarkus/vertx/web/context/DuplicatedContextTest.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,116 @@ | ||
package io.quarkus.vertx.web.context; | ||
|
||
import static io.restassured.RestAssured.get; | ||
|
||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.vertx.web.Route; | ||
import io.smallrye.common.annotation.Blocking; | ||
import io.smallrye.mutiny.Uni; | ||
import io.smallrye.mutiny.infrastructure.Infrastructure; | ||
import io.vertx.core.Context; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.http.HttpClientResponse; | ||
import io.vertx.core.http.HttpMethod; | ||
import io.vertx.core.impl.EventLoopContext; | ||
import io.vertx.core.impl.WorkerContext; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
/** | ||
* Verify that reactive routes are called on duplicated contexts and that they handled them properly. | ||
*/ | ||
public class DuplicatedContextTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(MyRoutes.class)); | ||
|
||
@Test | ||
public void testThatRoutesAreCalledOnDuplicatedContext() { | ||
// Creates a bunch of requests that will be executed concurrently. | ||
// So, we are sure that event loops are reused. | ||
List<Uni<Void>> unis = new ArrayList<>(); | ||
for (int i = 0; i < 500; i++) { | ||
String uuid = UUID.randomUUID().toString(); | ||
unis.add(Uni.createFrom().item(() -> { | ||
String resp = get("/context/" + uuid).asString(); | ||
Assertions.assertEquals(resp, "OK-" + uuid); | ||
return null; | ||
})); | ||
} | ||
|
||
Uni.join().all(unis).andFailFast() | ||
.runSubscriptionOn(Infrastructure.getDefaultExecutor()) | ||
.await().atMost(Duration.ofSeconds(10)); | ||
} | ||
|
||
@Test | ||
public void testThatBlockingRoutesAreCalledOnDuplicatedContext() { | ||
String uuid = UUID.randomUUID().toString(); | ||
String resp = get("/context-blocking/" + uuid).asString(); | ||
Assertions.assertEquals(resp, "OK-" + uuid); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyRoutes { | ||
|
||
@Inject | ||
Vertx vertx; | ||
|
||
@Route(path = "/context/:id", methods = Route.HttpMethod.GET) | ||
void get(RoutingContext ctx) { | ||
Assertions.assertTrue(Thread.currentThread().getName().contains("vert.x-eventloop")); | ||
process(ctx); | ||
} | ||
|
||
@Route(path = "/context-blocking/:id", methods = Route.HttpMethod.GET) | ||
@Blocking | ||
void getBlocking(RoutingContext ctx) { | ||
Assertions.assertFalse(Thread.currentThread().getName().contains("vert.x-eventloop")); | ||
process(ctx); | ||
} | ||
|
||
private void process(RoutingContext ctx) { | ||
Context context = Vertx.currentContext(); | ||
Assertions.assertFalse(context instanceof EventLoopContext); | ||
Assertions.assertFalse(context instanceof WorkerContext); | ||
|
||
String val = context.getLocal("key"); | ||
Assertions.assertNull(val); | ||
|
||
String id = ctx.pathParam("id"); | ||
context.putLocal("key", id); | ||
|
||
vertx.createHttpClient().request(HttpMethod.GET, 8081, "localhost", "/hey") | ||
.compose(request -> request.end().compose(x -> request.response())) | ||
.compose(HttpClientResponse::body) | ||
.map(Buffer::toString) | ||
.onSuccess(msg -> { | ||
Assertions.assertEquals("hey!", msg); | ||
Assertions.assertEquals(id, Vertx.currentContext().getLocal("key")); | ||
Assertions.assertSame(Vertx.currentContext(), context); | ||
ctx.response().end("OK-" + context.getLocal("key")); | ||
}); | ||
} | ||
|
||
@Route(path = "/hey", methods = Route.HttpMethod.GET) | ||
void hey(RoutingContext rc) { | ||
rc.response().end("hey!"); | ||
} | ||
|
||
} | ||
|
||
} |
273 changes: 273 additions & 0 deletions
273
extensions/vertx/deployment/src/test/java/io/quarkus/vertx/DuplicatedContextTest.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,273 @@ | ||
package io.quarkus.vertx; | ||
|
||
import static org.awaitility.Awaitility.await; | ||
|
||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.smallrye.common.annotation.Blocking; | ||
import io.smallrye.mutiny.Uni; | ||
import io.smallrye.mutiny.infrastructure.Infrastructure; | ||
import io.vertx.core.Context; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.http.HttpClientResponse; | ||
import io.vertx.core.http.HttpMethod; | ||
import io.vertx.core.impl.EventLoopContext; | ||
import io.vertx.core.impl.WorkerContext; | ||
import io.vertx.mutiny.core.eventbus.EventBus; | ||
import io.vertx.mutiny.core.eventbus.Message; | ||
|
||
/** | ||
* Verify that methods annotated with {@link ConsumeEvent} are called on duplicated contexts and that they handled | ||
* them properly. | ||
*/ | ||
public class DuplicatedContextTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(MyConsumers.class)); | ||
private Vertx vertx; | ||
|
||
@BeforeEach | ||
public void init() { | ||
vertx = Vertx.vertx(); | ||
vertx.createHttpServer() | ||
.requestHandler(req -> req.response().end("hey!")) | ||
.listen(8082).toCompletionStage().toCompletableFuture().join(); | ||
} | ||
|
||
@AfterEach | ||
public void cleanup() { | ||
if (vertx != null) { | ||
vertx.close().toCompletionStage().toCompletableFuture().join(); | ||
} | ||
} | ||
|
||
@Inject | ||
EventBus bus; | ||
|
||
@Inject | ||
MyConsumers consumers; | ||
|
||
@Test | ||
public void testThatMessageSentToTheEventBusAreProcessedOnUnsharedDuplicatedContext() { | ||
AtomicInteger expected = new AtomicInteger(); | ||
String id1 = UUID.randomUUID().toString(); | ||
bus.send("context-send", id1); | ||
await().until(() -> consumers.probes().contains(id1)); | ||
|
||
consumers.reset(); | ||
|
||
String id2 = UUID.randomUUID().toString(); | ||
bus.send("context-send-blocking", id2); | ||
await().until(() -> consumers.probes().contains(id2)); | ||
|
||
consumers.reset(); | ||
|
||
String id3 = UUID.randomUUID().toString(); | ||
bus.publish("context-publish", id3); | ||
await().until(() -> consumers.probes().size() == 2); | ||
await().until(() -> consumers.probes().contains(id3)); | ||
|
||
consumers.reset(); | ||
|
||
String id4 = UUID.randomUUID().toString(); | ||
bus.publish("context-publish-blocking", id4); | ||
await().until(() -> consumers.probes().size() == 2); | ||
await().until(() -> consumers.probes().contains(id4)); | ||
} | ||
|
||
@Test | ||
public void testThatEventConsumersAreCalledOnDuplicatedContext() { | ||
// Creates a bunch of requests that will be executed concurrently. | ||
// So, we are sure that event loops are reused. | ||
List<Uni<Void>> unis = new ArrayList<>(); | ||
for (int i = 0; i < 100; i++) { | ||
String uuid = UUID.randomUUID().toString(); | ||
unis.add( | ||
bus.<String> request("context", uuid) | ||
.map(Message::body) | ||
.invoke(resp -> { | ||
Assertions.assertEquals(resp, "OK-" + uuid); | ||
}) | ||
.replaceWithVoid()); | ||
} | ||
|
||
Uni.join().all(unis).andFailFast() | ||
.runSubscriptionOn(Infrastructure.getDefaultExecutor()) | ||
.await().atMost(Duration.ofSeconds(10)); | ||
} | ||
|
||
@Test | ||
public void testThatBlockingEventConsumersAreCalledOnDuplicatedContext() { | ||
// Creates a bunch of requests that will be executed concurrently. | ||
// So, we are sure that event loops are reused. | ||
List<Uni<Void>> unis = new ArrayList<>(); | ||
for (int i = 0; i < 500; i++) { | ||
String uuid = UUID.randomUUID().toString(); | ||
unis.add( | ||
bus.<String> request("context-blocking", uuid) | ||
.map(Message::body) | ||
.invoke(resp -> { | ||
Assertions.assertEquals(resp, "OK-" + uuid); | ||
}) | ||
.replaceWithVoid()); | ||
} | ||
|
||
Uni.join().all(unis).andFailFast() | ||
.runSubscriptionOn(Infrastructure.getDefaultExecutor()) | ||
.await().atMost(Duration.ofSeconds(10)); | ||
|
||
} | ||
|
||
@ApplicationScoped | ||
public static class MyConsumers { | ||
|
||
@Inject | ||
Vertx vertx; | ||
|
||
private List<String> probes = new CopyOnWriteArrayList<>(); | ||
|
||
@ConsumeEvent(value = "context") | ||
Uni<String> receive(String data) { | ||
Assertions.assertTrue(Thread.currentThread().getName().contains("vert.x-eventloop")); | ||
return process(data); | ||
} | ||
|
||
private Uni<String> process(String id) { | ||
Context context = Vertx.currentContext(); | ||
Assertions.assertFalse(context instanceof EventLoopContext); | ||
Assertions.assertFalse(context instanceof WorkerContext); | ||
|
||
String val = context.getLocal("key"); | ||
Assertions.assertNull(val); | ||
|
||
context.putLocal("key", id); | ||
|
||
return Uni.createFrom().completionStage( | ||
() -> vertx.createHttpClient().request(HttpMethod.GET, 8082, "localhost", "/hey") | ||
.compose(request -> request.end().compose(x -> request.response())) | ||
.compose(HttpClientResponse::body) | ||
.map(Buffer::toString) | ||
.map(msg -> { | ||
Assertions.assertEquals("hey!", msg); | ||
Assertions.assertEquals(id, Vertx.currentContext().getLocal("key")); | ||
Assertions.assertSame(Vertx.currentContext(), context); | ||
return "OK-" + context.getLocal("key"); | ||
}).toCompletionStage()); | ||
} | ||
|
||
@ConsumeEvent(value = "context-blocking") | ||
@Blocking | ||
String receiveBlocking(String data) { | ||
Assertions.assertFalse(Thread.currentThread().getName().contains("vert.x-eventloop")); | ||
return process(data).await().atMost(Duration.ofSeconds(4)); | ||
} | ||
|
||
@ConsumeEvent(value = "context-send") | ||
public void consumeSend(String s) { | ||
Context context = Vertx.currentContext(); | ||
Assertions.assertFalse(context instanceof EventLoopContext); | ||
Assertions.assertFalse(context instanceof WorkerContext); | ||
|
||
String val = context.getLocal("key"); | ||
Assertions.assertNull(val); | ||
context.putLocal("key", s); | ||
|
||
probes.add(s); | ||
} | ||
|
||
@ConsumeEvent(value = "context-send-blocking") | ||
public void consumeSendBlocking(String s) { | ||
Context context = Vertx.currentContext(); | ||
Assertions.assertFalse(context instanceof EventLoopContext); | ||
Assertions.assertFalse(context instanceof WorkerContext); | ||
|
||
String val = context.getLocal("key"); | ||
Assertions.assertNull(val); | ||
context.putLocal("key", s); | ||
|
||
probes.add(s); | ||
} | ||
|
||
@ConsumeEvent(value = "context-publish") | ||
public void consumePublish1(String s) { | ||
Context context = Vertx.currentContext(); | ||
Assertions.assertFalse(context instanceof EventLoopContext); | ||
Assertions.assertFalse(context instanceof WorkerContext); | ||
|
||
String val = context.getLocal("key"); | ||
Assertions.assertNull(val); | ||
context.putLocal("key", s); | ||
|
||
probes.add(s); | ||
} | ||
|
||
@ConsumeEvent(value = "context-publish") | ||
public void consumePublish2(String s) { | ||
Context context = Vertx.currentContext(); | ||
Assertions.assertFalse(context instanceof EventLoopContext); | ||
Assertions.assertFalse(context instanceof WorkerContext); | ||
|
||
String val = context.getLocal("key"); | ||
Assertions.assertNull(val); | ||
context.putLocal("key", s); | ||
|
||
probes.add(s); | ||
} | ||
|
||
@ConsumeEvent(value = "context-publish-blocking") | ||
@Blocking | ||
public void consumePublishBlocking1(String s) { | ||
Context context = Vertx.currentContext(); | ||
Assertions.assertFalse(context instanceof EventLoopContext); | ||
Assertions.assertFalse(context instanceof WorkerContext); | ||
|
||
String val = context.getLocal("key"); | ||
Assertions.assertNull(val); | ||
context.putLocal("key", s); | ||
|
||
probes.add(s); | ||
} | ||
|
||
@ConsumeEvent(value = "context-publish-blocking") | ||
@Blocking | ||
public void consumePublishBlocking2(String s) { | ||
Context context = Vertx.currentContext(); | ||
Assertions.assertFalse(context instanceof EventLoopContext); | ||
Assertions.assertFalse(context instanceof WorkerContext); | ||
|
||
String val = context.getLocal("key"); | ||
Assertions.assertNull(val); | ||
context.putLocal("key", s); | ||
|
||
probes.add(s); | ||
} | ||
|
||
public List<String> probes() { | ||
return probes; | ||
} | ||
|
||
public void reset() { | ||
probes.clear(); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.
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.
Would
context.isOnEventLoopThread()
still returntrue
?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.
in the case of blocking... yes.
I know it's confusing. It's because of
executeBlocking
which keeps your event loop context.I've opened a PR on Vert.x to get a better way to check if we are on a duplicated context.