Skip to content

Commit

Permalink
WebSockets Next: add test for virtual threads
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba committed May 7, 2024
1 parent 2bfd9cb commit a4f9f57
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
45 changes: 45 additions & 0 deletions extensions/websockets-next/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-websockets-next</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-test-vertx</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
Expand Down Expand Up @@ -69,4 +76,42 @@
</plugins>
</build>

<profiles>
<profile>
<id>java-21</id>
<activation>
<jdk>[21,)</jdk>
</activation>

<properties>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<!-- javaparser 3.25.10 used by impsort-maven-plugin does not define the "language level" for 21 -->
<!--maven.compiler.release>21</maven.compiler.release-->
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-java21-directory</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/java21</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.quarkus.websockets.next.test.virtualthreads;

import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.net.URI;

import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.vertx.VirtualThreadsAssertions;
import io.quarkus.websockets.next.OnError;
import io.quarkus.websockets.next.OnTextMessage;
import io.quarkus.websockets.next.WebSocket;
import io.quarkus.websockets.next.test.utils.WSClient;
import io.smallrye.common.annotation.RunOnVirtualThread;
import io.vertx.core.Vertx;
import jakarta.inject.Inject;

public class RunOnVirtualThreadTest {

@RegisterExtension
public static final QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot(root -> {
root.addClasses(Endpoint.class, WSClient.class)
.addAsResource(new StringAsset(
"quarkus.virtual-threads.name-prefix=wsnext-virtual-thread-"),
"application.properties");
});

@Inject
Vertx vertx;

@TestHTTPResource("end")
URI endUri;

@Test
void testVirtualThreads() {
try (WSClient client = new WSClient(vertx).connect(endUri)) {
client.sendAndAwait("foo");
client.sendAndAwait("bar");
client.waitForMessages(2);
String message1 = client.getMessages().get(0).toString();
String message2 = client.getMessages().get(1).toString();
assertNotEquals(message1, message2);
assertTrue(message1.startsWith("wsnext-virtual-thread-"));
assertTrue(message2.startsWith("wsnext-virtual-thread-"));
}
}

@WebSocket(path = "/end")
public static class Endpoint {

@RunOnVirtualThread
@OnTextMessage
String text(String ignored) {
VirtualThreadsAssertions.assertEverything();
return Thread.currentThread().getName();
}

@OnError
String error(Throwable t) {
return t.toString();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* Some types of messages bypass the encoding/decoding process:
* <ul>
* <li>{@code java.lang.Buffer},</li>
* <li>{@code io.vertx.core.buffer.Buffer},</li>
* <li>{@code byte[]},</li>
* <li>{@code java.lang.String},</li>
* <li>{@code io.vertx.core.json.JsonObject}.</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* Some types of messages bypass the encoding/decoding process:
* <ul>
* <li>{@code java.lang.Buffer},</li>
* <li>{@code io.vertx.core.buffer.Buffer},</li>
* <li>{@code byte[]},</li>
* <li>{@code java.lang.String},</li>
* <li>{@code io.vertx.core.json.JsonObject}.</li>
Expand Down

0 comments on commit a4f9f57

Please sign in to comment.