forked from Archives/Athou_commafeed
add test for websocket ping/pong
This commit is contained in:
@@ -8,7 +8,6 @@ import jakarta.websocket.CloseReason;
|
|||||||
import jakarta.websocket.CloseReason.CloseCodes;
|
import jakarta.websocket.CloseReason.CloseCodes;
|
||||||
import jakarta.websocket.Endpoint;
|
import jakarta.websocket.Endpoint;
|
||||||
import jakarta.websocket.EndpointConfig;
|
import jakarta.websocket.EndpointConfig;
|
||||||
import jakarta.websocket.MessageHandler;
|
|
||||||
import jakarta.websocket.Session;
|
import jakarta.websocket.Session;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -30,15 +29,9 @@ public class WebSocketEndpoint extends Endpoint {
|
|||||||
sessions.add(userId, session);
|
sessions.add(userId, session);
|
||||||
}
|
}
|
||||||
|
|
||||||
// converting this anonymous inner class to a lambda causes the following error when a message is sent from the client
|
session.addMessageHandler(String.class, message -> {
|
||||||
// Unable to find decoder for type <javax.websocket.MessageHandler$Whole>
|
if ("ping".equals(message)) {
|
||||||
// this error is only visible when registering a listener to ws.onclose on the client
|
session.getAsyncRemote().sendText("pong");
|
||||||
session.addMessageHandler(new MessageHandler.Whole<String>() {
|
|
||||||
@Override
|
|
||||||
public void onMessage(String message) {
|
|
||||||
if ("ping".equals(message)) {
|
|
||||||
session.getAsyncRemote().sendText("pong");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import jakarta.websocket.ContainerProvider;
|
|||||||
import jakarta.websocket.DeploymentException;
|
import jakarta.websocket.DeploymentException;
|
||||||
import jakarta.websocket.Endpoint;
|
import jakarta.websocket.Endpoint;
|
||||||
import jakarta.websocket.EndpointConfig;
|
import jakarta.websocket.EndpointConfig;
|
||||||
import jakarta.websocket.MessageHandler;
|
|
||||||
import jakarta.websocket.Session;
|
import jakarta.websocket.Session;
|
||||||
|
|
||||||
class WebSocketIT extends BaseIT {
|
class WebSocketIT extends BaseIT {
|
||||||
@@ -26,24 +25,14 @@ class WebSocketIT extends BaseIT {
|
|||||||
@Test
|
@Test
|
||||||
void subscribeAndGetsNotified() throws DeploymentException, IOException {
|
void subscribeAndGetsNotified() throws DeploymentException, IOException {
|
||||||
String sessionId = login();
|
String sessionId = login();
|
||||||
ClientEndpointConfig config = ClientEndpointConfig.Builder.create().configurator(new ClientEndpointConfig.Configurator() {
|
ClientEndpointConfig config = buildConfig(sessionId);
|
||||||
@Override
|
|
||||||
public void beforeRequest(Map<String, List<String>> headers) {
|
|
||||||
headers.put("Cookie", Collections.singletonList("JSESSIONID=" + sessionId));
|
|
||||||
}
|
|
||||||
}).build();
|
|
||||||
|
|
||||||
AtomicBoolean connected = new AtomicBoolean();
|
AtomicBoolean connected = new AtomicBoolean();
|
||||||
AtomicReference<String> messageRef = new AtomicReference<>();
|
AtomicReference<String> messageRef = new AtomicReference<>();
|
||||||
try (Session ignored = ContainerProvider.getWebSocketContainer().connectToServer(new Endpoint() {
|
try (Session ignored = ContainerProvider.getWebSocketContainer().connectToServer(new Endpoint() {
|
||||||
@Override
|
@Override
|
||||||
public void onOpen(Session session, EndpointConfig config) {
|
public void onOpen(Session session, EndpointConfig config) {
|
||||||
session.addMessageHandler(new MessageHandler.Whole<String>() {
|
session.addMessageHandler(String.class, messageRef::set);
|
||||||
@Override
|
|
||||||
public void onMessage(String message) {
|
|
||||||
messageRef.set(message);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
connected.set(true);
|
connected.set(true);
|
||||||
}
|
}
|
||||||
}, config, URI.create(getWebSocketUrl()))) {
|
}, config, URI.create(getWebSocketUrl()))) {
|
||||||
@@ -56,4 +45,36 @@ class WebSocketIT extends BaseIT {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void pingPong() throws DeploymentException, IOException {
|
||||||
|
String sessionId = login();
|
||||||
|
ClientEndpointConfig config = buildConfig(sessionId);
|
||||||
|
|
||||||
|
AtomicBoolean connected = new AtomicBoolean();
|
||||||
|
AtomicReference<String> messageRef = new AtomicReference<>();
|
||||||
|
try (Session session = ContainerProvider.getWebSocketContainer().connectToServer(new Endpoint() {
|
||||||
|
@Override
|
||||||
|
public void onOpen(Session session, EndpointConfig config) {
|
||||||
|
session.addMessageHandler(String.class, messageRef::set);
|
||||||
|
connected.set(true);
|
||||||
|
}
|
||||||
|
}, config, URI.create(getWebSocketUrl()))) {
|
||||||
|
Awaitility.await().atMost(15, TimeUnit.SECONDS).untilTrue(connected);
|
||||||
|
|
||||||
|
session.getAsyncRemote().sendText("ping");
|
||||||
|
|
||||||
|
Awaitility.await().atMost(15, TimeUnit.SECONDS).until(() -> messageRef.get() != null);
|
||||||
|
Assertions.assertEquals("pong", messageRef.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private ClientEndpointConfig buildConfig(String sessionId) {
|
||||||
|
return ClientEndpointConfig.Builder.create().configurator(new ClientEndpointConfig.Configurator() {
|
||||||
|
@Override
|
||||||
|
public void beforeRequest(Map<String, List<String>> headers) {
|
||||||
|
headers.put("Cookie", Collections.singletonList("JSESSIONID=" + sessionId));
|
||||||
|
}
|
||||||
|
}).build();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user