forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reactive routes: virtual threads support
- resolves quarkusio#36430
- Loading branch information
Showing
15 changed files
with
266 additions
and
13 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
36 changes: 36 additions & 0 deletions
36
...routes/runtime/src/main/java/io/quarkus/vertx/web/runtime/VirtualThreadsRouteHandler.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,36 @@ | ||
package io.quarkus.vertx.web.runtime; | ||
|
||
import io.quarkus.vertx.core.runtime.VertxCoreRecorder; | ||
import io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle; | ||
import io.quarkus.virtual.threads.VirtualThreadsRecorder; | ||
import io.smallrye.common.vertx.VertxContext; | ||
import io.vertx.core.Context; | ||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
public class VirtualThreadsRouteHandler implements Handler<RoutingContext> { | ||
|
||
private final Handler<RoutingContext> routeHandler; | ||
|
||
public VirtualThreadsRouteHandler(Handler<RoutingContext> routeHandler) { | ||
this.routeHandler = routeHandler; | ||
} | ||
|
||
@Override | ||
public void handle(RoutingContext context) { | ||
Context vertxContext = VertxContext.getOrCreateDuplicatedContext(VertxCoreRecorder.getVertx().get()); | ||
VertxContextSafetyToggle.setContextSafe(vertxContext, true); | ||
vertxContext.runOnContext(new Handler<Void>() { | ||
@Override | ||
public void handle(Void event) { | ||
VirtualThreadsRecorder.getCurrent().execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
routeHandler.handle(context); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
} |
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
76 changes: 76 additions & 0 deletions
76
integration-tests/virtual-threads/reactive-routes-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,76 @@ | ||
<?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-reactive-routes</artifactId> | ||
<name>Quarkus - Integration Tests - Virtual Threads - Reactive Routes</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-reactive-routes</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-junit5</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus.junit5</groupId> | ||
<artifactId>junit5-virtual-threads</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-reactive-routes-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
...ctive-routes-virtual-threads/src/main/java/io/quarkus/virtual/vertx/web/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.web; | ||
|
||
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"); | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ds/reactive-routes-virtual-threads/src/main/java/io/quarkus/virtual/vertx/web/Routes.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,16 @@ | ||
package io.quarkus.virtual.vertx.web; | ||
|
||
import io.quarkus.vertx.web.Route; | ||
import io.smallrye.common.annotation.RunOnVirtualThread; | ||
|
||
public class Routes { | ||
|
||
@RunOnVirtualThread | ||
@Route | ||
String hello() { | ||
AssertHelper.assertEverything(); | ||
// Quarkus specific - each VT has a unique name | ||
return Thread.currentThread().getName(); | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
...virtual-threads/reactive-routes-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,3 @@ | ||
quarkus.native.additional-build-args=--enable-preview | ||
|
||
quarkus.package.quiltflower.enabled=true |
2 changes: 1 addition & 1 deletion
2
...us/virtual/mail/RunOnVirtualThreadIT.java → ...rtual/vertx/web/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
25 changes: 25 additions & 0 deletions
25
...es-virtual-threads/src/test/java/io/quarkus/virtual/vertx/web/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,25 @@ | ||
package io.quarkus.virtual.vertx.web; | ||
|
||
import static io.restassured.RestAssured.get; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit5.virtual.ShouldNotPin; | ||
import io.quarkus.test.junit5.virtual.VirtualThreadUnit; | ||
|
||
@QuarkusTest | ||
@VirtualThreadUnit | ||
@ShouldNotPin | ||
class RunOnVirtualThreadTest { | ||
|
||
@Test | ||
void testRoute() { | ||
String bodyStr = get("/hello").then().statusCode(200).extract().asString(); | ||
// Each VT has a unique name in quarkus | ||
assertNotEquals(bodyStr, get("/hello").then().statusCode(200).extract().asString()); | ||
|
||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...uler-virtual-threads/src/test/java/io/quarkus/virtual/scheduler/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.scheduler; | ||
|
||
import io.quarkus.test.junit.QuarkusIntegrationTest; | ||
|
||
@QuarkusIntegrationTest | ||
class RunOnVirtualThreadIT extends RunOnVirtualThreadTest { | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
.../virtual/mail/RunOnVirtualThreadTest.java → ...ual/scheduler/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