-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate @RunOnVirtualThread with the @ConsumeEvent
Allows @ConsumeEvent method to run on a virtual thread. Also verify that sending and receiving from the event bus is not pinning the carrier thread.
1 parent
3dede5c
commit c14639b
Showing
20 changed files
with
432 additions
and
43 deletions.
There are no files selected for viewing
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
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
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
73 changes: 73 additions & 0 deletions
73
integration-tests/virtual-threads/vertx-event-bus-virtual-threads/pom.xml
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,73 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<artifactId>quarkus-virtual-threads-integration-tests-parent</artifactId> | ||
<groupId>io.quarkus</groupId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>quarkus-integration-test-virtual-threads-vertx-event-bus</artifactId> | ||
<name>Quarkus - Integration Tests - Virtual Threads - Vert.x Event Bus</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-resteasy-reactive-jackson</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-junit5</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.rest-assured</groupId> | ||
<artifactId>rest-assured</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.awaitility</groupId> | ||
<artifactId>awaitility</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
|
||
<!-- Minimal test dependencies to *-deployment artifacts for consistent build order --> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-resteasy-reactive-jackson-deployment</artifactId> | ||
<version>${project.version}</version> | ||
<type>pom</type> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>*</groupId> | ||
<artifactId>*</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-maven-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
71 changes: 71 additions & 0 deletions
71
.../vertx-event-bus-virtual-threads/src/main/java/io/quarkus/virtual/vertx/AssertHelper.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,71 @@ | ||
package io.quarkus.virtual.vertx; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import io.quarkus.arc.Arc; | ||
import io.smallrye.common.vertx.VertxContext; | ||
import io.vertx.core.Vertx; | ||
|
||
public class AssertHelper { | ||
|
||
/** | ||
* Asserts that the current method: | ||
* - runs on a duplicated context | ||
* - runs on a virtual thread | ||
* - has the request scope activated | ||
*/ | ||
public static void assertEverything() { | ||
assertThatTheRequestScopeIsActive(); | ||
assertThatItRunsOnVirtualThread(); | ||
assertThatItRunsOnADuplicatedContext(); | ||
} | ||
|
||
public static void assertThatTheRequestScopeIsActive() { | ||
if (!Arc.container().requestContext().isActive()) { | ||
throw new AssertionError(("Expected the request scope to be active")); | ||
} | ||
} | ||
|
||
public static void assertThatItRunsOnADuplicatedContext() { | ||
var context = Vertx.currentContext(); | ||
if (context == null) { | ||
throw new AssertionError("The method does not run on a Vert.x context"); | ||
} | ||
if (!VertxContext.isOnDuplicatedContext()) { | ||
throw new AssertionError("The method does not run on a Vert.x **duplicated** context"); | ||
} | ||
} | ||
|
||
public static void assertThatItRunsOnVirtualThread() { | ||
// We cannot depend on a Java 20. | ||
try { | ||
Method isVirtual = Thread.class.getMethod("isVirtual"); | ||
isVirtual.setAccessible(true); | ||
boolean virtual = (Boolean) isVirtual.invoke(Thread.currentThread()); | ||
if (!virtual) { | ||
throw new AssertionError("Thread " + Thread.currentThread() + " is not a virtual thread"); | ||
} | ||
} catch (Exception e) { | ||
throw new AssertionError( | ||
"Thread " + Thread.currentThread() + " is not a virtual thread - cannot invoke Thread.isVirtual()", e); | ||
} | ||
} | ||
|
||
public static void assertNotOnVirtualThread() { | ||
// We cannot depend on a Java 20. | ||
try { | ||
Method isVirtual = Thread.class.getMethod("isVirtual"); | ||
isVirtual.setAccessible(true); | ||
boolean virtual = (Boolean) isVirtual.invoke(Thread.currentThread()); | ||
if (virtual) { | ||
throw new AssertionError("Thread " + Thread.currentThread() + " is a virtual thread"); | ||
} | ||
} catch (Exception e) { | ||
// Trying using Thread name. | ||
var name = Thread.currentThread().toString(); | ||
if (name.toLowerCase().contains("virtual")) { | ||
throw new AssertionError("Thread " + Thread.currentThread() + " seems to be a virtual thread"); | ||
} | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...tx-event-bus-virtual-threads/src/main/java/io/quarkus/virtual/vertx/EventBusConsumer.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,29 @@ | ||
package io.quarkus.virtual.vertx; | ||
|
||
import io.quarkus.vertx.ConsumeEvent; | ||
import io.smallrye.common.annotation.RunOnVirtualThread; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
@ApplicationScoped | ||
public class EventBusConsumer { | ||
|
||
public static final List<String> ONE_WAY = new CopyOnWriteArrayList<>(); | ||
|
||
@ConsumeEvent("one-way") | ||
@RunOnVirtualThread | ||
void receive(String m) { | ||
AssertHelper.assertEverything(); | ||
ONE_WAY.add(m); | ||
} | ||
|
||
@ConsumeEvent("request-reply") | ||
@RunOnVirtualThread | ||
String process(String m) { | ||
AssertHelper.assertEverything(); | ||
return m.toUpperCase(); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...ent-bus-virtual-threads/src/main/java/io/quarkus/virtual/vertx/VertxEventBusResource.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,37 @@ | ||
package io.quarkus.virtual.vertx; | ||
|
||
import io.smallrye.common.annotation.RunOnVirtualThread; | ||
import io.vertx.mutiny.core.eventbus.EventBus; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
|
||
@RunOnVirtualThread | ||
@Path("/") | ||
public class VertxEventBusResource { | ||
|
||
@Inject | ||
EventBus bus; | ||
|
||
@GET | ||
@Path("/one-way") | ||
public void sentOneWay() { | ||
bus.send("one-way", "hello"); | ||
} | ||
|
||
@GET | ||
@Path("/one-way-verify") | ||
public List<String> oneWayVerification() { | ||
return EventBusConsumer.ONE_WAY; | ||
} | ||
|
||
@GET | ||
@Path("/request-reply") | ||
public String requestReply() { | ||
return bus.<String> request("request-reply", "hello").map(m -> m.body()).await().atMost(Duration.ofSeconds(10)); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...virtual-threads/vertx-event-bus-virtual-threads/src/main/resources/application.properties
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 @@ | ||
quarkus.native.additional-build-args=--enable-preview |
76 changes: 76 additions & 0 deletions
76
...rtx-event-bus-virtual-threads/src/test/java/io/quarkus/virtual/vertx/NoPinningVerify.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,76 @@ | ||
package io.quarkus.virtual.vertx; | ||
|
||
import java.io.File; | ||
import java.io.FilenameFilter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Node; | ||
import org.w3c.dom.NodeList; | ||
import org.xml.sax.SAXException; | ||
|
||
/** | ||
* An integration test reading the output of the unit test to verify that no tests where pinning the carrier thread. | ||
* It reads the reports generated by surefire. | ||
*/ | ||
public class NoPinningVerify { | ||
|
||
@Test | ||
void verify() throws IOException, ParserConfigurationException, SAXException { | ||
var reports = new File("target", "surefire-reports"); | ||
Assertions.assertTrue(reports.isDirectory(), | ||
"Unable to find " + reports.getAbsolutePath() + ", did you run the tests with Maven before?"); | ||
var list = reports.listFiles(new FilenameFilter() { | ||
@Override | ||
public boolean accept(File dir, String name) { | ||
return name.startsWith("TEST") && name.endsWith("Test.xml"); | ||
} | ||
}); | ||
Assertions.assertNotNull(list, | ||
"Unable to find " + reports.getAbsolutePath() + ", did you run the tests with Maven before?"); | ||
|
||
for (File report : list) { | ||
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(report); | ||
var suite = document.getFirstChild(); | ||
var cases = getChildren(suite.getChildNodes(), "testcase"); | ||
for (Node c : cases) { | ||
verify(report, c); | ||
} | ||
} | ||
|
||
} | ||
|
||
private void verify(File file, Node ca) { | ||
var fullname = ca.getAttributes().getNamedItem("classname").getTextContent() + "." | ||
+ ca.getAttributes().getNamedItem("name").getTextContent(); | ||
var output = getChildren(ca.getChildNodes(), "system-out"); | ||
if (output.isEmpty()) { | ||
return; | ||
} | ||
var sout = output.get(0).getTextContent(); | ||
if (sout.contains("VThreadContinuation.onPinned")) { | ||
throw new AssertionError("The test case " + fullname + " pinned the carrier thread, check " + file.getAbsolutePath() | ||
+ " for details (or the log of the test)"); | ||
} | ||
|
||
} | ||
|
||
private List<Node> getChildren(NodeList nodes, String name) { | ||
List<Node> list = new ArrayList<>(); | ||
for (int i = 0; i < nodes.getLength(); i++) { | ||
var node = nodes.item(i); | ||
if (node.getNodeName().equalsIgnoreCase(name)) { | ||
list.add(node); | ||
} | ||
} | ||
return list; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...vent-bus-virtual-threads/src/test/java/io/quarkus/virtual/vertx/RunOnVirtualThreadIT.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,8 @@ | ||
package io.quarkus.virtual.vertx; | ||
|
||
import io.quarkus.test.junit.QuarkusIntegrationTest; | ||
|
||
@QuarkusIntegrationTest | ||
class RunOnVirtualThreadIT extends RunOnVirtualThreadTest { | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...nt-bus-virtual-threads/src/test/java/io/quarkus/virtual/vertx/RunOnVirtualThreadTest.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,34 @@ | ||
package io.quarkus.virtual.vertx; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.restassured.RestAssured; | ||
import org.awaitility.Awaitility; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.Timeout; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
|
||
@QuarkusTest | ||
class RunOnVirtualThreadTest { | ||
|
||
@Test | ||
@Timeout(10) | ||
void testOneWay() { | ||
RestAssured.get("/one-way").then() | ||
.assertThat().statusCode(204); | ||
|
||
Awaitility.await().untilAsserted(() -> { | ||
RestAssured.get("/one-way-verify").then() | ||
.assertThat().statusCode(200) | ||
.body("size()", is(1)); | ||
}); | ||
} | ||
|
||
@Test | ||
@Timeout(10) | ||
void testRequestReply() { | ||
RestAssured.get("/request-reply").then() | ||
.assertThat().statusCode(200) | ||
.body(is("HELLO")); | ||
} | ||
} |