From a7b164e6b90cd7ece4e103a95cc2b6022b8ded8b Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 10 May 2017 07:46:56 -0700 Subject: [PATCH] Fixing JSR356 Client tests --- .../jsr356/JettyClientContainerProvider.java | 8 +++- .../jsr356/server/BinaryStreamTest.java | 1 - .../server/ExtensionStackProcessingTest.java | 1 - .../jsr356/server/JsrBatchModeTest.java | 1 - .../jsr356/server/MemoryUsageTest.java | 1 - .../jsr356/server/OnPartialTest.java | 1 - .../jsr356/server/TextStreamTest.java | 1 - .../websocket/client/WebSocketClient.java | 2 - .../websocket/client/ClientConnectTest.java | 44 ++++++++----------- 9 files changed, 25 insertions(+), 35 deletions(-) diff --git a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JettyClientContainerProvider.java b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JettyClientContainerProvider.java index 7e632fcd792..56cd4731bd7 100644 --- a/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JettyClientContainerProvider.java +++ b/jetty-websocket/javax-websocket-client-impl/src/main/java/org/eclipse/jetty/websocket/jsr356/JettyClientContainerProvider.java @@ -67,10 +67,14 @@ public class JettyClientContainerProvider extends ContainerProvider synchronized (lock) { if (INSTANCE == null) + { + INSTANCE = new ClientContainer(); + } + + if (!INSTANCE.isStarted()) { try { - INSTANCE = new ClientContainer(); INSTANCE.start(); } catch (Exception e) @@ -78,7 +82,7 @@ public class JettyClientContainerProvider extends ContainerProvider throw new RuntimeException("Unable to start Client Container", e); } } - + return INSTANCE; } } diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/BinaryStreamTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/BinaryStreamTest.java index a1f21e67689..a9cbcb1684e 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/BinaryStreamTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/BinaryStreamTest.java @@ -67,7 +67,6 @@ public class BinaryStreamTest server.start(); wsClient = ContainerProvider.getWebSocketContainer(); - server.addBean(wsClient, true); } @After diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/ExtensionStackProcessingTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/ExtensionStackProcessingTest.java index ef7be1a05da..4d3c5b85b56 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/ExtensionStackProcessingTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/ExtensionStackProcessingTest.java @@ -75,7 +75,6 @@ public class ExtensionStackProcessingTest container.addEndpoint(config); client = ContainerProvider.getWebSocketContainer(); - server.addBean(client, true); server.start(); } diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/JsrBatchModeTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/JsrBatchModeTest.java index 7d86059039a..73befc3c501 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/JsrBatchModeTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/JsrBatchModeTest.java @@ -64,7 +64,6 @@ public class JsrBatchModeTest server.start(); client = ContainerProvider.getWebSocketContainer(); - server.addBean(client, true); } @After diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/MemoryUsageTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/MemoryUsageTest.java index f64f8760bff..df958d564f2 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/MemoryUsageTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/MemoryUsageTest.java @@ -66,7 +66,6 @@ public class MemoryUsageTest server.start(); client = ContainerProvider.getWebSocketContainer(); - server.addBean(client, true); } @After diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/OnPartialTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/OnPartialTest.java index a8a456efe5a..c6b1fc0f465 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/OnPartialTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/OnPartialTest.java @@ -89,7 +89,6 @@ public class OnPartialTest @SuppressWarnings("resource") JsrSession session = new JsrSession(container,id,requestURI,driver,connection); - session.setPolicy(policy); session.start(); session.open(); return driver; diff --git a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/TextStreamTest.java b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/TextStreamTest.java index 1f940a9bbba..f6360b94e60 100644 --- a/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/TextStreamTest.java +++ b/jetty-websocket/javax-websocket-server-impl/src/test/java/org/eclipse/jetty/websocket/jsr356/server/TextStreamTest.java @@ -67,7 +67,6 @@ public class TextStreamTest server.start(); wsClient = ContainerProvider.getWebSocketContainer(); - server.addBean(wsClient, true); } @After 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 e2c6470caf2..bea2b75d10b 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,9 +133,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont * * @param bufferPool * byte buffer pool to use - * @deprecated use {@link #WebSocketClient(HttpClient)} instead */ - @Deprecated public WebSocketClient(ByteBufferPool bufferPool) { this(null,null,bufferPool); diff --git a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/ClientConnectTest.java b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/ClientConnectTest.java index 81508834d7f..77647fe59e9 100644 --- a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/ClientConnectTest.java +++ b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/ClientConnectTest.java @@ -18,6 +18,7 @@ package org.eclipse.jetty.websocket.client; +import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; @@ -36,7 +37,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.eclipse.jetty.client.HttpClient; -import org.eclipse.jetty.toolchain.test.OS; import org.eclipse.jetty.toolchain.test.TestTracker; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.UpgradeException; @@ -44,6 +44,7 @@ import org.eclipse.jetty.websocket.common.AcceptHash; import org.eclipse.jetty.websocket.common.test.BlockheadServer; import org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection; import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPoolRule; +import org.hamcrest.Matcher; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -67,21 +68,18 @@ public class ClientConnectTest private WebSocketClient client; @SuppressWarnings("unchecked") - private E assertExpectedError(ExecutionException e, JettyTrackingSocket wsocket, Class errorClass) throws IOException + private E assertExpectedError(ExecutionException e, JettyTrackingSocket wsocket, Matcher errorMatcher) throws IOException { // Validate thrown cause Throwable cause = e.getCause(); - if(!errorClass.isInstance(cause)) - { - cause.printStackTrace(System.err); - Assert.assertThat("ExecutionException.cause",cause,instanceOf(errorClass)); - } + + Assert.assertThat("ExecutionException.cause",cause,errorMatcher); // Validate websocket captured cause Assert.assertThat("Error Queue Length",wsocket.errorQueue.size(),greaterThanOrEqualTo(1)); Throwable capcause = wsocket.errorQueue.poll(); Assert.assertThat("Error Queue[0]",capcause,notNullValue()); - Assert.assertThat("Error Queue[0]",capcause,instanceOf(errorClass)); + Assert.assertThat("Error Queue[0]",capcause,errorMatcher); // Validate that websocket didn't see an open event wsocket.assertNotOpened(); @@ -214,7 +212,7 @@ public class ClientConnectTest catch (ExecutionException e) { // Expected Path - UpgradeException ue = assertExpectedError(e,wsocket,UpgradeException.class); + UpgradeException ue = assertExpectedError(e,wsocket,instanceOf(UpgradeException.class)); Assert.assertThat("UpgradeException.requestURI",ue.getRequestURI(),notNullValue()); Assert.assertThat("UpgradeException.requestURI",ue.getRequestURI().toASCIIString(),is(wsUri.toASCIIString())); Assert.assertThat("UpgradeException.responseStatusCode",ue.getResponseStatusCode(),is(404)); @@ -245,7 +243,7 @@ public class ClientConnectTest catch (ExecutionException e) { // Expected Path - UpgradeException ue = assertExpectedError(e,wsocket,UpgradeException.class); + UpgradeException ue = assertExpectedError(e,wsocket,instanceOf(UpgradeException.class)); Assert.assertThat("UpgradeException.requestURI",ue.getRequestURI(),notNullValue()); Assert.assertThat("UpgradeException.requestURI",ue.getRequestURI().toASCIIString(),is(wsUri.toASCIIString())); Assert.assertThat("UpgradeException.responseStatusCode",ue.getResponseStatusCode(),is(200)); @@ -282,7 +280,7 @@ public class ClientConnectTest catch (ExecutionException e) { // Expected Path - UpgradeException ue = assertExpectedError(e,wsocket,UpgradeException.class); + UpgradeException ue = assertExpectedError(e,wsocket,instanceOf(UpgradeException.class)); Assert.assertThat("UpgradeException.requestURI",ue.getRequestURI(),notNullValue()); Assert.assertThat("UpgradeException.requestURI",ue.getRequestURI().toASCIIString(),is(wsUri.toASCIIString())); Assert.assertThat("UpgradeException.responseStatusCode",ue.getResponseStatusCode(),is(200)); @@ -318,7 +316,7 @@ public class ClientConnectTest catch (ExecutionException e) { // Expected Path - UpgradeException ue = assertExpectedError(e,wsocket,UpgradeException.class); + UpgradeException ue = assertExpectedError(e,wsocket,instanceOf(UpgradeException.class)); Assert.assertThat("UpgradeException.requestURI",ue.getRequestURI(),notNullValue()); Assert.assertThat("UpgradeException.requestURI",ue.getRequestURI().toASCIIString(),is(wsUri.toASCIIString())); Assert.assertThat("UpgradeException.responseStatusCode",ue.getResponseStatusCode(),is(101)); @@ -354,7 +352,7 @@ public class ClientConnectTest catch (ExecutionException e) { // Expected Path - UpgradeException ue = assertExpectedError(e,wsocket,UpgradeException.class); + UpgradeException ue = assertExpectedError(e,wsocket,instanceOf(UpgradeException.class)); Assert.assertThat("UpgradeException.requestURI",ue.getRequestURI(),notNullValue()); Assert.assertThat("UpgradeException.requestURI",ue.getRequestURI().toASCIIString(),is(wsUri.toASCIIString())); Assert.assertThat("UpgradeException.responseStatusCode",ue.getResponseStatusCode(),is(101)); @@ -383,7 +381,7 @@ public class ClientConnectTest catch (ExecutionException e) { // Expected Path - UpgradeException ue = assertExpectedError(e,wsocket,UpgradeException.class); + UpgradeException ue = assertExpectedError(e,wsocket,instanceOf(UpgradeException.class)); Assert.assertThat("UpgradeException.requestURI",ue.getRequestURI(),notNullValue()); Assert.assertThat("UpgradeException.requestURI",ue.getRequestURI().toASCIIString(),is(wsUri.toASCIIString())); Assert.assertThat("UpgradeException.responseStatusCode",ue.getResponseStatusCode(),is(101)); @@ -408,7 +406,7 @@ public class ClientConnectTest } catch (ExecutionException e) { - assertExpectedError(e,wsocket,UpgradeException.class); + assertExpectedError(e,wsocket,instanceOf(UpgradeException.class)); // Possible Passing Path (active session wait timeout) wsocket.assertNotOpened(); } @@ -443,15 +441,11 @@ public class ClientConnectTest } catch (ExecutionException e) { - if(OS.IS_WINDOWS) - { - // On windows, this is a SocketTimeoutException - assertExpectedError(e, wsocket, SocketTimeoutException.class); - } else - { - // Expected path - java.net.ConnectException - assertExpectedError(e,wsocket,ConnectException.class); - } + assertExpectedError(e, wsocket, + anyOf( + instanceOf(UpgradeException.class), + instanceOf(SocketTimeoutException.class), + instanceOf(ConnectException.class))); } } @@ -477,7 +471,7 @@ public class ClientConnectTest catch (ExecutionException e) { // Expected path - java.net.ConnectException ? - assertExpectedError(e,wsocket,ConnectException.class); + assertExpectedError(e,wsocket,instanceOf(ConnectException.class)); } } }