Issue #3170 - fail the CompletableFuture if onOpen throws

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2019-02-28 17:32:38 +11:00
parent bd50e2941c
commit 37ab716bc5
2 changed files with 11 additions and 1 deletions

View File

@ -440,6 +440,13 @@ public class WebSocketChannel implements IncomingFrames, FrameHandler.CoreSessio
catch (Throwable t)
{
openCallback.failed(t);
/*
This is double handling of the exception but we need to do this because we have two separate
mechanisms for returning the CoreSession, onOpen and the CompletableFuture and both the onOpen callback
and the CompletableFuture require the exception.
*/
throw new RuntimeException(t);
}
}

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.net.URI;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@ -57,6 +58,7 @@ import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class WebSocketProxyTest
{
@ -231,7 +233,8 @@ public class WebSocketProxyTest
try (StacklessLogging stacklessLogging = new StacklessLogging(WebSocketChannel.class))
{
CompletableFuture<CoreSession> response = _client.connect(clientHandler, new URI("ws://localhost:8080/proxy/"));
response.get(5, TimeUnit.SECONDS);
Exception e = assertThrows(ExecutionException.class, ()->response.get(5, TimeUnit.SECONDS));
assertThat(e.getMessage(), containsString("simulated client onOpen error"));
clientHandler.awaitClose();
serverFrameHandler.awaitClose();
awaitProxyClose(proxyClientSide, proxyServerSide);