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.
WebSockets Next: add integration test module
- add this module in the native-tests.json config in the HTTP category - fixes quarkusio#44434
- Loading branch information
Showing
7 changed files
with
224 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?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-integration-tests-parent</artifactId> | ||
<groupId>io.quarkus</groupId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>quarkus-integration-test-websockets-next</artifactId> | ||
<name>Quarkus - Integration Tests - WebSockets Next</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-websockets-next</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-junit5</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- Minimal test dependencies to *-deployment artifacts for consistent build order --> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-websockets-next-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> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>build</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
40 changes: 40 additions & 0 deletions
40
integration-tests/websockets-next/src/main/java/io/quarkus/websockets/ChatServer.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,40 @@ | ||
package io.quarkus.websockets; | ||
|
||
import io.quarkus.runtime.annotations.RegisterForReflection; | ||
import io.quarkus.websockets.next.OnClose; | ||
import io.quarkus.websockets.next.OnOpen; | ||
import io.quarkus.websockets.next.OnTextMessage; | ||
import io.quarkus.websockets.next.PathParam; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.WebSocketConnection; | ||
|
||
@WebSocket(path = "/chat/{username}") | ||
public class ChatServer { | ||
|
||
public enum MessageType { | ||
USER_JOINED, | ||
USER_LEFT, | ||
CHAT_MESSAGE | ||
} | ||
|
||
@RegisterForReflection | ||
public record ChatMessage(MessageType type, String from, String message) { | ||
} | ||
|
||
@OnOpen(broadcast = true) | ||
public ChatMessage onOpen(@PathParam String username) { | ||
return new ChatMessage(MessageType.USER_JOINED, username, "Hello!"); | ||
} | ||
|
||
@OnClose | ||
public void onClose(WebSocketConnection connection) { | ||
connection.broadcast() | ||
.sendTextAndAwait(new ChatMessage(MessageType.USER_LEFT, connection.pathParam("username"), "Bye!")); | ||
} | ||
|
||
@OnTextMessage(broadcast = true) | ||
public ChatMessage onMessage(ChatMessage message) { | ||
return message; | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
integration-tests/websockets-next/src/test/java/io/quarkus/websockets/ChatClientTest.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,55 @@ | ||
package io.quarkus.websockets; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.net.URI; | ||
import java.util.concurrent.LinkedBlockingDeque; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.websockets.ChatServer.ChatMessage; | ||
import io.quarkus.websockets.ChatServer.MessageType; | ||
import io.quarkus.websockets.next.OnTextMessage; | ||
import io.quarkus.websockets.next.WebSocketClient; | ||
import io.quarkus.websockets.next.WebSocketClientConnection; | ||
import io.quarkus.websockets.next.WebSocketConnector; | ||
|
||
@QuarkusTest | ||
public class ChatClientTest { | ||
|
||
private static final LinkedBlockingDeque<ChatMessage> MESSAGES = new LinkedBlockingDeque<>(); | ||
|
||
@TestHTTPResource("/") | ||
URI uri; | ||
|
||
@Inject | ||
WebSocketConnector<ChatClient> connector; | ||
|
||
@Test | ||
public void testWebsocketChat() throws Exception { | ||
WebSocketClientConnection connection = connector | ||
.baseUri(uri) | ||
.pathParam("username", "Tom") | ||
.connectAndAwait(); | ||
assertEquals(new ChatMessage(MessageType.USER_JOINED, "Tom", "Hello!"), MESSAGES.poll(10, TimeUnit.SECONDS)); | ||
connection.sendTextAndAwait(new ChatMessage(MessageType.CHAT_MESSAGE, "Tom", "Ping")); | ||
assertEquals(new ChatMessage(MessageType.CHAT_MESSAGE, "Tom", "Ping"), MESSAGES.poll(10, TimeUnit.SECONDS)); | ||
connection.closeAndAwait(); | ||
} | ||
|
||
@WebSocketClient(path = "/chat/{username}") | ||
public static class ChatClient { | ||
|
||
@OnTextMessage | ||
void message(ChatMessage message) { | ||
MESSAGES.add(message); | ||
} | ||
|
||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
integration-tests/websockets-next/src/test/java/io/quarkus/websockets/ChatIT.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,7 @@ | ||
package io.quarkus.websockets; | ||
|
||
import io.quarkus.test.junit.QuarkusIntegrationTest; | ||
|
||
@QuarkusIntegrationTest | ||
public class ChatIT extends ChatTest { | ||
} |
59 changes: 59 additions & 0 deletions
59
integration-tests/websockets-next/src/test/java/io/quarkus/websockets/ChatTest.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,59 @@ | ||
package io.quarkus.websockets; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.websockets.ChatServer.ChatMessage; | ||
import io.quarkus.websockets.ChatServer.MessageType; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.http.WebSocketClient; | ||
import io.vertx.core.http.WebSocketConnectOptions; | ||
import io.vertx.core.json.Json; | ||
|
||
@QuarkusTest | ||
public class ChatTest { | ||
|
||
@TestHTTPResource("/chat/Tom") | ||
URI uri; | ||
|
||
@Test | ||
public void testWebsocketChat() throws Exception { | ||
CountDownLatch messageLatch = new CountDownLatch(2); | ||
List<ChatMessage> messages = new CopyOnWriteArrayList<>(); | ||
Vertx vertx = Vertx.vertx(); | ||
WebSocketClient client = vertx.createWebSocketClient(); | ||
try { | ||
client.connect(new WebSocketConnectOptions() | ||
.setHost(uri.getHost()) | ||
.setPort(uri.getPort()) | ||
.setURI(uri.getPath())) | ||
.onSuccess( | ||
ws -> { | ||
ws.textMessageHandler(m -> { | ||
messages.add(Json.decodeValue(m, ChatMessage.class)); | ||
messageLatch.countDown(); | ||
}); | ||
ws.writeTextMessage(Json.encode(new ChatMessage(MessageType.CHAT_MESSAGE, "Tom", "Ping"))); | ||
}); | ||
assertTrue(messageLatch.await(10, TimeUnit.SECONDS), messageLatch.toString()); | ||
assertEquals(new ChatMessage(MessageType.USER_JOINED, "Tom", "Hello!"), | ||
messages.get(0)); | ||
assertEquals(new ChatMessage(MessageType.CHAT_MESSAGE, "Tom", "Ping"), | ||
messages.get(1)); | ||
} finally { | ||
client.close().toCompletionStage().toCompletableFuture().get(5, TimeUnit.SECONDS); | ||
vertx.close(); | ||
} | ||
} | ||
|
||
} |