Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for websocket ping/pong #38745

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.quarkus.websockets.test;

import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

Expand All @@ -9,6 +12,7 @@
import jakarta.websocket.Endpoint;
import jakarta.websocket.EndpointConfig;
import jakarta.websocket.MessageHandler;
import jakarta.websocket.PongMessage;
import jakarta.websocket.Session;

import org.jboss.shrinkwrap.api.asset.StringAsset;
Expand Down Expand Up @@ -38,6 +42,7 @@ public class WebsocketRootPathTestCase {
public void testHttpRootPath() throws Exception {

LinkedBlockingDeque<String> message = new LinkedBlockingDeque<>();
LinkedBlockingDeque<String> pongMessages = new LinkedBlockingDeque<>();
Session session = ContainerProvider.getWebSocketContainer().connectToServer(new Endpoint() {
@Override
public void onOpen(Session session, EndpointConfig endpointConfig) {
Expand All @@ -47,12 +52,28 @@ public void onMessage(String s) {
message.add(s);
}
});
session.addMessageHandler(new MessageHandler.Whole<PongMessage>() {
@Override
public void onMessage(PongMessage s) {
ByteBuffer data = s.getApplicationData();
byte[] copy = new byte[data.remaining()];
data.get(copy);
pongMessages.add(new String(copy, StandardCharsets.UTF_8));
}
});
session.getAsyncRemote().sendText("hello");
try {
session.getAsyncRemote().sendPing(ByteBuffer.wrap("PING".getBytes(StandardCharsets.UTF_8)));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}, ClientEndpointConfig.Builder.create().build(), echoUri);

try {
Assertions.assertEquals("hello", message.poll(20, TimeUnit.SECONDS));

Assertions.assertEquals("PING", pongMessages.poll(20, TimeUnit.SECONDS));
} finally {
session.close();
}
Expand Down
Loading