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

Mark duplicated context created by Vert.x as safe in non-blocking @ConsumeEvent #24246

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 @@ -19,6 +19,7 @@
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle;
import io.smallrye.common.annotation.Blocking;
import io.smallrye.common.vertx.ContextLocals;
import io.smallrye.common.vertx.VertxContext;
Expand Down Expand Up @@ -147,12 +148,14 @@ public static class MyConsumers {
@ConsumeEvent(value = "context")
Uni<String> receive(String data) {
Assertions.assertTrue(Thread.currentThread().getName().contains("vert.x-eventloop"));
VertxContextSafetyToggle.validateContextIfExists("Not marked as safe", "Not marked as safe");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, would it make sense to add a VertxContextSafetyToggle.validateContextIfExists() method which only accepts one string used for both the errorMessageOnVeto and errorMessageOnDoubt? It would save some typing ;-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it's just for a test.
When you want to really check, ou need two different messages.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it's just for a test.

Yep, I know... but it would help with tests at least ;-)

return process(data);
}

private Uni<String> process(String id) {
Context context = Vertx.currentContext();
Assertions.assertTrue(VertxContext.isOnDuplicatedContext());
VertxContextSafetyToggle.validateContextIfExists("Not marked as safe", "Not marked as safe");

String val = ContextLocals.get("key", null);
Assertions.assertNull(val);
Expand All @@ -168,6 +171,7 @@ private Uni<String> process(String id) {
Assertions.assertEquals("hey!", msg);
Assertions.assertEquals(id, ContextLocals.get("key", null));
Assertions.assertSame(Vertx.currentContext(), context);
VertxContextSafetyToggle.validateContextIfExists("Not marked as safe", "Not marked as safe");
return "OK-" + ContextLocals.get("key", null);
}).toCompletionStage());
}
Expand All @@ -176,13 +180,16 @@ private Uni<String> process(String id) {
@Blocking
String receiveBlocking(String data) {
Assertions.assertFalse(Thread.currentThread().getName().contains("vert.x-eventloop"));
Assertions.assertTrue(VertxContext.isOnDuplicatedContext());
VertxContextSafetyToggle.validateContextIfExists("Not marked as safe", "Not marked as safe");
return process(data).await().atMost(Duration.ofSeconds(4));
}

@ConsumeEvent(value = "context-send")
public void consumeSend(String s) {
Context context = Vertx.currentContext();
Assertions.assertTrue(VertxContext.isOnDuplicatedContext());
VertxContextSafetyToggle.validateContextIfExists("Not marked as safe", "Not marked as safe");

String val = ContextLocals.get("key", null);
Assertions.assertNull(val);
Expand All @@ -195,6 +202,7 @@ public void consumeSend(String s) {
public void consumeSendBlocking(String s) {
Context context = Vertx.currentContext();
Assertions.assertTrue(VertxContext.isOnDuplicatedContext());
VertxContextSafetyToggle.validateContextIfExists("Not marked as safe", "Not marked as safe");

String val = ContextLocals.get("key", null);
Assertions.assertNull(val);
Expand All @@ -207,6 +215,7 @@ public void consumeSendBlocking(String s) {
public void consumePublish1(String s) {
Context context = Vertx.currentContext();
Assertions.assertTrue(VertxContext.isOnDuplicatedContext());
VertxContextSafetyToggle.validateContextIfExists("Not marked as safe", "Not marked as safe");

String val = ContextLocals.get("key", null);
Assertions.assertNull(val);
Expand All @@ -219,6 +228,7 @@ public void consumePublish1(String s) {
public void consumePublish2(String s) {
Context context = Vertx.currentContext();
Assertions.assertTrue(VertxContext.isOnDuplicatedContext());
VertxContextSafetyToggle.validateContextIfExists("Not marked as safe", "Not marked as safe");

String val = ContextLocals.get("key", null);
Assertions.assertNull(val);
Expand All @@ -232,6 +242,7 @@ public void consumePublish2(String s) {
public void consumePublishBlocking1(String s) {
Context context = Vertx.currentContext();
Assertions.assertTrue(VertxContext.isOnDuplicatedContext());
VertxContextSafetyToggle.validateContextIfExists("Not marked as safe", "Not marked as safe");

String val = ContextLocals.get("key", null);
Assertions.assertNull(val);
Expand All @@ -245,6 +256,7 @@ public void consumePublishBlocking1(String s) {
public void consumePublishBlocking2(String s) {
Context context = Vertx.currentContext();
Assertions.assertTrue(VertxContext.isOnDuplicatedContext());
VertxContextSafetyToggle.validateContextIfExists("Not marked as safe", "Not marked as safe");

String val = ContextLocals.get("key", null);
Assertions.assertNull(val);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.vertx.runtime;

import static io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle.setContextSafe;
import static io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle.setCurrentContextSafe;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
Expand Down Expand Up @@ -120,7 +121,11 @@ public void handle(Promise<Object> event) {
}
}, invoker.isOrdered(), null);
} else {
// Will run on the context used for the consumer registration
// Will run on the context used for the consumer registration.
// It's a duplicated context, but we need to mark it as safe.
// The safety comes from the fact that it's instantiated by Vert.x for every
// message.
setCurrentContextSafe(true);
try {
invoker.invoke(m);
} catch (Exception e) {
Expand Down