Merged branch 'lachlan-roberts-jetty-10.0.x-3696-connectToServer-ExecutionException' into 'jetty-10.0.x'.

This commit is contained in:
Simone Bordet 2019-05-29 19:21:52 +02:00
commit f4f9763761
2 changed files with 32 additions and 37 deletions

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -126,7 +127,7 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
JavaxClientUpgradeRequest upgradeRequest = new JavaxClientUpgradeRequest(this, getWebSocketCoreClient(), destURI, configuredEndpoint); JavaxClientUpgradeRequest upgradeRequest = new JavaxClientUpgradeRequest(this, getWebSocketCoreClient(), destURI, configuredEndpoint);
EndpointConfig config = configuredEndpoint.getConfig(); EndpointConfig config = configuredEndpoint.getConfig();
if (config != null && config instanceof ClientEndpointConfig) if (config instanceof ClientEndpointConfig)
{ {
ClientEndpointConfig clientEndpointConfig = (ClientEndpointConfig)config; ClientEndpointConfig clientEndpointConfig = (ClientEndpointConfig)config;
@ -140,26 +141,35 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
upgradeRequest.setSubProtocols(clientEndpointConfig.getPreferredSubprotocols()); upgradeRequest.setSubProtocols(clientEndpointConfig.getPreferredSubprotocols());
} }
long timeout = coreClient.getHttpClient().getConnectTimeout();
try try
{ {
Future<Session> sessionFuture = connect(upgradeRequest); Future<Session> sessionFuture = connect(upgradeRequest);
long timeout = coreClient.getHttpClient().getConnectTimeout();
if (timeout>0) if (timeout>0)
return sessionFuture.get(timeout+1000, TimeUnit.MILLISECONDS); return sessionFuture.get(timeout+1000, TimeUnit.MILLISECONDS);
return sessionFuture.get(); return sessionFuture.get();
} }
catch (ExecutionException e)
{
var cause = e.getCause();
if (cause instanceof RuntimeException)
throw (RuntimeException)cause;
if (cause instanceof IOException)
throw (IOException)cause;
throw new IOException(cause);
}
catch (TimeoutException e) catch (TimeoutException e)
{ {
throw new IOException("Connection future not completed " + destURI, e); throw new IOException("Connection future timeout " + timeout + " ms for " + destURI, e);
} }
catch (Exception e) catch (Throwable e)
{ {
throw new IOException("Unable to connect to " + destURI, e); throw new IOException("Unable to connect to " + destURI, e);
} }
} }
@Override @Override
public Session connectToServer(final Class<? extends Endpoint> endpointClass, final ClientEndpointConfig config, URI path) throws DeploymentException, IOException public Session connectToServer(final Class<? extends Endpoint> endpointClass, final ClientEndpointConfig config, URI path) throws IOException
{ {
ClientEndpointConfig clientEndpointConfig = config; ClientEndpointConfig clientEndpointConfig = config;
if (clientEndpointConfig == null) if (clientEndpointConfig == null)
@ -171,7 +181,7 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
} }
@Override @Override
public Session connectToServer(final Class<?> annotatedEndpointClass, final URI path) throws DeploymentException, IOException public Session connectToServer(final Class<?> annotatedEndpointClass, final URI path) throws IOException
{ {
ConfiguredEndpoint instance = newConfiguredEndpoint(annotatedEndpointClass, new EmptyClientEndpointConfig()); ConfiguredEndpoint instance = newConfiguredEndpoint(annotatedEndpointClass, new EmptyClientEndpointConfig());
return connect(instance, path); return connect(instance, path);
@ -212,11 +222,11 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
{ {
try try
{ {
return newConfiguredEndpoint(endpointClass.newInstance(), config); return newConfiguredEndpoint(endpointClass.getConstructor().newInstance(), config);
} }
catch (DeploymentException | InstantiationException | IllegalAccessException e) catch (Throwable e)
{ {
throw new InvalidWebSocketException("Unable to instantiate websocket: " + endpointClass.getClass()); throw new InvalidWebSocketException("Unable to instantiate websocket: " + endpointClass.getName());
} }
} }

View File

@ -18,18 +18,17 @@
package org.eclipse.jetty.websocket.javax.tests.client.misbehaving; package org.eclipse.jetty.websocket.javax.tests.client.misbehaving;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.websocket.ContainerProvider; import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer; import javax.websocket.WebSocketContainer;
import org.eclipse.jetty.util.log.StacklessLogging; import org.eclipse.jetty.util.log.StacklessLogging;
import org.eclipse.jetty.websocket.core.CloseException;
import org.eclipse.jetty.websocket.core.internal.WebSocketCoreSession; import org.eclipse.jetty.websocket.core.internal.WebSocketCoreSession;
import org.eclipse.jetty.websocket.javax.tests.CoreServer; import org.eclipse.jetty.websocket.javax.tests.CoreServer;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
@ -39,32 +38,21 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
public class MisbehavingClassTest public class MisbehavingClassTest
{ {
private CoreServer server;
private static CoreServer server; @BeforeEach
public void startServer() throws Exception
@SuppressWarnings("Duplicates")
@BeforeAll
public static void startServer() throws Exception
{ {
server = new CoreServer(new CoreServer.EchoNegotiator()); server = new CoreServer(new CoreServer.EchoNegotiator());
// Start Server
server.start(); server.start();
} }
@AfterAll @AfterEach
public static void stopServer() public void stopServer() throws Exception
{ {
try server.stop();
{
server.stop();
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
} }
@SuppressWarnings("Duplicates")
@Test @Test
public void testEndpointRuntimeOnOpen() throws Exception public void testEndpointRuntimeOnOpen() throws Exception
{ {
@ -74,9 +62,8 @@ public class MisbehavingClassTest
try (StacklessLogging ignored = new StacklessLogging(WebSocketCoreSession.class)) try (StacklessLogging ignored = new StacklessLogging(WebSocketCoreSession.class))
{ {
// expecting IOException during onOpen // Expecting CloseException during onOpen().
Exception e = assertThrows(IOException.class, () -> container.connectToServer(socket, server.getWsUri()), "Should have failed .connectToServer()"); assertThrows(CloseException.class, () -> container.connectToServer(socket, server.getWsUri()), "Should have failed .connectToServer()");
assertThat(e.getCause(), instanceOf(ExecutionException.class));
assertThat("Close should have occurred", socket.closeLatch.await(1, TimeUnit.SECONDS), is(true)); assertThat("Close should have occurred", socket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
@ -85,7 +72,6 @@ public class MisbehavingClassTest
} }
} }
@SuppressWarnings("Duplicates")
@Test @Test
public void testAnnotatedRuntimeOnOpen() throws Exception public void testAnnotatedRuntimeOnOpen() throws Exception
{ {
@ -95,9 +81,8 @@ public class MisbehavingClassTest
try (StacklessLogging ignored = new StacklessLogging(WebSocketCoreSession.class)) try (StacklessLogging ignored = new StacklessLogging(WebSocketCoreSession.class))
{ {
// expecting IOException during onOpen // Expecting CloseException during onOpen().
Exception e = assertThrows(IOException.class, () -> container.connectToServer(socket, server.getWsUri()), "Should have failed .connectToServer()"); assertThrows(CloseException.class, () -> container.connectToServer(socket, server.getWsUri()), "Should have failed .connectToServer()");
assertThat(e.getCause(), instanceOf(ExecutionException.class));
assertThat("Close should have occurred", socket.closeLatch.await(5, TimeUnit.SECONDS), is(true)); assertThat("Close should have occurred", socket.closeLatch.await(5, TimeUnit.SECONDS), is(true));