From ca404f157f6912118cc53747b55fb37ef4c1fccd Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Mon, 13 Jul 2020 14:53:55 +1000 Subject: [PATCH] clean up and rename fields in EventSocket WebSocket test utility class Signed-off-by: Lachlan Roberts --- .../tests/ConcurrentConnectTest.java | 14 ++--- .../jetty/websocket/tests/EchoSocket.java | 19 ++++--- .../jetty/websocket/tests/ErrorCloseTest.java | 34 ++++++------ .../jetty/websocket/tests/EventSocket.java | 54 +++++++++---------- .../websocket/tests/SuspendResumeTest.java | 38 ++++++------- .../websocket/tests/WebSocketStatsTest.java | 2 +- .../websocket/tests/WriteAfterStopTest.java | 8 +-- .../tests/client/ClientTimeoutTest.java | 3 +- 8 files changed, 84 insertions(+), 88 deletions(-) diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/ConcurrentConnectTest.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/ConcurrentConnectTest.java index 9fa1dc4ed34..aad72b66fdd 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/ConcurrentConnectTest.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/ConcurrentConnectTest.java @@ -69,7 +69,7 @@ public class ConcurrentConnectTest @Override public void configure(WebSocketServletFactory factory) { - factory.register(EventSocket.EchoSocket.class); + factory.register(EchoSocket.class); serverFactory = (WebSocketServerFactory)factory; } }; @@ -116,22 +116,22 @@ public class ConcurrentConnectTest for (EventSocket l : listeners) { - assertTrue(l.open.await(5, TimeUnit.SECONDS)); + assertTrue(l.openLatch.await(5, TimeUnit.SECONDS)); } for (EventSocket l : listeners) { - l.getSession().getRemote().sendString("ping"); - assertThat(l.receivedMessages.poll(5, TimeUnit.SECONDS), is("ping")); - l.getSession().close(StatusCode.NORMAL, "close from client"); + l.session.getRemote().sendString("ping"); + assertThat(l.textMessages.poll(5, TimeUnit.SECONDS), is("ping")); + l.session.close(StatusCode.NORMAL, "close from client"); } for (EventSocket l : listeners) { - assertTrue(l.closed.await(5, TimeUnit.SECONDS)); + assertTrue(l.closeLatch.await(5, TimeUnit.SECONDS)); assertThat(l.closeCode, is(StatusCode.NORMAL)); assertThat(l.closeReason, is("close from client")); - assertNull(l.failure); + assertNull(l.error); } closeListener.closeLatch.await(5, TimeUnit.SECONDS); diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EchoSocket.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EchoSocket.java index 9df7bba33e6..136471bc7e3 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EchoSocket.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EchoSocket.java @@ -21,23 +21,22 @@ package org.eclipse.jetty.websocket.tests; import java.io.IOException; import java.nio.ByteBuffer; -import org.eclipse.jetty.websocket.api.Session; -import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; import org.eclipse.jetty.websocket.api.annotations.WebSocket; -@SuppressWarnings("unused") @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); } - @OnWebSocketMessage - public void onBinaryMessage(Session session, byte[] data, int offset, int len) throws IOException + @Override + public void onMessage(byte[] buf, int offset, int len) throws IOException { - session.getRemote().sendBytes(ByteBuffer.wrap(data, offset, len)); + super.onMessage(buf, offset, len); + session.getRemote().sendBytes(ByteBuffer.wrap(buf, offset, len)); } } diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/ErrorCloseTest.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/ErrorCloseTest.java index 5ff5cefbbf2..d8c484bc686 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/ErrorCloseTest.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/ErrorCloseTest.java @@ -138,9 +138,9 @@ public class ErrorCloseTest EventSocket clientSocket = new EventSocket(); client.connect(clientSocket, serverUri).get(5, TimeUnit.SECONDS); - assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS)); - assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS)); - assertThat(serverSocket.failure.getMessage(), is("throwing from onOpen")); + assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS)); + assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS)); + assertThat(serverSocket.error.getMessage(), is("throwing from onOpen")); // Check we have stopped the WebSocketSession properly. assertFalse(serverSocket.session.isOpen()); @@ -156,9 +156,9 @@ public class ErrorCloseTest client.connect(clientSocket, serverUri).get(5, TimeUnit.SECONDS); clientSocket.session.getRemote().sendString("trigger onMessage error"); - assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS)); - assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS)); - assertThat(serverSocket.failure.getMessage(), is("throwing from onMessage")); + assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS)); + assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS)); + assertThat(serverSocket.error.getMessage(), is("throwing from onMessage")); // Check we have stopped the WebSocketSession properly. assertFalse(serverSocket.session.isOpen()); @@ -177,8 +177,8 @@ public class ErrorCloseTest try (StacklessLogging stacklessLogging = new StacklessLogging(WebSocketSession.class)) { client.connect(clientSocket, serverUri).get(5, TimeUnit.SECONDS); - assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS)); - assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS)); + assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS)); + assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS)); } // Check we have stopped the WebSocketSession properly. @@ -199,8 +199,8 @@ public class ErrorCloseTest try (StacklessLogging stacklessLogging = new StacklessLogging(WebSocketSession.class)) { clientSocket.session.getRemote().sendString("trigger onMessage error"); - assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS)); - assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS)); + assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS)); + assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS)); } // Check we have stopped the WebSocketSession properly. @@ -218,14 +218,14 @@ public class ErrorCloseTest client.connect(clientSocket, serverUri).get(5, TimeUnit.SECONDS); // Set a short idleTimeout on the server. - assertTrue(serverSocket.open.await(5, TimeUnit.SECONDS)); + assertTrue(serverSocket.openLatch.await(5, TimeUnit.SECONDS)); serverSocket.session.setIdleTimeout(1000); // Wait for server to timeout. try (StacklessLogging stacklessLogging = new StacklessLogging(WebSocketSession.class)) { - assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS)); - assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS)); + assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS)); + assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS)); } // Check we have stopped the WebSocketSession properly. @@ -245,8 +245,8 @@ public class ErrorCloseTest try (StacklessLogging stacklessLogging = new StacklessLogging(WebSocketSession.class)) { clientSocket.session.disconnect(); - assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS)); - assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS)); + assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS)); + assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS)); } // Check we have stopped the WebSocketSession properly. @@ -266,8 +266,8 @@ public class ErrorCloseTest try (StacklessLogging stacklessLogging = new StacklessLogging(WebSocketSession.class)) { serverSocket.session.disconnect(); - assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS)); - assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS)); + assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS)); + assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS)); } // Check we have stopped the WebSocketSession properly. diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EventSocket.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EventSocket.java index 23ed12a80b7..55ebf1ddab8 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EventSocket.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/EventSocket.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.websocket.tests; import java.io.IOException; +import java.nio.ByteBuffer; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; @@ -26,6 +27,7 @@ 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.StatusCode; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError; @@ -35,24 +37,20 @@ import org.eclipse.jetty.websocket.api.annotations.WebSocket; @WebSocket public class EventSocket { - private static Logger LOG = Log.getLogger(EventSocket.class); + private static final Logger LOG = Log.getLogger(EventSocket.class); public Session session; private String behavior; - public volatile Throwable failure = null; - public volatile int closeCode = -1; - public volatile String closeReason = null; - public BlockingQueue receivedMessages = new BlockingArrayQueue<>(); + public BlockingQueue textMessages = new BlockingArrayQueue<>(); + public BlockingQueue binaryMessages = new BlockingArrayQueue<>(); + public volatile int closeCode = StatusCode.UNDEFINED; + public volatile String closeReason; + public volatile Throwable error = null; - public CountDownLatch open = new CountDownLatch(1); - public CountDownLatch error = new CountDownLatch(1); - public CountDownLatch closed = new CountDownLatch(1); - - public Session getSession() - { - return session; - } + public CountDownLatch openLatch = new CountDownLatch(1); + public CountDownLatch errorLatch = new CountDownLatch(1); + public CountDownLatch closeLatch = new CountDownLatch(1); @OnWebSocketConnect public void onOpen(Session session) @@ -61,7 +59,7 @@ public class EventSocket behavior = session.getPolicy().getBehavior().name(); if (LOG.isDebugEnabled()) LOG.debug("{} onOpen(): {}", toString(), session); - open.countDown(); + openLatch.countDown(); } @OnWebSocketMessage @@ -69,7 +67,16 @@ public class EventSocket { if (LOG.isDebugEnabled()) LOG.debug("{} onMessage(): {}", toString(), message); - receivedMessages.offer(message); + textMessages.offer(message); + } + + @OnWebSocketMessage + public void onMessage(byte[] buf, int offset, int len) throws IOException + { + ByteBuffer message = ByteBuffer.wrap(buf, offset, len); + if (LOG.isDebugEnabled()) + LOG.debug("{} onMessage(): {}", toString(), message); + binaryMessages.offer(message); } @OnWebSocketClose @@ -79,7 +86,7 @@ public class EventSocket LOG.debug("{} onClose(): {}:{}", toString(), statusCode, reason); closeCode = statusCode; closeReason = reason; - closed.countDown(); + closeLatch.countDown(); } @OnWebSocketError @@ -87,8 +94,8 @@ public class EventSocket { if (LOG.isDebugEnabled()) LOG.debug("{} onError(): {}", toString(), cause); - failure = cause; - error.countDown(); + error = cause; + errorLatch.countDown(); } @Override @@ -96,15 +103,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); - } - } } diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/SuspendResumeTest.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/SuspendResumeTest.java index 2925a42fc9c..473c2c6ddf5 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/SuspendResumeTest.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/SuspendResumeTest.java @@ -115,21 +115,21 @@ public class SuspendResumeTest clientSocket.session.getRemote().sendString("suspend"); clientSocket.session.getRemote().sendString("hello world"); - assertThat(serverSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("suspend")); - assertNull(serverSocket.receivedMessages.poll(1, TimeUnit.SECONDS)); + assertThat(serverSocket.textMessages.poll(5, TimeUnit.SECONDS), is("suspend")); + assertNull(serverSocket.textMessages.poll(1, TimeUnit.SECONDS)); serverSocket.suspendToken.resume(); - assertThat(serverSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("suspend")); - assertNull(serverSocket.receivedMessages.poll(1, TimeUnit.SECONDS)); + assertThat(serverSocket.textMessages.poll(5, TimeUnit.SECONDS), is("suspend")); + assertNull(serverSocket.textMessages.poll(1, TimeUnit.SECONDS)); serverSocket.suspendToken.resume(); - assertThat(serverSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("hello world")); - assertNull(serverSocket.receivedMessages.poll(1, TimeUnit.SECONDS)); + assertThat(serverSocket.textMessages.poll(5, TimeUnit.SECONDS), is("hello world")); + assertNull(serverSocket.textMessages.poll(1, TimeUnit.SECONDS)); // make sure both sides are closed clientSocket.session.close(); - assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS)); - assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS)); + assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS)); + assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS)); // check we closed normally assertThat(clientSocket.closeCode, is(StatusCode.NORMAL)); @@ -145,29 +145,29 @@ public class SuspendResumeTest connect.get(5, TimeUnit.SECONDS); // verify connection by sending a message from server to client - assertTrue(serverSocket.open.await(5, TimeUnit.SECONDS)); + assertTrue(serverSocket.openLatch.await(5, TimeUnit.SECONDS)); serverSocket.session.getRemote().sendString("verification"); - assertThat(clientSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("verification")); + assertThat(clientSocket.textMessages.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().sendString("message-from-client"); - assertThat(serverSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("message-from-client")); + assertThat(serverSocket.textMessages.poll(5, TimeUnit.SECONDS), is("message-from-client")); // the message is not received as it is suspended serverSocket.session.getRemote().sendString("message-from-server"); - assertNull(clientSocket.receivedMessages.poll(2, TimeUnit.SECONDS)); + assertNull(clientSocket.textMessages.poll(2, TimeUnit.SECONDS)); // client should receive message after it resumes suspendToken.resume(); - assertThat(clientSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("message-from-server")); + assertThat(clientSocket.textMessages.poll(5, TimeUnit.SECONDS), is("message-from-server")); // make sure both sides are closed clientSocket.session.close(); - assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS)); - assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS)); + assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS)); + assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS)); // check we closed normally assertThat(clientSocket.closeCode, is(StatusCode.NORMAL)); @@ -183,14 +183,14 @@ public class SuspendResumeTest connect.get(5, TimeUnit.SECONDS); // verify connection by sending a message from server to client - assertTrue(serverSocket.open.await(5, TimeUnit.SECONDS)); + assertTrue(serverSocket.openLatch.await(5, TimeUnit.SECONDS)); serverSocket.session.getRemote().sendString("verification"); - assertThat(clientSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("verification")); + assertThat(clientSocket.textMessages.poll(5, TimeUnit.SECONDS), is("verification")); // make sure both sides are closed clientSocket.session.close(); - assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS)); - assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS)); + assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS)); + assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS)); // check we closed normally assertThat(clientSocket.closeCode, is(StatusCode.NORMAL)); diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketStatsTest.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketStatsTest.java index fb90058bb74..f096535dcbe 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketStatsTest.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketStatsTest.java @@ -144,7 +144,7 @@ public class WebSocketStatsTest } } - 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)); diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WriteAfterStopTest.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WriteAfterStopTest.java index c4ba1bdff11..c5fe6dbd3d6 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WriteAfterStopTest.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WriteAfterStopTest.java @@ -89,14 +89,14 @@ public class WriteAfterStopTest ClientUpgradeRequest upgradeRequest = new ClientUpgradeRequest(); upgradeRequest.addExtensions("permessage-deflate"); Session session = client.connect(clientSocket, uri, upgradeRequest).get(5, TimeUnit.SECONDS); - clientSocket.getSession().getRemote().sendStringByFuture("init deflater"); - assertThat(serverSocket.receivedMessages.poll(5, TimeUnit.SECONDS), is("init deflater")); + clientSocket.session.getRemote().sendStringByFuture("init deflater"); + assertThat(serverSocket.textMessages.poll(5, TimeUnit.SECONDS), is("init deflater")); session.close(StatusCode.NORMAL, null); // make sure both sides are closed clientSocket.session.close(); - assertTrue(clientSocket.closed.await(5, TimeUnit.SECONDS)); - assertTrue(serverSocket.closed.await(5, TimeUnit.SECONDS)); + assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS)); + assertTrue(serverSocket.closeLatch.await(5, TimeUnit.SECONDS)); // check we closed normally assertThat(clientSocket.closeCode, is(StatusCode.NORMAL)); diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/ClientTimeoutTest.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/ClientTimeoutTest.java index 8cc2a7e70fa..95a0b23a842 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/ClientTimeoutTest.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/ClientTimeoutTest.java @@ -36,6 +36,7 @@ import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; import org.eclipse.jetty.websocket.client.WebSocketClient; import org.eclipse.jetty.websocket.server.NativeWebSocketServletContainerInitializer; import org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter; +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; @@ -69,7 +70,7 @@ public class ClientTimeoutTest try { createEndpoint.await(5, TimeUnit.SECONDS); - return new EventSocket.EchoSocket(); + return new EchoSocket(); } catch (InterruptedException e) {