Issue #5043 - add testing for WebSocketListener / Endpoint as anonymous classes
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
parent
ef60808bae
commit
00777fe05a
|
@ -50,11 +50,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||
|
||||
public class JavaxOnCloseTest
|
||||
{
|
||||
private static BlockingArrayQueue<OnCloseEndpoint> serverEndpoints = new BlockingArrayQueue<>();
|
||||
private static final BlockingArrayQueue<OnCloseEndpoint> serverEndpoints = new BlockingArrayQueue<>();
|
||||
|
||||
private Server server;
|
||||
private ServerConnector connector;
|
||||
private JavaxWebSocketClientContainer client = new JavaxWebSocketClientContainer();
|
||||
private final JavaxWebSocketClientContainer client = new JavaxWebSocketClientContainer();
|
||||
|
||||
@ServerEndpoint("/")
|
||||
public static class OnCloseEndpoint extends EventSocket
|
||||
|
@ -84,7 +84,7 @@ public class JavaxOnCloseTest
|
|||
@ClientEndpoint
|
||||
public static class BlockingClientEndpoint extends EventSocket
|
||||
{
|
||||
private CountDownLatch blockInClose = new CountDownLatch(1);
|
||||
private final CountDownLatch blockInClose = new CountDownLatch(1);
|
||||
|
||||
public void unBlockClose()
|
||||
{
|
||||
|
|
|
@ -18,13 +18,18 @@
|
|||
|
||||
package org.eclipse.jetty.websocket.javax.tests.client;
|
||||
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.websocket.CloseReason;
|
||||
import javax.websocket.ContainerProvider;
|
||||
import javax.websocket.Endpoint;
|
||||
import javax.websocket.EndpointConfig;
|
||||
import javax.websocket.MessageHandler;
|
||||
import javax.websocket.Session;
|
||||
import javax.websocket.WebSocketContainer;
|
||||
|
||||
import org.eclipse.jetty.util.BlockingArrayQueue;
|
||||
import org.eclipse.jetty.websocket.javax.common.JavaxWebSocketSession;
|
||||
import org.eclipse.jetty.websocket.javax.tests.LocalServer;
|
||||
import org.eclipse.jetty.websocket.javax.tests.WSEndpointTracker;
|
||||
|
@ -35,6 +40,7 @@ import org.junit.jupiter.api.Test;
|
|||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class EndpointEchoTest
|
||||
{
|
||||
|
@ -105,4 +111,46 @@ public class EndpointEchoTest
|
|||
session.close();
|
||||
endpoint.awaitCloseEvent("Client");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEchoAnonymousInstance() throws Exception
|
||||
{
|
||||
|
||||
CountDownLatch openLatch = new CountDownLatch(1);
|
||||
CountDownLatch closeLatch = new CountDownLatch(1);
|
||||
BlockingQueue<String> textMessages = new BlockingArrayQueue<>();
|
||||
Endpoint clientEndpoint = new Endpoint()
|
||||
{
|
||||
@Override
|
||||
public void onOpen(Session session, EndpointConfig config)
|
||||
{
|
||||
// Cannot replace this with a lambda or it breaks ReflectUtils.findGenericClassFor().
|
||||
session.addMessageHandler(new MessageHandler.Whole<String>()
|
||||
{
|
||||
@Override
|
||||
public void onMessage(String message)
|
||||
{
|
||||
textMessages.add(message);
|
||||
}
|
||||
});
|
||||
openLatch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose(Session session, CloseReason closeReason)
|
||||
{
|
||||
closeLatch.countDown();
|
||||
}
|
||||
};
|
||||
|
||||
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
|
||||
Session session = container.connectToServer(clientEndpoint, null, server.getWsUri().resolve("/echo/text"));
|
||||
assertTrue(openLatch.await(5, TimeUnit.SECONDS));
|
||||
session.getBasicRemote().sendText("Echo");
|
||||
|
||||
String resp = textMessages.poll(1, TimeUnit.SECONDS);
|
||||
assertThat("Response echo", resp, is("Echo"));
|
||||
session.close();
|
||||
assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,8 @@ package org.eclipse.jetty.websocket.tests.listeners;
|
|||
import java.net.URI;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
@ -28,13 +30,18 @@ import java.util.stream.Stream;
|
|||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.util.BlockingArrayQueue;
|
||||
import org.eclipse.jetty.util.BufferUtil;
|
||||
import org.eclipse.jetty.websocket.api.Session;
|
||||
import org.eclipse.jetty.websocket.api.StatusCode;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketListener;
|
||||
import org.eclipse.jetty.websocket.client.WebSocketClient;
|
||||
import org.eclipse.jetty.websocket.server.config.JettyWebSocketServletContainerInitializer;
|
||||
import org.eclipse.jetty.websocket.tests.EchoSocket;
|
||||
import org.eclipse.jetty.websocket.tests.EventSocket;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
@ -60,6 +67,8 @@ public class WebSocketListenerTest
|
|||
contextHandler.setContextPath("/");
|
||||
JettyWebSocketServletContainerInitializer.configure(contextHandler, (context, container) ->
|
||||
{
|
||||
container.addMapping("/echo", (req, res) -> new EchoSocket());
|
||||
|
||||
for (Class<?> c : getClassListFromArguments(TextListeners.getTextListeners()))
|
||||
{
|
||||
container.addMapping("/text/" + c.getSimpleName(), (req, res) -> construct(c));
|
||||
|
@ -125,6 +134,46 @@ public class WebSocketListenerTest
|
|||
assertThat(clientEndpoint.closeReason, is("standard close"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnonymousListener() throws Exception
|
||||
{
|
||||
CountDownLatch openLatch = new CountDownLatch(1);
|
||||
CountDownLatch closeLatch = new CountDownLatch(1);
|
||||
BlockingQueue<String> textMessages = new BlockingArrayQueue<>();
|
||||
WebSocketListener clientEndpoint = new WebSocketListener() {
|
||||
@Override
|
||||
public void onWebSocketConnect(Session session)
|
||||
{
|
||||
openLatch.countDown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWebSocketText(String message)
|
||||
{
|
||||
textMessages.add(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWebSocketClose(int statusCode, String reason)
|
||||
{
|
||||
closeLatch.countDown();
|
||||
}
|
||||
};
|
||||
|
||||
Session session = client.connect(clientEndpoint, serverUri.resolve("/echo")).get(5, TimeUnit.SECONDS);
|
||||
assertTrue(openLatch.await(5, TimeUnit.SECONDS));
|
||||
|
||||
// Send and receive echo on client.
|
||||
String payload = "hello world";
|
||||
session.getRemote().sendString(payload);
|
||||
String echoMessage = textMessages.poll(5, TimeUnit.SECONDS);
|
||||
assertThat(echoMessage, is(payload));
|
||||
|
||||
// Close normally.
|
||||
session.close(StatusCode.NORMAL, "standard close");
|
||||
assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
private List<Class<?>> getClassListFromArguments(Stream<Arguments> stream)
|
||||
{
|
||||
return stream.map(arguments -> (Class<?>)arguments.get()[0]).collect(Collectors.toList());
|
||||
|
|
Loading…
Reference in New Issue