diff --git a/jetty-websocket/javax-websocket-server/src/test/java/org/eclipse/jetty/websocket/javax/server/WebSocketServerExamplesTest.java b/jetty-websocket/javax-websocket-server/src/test/java/org/eclipse/jetty/websocket/javax/server/WebSocketServerExamplesTest.java index c9673d5aa8c..90aa212497d 100644 --- a/jetty-websocket/javax-websocket-server/src/test/java/org/eclipse/jetty/websocket/javax/server/WebSocketServerExamplesTest.java +++ b/jetty-websocket/javax-websocket-server/src/test/java/org/eclipse/jetty/websocket/javax/server/WebSocketServerExamplesTest.java @@ -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); } } diff --git a/jetty-websocket/javax-websocket-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/MessageReceivingTest.java b/jetty-websocket/javax-websocket-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/MessageReceivingTest.java index 5b051743685..091c44803f9 100644 --- a/jetty-websocket/javax-websocket-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/MessageReceivingTest.java +++ b/jetty-websocket/javax-websocket-tests/src/test/java/org/eclipse/jetty/websocket/javax/tests/client/MessageReceivingTest.java @@ -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) { 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 4908c6cb29c..a9b8d404f3f 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 @@ -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); } } 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 7baf677c960..ed9d2b14635 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 @@ -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 receivedMessages = new BlockingArrayQueue<>(); + public BlockingQueue 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); - } - } } diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketExtensionConfigTest.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketExtensionConfigTest.java index 233f27f089b..197e0b0450a 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketExtensionConfigTest.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketExtensionConfigTest.java @@ -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")); } } diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketFilterTest.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketFilterTest.java index c374fb0c829..c3929ff64c0 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketFilterTest.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketFilterTest.java @@ -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")); } } diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketNegotiationTest.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketNegotiationTest.java index 0568b8c41eb..3f7991148f4 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketNegotiationTest.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketNegotiationTest.java @@ -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"); diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketServletTest.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketServletTest.java index 041c2c4a896..046635b032f 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketServletTest.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebSocketServletTest.java @@ -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")); } } 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 8ffba97cc5e..4f16cc63bb7 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 @@ -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 messages = new BlockingArrayQueue<>(); - CountDownLatch openLatch = new CountDownLatch(1); - CountDownLatch closeLatch = new CountDownLatch(1); - AtomicReference 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); } } diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketServletExamplesTest.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketServletExamplesTest.java index b8bb70e76c7..759111e0631 100644 --- a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketServletExamplesTest.java +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/WebSocketServletExamplesTest.java @@ -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 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 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 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)); } } 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 226ff8f2e01..f5268d02011 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 @@ -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 connect = client.connect(socket, uri); final long numMessages = 1000; @@ -194,7 +143,7 @@ public class WebSocketStatsTest for (int i=0; i