forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
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 Quarkus scheduler (quarkus-sch…
…eduler and Quartz) Allows @scheduled methods to run on a virtual thread. Also extend the programmatic API to allow defining jobs that will run on virtual thread if the JVM offers this possibility.
1 parent
f034dd1
commit c951629
Showing
29 changed files
with
836 additions
and
28 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
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
89 changes: 89 additions & 0 deletions
89
integration-tests/virtual-threads/quartz-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,89 @@ | ||
<?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-quartz</artifactId> | ||
<name>Quarkus - Integration Tests - Virtual Threads - Quartz Scheduler</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-resteasy-reactive-jackson</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-quartz</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> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-quartz-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
...reads/quartz-virtual-threads/src/main/java/io/quarkus/virtual/scheduler/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.scheduler; | ||
|
||
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"); | ||
} | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
.../quartz-virtual-threads/src/main/java/io/quarkus/virtual/scheduler/ScheduledResource.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,50 @@ | ||
package io.quarkus.virtual.scheduler; | ||
|
||
import java.util.Set; | ||
import java.util.concurrent.CopyOnWriteArraySet; | ||
|
||
import jakarta.enterprise.event.Observes; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
import io.quarkus.runtime.StartupEvent; | ||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.scheduler.Scheduler; | ||
import io.smallrye.common.annotation.RunOnVirtualThread; | ||
|
||
@Path("/") | ||
public class ScheduledResource { | ||
|
||
Set<String> executions = new CopyOnWriteArraySet<>(); | ||
Set<String> programmaticExecutions = new CopyOnWriteArraySet<>(); | ||
|
||
public void init(@Observes StartupEvent ev, Scheduler scheduler) { | ||
scheduler.newJob("my-programmatic-job") | ||
.setInterval("1s") | ||
.setTask(ex -> { | ||
AssertHelper.assertEverything(); | ||
// Quarkus specific - each VT has a unique name | ||
programmaticExecutions.add(Thread.currentThread().getName()); | ||
}, true) | ||
.schedule(); | ||
} | ||
|
||
@Scheduled(every = "1s") | ||
@RunOnVirtualThread | ||
void run() { | ||
AssertHelper.assertEverything(); | ||
// Quarkus specific - each VT has a unique name | ||
executions.add(Thread.currentThread().getName()); | ||
} | ||
|
||
@GET | ||
public Set<String> getExecutions() { | ||
return executions; | ||
} | ||
|
||
@GET | ||
@Path("/programmatic") | ||
public Set<String> getProgrammaticExecutions() { | ||
return programmaticExecutions; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...on-tests/virtual-threads/quartz-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 |
76 changes: 76 additions & 0 deletions
76
...threads/quartz-virtual-threads/src/test/java/io/quarkus/virtual/mail/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.mail; | ||
|
||
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
...ds/quartz-virtual-threads/src/test/java/io/quarkus/virtual/mail/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.mail; | ||
|
||
import io.quarkus.test.junit.QuarkusIntegrationTest; | ||
|
||
@QuarkusIntegrationTest | ||
class RunOnVirtualThreadIT extends RunOnVirtualThreadTest { | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
.../quartz-virtual-threads/src/test/java/io/quarkus/virtual/mail/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,43 @@ | ||
package io.quarkus.virtual.mail; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
|
||
import org.awaitility.Awaitility; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.restassured.RestAssured; | ||
import io.restassured.common.mapper.TypeRef; | ||
|
||
@QuarkusTest | ||
class RunOnVirtualThreadTest { | ||
|
||
@Test | ||
void testScheduledMethods() { | ||
Awaitility.await() | ||
.pollDelay(Duration.ofSeconds(2)) | ||
.untilAsserted(() -> { | ||
var list = RestAssured.get().then() | ||
.assertThat().statusCode(200) | ||
.extract().as(new TypeRef<List<String>>() { | ||
}); | ||
Assertions.assertTrue(list.size() > 3); | ||
}); | ||
} | ||
|
||
@Test | ||
void testScheduledMethodsUsingApi() { | ||
Awaitility.await() | ||
.pollDelay(Duration.ofSeconds(2)) | ||
.untilAsserted(() -> { | ||
var list = RestAssured.get("/programmatic").then() | ||
.assertThat().statusCode(200) | ||
.extract().as(new TypeRef<List<String>>() { | ||
}); | ||
Assertions.assertTrue(list.size() > 3); | ||
}); | ||
} | ||
|
||
} |
89 changes: 89 additions & 0 deletions
89
integration-tests/virtual-threads/scheduler-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,89 @@ | ||
<?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-scheduler</artifactId> | ||
<name>Quarkus - Integration Tests - Virtual Threads - Scheduler</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-resteasy-reactive-jackson</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-scheduler</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> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-scheduler-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
...ds/scheduler-virtual-threads/src/main/java/io/quarkus/virtual/scheduler/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.scheduler; | ||
|
||
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"); | ||
} | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...heduler-virtual-threads/src/main/java/io/quarkus/virtual/scheduler/ScheduledResource.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,50 @@ | ||
package io.quarkus.virtual.scheduler; | ||
|
||
import java.util.Set; | ||
import java.util.concurrent.CopyOnWriteArraySet; | ||
|
||
import jakarta.enterprise.event.Observes; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
|
||
import io.quarkus.runtime.StartupEvent; | ||
import io.quarkus.scheduler.Scheduled; | ||
import io.quarkus.scheduler.Scheduler; | ||
import io.smallrye.common.annotation.RunOnVirtualThread; | ||
|
||
@Path("/") | ||
public class ScheduledResource { | ||
|
||
Set<String> executions = new CopyOnWriteArraySet<>(); | ||
Set<String> programmaticExecutions = new CopyOnWriteArraySet<>(); | ||
|
||
public void init(@Observes StartupEvent ev, Scheduler scheduler) { | ||
scheduler.newJob("my-programmatic-job") | ||
.setInterval("1s") | ||
.setTask(ex -> { | ||
AssertHelper.assertEverything(); | ||
// Quarkus specific - each VT has a unique name | ||
programmaticExecutions.add(Thread.currentThread().getName()); | ||
}, true) | ||
.schedule(); | ||
} | ||
|
||
@Scheduled(every = "1s") | ||
@RunOnVirtualThread | ||
void run() { | ||
AssertHelper.assertEverything(); | ||
// Quarkus specific - each VT has a unique name | ||
executions.add(Thread.currentThread().getName()); | ||
} | ||
|
||
@GET | ||
public Set<String> getExecutions() { | ||
return executions; | ||
} | ||
|
||
@GET | ||
@Path("/programmatic") | ||
public Set<String> getProgrammaticExecutions() { | ||
return programmaticExecutions; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...tests/virtual-threads/scheduler-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 |
76 changes: 76 additions & 0 deletions
76
...eads/scheduler-virtual-threads/src/test/java/io/quarkus/virtual/mail/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.mail; | ||
|
||
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
...scheduler-virtual-threads/src/test/java/io/quarkus/virtual/mail/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.mail; | ||
|
||
import io.quarkus.test.junit.QuarkusIntegrationTest; | ||
|
||
@QuarkusIntegrationTest | ||
class RunOnVirtualThreadIT extends RunOnVirtualThreadTest { | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...heduler-virtual-threads/src/test/java/io/quarkus/virtual/mail/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,43 @@ | ||
package io.quarkus.virtual.mail; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
|
||
import org.awaitility.Awaitility; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.restassured.RestAssured; | ||
import io.restassured.common.mapper.TypeRef; | ||
|
||
@QuarkusTest | ||
class RunOnVirtualThreadTest { | ||
|
||
@Test | ||
void testScheduledMethods() { | ||
Awaitility.await() | ||
.pollDelay(Duration.ofSeconds(2)) | ||
.untilAsserted(() -> { | ||
var list = RestAssured.get().then() | ||
.assertThat().statusCode(200) | ||
.extract().as(new TypeRef<List<String>>() { | ||
}); | ||
Assertions.assertTrue(list.size() > 3); | ||
}); | ||
} | ||
|
||
@Test | ||
void testScheduledMethodsUsingApi() { | ||
Awaitility.await() | ||
.pollDelay(Duration.ofSeconds(2)) | ||
.untilAsserted(() -> { | ||
var list = RestAssured.get("/programmatic").then() | ||
.assertThat().statusCode(200) | ||
.extract().as(new TypeRef<List<String>>() { | ||
}); | ||
Assertions.assertTrue(list.size() > 3); | ||
}); | ||
} | ||
|
||
} |