Skip to content

Commit

Permalink
Issue #5043 - add testing for WebSocketListener / Endpoint as anonymo…
Browse files Browse the repository at this point in the history
…us classes

Signed-off-by: Lachlan Roberts <[email protected]>
  • Loading branch information
lachlan-roberts committed Jul 16, 2020
1 parent ef60808 commit 00777fe
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@

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
Expand Down Expand Up @@ -84,7 +84,7 @@ public void onClose(CloseReason reason)
@ClientEndpoint
public static class BlockingClientEndpoint extends EventSocket
{
private CountDownLatch blockInClose = new CountDownLatch(1);
private final CountDownLatch blockInClose = new CountDownLatch(1);

public void unBlockClose()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,6 +40,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class EndpointEchoTest
{
Expand Down Expand Up @@ -105,4 +111,46 @@ public void testEchoClassRef() throws Exception
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@
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;

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;
Expand All @@ -60,6 +67,8 @@ public void before() throws Exception
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));
Expand Down Expand Up @@ -125,6 +134,46 @@ public void testBinaryListeners(Class<?> clazz) throws Exception
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());
Expand Down

0 comments on commit 00777fe

Please sign in to comment.