diff --git a/src/main/java/io/appium/java_client/AppiumClientConfig.java b/src/main/java/io/appium/java_client/AppiumClientConfig.java index 4e297d4ff..2c0cce1d0 100644 --- a/src/main/java/io/appium/java_client/AppiumClientConfig.java +++ b/src/main/java/io/appium/java_client/AppiumClientConfig.java @@ -234,9 +234,10 @@ public Duration wsTimeout() { } /** - * Sets the web socket timeout. + * Sets the WebSocket handshake timeout. Zero or negative values cause + * a fallback to {@link #readTimeout()}. * - * @param timeout the maximum time to wait for a web socket message + * @param timeout the maximum time to wait for a WebSocket handshake to complete * @return A new instance of AppiumClientConfig */ public AppiumClientConfig wsTimeout(Duration timeout) { diff --git a/src/main/java/io/appium/java_client/http/ClientConfig.java b/src/main/java/io/appium/java_client/http/ClientConfig.java index df9e0d1e4..22fa3ce40 100644 --- a/src/main/java/io/appium/java_client/http/ClientConfig.java +++ b/src/main/java/io/appium/java_client/http/ClientConfig.java @@ -52,7 +52,8 @@ public interface ClientConfig { Duration readTimeout(); /** - * The maximum time to wait for a WebSocket connection to be established. + * The maximum time to wait for a WebSocket handshake to complete. + * Zero or negative values cause a fallback to {@link #readTimeout()}. * * @return the WebSocket timeout */ diff --git a/src/main/java/io/appium/java_client/internal/http/JdkHttpClient.java b/src/main/java/io/appium/java_client/internal/http/JdkHttpClient.java index 4ed776177..8ade66372 100644 --- a/src/main/java/io/appium/java_client/internal/http/JdkHttpClient.java +++ b/src/main/java/io/appium/java_client/internal/http/JdkHttpClient.java @@ -85,6 +85,7 @@ public class JdkHttpClient implements HttpClient { private final ExecutorService executorService; private final Duration readTimeout; private final Duration connectTimeout; + private final Duration wsTimeout; private java.net.http.HttpClient client; JdkHttpClient(ClientConfig config) { @@ -92,6 +93,10 @@ public class JdkHttpClient implements HttpClient { this.messages = new JdkHttpMessages(config); this.readTimeout = config.readTimeout(); this.connectTimeout = config.connectionTimeout(); + Duration configuredWsTimeout = config.wsTimeout(); + this.wsTimeout = configuredWsTimeout != null && configuredWsTimeout.compareTo(Duration.ZERO) > 0 + ? configuredWsTimeout + : this.readTimeout; this.handler = config.filter().andFinally(this::execute0); String poolName = "JdkHttpClient-" + POOL_COUNTER.getAndIncrement(); @@ -154,12 +159,11 @@ public WebSocket openSocket(HttpRequest request, WebSocket.Listener listener) { request.getHeaderNames().forEach(name -> builder.header(name, request.getHeader(name))); CompletableFuture closed = new CompletableFuture<>(); CompletableFuture socketFuture = builder - .connectTimeout(connectTimeout) .buildAsync(uri, new JdkListener(listener, closed)); java.net.http.WebSocket underlyingSocket; try { - underlyingSocket = socketFuture.get(readTimeout.toMillis(), TimeUnit.MILLISECONDS); + underlyingSocket = socketFuture.get(wsTimeout.toMillis(), TimeUnit.MILLISECONDS); } catch (CancellationException e) { throw new ConnectionFailedException("JdkWebSocket initial request canceled", e); } catch (ExecutionException e) { diff --git a/src/test/java/io/appium/java_client/internal/http/JdkWebSocketTest.java b/src/test/java/io/appium/java_client/internal/http/JdkWebSocketTest.java index 894a4127a..476967cf9 100644 --- a/src/test/java/io/appium/java_client/internal/http/JdkWebSocketTest.java +++ b/src/test/java/io/appium/java_client/internal/http/JdkWebSocketTest.java @@ -25,6 +25,9 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.openqa.selenium.Credentials; import javax.net.ssl.SSLContext; @@ -48,6 +51,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -133,25 +137,34 @@ private static String readTextFrame(DataInputStream in) throws IOException { } private ClientConfig config() { + return config(Duration.ofSeconds(5), Duration.ofSeconds(5), Duration.ofSeconds(5)); + } + + private ClientConfig config(Duration wsTimeout, Duration readTimeout, Duration connectionTimeout) { + return config(URI.create("http://127.0.0.1:" + serverSocket.getLocalPort()), + wsTimeout, readTimeout, connectionTimeout); + } + + private ClientConfig config(URI uri, Duration wsTimeout, Duration readTimeout, Duration connectionTimeout) { return new ClientConfig() { @Override public URI baseUri() { - return URI.create("http://127.0.0.1:" + serverSocket.getLocalPort()); + return uri; } @Override public Duration connectionTimeout() { - return Duration.ofSeconds(5); + return connectionTimeout; } @Override public Duration readTimeout() { - return Duration.ofSeconds(5); + return readTimeout; } @Override public Duration wsTimeout() { - return Duration.ofSeconds(5); + return wsTimeout; } @Override @@ -221,4 +234,55 @@ void failsIfTheServerDoesNotAcceptTheConnection() throws IOException { () -> client.openSocket(new HttpRequest(HttpMethod.GET, "http://127.0.0.1:" + port + "/ws"), new WebSocket.Listener() { })); } + + static Stream handshakeTimeoutCases() { + return Stream.of( + Arguments.of("positive wsTimeout", + Duration.ofMillis(200), Duration.ofMinutes(5)), + Arguments.of("zero wsTimeout falls back to readTimeout", + Duration.ZERO, Duration.ofMillis(200)), + Arguments.of("negative wsTimeout falls back to readTimeout", + Duration.ofMillis(-100), Duration.ofMillis(200)) + ); + } + + @ParameterizedTest(name = "{0}") + @MethodSource("handshakeTimeoutCases") + void timesOutWhenHandshakeDoesNotComplete(String description, Duration wsTimeout, + Duration readTimeout) throws Exception { + try (ServerSocket stallServer = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) { + Thread thread = new Thread(() -> { + try (Socket ignored = stallServer.accept()) { + Thread.sleep(10_000); + } catch (InterruptedException ignored) { + Thread.currentThread().interrupt(); + } catch (IOException ignored) { + // Closed when stallServer closes + } + }); + thread.setDaemon(true); + thread.start(); + + try { + client = new JdkHttpClient(config( + URI.create("http://127.0.0.1:" + stallServer.getLocalPort()), + wsTimeout, readTimeout, Duration.ofSeconds(10))); + + long startNanos = System.nanoTime(); + ConnectionFailedException ex = assertThrows(ConnectionFailedException.class, + () -> client.openSocket( + new HttpRequest(HttpMethod.GET, "/ws"), new WebSocket.Listener() { })); + long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos); + + assertTrue(ex.getMessage().contains("initial request timeout"), + "Expected timeout message, got: " + ex.getMessage()); + assertTrue(elapsedMillis >= 150, + "Should wait at least 150ms, took " + elapsedMillis + "ms"); + assertTrue(elapsedMillis < 3000, + "Should time out near the effective timeout, took " + elapsedMillis + "ms"); + } finally { + thread.interrupt(); + } + } + } }