Skip to content

Commit

Permalink
Merge pull request #44442 from mkouba/issue-44434
Browse files Browse the repository at this point in the history
WebSockets Next: add integration test module
  • Loading branch information
gsmet authored Nov 13, 2024
2 parents 3f10e98 + 3c955f4 commit 323ba29
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/native-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@
},
{
"category": "HTTP",
"timeout": 120,
"test-modules": "elytron-resteasy, resteasy-jackson, elytron-resteasy-reactive, resteasy-mutiny, resteasy-reactive-kotlin/standard, vertx, vertx-http, vertx-web, vertx-http-compressors/all, vertx-http-compressors/some, vertx-web-jackson, vertx-graphql, virtual-http, rest-client, rest-client-reactive, rest-client-reactive-stork, rest-client-reactive-multipart, websockets, management-interface, management-interface-auth, mutiny-native-jctools",
"timeout": 130,
"test-modules": "elytron-resteasy, resteasy-jackson, elytron-resteasy-reactive, resteasy-mutiny, resteasy-reactive-kotlin/standard, vertx, vertx-http, vertx-web, vertx-http-compressors/all, vertx-http-compressors/some, vertx-web-jackson, vertx-graphql, virtual-http, rest-client, rest-client-reactive, rest-client-reactive-stork, rest-client-reactive-multipart, websockets, websockets-next, management-interface, management-interface-auth, mutiny-native-jctools",
"os-name": "ubuntu-latest"
},
{
Expand Down
1 change: 1 addition & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
<module>vertx</module>
<module>vertx-kotlin</module>
<module>websockets</module>
<module>websockets-next</module>
<module>spring-di</module>
<module>spring-web</module>
<module>spring-data-jpa</module>
Expand Down
60 changes: 60 additions & 0 deletions integration-tests/websockets-next/pom.xml
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>
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;
}

}
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);
}

}

}
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 {
}
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();
}
}

}

0 comments on commit 323ba29

Please sign in to comment.