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.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@ -126,7 +127,7 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
JavaxClientUpgradeRequest upgradeRequest = new JavaxClientUpgradeRequest(this, getWebSocketCoreClient(), destURI, configuredEndpoint);
EndpointConfig config = configuredEndpoint.getConfig();
if (config != null && config instanceof ClientEndpointConfig)
if (config instanceof ClientEndpointConfig)
{
ClientEndpointConfig clientEndpointConfig = (ClientEndpointConfig)config;
@ -140,26 +141,35 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
upgradeRequest.setSubProtocols(clientEndpointConfig.getPreferredSubprotocols());
}
long timeout = coreClient.getHttpClient().getConnectTimeout();
try
{
Future<Session> sessionFuture = connect(upgradeRequest);
long timeout = coreClient.getHttpClient().getConnectTimeout();
if (timeout>0)
return sessionFuture.get(timeout+1000, TimeUnit.MILLISECONDS);
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)
{
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);
}
}
@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;
if (clientEndpointConfig == null)
@ -171,7 +181,7 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
}
@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());
return connect(instance, path);
@ -212,11 +222,11 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
{
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;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;
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.javax.tests.CoreServer;
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;
@ -39,32 +38,21 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
public class MisbehavingClassTest
{
private CoreServer server;
private static CoreServer server;
@SuppressWarnings("Duplicates")
@BeforeAll
public static void startServer() throws Exception
@BeforeEach
public void startServer() throws Exception
{
server = new CoreServer(new CoreServer.EchoNegotiator());
// Start Server
server.start();
}
@AfterAll
public static void stopServer()
@AfterEach
public void stopServer() throws Exception
{
try
{
server.stop();
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
server.stop();
}
@SuppressWarnings("Duplicates")
@Test
public void testEndpointRuntimeOnOpen() throws Exception
{
@ -74,9 +62,8 @@ public class MisbehavingClassTest
try (StacklessLogging ignored = new StacklessLogging(WebSocketCoreSession.class))
{
// expecting IOException during onOpen
Exception e = assertThrows(IOException.class, () -> container.connectToServer(socket, server.getWsUri()), "Should have failed .connectToServer()");
assertThat(e.getCause(), instanceOf(ExecutionException.class));
// Expecting CloseException during onOpen().
assertThrows(CloseException.class, () -> container.connectToServer(socket, server.getWsUri()), "Should have failed .connectToServer()");
assertThat("Close should have occurred", socket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
@ -85,7 +72,6 @@ public class MisbehavingClassTest
}
}
@SuppressWarnings("Duplicates")
@Test
public void testAnnotatedRuntimeOnOpen() throws Exception
{
@ -95,9 +81,8 @@ public class MisbehavingClassTest
try (StacklessLogging ignored = new StacklessLogging(WebSocketCoreSession.class))
{
// expecting IOException during onOpen
Exception e = assertThrows(IOException.class, () -> container.connectToServer(socket, server.getWsUri()), "Should have failed .connectToServer()");
assertThat(e.getCause(), instanceOf(ExecutionException.class));
// Expecting CloseException during onOpen().
assertThrows(CloseException.class, () -> container.connectToServer(socket, server.getWsUri()), "Should have failed .connectToServer()");
assertThat("Close should have occurred", socket.closeLatch.await(5, TimeUnit.SECONDS), is(true));