Skip to content

Commit b945bfa

Browse files
committed
Add integration tests for connection timeouts
This change adds basic integration test coverage for connection timeouts for the sync and async clients. The tests work by sending a request to a non-routable IP address, with timeouts configured two different ways (`ConnectionConfig` and `RequestConfig`), using both HTTP and HTTPS. The connection request gets blackholed pretty reliably, and then the tests assert that we actually waited a reasonable length of time with respect to the configured timeout. Finally, there are some client-specific assertions for the type and content of the exception thrown.
1 parent e2e07eb commit b945bfa

2 files changed

Lines changed: 226 additions & 0 deletions

File tree

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* ====================================================================
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
* ====================================================================
20+
*
21+
* This software consists of voluntary contributions made by many
22+
* individuals on behalf of the Apache Software Foundation. For more
23+
* information on the Apache Software Foundation, please see
24+
* <http://www.apache.org/>.
25+
*
26+
*/
27+
28+
package org.apache.hc.client5.testing.async;
29+
30+
import org.apache.hc.client5.http.ConnectTimeoutException;
31+
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
32+
import org.apache.hc.client5.http.config.ConnectionConfig;
33+
import org.apache.hc.client5.http.config.RequestConfig;
34+
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
35+
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
36+
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
37+
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
38+
import org.apache.hc.core5.reactor.IOReactorConfig;
39+
import org.junit.jupiter.api.Timeout;
40+
import org.junit.jupiter.params.ParameterizedTest;
41+
import org.junit.jupiter.params.provider.ValueSource;
42+
43+
import java.time.Duration;
44+
import java.time.temporal.ChronoUnit;
45+
import java.util.concurrent.ExecutionException;
46+
47+
import static java.lang.String.format;
48+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
49+
import static org.apache.hc.core5.util.TimeValue.ofMilliseconds;
50+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
51+
import static org.junit.jupiter.api.Assertions.assertThrows;
52+
import static org.junit.jupiter.api.Assertions.assertTrue;
53+
54+
@SuppressWarnings("deprecation")
55+
class TestAsyncConnectionTimeouts {
56+
private static final Duration EXPECTED_TIMEOUT = Duration.ofMillis(500);
57+
58+
@Timeout(5)
59+
@ParameterizedTest
60+
@ValueSource(strings = { "http", "https" })
61+
void testRequestConfig(final String scheme) throws Exception {
62+
try (
63+
final CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create()
64+
.setIOReactorConfig(IOReactorConfig.custom().setSelectInterval(ofMilliseconds(50)).build())
65+
.setDefaultRequestConfig(RequestConfig.custom()
66+
.setConnectTimeout(EXPECTED_TIMEOUT.toMillis(), MILLISECONDS)
67+
.build())
68+
.build()
69+
) {
70+
warmUpClient(client);
71+
assertTimeout(getRequest(scheme), client);
72+
}
73+
}
74+
75+
@Timeout(5)
76+
@ParameterizedTest
77+
@ValueSource(strings = { "http", "https" })
78+
void testConnectionConfig(final String scheme) throws Exception {
79+
final PoolingAsyncClientConnectionManager connMgr = PoolingAsyncClientConnectionManagerBuilder.create()
80+
.setDefaultConnectionConfig(
81+
ConnectionConfig.custom()
82+
.setConnectTimeout(EXPECTED_TIMEOUT.toMillis(), MILLISECONDS)
83+
.build())
84+
.build();
85+
try (
86+
final CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create()
87+
.setIOReactorConfig(IOReactorConfig.custom().setSelectInterval(ofMilliseconds(50)).build())
88+
.setConnectionManager(connMgr)
89+
.build()
90+
) {
91+
warmUpClient(client);
92+
assertTimeout(getRequest(scheme), client);
93+
}
94+
}
95+
96+
private static void warmUpClient(final CloseableHttpAsyncClient client) {
97+
client.start();
98+
}
99+
100+
private static SimpleHttpRequest getRequest(final String scheme) {
101+
return SimpleHttpRequest.create("GET", scheme + "://198.51.100.1/ping");
102+
}
103+
104+
private static void assertTimeout(final SimpleHttpRequest request, final CloseableHttpAsyncClient client) {
105+
final long startTime = System.nanoTime();
106+
final Throwable ex = assertThrows(ExecutionException.class, () -> client.execute(request, null).get())
107+
.getCause();
108+
final Duration actualTime = Duration.of(System.nanoTime() - startTime, ChronoUnit.NANOS);
109+
assertTrue(actualTime.toMillis() > EXPECTED_TIMEOUT.toMillis() / 2,
110+
format("Connection attempt timed out too soon (only %,d out of %,d ms)",
111+
actualTime.toMillis(),
112+
EXPECTED_TIMEOUT.toMillis()));
113+
assertTrue(actualTime.toMillis() < EXPECTED_TIMEOUT.toMillis() * 2,
114+
format("Connection attempt timed out too late (%,d out of %,d ms)",
115+
actualTime.toMillis(),
116+
EXPECTED_TIMEOUT.toMillis()));
117+
assertInstanceOf(ConnectTimeoutException.class, ex);
118+
assertTrue(ex.getMessage().contains(EXPECTED_TIMEOUT.toMillis() + " MILLISECONDS"), ex.getMessage());
119+
}
120+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* ====================================================================
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
* ====================================================================
20+
*
21+
* This software consists of voluntary contributions made by many
22+
* individuals on behalf of the Apache Software Foundation. For more
23+
* information on the Apache Software Foundation, please see
24+
* <http://www.apache.org/>.
25+
*
26+
*/
27+
28+
package org.apache.hc.client5.testing.sync;
29+
30+
import org.apache.hc.client5.http.ConnectTimeoutException;
31+
import org.apache.hc.client5.http.classic.HttpClient;
32+
import org.apache.hc.client5.http.classic.methods.HttpGet;
33+
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
34+
import org.apache.hc.client5.http.config.ConnectionConfig;
35+
import org.apache.hc.client5.http.config.RequestConfig;
36+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
37+
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
38+
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
39+
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
40+
import org.apache.hc.core5.http.ClassicHttpRequest;
41+
import org.junit.jupiter.api.Timeout;
42+
import org.junit.jupiter.params.ParameterizedTest;
43+
import org.junit.jupiter.params.provider.ValueSource;
44+
45+
import java.time.Duration;
46+
import java.time.temporal.ChronoUnit;
47+
48+
import static java.lang.String.format;
49+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
50+
import static org.junit.jupiter.api.Assertions.assertThrows;
51+
import static org.junit.jupiter.api.Assertions.assertTrue;
52+
53+
@SuppressWarnings("deprecation")
54+
class TestConnectionTimeouts {
55+
private static final Duration EXPECTED_TIMEOUT = Duration.ofMillis(500);
56+
57+
@Timeout(5)
58+
@ParameterizedTest
59+
@ValueSource(strings = { "http", "https" })
60+
void testRequestConfig(final String scheme) throws Exception {
61+
try (final CloseableHttpClient client = HttpClientBuilder.create()
62+
.setDefaultRequestConfig(RequestConfig.custom()
63+
.setConnectTimeout(EXPECTED_TIMEOUT.toMillis(), MILLISECONDS)
64+
.build())
65+
.build()) {
66+
assertTimeout(getRequest(scheme), client);
67+
}
68+
}
69+
70+
@Timeout(5)
71+
@ParameterizedTest
72+
@ValueSource(strings = { "http", "https" })
73+
void testConnectionConfig(final String scheme) throws Exception {
74+
final PoolingHttpClientConnectionManager connMgr = PoolingHttpClientConnectionManagerBuilder.create()
75+
.setDefaultConnectionConfig(
76+
ConnectionConfig.custom()
77+
.setConnectTimeout(EXPECTED_TIMEOUT.toMillis(), MILLISECONDS)
78+
.build())
79+
.build();
80+
try (final CloseableHttpClient client = HttpClientBuilder.create().setConnectionManager(connMgr).build()) {
81+
assertTimeout(getRequest(scheme), client);
82+
}
83+
}
84+
85+
private static HttpUriRequestBase getRequest(final String scheme) {
86+
return new HttpGet(scheme + "://198.51.100.1/ping");
87+
}
88+
89+
private static void assertTimeout(final ClassicHttpRequest request, final HttpClient client) {
90+
final long startTime = System.nanoTime();
91+
final ConnectTimeoutException ex = assertThrows(ConnectTimeoutException.class,
92+
() -> client.execute(request));
93+
final Duration actualTime = Duration.of(System.nanoTime() - startTime, ChronoUnit.NANOS);
94+
assertTrue(actualTime.toMillis() > EXPECTED_TIMEOUT.toMillis() / 2,
95+
format("Connection attempt timed out too soon (only %,d out of %,d ms)",
96+
actualTime.toMillis(),
97+
EXPECTED_TIMEOUT.toMillis()));
98+
assertTrue(actualTime.toMillis() < EXPECTED_TIMEOUT.toMillis() * 2,
99+
format("Connection attempt timed out too late (%,d out of %,d ms)",
100+
actualTime.toMillis(),
101+
EXPECTED_TIMEOUT.toMillis()));
102+
// Capitalization (`connect` vs `Connect`) varies by Java version
103+
final String message = ex.getMessage();
104+
assertTrue(message.contains("connect timed out") || message.contains("Connect timed out"), message);
105+
}
106+
}

0 commit comments

Comments
 (0)