From 56fac5fdb17aaa94b4569a6a9c6d8dd9bcc7845e Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Thu, 27 Apr 2017 16:26:32 -0700 Subject: [PATCH] Issue #207 - more test failure fixes --- .../jsr356/server/AltFilterTest.java | 19 ++--- .../server/AnnotatedServerEndpointTest.java | 24 +++---- .../jsr356/server/BasicEndpointTest.java | 17 ++--- .../jsr356/server/IdleTimeoutTest.java | 54 ++++---------- .../jsr356/server/JettyEchoSocket.java | 72 +++++++++---------- .../jsr356/server/LargeAnnotatedTest.java | 25 ++++--- .../jsr356/server/LargeContainerTest.java | 25 ++++--- .../jsr356/server/OnMessageReturnTest.java | 26 ++++--- .../websocket/jsr356/server/SessionTest.java | 68 ++++++++---------- .../websocket/client/WebSocketClient.java | 3 + .../jetty/websocket/tests/WSServer.java | 1 + 11 files changed, 158 insertions(+), 176 deletions(-) diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/AltFilterTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/AltFilterTest.java index 9076ab83a5e..1c92fc85503 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/AltFilterTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/AltFilterTest.java @@ -20,10 +20,10 @@ package org.eclipse.jetty.websocket.jsr356.server; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import java.net.URI; -import java.util.List; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -33,7 +33,6 @@ import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.client.WebSocketClient; import org.eclipse.jetty.websocket.jsr356.server.samples.echo.BasicEchoSocket; -import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -72,14 +71,16 @@ public class AltFilterTest try { client.start(); - JettyEchoSocket clientEcho = new JettyEchoSocket(); - Future> clientMessagesFuture = clientEcho.expectedMessages(1); - Future future = client.connect(clientEcho,uri.resolve("echo")); + JettyEchoSocket clientSocket = new JettyEchoSocket(); + Future clientConnectFuture = client.connect(clientSocket,uri.resolve("echo")); // wait for connect - future.get(1,TimeUnit.SECONDS); - clientEcho.sendMessage("Hello Echo"); - List msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS); - Assert.assertEquals("Expected message","Hello Echo",msgs.get(0)); + Session clientSession = clientConnectFuture.get(5,TimeUnit.SECONDS); + clientSocket.sendMessage("Hello Echo"); + + String incomingMessage = clientSocket.messageQueue.poll(1, TimeUnit.SECONDS); + assertEquals("Expected message","Hello Echo",incomingMessage); + clientSession.close(); + clientSocket.awaitCloseEvent("Client"); } finally { diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointTest.java index 02331dfae84..e96a561f669 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/AnnotatedServerEndpointTest.java @@ -22,7 +22,6 @@ import static org.hamcrest.Matchers.containsString; import java.net.URI; import java.nio.file.Path; -import java.util.List; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -75,29 +74,28 @@ public class AnnotatedServerEndpointTest server.stop(); } - private void assertResponse(String message, String... expectedTexts) throws Exception + private void assertResponse(String message, String expectedText) throws Exception { WebSocketClient client = new WebSocketClient(bufferPool); try { client.start(); - JettyEchoSocket clientEcho = new JettyEchoSocket(); - Future> clientMessagesFuture = clientEcho.expectedMessages(1); + JettyEchoSocket clientSocket = new JettyEchoSocket(); + URI uri = server.getServerBaseURI().resolve("echo"); ClientUpgradeRequest req = new ClientUpgradeRequest(); req.setSubProtocols("echo"); - Future foo = client.connect(clientEcho,uri,req); + Future clientConnectFuture = client.connect(clientSocket,uri,req); // wait for connect - foo.get(1,TimeUnit.SECONDS); + Session clientSession = clientConnectFuture.get(5,TimeUnit.SECONDS); - clientEcho.sendMessage(message); - List msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS); + clientSocket.sendMessage(message); + + String incomingMessage = clientSocket.messageQueue.poll(1, TimeUnit.SECONDS); + Assert.assertThat("Expected message",incomingMessage,containsString(expectedText)); - String response = msgs.get(0); - for (String expected : expectedTexts) - { - Assert.assertThat("Expected message",response,containsString(expected)); - } + clientSession.close(); + clientSocket.awaitCloseEvent("Client"); } finally { diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/BasicEndpointTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/BasicEndpointTest.java index 9add6ba2de5..4540b46814a 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/BasicEndpointTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/BasicEndpointTest.java @@ -19,7 +19,6 @@ package org.eclipse.jetty.websocket.jsr356.server; import java.net.URI; -import java.util.List; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -68,14 +67,16 @@ public class BasicEndpointTest try { client.start(); - JettyEchoSocket clientEcho = new JettyEchoSocket(); - Future> clientMessagesFuture = clientEcho.expectedMessages(1); - Future future = client.connect(clientEcho,uri.resolve("echo")); + JettyEchoSocket clientSocket = new JettyEchoSocket(); + Future clientConnectFuture = client.connect(clientSocket,uri.resolve("echo")); // wait for connect - future.get(1,TimeUnit.SECONDS); - clientEcho.sendMessage("Hello World"); - List msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS); - Assert.assertEquals("Expected message","Hello World",msgs.get(0)); + Session clientSession = clientConnectFuture.get(5,TimeUnit.SECONDS); + clientSocket.sendMessage("Hello World"); + + String incomingMessage = clientSocket.messageQueue.poll(1, TimeUnit.SECONDS); + Assert.assertEquals("Expected message","Hello World",incomingMessage); + + clientSession.close(); } finally { diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/IdleTimeoutTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/IdleTimeoutTest.java index 5a4e883dfb8..546aaa3e4a2 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/IdleTimeoutTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/IdleTimeoutTest.java @@ -19,24 +19,18 @@ package org.eclipse.jetty.websocket.jsr356.server; import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThat; -import java.io.IOException; import java.net.URI; -import java.util.List; -import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.toolchain.test.TestingDir; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.webapp.WebAppContext; -import org.eclipse.jetty.websocket.api.CloseException; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.StatusCode; import org.eclipse.jetty.websocket.client.WebSocketClient; @@ -86,50 +80,30 @@ public class IdleTimeoutTest server.stop(); } - private void assertConnectionTimeout(URI uri) throws Exception, IOException, InterruptedException, ExecutionException, TimeoutException + private void assertConnectionTimeout(URI uri) throws Exception { WebSocketClient client = new WebSocketClient(bufferPool); try { client.start(); - JettyEchoSocket clientEcho = new JettyEchoSocket(); - Future> clientMessagesFuture = clientEcho.expectedMessages(1); + JettyEchoSocket clientSocket = new JettyEchoSocket(); - if (LOG.isDebugEnabled()) - LOG.debug("Client Attempting to connnect"); - Future future = client.connect(clientEcho,uri); + Future clientConnectFuture = client.connect(clientSocket,uri); // wait for connect - future.get(1,TimeUnit.SECONDS); - if (LOG.isDebugEnabled()) - LOG.debug("Client Connected"); + clientConnectFuture.get(1,TimeUnit.SECONDS); // wait 1 second - if (LOG.isDebugEnabled()) - LOG.debug("Waiting 1 second"); TimeUnit.SECONDS.sleep(1); - if (LOG.isDebugEnabled()) - LOG.debug("Waited 1 second"); // Try to write - clientEcho.sendMessage("You shouldn't be there"); - try - { - List msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS); - assertThat("Should not have received messages echoed back",msgs,is(empty())); - } - catch (ExecutionException e) - { - Throwable cause = e.getCause(); - if(cause instanceof CloseException) - { - CloseException ce = (CloseException) cause; - assertThat("CloseException.statusCode", ce.getStatusCode(), is(StatusCode.SHUTDOWN)); - assertThat("CloseException.reason", ce.getMessage(), containsString("Idle Timeout")); - } - else - { - throw e; - } - } + clientSocket.sendMessage("You shouldn't be there"); + + // See if remote sent anything (it shouldn't have) + String incomingMessage = clientSocket.messageQueue.poll(1, TimeUnit.SECONDS); + assertThat("Should not have received messages echoed back",incomingMessage,nullValue()); + + // wait for local close + clientSocket.awaitCloseEvent("Client"); + clientSocket.assertCloseInfo("Client", StatusCode.SHUTDOWN, containsString("Idle Timeout")); } finally { diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/JettyEchoSocket.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/JettyEchoSocket.java index 30a9a2da13e..d4a01f85f4b 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/JettyEchoSocket.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/JettyEchoSocket.java @@ -18,25 +18,29 @@ package org.eclipse.jetty.websocket.jsr356.server; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Future; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.websocket.api.BatchMode; -import org.eclipse.jetty.websocket.api.CloseException; import org.eclipse.jetty.websocket.api.RemoteEndpoint; 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; import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; import org.eclipse.jetty.websocket.api.annotations.WebSocket; +import org.eclipse.jetty.websocket.common.CloseInfo; +import org.hamcrest.Matcher; +import org.hamcrest.Matchers; /** * This is a Jetty API version of a websocket. @@ -48,53 +52,30 @@ public class JettyEchoSocket { private static final Logger LOG = Log.getLogger(JettyEchoSocket.class); private RemoteEndpoint remote; - private CompletableFuture> expectedMessagesFuture; - private AtomicInteger expectedMessageCount; - private List messages = new ArrayList<>(); - - public Future> expectedMessages(int expected) - { - expectedMessagesFuture = new CompletableFuture<>(); - expectedMessageCount = new AtomicInteger(expected); - return expectedMessagesFuture; - } + public CountDownLatch closeLatch = new CountDownLatch(1); + public BlockingQueue messageQueue = new LinkedBlockingDeque<>(); + public AtomicReference closeInfo = new AtomicReference<>(); @OnWebSocketClose - public void onClose(int code, String reason) + public void onClose(int statusCode, String reason) { remote = null; - synchronized (expectedMessagesFuture) - { - if ((code != StatusCode.NORMAL) || - (code != StatusCode.NO_CODE)) - { - expectedMessagesFuture.completeExceptionally(new CloseException(code, reason)); - } - } + CloseInfo close = new CloseInfo(statusCode, reason); + boolean closeTracked = closeInfo.compareAndSet(null, close); + this.closeLatch.countDown(); + assertTrue("Close only happened once", closeTracked); } @OnWebSocketError public void onError(Throwable t) { LOG.warn(t); - synchronized (expectedMessagesFuture) - { - expectedMessagesFuture.completeExceptionally(t); - } } @OnWebSocketMessage public void onMessage(String msg) throws IOException { - messages.add(msg); - synchronized (expectedMessagesFuture) - { - int countLeft = expectedMessageCount.decrementAndGet(); - if (countLeft <= 0) - { - expectedMessagesFuture.complete(messages); - } - } + messageQueue.offer(msg); sendMessage(msg); } @@ -104,6 +85,19 @@ public class JettyEchoSocket this.remote = session.getRemote(); } + public void awaitCloseEvent(String prefix) throws InterruptedException + { + assertTrue(prefix + " onClose event", closeLatch.await(5, TimeUnit.SECONDS)); + } + + public void assertCloseInfo(String prefix, int expectedCloseStatusCode, Matcher reasonMatcher) throws InterruptedException + { + CloseInfo close = closeInfo.get(); + assertThat(prefix + " close info", close, Matchers.notNullValue()); + assertThat(prefix + " received close code", close.getStatusCode(), Matchers.is(expectedCloseStatusCode)); + assertThat(prefix + " received close reason", close.getReason(), reasonMatcher); + } + public void sendMessage(String msg) throws IOException { RemoteEndpoint r = remote; diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeAnnotatedTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeAnnotatedTest.java index 19e5a034553..f0952396691 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeAnnotatedTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeAnnotatedTest.java @@ -18,10 +18,12 @@ package org.eclipse.jetty.websocket.jsr356.server; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.Arrays; -import java.util.List; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -31,7 +33,6 @@ import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.client.WebSocketClient; import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPoolRule; import org.eclipse.jetty.websocket.jsr356.server.samples.echo.LargeEchoConfiguredSocket; -import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -68,18 +69,24 @@ public class LargeAnnotatedTest { client.getPolicy().setMaxTextMessageSize(128*1024); client.start(); - JettyEchoSocket clientEcho = new JettyEchoSocket(); - Future> clientMessagesFuture = clientEcho.expectedMessages(1); - Future foo = client.connect(clientEcho,uri.resolve("echo/large")); + + JettyEchoSocket clientSocket = new JettyEchoSocket(); + + Future clientConnectFuture = client.connect(clientSocket,uri.resolve("echo/large")); // wait for connect - foo.get(1,TimeUnit.SECONDS); + Session clientSession = clientConnectFuture.get(1,TimeUnit.SECONDS); + // The message size should be bigger than default, but smaller than the limit that LargeEchoSocket specifies byte txt[] = new byte[100 * 1024]; Arrays.fill(txt,(byte)'o'); String msg = new String(txt,StandardCharsets.UTF_8); - clientEcho.sendMessage(msg); - List msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS); - Assert.assertEquals("Expected message",msg,msgs.get(0)); + clientSocket.sendMessage(msg); + + // Receive echo + String incomingMessage = clientSocket.messageQueue.poll(5, TimeUnit.SECONDS); + assertThat("Expected message",incomingMessage,is(msg)); + + clientSession.close(); } finally { diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeContainerTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeContainerTest.java index 5c1681cab0b..31db5c28deb 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeContainerTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/LargeContainerTest.java @@ -18,10 +18,12 @@ package org.eclipse.jetty.websocket.jsr356.server; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.Arrays; -import java.util.List; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -31,7 +33,6 @@ import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.client.WebSocketClient; import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPoolRule; import org.eclipse.jetty.websocket.jsr356.server.samples.echo.LargeEchoDefaultSocket; -import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -68,18 +69,24 @@ public class LargeContainerTest { client.getPolicy().setMaxTextMessageSize(128*1024); client.start(); - JettyEchoSocket clientEcho = new JettyEchoSocket(); - Future> clientMessagesFuture = clientEcho.expectedMessages(1); - Future foo = client.connect(clientEcho,uri.resolve("echo/large")); + + JettyEchoSocket clientSocket = new JettyEchoSocket(); + Future clientConnectFuture = client.connect(clientSocket,uri.resolve("echo/large")); + // wait for connect - foo.get(1,TimeUnit.SECONDS); + Session clientSession = clientConnectFuture.get(5,TimeUnit.SECONDS); + // The message size should be bigger than default, but smaller than the limit that LargeEchoSocket specifies byte txt[] = new byte[100 * 1024]; Arrays.fill(txt,(byte)'o'); String msg = new String(txt,StandardCharsets.UTF_8); - clientEcho.sendMessage(msg); - List msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS); - Assert.assertEquals("Expected message",msg,msgs.get(0)); + clientSocket.sendMessage(msg); + + // Confirm echo + String incomingMessage = clientSocket.messageQueue.poll(5, TimeUnit.SECONDS); + assertThat("Expected message",incomingMessage,is(msg)); + + clientSession.close(); } finally { diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/OnMessageReturnTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/OnMessageReturnTest.java index 60f1694e979..aac749c6ebf 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/OnMessageReturnTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/OnMessageReturnTest.java @@ -18,8 +18,10 @@ package org.eclipse.jetty.websocket.jsr356.server; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + import java.net.URI; -import java.util.List; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -29,7 +31,6 @@ import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.client.WebSocketClient; import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPoolRule; import org.eclipse.jetty.websocket.jsr356.server.samples.echo.EchoReturnEndpoint; -import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -60,14 +61,21 @@ public class OnMessageReturnTest try { client.start(); - JettyEchoSocket clientEcho = new JettyEchoSocket(); - Future> clientMessagesFuture = clientEcho.expectedMessages(1); - Future future = client.connect(clientEcho,uri.resolve("echoreturn")); + + JettyEchoSocket clientSocket = new JettyEchoSocket(); + Future clientConnectFuture = client.connect(clientSocket,uri.resolve("echoreturn")); + // wait for connect - future.get(1,TimeUnit.SECONDS); - clientEcho.sendMessage("Hello World"); - List msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS); - Assert.assertEquals("Expected message","Hello World",msgs.get(0)); + Session clientSession = clientConnectFuture.get(5,TimeUnit.SECONDS); + + // Send message + clientSocket.sendMessage("Hello World"); + + // Confirm response + String incomingMessage = clientSocket.messageQueue.poll(5, TimeUnit.SECONDS); + assertThat("Expected message",incomingMessage,is("Hello World")); + + clientSession.close(); } finally { diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/SessionTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/SessionTest.java index 35c21882494..e81264aea9c 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/SessionTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/SessionTest.java @@ -46,9 +46,9 @@ import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class SessionTest { - private static interface Case + private interface Case { - public void customize(WebAppContext context); + void customize(WebAppContext context); } @Parameters @@ -56,44 +56,28 @@ public class SessionTest { List cases = new ArrayList<>(); cases.add(new Case[] - { new Case() + {context -> { - @Override - public void customize(WebAppContext context) - { - // no customization - } - } }); + // no customization + }}); cases.add(new Case[] - { new Case() + {context -> { - @Override - public void customize(WebAppContext context) - { - // Test with DefaultServlet only - context.addServlet(DefaultServlet.class,"/"); - } - } }); + // Test with DefaultServlet only + context.addServlet(DefaultServlet.class,"/"); + }}); cases.add(new Case[] - { new Case() + {context -> { - @Override - public void customize(WebAppContext context) - { - // Test with Servlet mapped to "/*" - context.addServlet(DefaultServlet.class,"/*"); - } - } }); + // Test with Servlet mapped to "/*" + context.addServlet(DefaultServlet.class,"/*"); + }}); cases.add(new Case[] - { new Case() + {context -> { - @Override - public void customize(WebAppContext context) - { - // Test with Servlet mapped to "/info/*" - context.addServlet(DefaultServlet.class,"/info/*"); - } - } }); + // Test with Servlet mapped to "/info/*" + context.addServlet(DefaultServlet.class,"/info/*"); + }}); return cases; } @@ -137,14 +121,18 @@ public class SessionTest try { client.start(); - JettyEchoSocket clientEcho = new JettyEchoSocket(); - Future> clientMessagesFuture = clientEcho.expectedMessages(1); - Future future = client.connect(clientEcho,serverUri.resolve(requestPath)); + + JettyEchoSocket clientSocket = new JettyEchoSocket(); + Future clientConnectFuture = client.connect(clientSocket,serverUri.resolve(requestPath)); // wait for connect - future.get(1,TimeUnit.SECONDS); - clientEcho.sendMessage(requestMessage); - List msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS); - Assert.assertThat("Expected message",msgs.get(0),is(expectedResponse)); + Session clientSession = clientConnectFuture.get(5,TimeUnit.SECONDS); + clientSocket.sendMessage(requestMessage); + + String incomingMessage = clientSocket.messageQueue.poll(5, TimeUnit.SECONDS); + Assert.assertThat("Expected message",incomingMessage,is(expectedResponse)); + + clientSession.close(); + clientSocket.awaitCloseEvent("Client"); } finally { diff --git a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java index 6a385f7f690..c75f1230cc7 100644 --- a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java +++ b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java @@ -133,6 +133,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont public WebSocketClient(ByteBufferPool bufferPool) { this(new HttpClient()); + addBean(this.httpClient); this.httpClient.setByteBufferPool(bufferPool); } @@ -145,6 +146,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont public WebSocketClient(SslContextFactory sslContextFactory) { this(new HttpClient(sslContextFactory)); + addBean(this.httpClient); } /** @@ -158,6 +160,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont public WebSocketClient(SslContextFactory sslContextFactory, Executor executor) { this(new HttpClient(sslContextFactory)); + addBean(this.httpClient); this.httpClient.setExecutor(executor); } diff --git a/jetty-websocket/websocket-tests/src/main/java/org/eclipse/jetty/websocket/tests/WSServer.java b/jetty-websocket/websocket-tests/src/main/java/org/eclipse/jetty/websocket/tests/WSServer.java index 70b707b841f..5206ca1ae8c 100644 --- a/jetty-websocket/websocket-tests/src/main/java/org/eclipse/jetty/websocket/tests/WSServer.java +++ b/jetty-websocket/websocket-tests/src/main/java/org/eclipse/jetty/websocket/tests/WSServer.java @@ -162,6 +162,7 @@ public class WSServer { contexts.addHandler(webapp); contexts.manage(webapp); + webapp.setThrowUnavailableOnStartupException(true); webapp.start(); if (LOG.isDebugEnabled()) {