nastra commented on code in PR #10053: URL: https://github.com/apache/iceberg/pull/10053#discussion_r1555349405
########## core/src/test/java/org/apache/iceberg/rest/TestHTTPClient.java: ########## @@ -133,6 +136,67 @@ public void testDynamicHttpRequestInterceptorLoading() { assertThat(((TestHttpRequestInterceptor) interceptor).properties).isEqualTo(properties); } + @Test + public void testSocketAndConnectionTimeoutSet() { + long connectionTimeoutMs = 10L; + int socketTimeoutMs = 10; + Map<String, String> properties = + ImmutableMap.of( + HTTPClient.REST_CONNECTION_TIMEOUT_MS, String.valueOf(connectionTimeoutMs), + HTTPClient.REST_SOCKET_TIMEOUT_MS, String.valueOf(socketTimeoutMs)); + + ConnectionConfig connectionConfig = HTTPClient.configureConnectionConfig(properties); + assertThat(connectionConfig).isNotNull(); + assertThat(connectionConfig.getConnectTimeout().getDuration()).isEqualTo(connectionTimeoutMs); + assertThat(connectionConfig.getSocketTimeout().getDuration()).isEqualTo(socketTimeoutMs); + } + + @Test + public void testSocketTimeout() throws IOException { + long socketTimeoutMs = 2000L; + Map<String, String> properties = + ImmutableMap.of(HTTPClient.REST_SOCKET_TIMEOUT_MS, String.valueOf(socketTimeoutMs)); + String path = "socket/timeout/path"; + + try (HTTPClient client = HTTPClient.builder(properties).uri(URI).build()) { + HttpRequest mockRequest = + request() + .withPath("/" + path) + .withMethod(HttpMethod.HEAD.name().toUpperCase(Locale.ROOT)); + // Setting a response delay of 5 seconds to simulate hitting the configured socket timeout of + // 2 seconds + HttpResponse mockResponse = + response() + .withStatusCode(200) + .withBody("Delayed response") + .withDelay(TimeUnit.MILLISECONDS, 5000); + mockServer.when(mockRequest).respond(mockResponse); + + Assertions.assertThatThrownBy(() -> client.head(path, ImmutableMap.of(), (unused) -> {})) + .cause() + .isInstanceOf(SocketTimeoutException.class); + } + } + + @Test + public void testInvalidConnectionTimeout() { + testInvalidTimeouts(HTTPClient.REST_CONNECTION_TIMEOUT_MS); + } + + @Test + public void testInvalidSocketTimeout() { + testInvalidTimeouts(HTTPClient.REST_SOCKET_TIMEOUT_MS); + } + + private void testInvalidTimeouts(String timeoutMsType) { Review Comment: ```suggestion @ParameterizedTest @ValueSource(strings = {HTTPClient.REST_CONNECTION_TIMEOUT_MS), HTTPClient.REST_SOCKET_TIMEOUT_MS}) public void testInvalidTimeout(String timeoutType) { ``` ########## core/src/test/java/org/apache/iceberg/rest/TestHTTPClient.java: ########## @@ -133,6 +136,67 @@ public void testDynamicHttpRequestInterceptorLoading() { assertThat(((TestHttpRequestInterceptor) interceptor).properties).isEqualTo(properties); } + @Test + public void testSocketAndConnectionTimeoutSet() { + long connectionTimeoutMs = 10L; + int socketTimeoutMs = 10; + Map<String, String> properties = + ImmutableMap.of( + HTTPClient.REST_CONNECTION_TIMEOUT_MS, String.valueOf(connectionTimeoutMs), + HTTPClient.REST_SOCKET_TIMEOUT_MS, String.valueOf(socketTimeoutMs)); + + ConnectionConfig connectionConfig = HTTPClient.configureConnectionConfig(properties); + assertThat(connectionConfig).isNotNull(); + assertThat(connectionConfig.getConnectTimeout().getDuration()).isEqualTo(connectionTimeoutMs); + assertThat(connectionConfig.getSocketTimeout().getDuration()).isEqualTo(socketTimeoutMs); + } + + @Test + public void testSocketTimeout() throws IOException { + long socketTimeoutMs = 2000L; + Map<String, String> properties = + ImmutableMap.of(HTTPClient.REST_SOCKET_TIMEOUT_MS, String.valueOf(socketTimeoutMs)); + String path = "socket/timeout/path"; + + try (HTTPClient client = HTTPClient.builder(properties).uri(URI).build()) { + HttpRequest mockRequest = + request() + .withPath("/" + path) + .withMethod(HttpMethod.HEAD.name().toUpperCase(Locale.ROOT)); + // Setting a response delay of 5 seconds to simulate hitting the configured socket timeout of + // 2 seconds + HttpResponse mockResponse = + response() + .withStatusCode(200) + .withBody("Delayed response") + .withDelay(TimeUnit.MILLISECONDS, 5000); + mockServer.when(mockRequest).respond(mockResponse); + + Assertions.assertThatThrownBy(() -> client.head(path, ImmutableMap.of(), (unused) -> {})) + .cause() + .isInstanceOf(SocketTimeoutException.class); + } + } + + @Test + public void testInvalidConnectionTimeout() { + testInvalidTimeouts(HTTPClient.REST_CONNECTION_TIMEOUT_MS); + } + + @Test + public void testInvalidSocketTimeout() { + testInvalidTimeouts(HTTPClient.REST_SOCKET_TIMEOUT_MS); + } + + private void testInvalidTimeouts(String timeoutMsType) { Review Comment: you can then remove `testInvalidConnectionTimeout` / `testInvalidSocketTimeout` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org