From 5ef195b8c222c8dc8db3d85e0b2d8a8bb585e2d3 Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Tue, 13 Feb 2024 14:02:45 +1100 Subject: [PATCH] Add test for ping/pong Fixes #31157 --- .../test/WebsocketRootPathTestCase.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/extensions/websockets/server/deployment/src/test/java/io/quarkus/websockets/test/WebsocketRootPathTestCase.java b/extensions/websockets/server/deployment/src/test/java/io/quarkus/websockets/test/WebsocketRootPathTestCase.java index a0c823eb99fc9..502cfe3a7dce2 100644 --- a/extensions/websockets/server/deployment/src/test/java/io/quarkus/websockets/test/WebsocketRootPathTestCase.java +++ b/extensions/websockets/server/deployment/src/test/java/io/quarkus/websockets/test/WebsocketRootPathTestCase.java @@ -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; @@ -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; @@ -38,6 +42,7 @@ public class WebsocketRootPathTestCase { public void testHttpRootPath() throws Exception { LinkedBlockingDeque message = new LinkedBlockingDeque<>(); + LinkedBlockingDeque pongMessages = new LinkedBlockingDeque<>(); Session session = ContainerProvider.getWebSocketContainer().connectToServer(new Endpoint() { @Override public void onOpen(Session session, EndpointConfig endpointConfig) { @@ -47,12 +52,28 @@ public void onMessage(String s) { message.add(s); } }); + session.addMessageHandler(new MessageHandler.Whole() { + @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(); }