-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23238 from gastaldi/websockets
Add websockets integration tests
- Loading branch information
Showing
5 changed files
with
201 additions
and
0 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,87 @@ | ||
<?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</artifactId> | ||
<name>Quarkus - Integration Tests - WebSockets</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-websockets</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-arc</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> | ||
|
||
<!-- Minimal test dependencies to *-deployment artifacts for consistent build order --> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-arc-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-websockets-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> |
57 changes: 57 additions & 0 deletions
57
integration-tests/websockets/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,57 @@ | ||
package io.quarkus.websockets; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.websocket.OnClose; | ||
import javax.websocket.OnError; | ||
import javax.websocket.OnMessage; | ||
import javax.websocket.OnOpen; | ||
import javax.websocket.Session; | ||
import javax.websocket.server.PathParam; | ||
import javax.websocket.server.ServerEndpoint; | ||
|
||
@ServerEndpoint("/chat/{username}") | ||
@ApplicationScoped | ||
public class ChatServer { | ||
|
||
Map<String, Session> sessions = new ConcurrentHashMap<>(); | ||
|
||
@OnOpen | ||
public void onOpen(Session session, @PathParam("username") String username) { | ||
sessions.put(username, session); | ||
} | ||
|
||
@OnClose | ||
public void onClose(Session session, @PathParam("username") String username) { | ||
sessions.remove(username); | ||
broadcast("User " + username + " left"); | ||
} | ||
|
||
@OnError | ||
public void onError(Session session, @PathParam("username") String username, Throwable throwable) { | ||
sessions.remove(username); | ||
broadcast("User " + username + " left on error: " + throwable); | ||
} | ||
|
||
@OnMessage | ||
public void onMessage(String message, @PathParam("username") String username) { | ||
if (message.equalsIgnoreCase("_ready_")) { | ||
broadcast("User " + username + " joined"); | ||
} else { | ||
broadcast(">> " + username + ": " + message); | ||
} | ||
} | ||
|
||
private void broadcast(String message) { | ||
sessions.values().forEach(s -> { | ||
s.getAsyncRemote().sendObject(message, result -> { | ||
if (result.getException() != null) { | ||
System.out.println("Unable to send message: " + result.getException()); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
integration-tests/websockets/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,55 @@ | ||
package io.quarkus.websockets; | ||
|
||
import java.net.URI; | ||
import java.util.concurrent.LinkedBlockingDeque; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.websocket.ClientEndpoint; | ||
import javax.websocket.ContainerProvider; | ||
import javax.websocket.OnMessage; | ||
import javax.websocket.OnOpen; | ||
import javax.websocket.Session; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
public class ChatTest { | ||
|
||
private static final LinkedBlockingDeque<String> MESSAGES = new LinkedBlockingDeque<>(); | ||
|
||
@TestHTTPResource("/chat/stu") | ||
URI uri; | ||
|
||
@Test | ||
public void testWebsocketChat() throws Exception { | ||
try (Session session = ContainerProvider.getWebSocketContainer().connectToServer(Client.class, uri)) { | ||
Assertions.assertEquals("CONNECT", MESSAGES.poll(10, TimeUnit.SECONDS)); | ||
Assertions.assertEquals("User stu joined", MESSAGES.poll(10, TimeUnit.SECONDS)); | ||
session.getAsyncRemote().sendText("hello world"); | ||
Assertions.assertEquals(">> stu: hello world", MESSAGES.poll(10, TimeUnit.SECONDS)); | ||
} | ||
} | ||
|
||
@ClientEndpoint | ||
public static class Client { | ||
|
||
@OnOpen | ||
public void open(Session session) { | ||
MESSAGES.add("CONNECT"); | ||
// Send a message to indicate that we are ready, | ||
// as the message handler may not be registered immediately after this callback. | ||
session.getAsyncRemote().sendText("_ready_"); | ||
} | ||
|
||
@OnMessage | ||
void message(String msg) { | ||
MESSAGES.add(msg); | ||
} | ||
|
||
} | ||
|
||
} |