remove exception stack traces and prints from websocket tests

Signed-off-by: lachan-roberts <lachlan@webtide.com>
This commit is contained in:
lachan-roberts 2019-04-29 12:32:23 +10:00 committed by Greg Wilkins
parent 15d4659cc0
commit 8360aad619
11 changed files with 87 additions and 228 deletions

View File

@ -43,6 +43,8 @@ import org.eclipse.jetty.security.authentication.BasicAuthenticator;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.util.security.Credential;
import org.eclipse.jetty.websocket.javax.server.examples.GetHttpSessionSocket;
@ -57,6 +59,8 @@ import static org.hamcrest.Matchers.is;
public class WebSocketServerExamplesTest
{
private static final Logger LOG = Log.getLogger(WebSocketServerExamplesTest.class);
@ClientEndpoint
public static class ClientSocket
{
@ -66,27 +70,27 @@ public class WebSocketServerExamplesTest
@OnOpen
public void onOpen(Session sess)
{
System.err.println("ClientSocket Connected: " + sess);
LOG.debug("ClientSocket Connected: " + sess);
}
@OnMessage
public void onMessage(String message)
{
messageQueue.offer(message);
System.err.println("Received TEXT message: " + message);
LOG.debug("Received TEXT message: " + message);
}
@OnClose
public void onClose(CloseReason closeReason)
{
System.err.println("ClientSocket Closed: " + closeReason);
LOG.debug("ClientSocket Closed: " + closeReason);
closed.countDown();
}
@OnError
public void onError(Throwable cause)
{
cause.printStackTrace(System.err);
LOG.debug(cause);
}
}

View File

@ -25,10 +25,12 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.CloseReason;
import javax.websocket.ContainerProvider;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
@ -132,6 +134,8 @@ public class MessageReceivingTest
msg = clientEndpoint.handler.messageQueue.poll(5, TimeUnit.SECONDS);
assertThat("Echo'd Message: another", msg, is("Another Echo"));
}
clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS);
}
/**
@ -162,6 +166,8 @@ public class MessageReceivingTest
msg = clientEndpoint.handler.messageQueue.poll(5, TimeUnit.SECONDS);
assertThat("Received Message", msg, is("I can live for two months on a good compliment."));
}
clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS);
}
/**
@ -190,6 +196,8 @@ public class MessageReceivingTest
msg = clientEndpoint.handler.messageQueue.poll(5, TimeUnit.SECONDS);
assertThat("Received Message", msg, is("Echo"));
}
clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS);
}
/**
@ -228,6 +236,8 @@ public class MessageReceivingTest
msg = clientEndpoint.handler.messageQueue.poll(5, TimeUnit.SECONDS);
assertThat("Received Message: another", msg, is("Another Echo"));
}
clientEndpoint.closeLatch.await(5, TimeUnit.SECONDS);
}
public static class SendPartialTextFrameHandler extends MessageHandler
@ -373,6 +383,7 @@ public class MessageReceivingTest
public static class TestEndpoint extends Endpoint
{
public final AbstractHandler handler;
public final CountDownLatch closeLatch = new CountDownLatch(1);
public TestEndpoint(AbstractHandler handler)
{
@ -387,6 +398,12 @@ public class MessageReceivingTest
session.addMessageHandler(handler);
}
@Override
public void onClose(Session session, CloseReason closeReason)
{
closeLatch.countDown();
}
@Override
public void onError(Session session, Throwable thr)
{

View File

@ -20,16 +20,15 @@ package org.eclipse.jetty.websocket.tests;
import java.io.IOException;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
@WebSocket
public class EchoSocket
public class EchoSocket extends EventSocket
{
@OnWebSocketMessage
public void onMessage(Session session, String msg) throws IOException
@Override
public void onMessage(String message) throws IOException
{
session.getRemote().sendString(msg);
super.onMessage(message);
session.getRemote().sendString(message);
}
}

View File

@ -35,17 +35,17 @@ import org.eclipse.jetty.websocket.api.annotations.WebSocket;
@WebSocket
public class EventSocket
{
private static Logger LOG = Log.getLogger(EventSocket.class);
private final static Logger LOG = Log.getLogger(EventSocket.class);
protected Session session;
private String behavior;
public volatile Throwable failure = null;
public BlockingQueue<String> receivedMessages = new BlockingArrayQueue<>();
public BlockingQueue<String> messageQueue = new BlockingArrayQueue<>();
public volatile Throwable error = null;
public CountDownLatch open = new CountDownLatch(1);
public CountDownLatch error = new CountDownLatch(1);
public CountDownLatch closed = new CountDownLatch(1);
public CountDownLatch openLatch = new CountDownLatch(1);
public CountDownLatch errorLatch = new CountDownLatch(1);
public CountDownLatch closeLatch = new CountDownLatch(1);
@OnWebSocketConnect
public void onOpen(Session session)
@ -53,29 +53,29 @@ public class EventSocket
this.session = session;
behavior = session.getPolicy().getBehavior().name();
LOG.info("{} onOpen(): {}", toString(), session);
open.countDown();
openLatch.countDown();
}
@OnWebSocketMessage
public void onMessage(String message) throws IOException
{
LOG.info("{} onMessage(): {}", toString(), message);
receivedMessages.offer(message);
messageQueue.offer(message);
}
@OnWebSocketClose
public void onClose(int statusCode, String reason)
{
LOG.info("{} onClose(): {}:{}", toString(), statusCode, reason);
closed.countDown();
closeLatch.countDown();
}
@OnWebSocketError
public void onError(Throwable cause)
{
LOG.info("{} onError(): {}", toString(), cause);
failure = cause;
error.countDown();
error = cause;
errorLatch.countDown();
}
@Override
@ -83,15 +83,4 @@ public class EventSocket
{
return String.format("[%s@%s]", behavior, Integer.toHexString(hashCode()));
}
@WebSocket
public static class EchoSocket extends EventSocket
{
@Override
public void onMessage(String message) throws IOException
{
super.onMessage(message);
session.getRemote().sendStringByFuture(message);
}
}
}

View File

@ -51,8 +51,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class JettyWebSocketExtensionConfigTest
{
Server server;
WebSocketClient client;
private Server server;
private WebSocketClient client;
@BeforeEach
public void start() throws Exception
@ -127,10 +127,10 @@ public class JettyWebSocketExtensionConfigTest
{
session.getRemote().sendString("hello world");
}
assertTrue(socket.closed.await(5, TimeUnit.SECONDS));
assertTrue(socket.closeLatch.await(5, TimeUnit.SECONDS));
assertTrue(correctResponseExtensions.await(5, TimeUnit.SECONDS));
String msg = socket.receivedMessages.poll();
String msg = socket.messageQueue.poll();
assertThat(msg, is("hello world"));
}
}

View File

@ -39,8 +39,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class JettyWebSocketFilterTest
{
Server server;
WebSocketClient client;
private Server server;
private WebSocketClient client;
@BeforeEach
public void start() throws Exception
@ -55,7 +55,7 @@ public class JettyWebSocketFilterTest
server.setHandler(contextHandler);
JettyWebSocketServerContainer container = JettyWebSocketServletContainerInitializer.configureContext(contextHandler);
container.addMapping("/", (req, resp)->new EventSocket.EchoSocket());
container.addMapping("/", (req, resp)->new EchoSocket());
server.start();
client = new WebSocketClient();
@ -79,9 +79,9 @@ public class JettyWebSocketFilterTest
{
session.getRemote().sendString("hello world");
}
assertTrue(socket.closed.await(10, TimeUnit.SECONDS));
assertTrue(socket.closeLatch.await(10, TimeUnit.SECONDS));
String msg = socket.receivedMessages.poll();
String msg = socket.messageQueue.poll();
assertThat(msg, is("hello world"));
}
}

View File

@ -44,9 +44,9 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
public class JettyWebSocketNegotiationTest
{
Server server;
WebSocketClient client;
ServletContextHandler contextHandler;
private Server server;
private WebSocketClient client;
private ServletContextHandler contextHandler;
@BeforeEach
public void start() throws Exception
@ -76,7 +76,7 @@ public class JettyWebSocketNegotiationTest
public void testBadRequest() throws Exception
{
JettyWebSocketServerContainer container = JettyWebSocketServletContainerInitializer.configureContext(contextHandler);
container.addMapping("/", (req, resp)->new EventSocket.EchoSocket());
container.addMapping("/", (req, resp)->new EchoSocket());
URI uri = URI.create("ws://localhost:8080/filterPath");
EventSocket socket = new EventSocket();
@ -90,7 +90,6 @@ public class JettyWebSocketNegotiationTest
assertThat(t.getMessage(), containsString("400 Bad Request"));
}
@Test
public void testServerError() throws Exception
{
@ -98,7 +97,7 @@ public class JettyWebSocketNegotiationTest
container.addMapping("/", (req, resp)->
{
resp.setAcceptedSubProtocol("errorSubProtocol");
return new EventSocket.EchoSocket();
return new EchoSocket();
});
URI uri = URI.create("ws://localhost:8080/filterPath");

View File

@ -45,12 +45,12 @@ public class JettyWebSocketServletTest
@Override
public void configure(JettyWebSocketServletFactory factory)
{
factory.addMapping("/",(req, resp)->new EventSocket.EchoSocket());
factory.addMapping("/",(req, resp)->new EchoSocket());
}
}
Server server;
WebSocketClient client;
private Server server;
private WebSocketClient client;
@BeforeEach
public void start() throws Exception
@ -73,7 +73,6 @@ public class JettyWebSocketServletTest
client.start();
}
@AfterEach
public void stop() throws Exception
{
@ -91,9 +90,9 @@ public class JettyWebSocketServletTest
{
session.getRemote().sendString("hello world");
}
assertTrue(socket.closed.await(10, TimeUnit.SECONDS));
assertTrue(socket.closeLatch.await(10, TimeUnit.SECONDS));
String msg = socket.receivedMessages.poll();
String msg = socket.messageQueue.poll();
assertThat(msg, is("hello world"));
}
}

View File

@ -19,25 +19,15 @@
package org.eclipse.jetty.websocket.tests;
import java.net.URI;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.BlockingArrayQueue;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.SuspendToken;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.server.JettyWebSocketServlet;
import org.eclipse.jetty.websocket.server.JettyWebSocketServletContainerInitializer;
@ -53,48 +43,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class SuspendResumeTest
{
@WebSocket
public static class EventSocket
{
private static final Logger LOG = Log.getLogger(EventSocket.class);
BlockingArrayQueue<String> messages = new BlockingArrayQueue<>();
CountDownLatch openLatch = new CountDownLatch(1);
CountDownLatch closeLatch = new CountDownLatch(1);
AtomicReference<Throwable> error = new AtomicReference<>();
Session session;
@OnWebSocketConnect
public void onConnect(Session session)
{
LOG.info("onConnect(): " + session);
this.session = session;
openLatch.countDown();
}
@OnWebSocketMessage
public void onMessage(String message)
{
LOG.info("onMessage(): " + message);
messages.offer(message);
}
@OnWebSocketError
public void onError(Throwable t)
{
LOG.info("onError(): " + t);
error.compareAndSet(null, t);
}
@OnWebSocketClose
public void onClose(int statusCode, String reason)
{
LOG.info("onClose(): " + statusCode + ":" + reason);
closeLatch.countDown();
}
}
public class UpgradeServlet extends JettyWebSocketServlet
{
@Override
@ -145,22 +93,22 @@ public class SuspendResumeTest
// verify connection by sending a message from server to client
assertTrue(serverSocket.openLatch.await(5, TimeUnit.SECONDS));
serverSocket.session.getRemote().sendStringByFuture("verification");
assertThat(clientSocket.messages.poll(5, TimeUnit.SECONDS), is("verification"));
assertThat(clientSocket.messageQueue.poll(5, TimeUnit.SECONDS), is("verification"));
// suspend the client so that no read events occur
SuspendToken suspendToken = clientSocket.session.suspend();
// verify client can still send messages
clientSocket.session.getRemote().sendStringByFuture("message-from-client");
assertThat(serverSocket.messages.poll(5, TimeUnit.SECONDS), is("message-from-client"));
assertThat(serverSocket.messageQueue.poll(5, TimeUnit.SECONDS), is("message-from-client"));
// the message is not received as it is suspended
serverSocket.session.getRemote().sendStringByFuture("message-from-server");
assertNull(clientSocket.messages.poll(2, TimeUnit.SECONDS));
assertNull(clientSocket.messageQueue.poll(2, TimeUnit.SECONDS));
// client should receive message after it resumes
suspendToken.resume();
assertThat(clientSocket.messages.poll(5, TimeUnit.SECONDS), is("message-from-server"));
assertThat(clientSocket.messageQueue.poll(5, TimeUnit.SECONDS), is("message-from-server"));
// make sure both sides are closed
clientSocket.session.close();
@ -168,7 +116,7 @@ public class SuspendResumeTest
assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
// check no errors occurred
assertNull(clientSocket.error.get());
assertNull(serverSocket.error.get());
assertNull(clientSocket.error);
assertNull(serverSocket.error);
}
}

View File

@ -19,9 +19,7 @@
package org.eclipse.jetty.websocket.tests;
import java.net.URI;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.client.api.AuthenticationStore;
@ -39,19 +37,14 @@ import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.util.security.Credential;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.server.JettyWebSocketServletContainerInitializer;
import org.eclipse.jetty.websocket.tests.examples.MyAdvancedEchoServlet;
import org.eclipse.jetty.websocket.tests.examples.MyAuthedServlet;
import org.eclipse.jetty.websocket.tests.examples.MyEchoServlet;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
@ -60,46 +53,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class WebSocketServletExamplesTest
{
private Server _server;
private ServletContextHandler _context;
@WebSocket
public static class ClientSocket
{
CountDownLatch closed = new CountDownLatch(1);
ArrayBlockingQueue<String> messageQueue = new ArrayBlockingQueue<>(2);
@OnWebSocketConnect
public void onOpen(Session sess)
{
System.err.println("ClientSocket Connected: " + sess);
}
@OnWebSocketMessage
public void onMessage(String message)
{
messageQueue.offer(message);
System.err.println("Received TEXT message: " + message);
}
@OnWebSocketClose
public void onClose(int statusCode, String reason)
{
System.err.println("ClientSocket Closed: " + statusCode + ":" + reason);
closed.countDown();
}
@OnWebSocketError
public void onError(Throwable cause)
{
cause.printStackTrace(System.err);
}
}
static Server _server;
static ServletContextHandler _context;
@BeforeAll
public static void setup() throws Exception
@BeforeEach
public void setup() throws Exception
{
_server = new Server();
ServerConnector connector = new ServerConnector(_server);
@ -119,8 +77,8 @@ public class WebSocketServletExamplesTest
_server.start();
}
@AfterAll
public static void stop() throws Exception
@AfterEach
public void stop() throws Exception
{
_server.stop();
}
@ -150,7 +108,6 @@ public class WebSocketServletExamplesTest
return security;
}
@Test
public void testEchoServlet() throws Exception
{
@ -158,7 +115,7 @@ public class WebSocketServletExamplesTest
client.start();
URI uri = URI.create("ws://localhost:8080/echo");
ClientSocket socket = new ClientSocket();
EventSocket socket = new EventSocket();
CompletableFuture<Session> connect = client.connect(socket, uri);
try (Session session = connect.get(5, TimeUnit.SECONDS))
{
@ -169,10 +126,9 @@ public class WebSocketServletExamplesTest
assertThat(response, is(message));
}
assertTrue(socket.closed.await(10, TimeUnit.SECONDS));
assertTrue(socket.closeLatch.await(10, TimeUnit.SECONDS));
}
@Test
public void testAdvancedEchoServlet() throws Exception
{
@ -180,7 +136,7 @@ public class WebSocketServletExamplesTest
client.start();
URI uri = URI.create("ws://localhost:8080/advancedEcho");
ClientSocket socket = new ClientSocket();
EventSocket socket = new EventSocket();
UpgradeRequest upgradeRequest = new ClientUpgradeRequest();
upgradeRequest.setSubProtocols("text");
@ -194,10 +150,9 @@ public class WebSocketServletExamplesTest
assertThat(response, is(message));
}
assertTrue(socket.closed.await(10, TimeUnit.SECONDS));
assertTrue(socket.closeLatch.await(10, TimeUnit.SECONDS));
}
@Test
public void testAuthedServlet() throws Exception
{
@ -210,7 +165,7 @@ public class WebSocketServletExamplesTest
BasicAuthentication basicAuthentication = new BasicAuthentication(uri, "testRealm", "user", "password");
authenticationStore.addAuthentication(basicAuthentication);
ClientSocket socket = new ClientSocket();
EventSocket socket = new EventSocket();
CompletableFuture<Session> connect = client.connect(socket, uri);
try (Session session = connect.get(5, TimeUnit.SECONDS))
{
@ -221,6 +176,6 @@ public class WebSocketServletExamplesTest
assertThat(response, is(message));
}
assertTrue(socket.closed.await(10, TimeUnit.SECONDS));
assertTrue(socket.closeLatch.await(10, TimeUnit.SECONDS));
}
}

View File

@ -34,12 +34,6 @@ import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WriteCallback;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.core.Frame;
import org.eclipse.jetty.websocket.core.OpCode;
@ -58,51 +52,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class WebSocketStatsTest
{
@WebSocket
public static class ClientSocket
{
CountDownLatch closed = new CountDownLatch(1);
String behavior;
@OnWebSocketConnect
public void onOpen(Session session)
{
behavior = session.getPolicy().getBehavior().name();
System.err.println(toString() + " Socket Connected: " + session);
}
@OnWebSocketClose
public void onClose(int statusCode, String reason)
{
System.err.println(toString() + " Socket Closed: " + statusCode + ":" + reason);
closed.countDown();
}
@OnWebSocketError
public void onError(Throwable cause)
{
cause.printStackTrace(System.err);
}
@Override
public String toString()
{
return String.format("[%s@%s]", behavior, Integer.toHexString(hashCode()));
}
}
@WebSocket
public static class EchoSocket extends ClientSocket
{
@OnWebSocketMessage
public void onMessage(Session session, String message)
{
session.getRemote().sendString(message, WriteCallback.NOOP);
}
}
public static class MyWebSocketServlet extends JettyWebSocketServlet
{
@Override
@ -113,11 +62,11 @@ public class WebSocketStatsTest
}
}
Server server;
WebSocketClient client;
ConnectionStatistics statistics;
CountDownLatch wsUpgradeComplete = new CountDownLatch(1);
CountDownLatch wsConnectionClosed = new CountDownLatch(1);
private Server server;
private WebSocketClient client;
private ConnectionStatistics statistics;
private CountDownLatch wsUpgradeComplete = new CountDownLatch(1);
private CountDownLatch wsConnectionClosed = new CountDownLatch(1);
@BeforeEach
public void start() throws Exception
@ -176,7 +125,7 @@ public class WebSocketStatsTest
public void echoStatsTest() throws Exception
{
URI uri = URI.create("ws://localhost:8080/testPath");
ClientSocket socket = new ClientSocket();
EventSocket socket = new EventSocket();
CompletableFuture<Session> connect = client.connect(socket, uri);
final long numMessages = 1000;
@ -194,7 +143,7 @@ public class WebSocketStatsTest
for (int i=0; i<numMessages; i++)
session.getRemote().sendString(msgText);
}
assertTrue(socket.closed.await(5, TimeUnit.SECONDS));
assertTrue(socket.closeLatch.await(5, TimeUnit.SECONDS));
assertTrue(wsConnectionClosed.await(5, TimeUnit.SECONDS));
assertThat(statistics.getConnectionsMax(), is(1L));