From 2669f0f37cb9155ae5cfe39df93307e8bfe36079 Mon Sep 17 00:00:00 2001 From: Abdelrahman Ellithy Date: Mon, 23 Nov 2026 15:48:25 +0300 Subject: [PATCH 1/3] fix: respect wsTimeout when opening WebSocket in JdkHttpClient JdkHttpClient previously ignored ClientConfig#wsTimeout() when opening a WebSocket connection, falling back to connectTimeout on the WebSocket builder and readTimeout (default 10 minutes) on the handshake future. - Store wsTimeout in JdkHttpClient from the provided ClientConfig. - Configure WebSocket.Builder#connectTimeout to wsTimeout when positive, avoiding IllegalArgumentException on Duration.ZERO. - Await the handshake future using wsTimeout (or indefinitely if zero). - Add unit tests verifying both handshake timeout and zero-timeout connection. --- .../internal/http/JdkHttpClient.java | 10 ++- .../internal/http/JdkWebSocketTest.java | 82 ++++++++++++++++++- 2 files changed, 86 insertions(+), 6 deletions(-) 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..a1e78d76b 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,7 @@ public class JdkHttpClient implements HttpClient { this.messages = new JdkHttpMessages(config); this.readTimeout = config.readTimeout(); this.connectTimeout = config.connectionTimeout(); + this.wsTimeout = config.wsTimeout(); this.handler = config.filter().andFinally(this::execute0); String poolName = "JdkHttpClient-" + POOL_COUNTER.getAndIncrement(); @@ -152,14 +154,18 @@ public WebSocket openSocket(HttpRequest request, WebSocket.Listener listener) { var builder = client.newWebSocketBuilder(); request.getHeaderNames().forEach(name -> builder.header(name, request.getHeader(name))); + if (!wsTimeout.isZero() && !wsTimeout.isNegative()) { + builder.connectTimeout(wsTimeout); + } 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 = wsTimeout.isZero() + ? socketFuture.get() + : socketFuture.get(wsTimeout.toNanos(), TimeUnit.NANOSECONDS); } 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..f2fb81e1c 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 @@ -133,25 +133,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 +230,69 @@ void failsIfTheServerDoesNotAcceptTheConnection() throws IOException { () -> client.openSocket(new HttpRequest(HttpMethod.GET, "http://127.0.0.1:" + port + "/ws"), new WebSocket.Listener() { })); } + + @Test + void timesOutWhenWebSocketHandshakeDoesNotComplete() 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()), + Duration.ofMillis(200), + Duration.ofMinutes(5), + Duration.ofSeconds(10))); + + long startNanos = System.nanoTime(); + assertThrows(ConnectionFailedException.class, () -> client.openSocket( + new HttpRequest(HttpMethod.GET, "/ws"), new WebSocket.Listener() { })); + long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos); + + assertTrue(elapsedMillis >= 150, + "Should wait for wsTimeout (at least 150ms), took " + elapsedMillis + "ms"); + assertTrue(elapsedMillis < 3000, + "Should time out close to wsTimeout rather than readTimeout or connectionTimeout, took " + + elapsedMillis + "ms"); + } finally { + thread.interrupt(); + } + } + } + + @Test + void connectsWithZeroWebSocketTimeoutWithoutThrowing() throws Exception { + BlockingQueue received = new LinkedBlockingQueue<>(); + CompletableFuture closeCode = new CompletableFuture<>(); + client = new JdkHttpClient(config(Duration.ZERO, Duration.ofSeconds(5), Duration.ofSeconds(5))); + + var listener = new WebSocket.Listener() { + @Override + public void onText(CharSequence data) { + received.add(data.toString()); + } + + @Override + public void onClose(int code, String reason) { + closeCode.complete(code); + } + }; + + WebSocket socket = client.openSocket( + new HttpRequest(HttpMethod.GET, "/ws/zero"), listener); + socket.sendText("hello with zero timeout"); + + assertEquals("hello from server", received.poll(5, TimeUnit.SECONDS)); + assertEquals("hello with zero timeout", clientFrame.get(5, TimeUnit.SECONDS)); + assertEquals(1000, closeCode.get(5, TimeUnit.SECONDS)); + } } From 42975bec8ba4b21a02eca5dd1401e5e1242a7883 Mon Sep 17 00:00:00 2001 From: Abdelrahman Ellithy Date: Mon, 23 Nov 2026 18:04:21 +0300 Subject: [PATCH 2/3] fix: address wsTimeout review feedback in JdkHttpClient --- .../java_client/AppiumClientConfig.java | 6 +- .../appium/java_client/http/ClientConfig.java | 3 + .../internal/http/JdkHttpClient.java | 10 +- .../internal/http/JdkWebSocketTest.java | 94 ++++++++++++++----- 4 files changed, 84 insertions(+), 29 deletions(-) diff --git a/src/main/java/io/appium/java_client/AppiumClientConfig.java b/src/main/java/io/appium/java_client/AppiumClientConfig.java index 4e297d4ff..f87e84042 100644 --- a/src/main/java/io/appium/java_client/AppiumClientConfig.java +++ b/src/main/java/io/appium/java_client/AppiumClientConfig.java @@ -234,9 +234,11 @@ public Duration wsTimeout() { } /** - * Sets the web socket timeout. + * Sets the web socket timeout used to bound WebSocket handshake establishment + * in {@link io.appium.java_client.internal.http.JdkHttpClient}. 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 connection to be established * @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..b458a7e63 100644 --- a/src/main/java/io/appium/java_client/http/ClientConfig.java +++ b/src/main/java/io/appium/java_client/http/ClientConfig.java @@ -53,6 +53,9 @@ public interface ClientConfig { /** * The maximum time to wait for a WebSocket connection to be established. + * {@link io.appium.java_client.internal.http.JdkHttpClient} uses this as the + * WebSocket handshake timeout. 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 a1e78d76b..78d48d288 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 @@ -154,18 +154,16 @@ public WebSocket openSocket(HttpRequest request, WebSocket.Listener listener) { var builder = client.newWebSocketBuilder(); request.getHeaderNames().forEach(name -> builder.header(name, request.getHeader(name))); - if (!wsTimeout.isZero() && !wsTimeout.isNegative()) { - builder.connectTimeout(wsTimeout); - } CompletableFuture closed = new CompletableFuture<>(); CompletableFuture socketFuture = builder .buildAsync(uri, new JdkListener(listener, closed)); + Duration handshakeTimeout = wsTimeout.compareTo(Duration.ZERO) > 0 + ? wsTimeout + : readTimeout; java.net.http.WebSocket underlyingSocket; try { - underlyingSocket = wsTimeout.isZero() - ? socketFuture.get() - : socketFuture.get(wsTimeout.toNanos(), TimeUnit.NANOSECONDS); + underlyingSocket = socketFuture.get(handshakeTimeout.toNanos(), TimeUnit.NANOSECONDS); } 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 f2fb81e1c..8219163bd 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 @@ -254,10 +254,13 @@ void timesOutWhenWebSocketHandshakeDoesNotComplete() throws Exception { Duration.ofSeconds(10))); long startNanos = System.nanoTime(); - assertThrows(ConnectionFailedException.class, () -> client.openSocket( - new HttpRequest(HttpMethod.GET, "/ws"), new WebSocket.Listener() { })); + 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 deterministic timeout message, got: " + ex.getMessage()); assertTrue(elapsedMillis >= 150, "Should wait for wsTimeout (at least 150ms), took " + elapsedMillis + "ms"); assertTrue(elapsedMillis < 3000, @@ -270,29 +273,78 @@ void timesOutWhenWebSocketHandshakeDoesNotComplete() throws Exception { } @Test - void connectsWithZeroWebSocketTimeoutWithoutThrowing() throws Exception { - BlockingQueue received = new LinkedBlockingQueue<>(); - CompletableFuture closeCode = new CompletableFuture<>(); - client = new JdkHttpClient(config(Duration.ZERO, Duration.ofSeconds(5), Duration.ofSeconds(5))); + void fallsBackToReadTimeoutWhenWsTimeoutIsZero() 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(); - var listener = new WebSocket.Listener() { - @Override - public void onText(CharSequence data) { - received.add(data.toString()); - } + try { + client = new JdkHttpClient(config( + URI.create("http://127.0.0.1:" + stallServer.getLocalPort()), + Duration.ZERO, + Duration.ofMillis(200), + Duration.ofSeconds(10))); - @Override - public void onClose(int code, String reason) { - closeCode.complete(code); + long startNanos = System.nanoTime(); + assertThrows(ConnectionFailedException.class, () -> client.openSocket( + new HttpRequest(HttpMethod.GET, "/ws"), new WebSocket.Listener() { })); + long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos); + + assertTrue(elapsedMillis >= 150, + "Should wait for readTimeout fallback (at least 150ms), took " + elapsedMillis + "ms"); + assertTrue(elapsedMillis < 3000, + "Should time out close to readTimeout (200ms), not wait forever, took " + + elapsedMillis + "ms"); + } finally { + thread.interrupt(); } - }; + } + } - WebSocket socket = client.openSocket( - new HttpRequest(HttpMethod.GET, "/ws/zero"), listener); - socket.sendText("hello with zero timeout"); + @Test + void fallsBackToReadTimeoutWhenWsTimeoutIsNegative() 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(); - assertEquals("hello from server", received.poll(5, TimeUnit.SECONDS)); - assertEquals("hello with zero timeout", clientFrame.get(5, TimeUnit.SECONDS)); - assertEquals(1000, closeCode.get(5, TimeUnit.SECONDS)); + try { + client = new JdkHttpClient(config( + URI.create("http://127.0.0.1:" + stallServer.getLocalPort()), + Duration.ofMillis(-100), + Duration.ofMillis(200), + Duration.ofSeconds(10))); + + long startNanos = System.nanoTime(); + assertThrows(ConnectionFailedException.class, () -> client.openSocket( + new HttpRequest(HttpMethod.GET, "/ws"), new WebSocket.Listener() { })); + long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos); + + assertTrue(elapsedMillis >= 150, + "Should wait for readTimeout fallback (at least 150ms), took " + elapsedMillis + "ms"); + assertTrue(elapsedMillis < 3000, + "Should time out close to readTimeout (200ms), not fail immediately, took " + + elapsedMillis + "ms"); + } finally { + thread.interrupt(); + } + } } } From 1b8bcdeb4f28b8080a7f2572e3bff4fd6cb41a0e Mon Sep 17 00:00:00 2001 From: Abdelrahman Ellithy Date: Mon, 23 Nov 2026 21:29:11 +0300 Subject: [PATCH 3/3] fix: refine wsTimeout resolution, Javadoc, and tests --- .../java_client/AppiumClientConfig.java | 7 +- .../appium/java_client/http/ClientConfig.java | 6 +- .../internal/http/JdkHttpClient.java | 10 +- .../internal/http/JdkWebSocketTest.java | 108 ++++-------------- 4 files changed, 33 insertions(+), 98 deletions(-) diff --git a/src/main/java/io/appium/java_client/AppiumClientConfig.java b/src/main/java/io/appium/java_client/AppiumClientConfig.java index f87e84042..2c0cce1d0 100644 --- a/src/main/java/io/appium/java_client/AppiumClientConfig.java +++ b/src/main/java/io/appium/java_client/AppiumClientConfig.java @@ -234,11 +234,10 @@ public Duration wsTimeout() { } /** - * Sets the web socket timeout used to bound WebSocket handshake establishment - * in {@link io.appium.java_client.internal.http.JdkHttpClient}. Zero or negative - * values cause a fallback to {@link #readTimeout()}. + * Sets the WebSocket handshake timeout. Zero or negative values cause + * a fallback to {@link #readTimeout()}. * - * @param timeout the maximum time to wait for a WebSocket connection to be established + * @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 b458a7e63..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,10 +52,8 @@ public interface ClientConfig { Duration readTimeout(); /** - * The maximum time to wait for a WebSocket connection to be established. - * {@link io.appium.java_client.internal.http.JdkHttpClient} uses this as the - * WebSocket handshake timeout. Zero or negative values cause a fallback to - * {@link #readTimeout()}. + * 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 78d48d288..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 @@ -93,7 +93,10 @@ public class JdkHttpClient implements HttpClient { this.messages = new JdkHttpMessages(config); this.readTimeout = config.readTimeout(); this.connectTimeout = config.connectionTimeout(); - this.wsTimeout = config.wsTimeout(); + 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(); @@ -158,12 +161,9 @@ public WebSocket openSocket(HttpRequest request, WebSocket.Listener listener) { CompletableFuture socketFuture = builder .buildAsync(uri, new JdkListener(listener, closed)); - Duration handshakeTimeout = wsTimeout.compareTo(Duration.ZERO) > 0 - ? wsTimeout - : readTimeout; java.net.http.WebSocket underlyingSocket; try { - underlyingSocket = socketFuture.get(handshakeTimeout.toNanos(), TimeUnit.NANOSECONDS); + 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 8219163bd..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; @@ -231,8 +235,21 @@ void failsIfTheServerDoesNotAcceptTheConnection() throws IOException { new WebSocket.Listener() { })); } - @Test - void timesOutWhenWebSocketHandshakeDoesNotComplete() throws Exception { + 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()) { @@ -249,9 +266,7 @@ void timesOutWhenWebSocketHandshakeDoesNotComplete() throws Exception { try { client = new JdkHttpClient(config( URI.create("http://127.0.0.1:" + stallServer.getLocalPort()), - Duration.ofMillis(200), - Duration.ofMinutes(5), - Duration.ofSeconds(10))); + wsTimeout, readTimeout, Duration.ofSeconds(10))); long startNanos = System.nanoTime(); ConnectionFailedException ex = assertThrows(ConnectionFailedException.class, @@ -260,88 +275,11 @@ void timesOutWhenWebSocketHandshakeDoesNotComplete() throws Exception { long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos); assertTrue(ex.getMessage().contains("initial request timeout"), - "Expected deterministic timeout message, got: " + ex.getMessage()); - assertTrue(elapsedMillis >= 150, - "Should wait for wsTimeout (at least 150ms), took " + elapsedMillis + "ms"); - assertTrue(elapsedMillis < 3000, - "Should time out close to wsTimeout rather than readTimeout or connectionTimeout, took " - + elapsedMillis + "ms"); - } finally { - thread.interrupt(); - } - } - } - - @Test - void fallsBackToReadTimeoutWhenWsTimeoutIsZero() 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()), - Duration.ZERO, - Duration.ofMillis(200), - Duration.ofSeconds(10))); - - long startNanos = System.nanoTime(); - assertThrows(ConnectionFailedException.class, () -> client.openSocket( - new HttpRequest(HttpMethod.GET, "/ws"), new WebSocket.Listener() { })); - long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos); - - assertTrue(elapsedMillis >= 150, - "Should wait for readTimeout fallback (at least 150ms), took " + elapsedMillis + "ms"); - assertTrue(elapsedMillis < 3000, - "Should time out close to readTimeout (200ms), not wait forever, took " - + elapsedMillis + "ms"); - } finally { - thread.interrupt(); - } - } - } - - @Test - void fallsBackToReadTimeoutWhenWsTimeoutIsNegative() 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()), - Duration.ofMillis(-100), - Duration.ofMillis(200), - Duration.ofSeconds(10))); - - long startNanos = System.nanoTime(); - assertThrows(ConnectionFailedException.class, () -> client.openSocket( - new HttpRequest(HttpMethod.GET, "/ws"), new WebSocket.Listener() { })); - long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos); - + "Expected timeout message, got: " + ex.getMessage()); assertTrue(elapsedMillis >= 150, - "Should wait for readTimeout fallback (at least 150ms), took " + elapsedMillis + "ms"); + "Should wait at least 150ms, took " + elapsedMillis + "ms"); assertTrue(elapsedMillis < 3000, - "Should time out close to readTimeout (200ms), not fail immediately, took " - + elapsedMillis + "ms"); + "Should time out near the effective timeout, took " + elapsedMillis + "ms"); } finally { thread.interrupt(); }