Skip to content

Commit b8736f9

Browse files
committed
Include a timeout on all Future.get() calls.
1 parent 3e54dfe commit b8736f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+234
-188
lines changed

api/src/test/java/org/asynchttpclient/async/AsyncProvidersBasicTest.java

Lines changed: 43 additions & 43 deletions
Large diffs are not rendered by default.

api/src/test/java/org/asynchttpclient/async/AsyncStreamHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ public STATE onStatusReceived(HttpResponseStatus responseStatus) throws Exceptio
560560
public Response onCompleted() throws Exception {
561561
return builder.build();
562562
}
563-
}).get();
563+
}).get(5, TimeUnit.SECONDS);
564564

565565
assertNotNull(r);
566566
assertEquals(r.getStatusCode(), 200);

api/src/test/java/org/asynchttpclient/async/AuthTimeoutTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void basicAuthTimeoutTest() throws Exception {
8585
AsyncHttpClient client = newClient();
8686
try {
8787
Future<Response> f = execute(client, server, false);
88-
f.get();
88+
f.get(5, TimeUnit.SECONDS);
8989
fail("expected timeout");
9090
} catch (Exception e) {
9191
inspectException(e);
@@ -99,7 +99,7 @@ public void basicPreemptiveAuthTimeoutTest() throws Exception {
9999
AsyncHttpClient client = newClient();
100100
try {
101101
Future<Response> f = execute(client, server, true);
102-
f.get();
102+
f.get(5, TimeUnit.SECONDS);
103103
fail("expected timeout");
104104
} catch (Exception e) {
105105
inspectException(e);
@@ -113,7 +113,7 @@ public void digestAuthTimeoutTest() throws Exception {
113113
AsyncHttpClient client = newClient();
114114
try {
115115
Future<Response> f = execute(client, server2, false);
116-
f.get();
116+
f.get(5, TimeUnit.SECONDS);
117117
fail("expected timeout");
118118
} catch (Exception e) {
119119
inspectException(e);
@@ -127,7 +127,7 @@ public void digestPreemptiveAuthTimeoutTest() throws Exception {
127127
AsyncHttpClient client = newClient();
128128
try {
129129
Future<Response> f = execute(client, server2, true);
130-
f.get();
130+
f.get(5, TimeUnit.SECONDS);
131131
fail("expected timeout");
132132
} catch (Exception e) {
133133
inspectException(e);

api/src/test/java/org/asynchttpclient/async/BasicAuthTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public void stringBuilderBodyConsumerTest() throws Exception {
351351
StringBuilder s = new StringBuilder();
352352
Future<Response> future = client.post(new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes())), new AppendableBodyConsumer(s));
353353

354-
Response response = future.get();
354+
Response response = future.get(3, TimeUnit.SECONDS);
355355
assertEquals(response.getStatusCode(), 200);
356356
assertEquals(s.toString(), MY_MESSAGE);
357357
assertEquals(response.getStatusCode(), HttpServletResponse.SC_OK);

api/src/test/java/org/asynchttpclient/async/BasicHttpsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void zeroCopyPostTest() throws Exception {
4242

4343
final AsyncHttpClient client = getAsyncHttpClient(new Builder().setSSLContext(createSSLContext(new AtomicBoolean(true))).build());
4444
try {
45-
Response resp = client.preparePost(getTargetUrl()).setBody(SIMPLE_TEXT_FILE).setHeader("Content-Type", "text/html").execute().get();
45+
Response resp = client.preparePost(getTargetUrl()).setBody(SIMPLE_TEXT_FILE).setHeader("Content-Type", "text/html").execute().get(5, TimeUnit.SECONDS);
4646
assertNotNull(resp);
4747
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
4848
assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING);
@@ -80,7 +80,7 @@ public void multipleSSLWithoutCacheTest() throws Exception {
8080

8181
c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute();
8282

83-
Response response = c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute().get();
83+
Response response = c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute().get(5, TimeUnit.SECONDS);
8484

8585
assertEquals(response.getResponseBody(), body);
8686
} finally {

api/src/test/java/org/asynchttpclient/async/BodyChunkTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.io.ByteArrayInputStream;
2626
import java.util.concurrent.Future;
27+
import java.util.concurrent.TimeUnit;
2728

2829
import static org.testng.Assert.assertEquals;
2930

@@ -50,7 +51,7 @@ public void negativeContentTypeTest() throws Exception {
5051
Future<Response> future = client.executeRequest(requestBuilder.build());
5152

5253
System.out.println("waiting for response");
53-
Response response = future.get();
54+
Response response = future.get(5, TimeUnit.SECONDS);
5455
assertEquals(response.getStatusCode(), 200);
5556
assertEquals(response.getResponseBody(), MY_MESSAGE);
5657
} finally {

api/src/test/java/org/asynchttpclient/async/BodyDeferringAsyncHandlerTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.PipedOutputStream;
2323
import java.util.concurrent.ExecutionException;
2424
import java.util.concurrent.Future;
25+
import java.util.concurrent.TimeUnit;
2526
import java.util.concurrent.TimeoutException;
2627

2728
import javax.servlet.ServletException;
@@ -126,7 +127,7 @@ public void deferredSimple() throws IOException, ExecutionException, TimeoutExce
126127

127128
// now be polite and wait for body arrival too (otherwise we would be
128129
// dropping the "line" on server)
129-
f.get();
130+
f.get(5, TimeUnit.SECONDS);
130131
// it all should be here now
131132
assertEquals(cos.getByteCount(), HALF_GIG);
132133
} finally {
@@ -155,7 +156,7 @@ public void deferredSimpleWithFailure() throws IOException, ExecutionException,
155156
// now be polite and wait for body arrival too (otherwise we would be
156157
// dropping the "line" on server)
157158
try {
158-
f.get();
159+
f.get(5, TimeUnit.SECONDS);
159160
fail("get() should fail with IOException!");
160161
} catch (Exception e) {
161162
// good

api/src/test/java/org/asynchttpclient/async/ByteBufferCapacityTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.InputStream;
2121
import java.io.OutputStream;
2222
import java.util.Enumeration;
23+
import java.util.concurrent.TimeUnit;
2324
import java.util.concurrent.atomic.AtomicInteger;
2425

2526
import javax.servlet.ServletException;
@@ -86,7 +87,7 @@ public STATE onBodyPartReceived(final HttpResponseBodyPart content) throws Excep
8687
return super.onBodyPartReceived(content);
8788
}
8889

89-
}).get();
90+
}).get(5, TimeUnit.SECONDS);
9091

9192
assertNotNull(response);
9293
assertEquals(response.getStatusCode(), 200);

api/src/test/java/org/asynchttpclient/async/ChunkingTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.BufferedInputStream;
2020
import java.io.FileInputStream;
21+
import java.util.concurrent.TimeUnit;
2122

2223
import org.asynchttpclient.AsyncHttpClient;
2324
import org.asynchttpclient.AsyncHttpClientConfig;
@@ -60,7 +61,7 @@ public void testCustomChunking() throws Exception {
6061

6162
Request r = builder.build();
6263

63-
Response response = c.executeRequest(r).get();
64+
Response response = c.executeRequest(r).get(5, TimeUnit.SECONDS);
6465
if (500 == response.getStatusCode()) {
6566
StringBuilder sb = new StringBuilder();
6667
sb.append("==============\n");

api/src/test/java/org/asynchttpclient/async/ConnectionPoolTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void testMaxTotalConnections() {
4747
for (i = 0; i < 3; i++) {
4848
try {
4949
log.info("{} requesting url [{}]...", i, url);
50-
Response response = client.prepareGet(url).execute().get();
50+
Response response = client.prepareGet(url).execute().get(5, TimeUnit.SECONDS);
5151
log.info("{} response [{}].", i, response);
5252
} catch (Exception ex) {
5353
exception = ex;
@@ -71,7 +71,7 @@ public void testMaxTotalConnectionsException() {
7171
log.info("{} requesting url [{}]...", i, url);
7272

7373
if (i < 5) {
74-
client.prepareGet(url).execute().get();
74+
client.prepareGet(url).execute().get(5, TimeUnit.SECONDS);
7575
} else {
7676
client.prepareGet(url).execute();
7777
}
@@ -112,7 +112,7 @@ public Response onCompleted(Response response) throws Exception {
112112
}
113113
};
114114

115-
client.prepareGet(getTargetUrl()).execute(handler).get();
115+
client.prepareGet(getTargetUrl()).execute(handler).get(5, TimeUnit.SECONDS);
116116
server.stop();
117117
server.start();
118118
client.prepareGet(getTargetUrl()).execute(handler);
@@ -215,7 +215,7 @@ public Response onCompleted(Response response) throws Exception {
215215
};
216216

217217
try {
218-
client.prepareGet(getTargetUrl()).execute(handler).get();
218+
client.prepareGet(getTargetUrl()).execute(handler).get(5, TimeUnit.SECONDS);
219219
fail("Must have received an exception");
220220
} catch (ExecutionException ex) {
221221
assertNotNull(ex);

api/src/test/java/org/asynchttpclient/async/EmptyBodyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public Object onCompleted() throws Exception {
127127
public void testPutEmptyBody() throws Exception {
128128
AsyncHttpClient ahc = getAsyncHttpClient(null);
129129
try {
130-
Response response = ahc.preparePut(getTargetUrl()).setBody("String").execute().get();
130+
Response response = ahc.preparePut(getTargetUrl()).setBody("String").execute().get(5, TimeUnit.SECONDS);
131131

132132
assertNotNull(response);
133133
assertEquals(response.getStatusCode(), 204);

api/src/test/java/org/asynchttpclient/async/Expect100ContinueTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.IOException;
2222
import java.util.concurrent.Future;
23+
import java.util.concurrent.TimeUnit;
2324

2425
import javax.servlet.ServletException;
2526
import javax.servlet.http.HttpServletRequest;
@@ -64,7 +65,7 @@ public void Expect100Continue() throws Exception {
6465
AsyncHttpClient client = getAsyncHttpClient(null);
6566
try {
6667
Future<Response> f = client.preparePut("http://127.0.0.1:" + port1 + "/").setHeader("Expect", "100-continue").setBody(SIMPLE_TEXT_FILE).execute();
67-
Response resp = f.get();
68+
Response resp = f.get(5, TimeUnit.SECONDS);
6869
assertNotNull(resp);
6970
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
7071
assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING);

api/src/test/java/org/asynchttpclient/async/FilePartLargeFileTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.File;
1919
import java.io.IOException;
20+
import java.util.concurrent.TimeUnit;
2021

2122
import javax.servlet.ServletException;
2223
import javax.servlet.ServletInputStream;
@@ -62,7 +63,7 @@ public void handle(String arg0, Request arg1, HttpServletRequest req, HttpServle
6263
public void testPutImageFile() throws Exception {
6364
AsyncHttpClient client = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setRequestTimeoutInMs(100 * 6000).build());
6465
try {
65-
Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", LARGE_IMAGE_FILE, "application/octet-stream", "UTF-8")).execute().get();
66+
Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", LARGE_IMAGE_FILE, "application/octet-stream", "UTF-8")).execute().get(5, TimeUnit.SECONDS);
6667
assertEquals(response.getStatusCode(), 200);
6768
} finally {
6869
client.close();
@@ -75,7 +76,7 @@ public void testPutLargeTextFile() throws Exception {
7576

7677
AsyncHttpClient client = getAsyncHttpClient(null);
7778
try {
78-
Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", file, "application/octet-stream", "UTF-8")).execute().get();
79+
Response response = client.preparePut(getTargetUrl()).addBodyPart(new FilePart("test", file, "application/octet-stream", "UTF-8")).execute().get(5, TimeUnit.SECONDS);
7980
assertEquals(response.getStatusCode(), 200);
8081
} finally {
8182
client.close();

api/src/test/java/org/asynchttpclient/async/FilterTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Enumeration;
3333
import java.util.List;
3434
import java.util.concurrent.Future;
35+
import java.util.concurrent.TimeUnit;
3536
import java.util.concurrent.atomic.AtomicBoolean;
3637

3738
import static org.testng.Assert.assertEquals;
@@ -73,7 +74,7 @@ public void basicTest() throws Exception {
7374

7475
AsyncHttpClient c = getAsyncHttpClient(b.build());
7576
try {
76-
Response response = c.preparePost(getTargetUrl()).execute().get();
77+
Response response = c.preparePost(getTargetUrl()).execute().get(5, TimeUnit.SECONDS);
7778
assertNotNull(response);
7879
assertEquals(response.getStatusCode(), 200);
7980
} finally {
@@ -94,7 +95,7 @@ public void loadThrottleTest() throws Exception {
9495
}
9596

9697
for (Future<Response> f : futures) {
97-
Response r = f.get();
98+
Response r = f.get(5, TimeUnit.SECONDS);
9899
assertNotNull(f.get());
99100
assertEquals(r.getStatusCode(), 200);
100101
}
@@ -110,7 +111,7 @@ public void maxConnectionsText() throws Exception {
110111
AsyncHttpClient c = getAsyncHttpClient(b.build());
111112

112113
try {
113-
/* Response response = */c.preparePost(getTargetUrl()).execute().get();
114+
/* Response response = */c.preparePost(getTargetUrl()).execute().get(5, TimeUnit.SECONDS);
114115
fail("Should have timed out");
115116
} catch (IOException ex) {
116117
assertNotNull(ex);
@@ -134,7 +135,7 @@ public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException
134135
AsyncHttpClient c = getAsyncHttpClient(b.build());
135136

136137
try {
137-
Response response = c.preparePost(getTargetUrl()).execute().get();
138+
Response response = c.preparePost(getTargetUrl()).execute().get(5, TimeUnit.SECONDS);
138139

139140
assertNotNull(response);
140141
assertEquals(response.getStatusCode(), 200);
@@ -165,7 +166,7 @@ public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException
165166
AsyncHttpClient c = getAsyncHttpClient(b.build());
166167

167168
try {
168-
Response response = c.preparePost(getTargetUrl()).execute().get();
169+
Response response = c.preparePost(getTargetUrl()).execute().get(5, TimeUnit.SECONDS);
169170

170171
assertNotNull(response);
171172
assertEquals(response.getStatusCode(), 200);
@@ -197,7 +198,7 @@ public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException
197198
AsyncHttpClient c = getAsyncHttpClient(b.build());
198199

199200
try {
200-
Response response = c.preparePost(getTargetUrl()).execute().get();
201+
Response response = c.preparePost(getTargetUrl()).execute().get(5, TimeUnit.SECONDS);
201202

202203
assertNotNull(response);
203204
assertEquals(response.getStatusCode(), 200);
@@ -230,7 +231,7 @@ public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException
230231
AsyncHttpClient c = getAsyncHttpClient(b.build());
231232

232233
try {
233-
Response response = c.preparePost(getTargetUrl()).addHeader("Ping", "Pong").execute().get();
234+
Response response = c.preparePost(getTargetUrl()).addHeader("Ping", "Pong").execute().get(5, TimeUnit.SECONDS);
234235

235236
assertNotNull(response);
236237
assertEquals(response.getStatusCode(), 200);

api/src/test/java/org/asynchttpclient/async/HostnameVerifierTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Enumeration;
2121
import java.util.concurrent.ExecutionException;
2222
import java.util.concurrent.Future;
23+
import java.util.concurrent.TimeUnit;
2324
import java.util.concurrent.atomic.AtomicBoolean;
2425

2526
import javax.net.ssl.HostnameVerifier;
@@ -130,7 +131,7 @@ public void positiveHostnameVerifierTest() throws Exception {
130131
final AsyncHttpClient client = getAsyncHttpClient(new Builder().setHostnameVerifier(new PositiveHostVerifier()).setSSLContext(createSSLContext(new AtomicBoolean(true))).build());
131132
try {
132133
Future<Response> f = client.preparePost(getTargetUrl()).setBody(SIMPLE_TEXT_FILE).setHeader("Content-Type", "text/html").execute();
133-
Response resp = f.get();
134+
Response resp = f.get(5, TimeUnit.SECONDS);
134135
assertNotNull(resp);
135136
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
136137
assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING);
@@ -145,7 +146,7 @@ public void negativeHostnameVerifierTest() throws Exception {
145146
final AsyncHttpClient client = getAsyncHttpClient(new Builder().setHostnameVerifier(new NegativeHostVerifier()).setSSLContext(createSSLContext(new AtomicBoolean(true))).build());
146147
try {
147148
try {
148-
client.preparePost(getTargetUrl()).setBody(SIMPLE_TEXT_FILE).setHeader("Content-Type", "text/html").execute().get();
149+
client.preparePost(getTargetUrl()).setBody(SIMPLE_TEXT_FILE).setHeader("Content-Type", "text/html").execute().get(5, TimeUnit.SECONDS);
149150
fail("ConnectException expected");
150151
} catch (ExecutionException ex) {
151152
assertEquals(ex.getCause().getClass(), ConnectException.class);
@@ -160,7 +161,7 @@ public void remoteIDHostnameVerifierTest() throws Exception {
160161

161162
final AsyncHttpClient client = getAsyncHttpClient(new Builder().setHostnameVerifier(new CheckHost("bouette")).setSSLContext(createSSLContext(new AtomicBoolean(true))).build());
162163
try {
163-
client.preparePost(getTargetUrl()).setBody(SIMPLE_TEXT_FILE).setHeader("Content-Type", "text/html").execute().get();
164+
client.preparePost(getTargetUrl()).setBody(SIMPLE_TEXT_FILE).setHeader("Content-Type", "text/html").execute().get(5, TimeUnit.SECONDS);
164165
fail("ConnectException expected");
165166
} catch (ExecutionException ex) {
166167
assertEquals(ex.getCause().getClass(), ConnectException.class);
@@ -174,7 +175,7 @@ public void remoteNegHostnameVerifierTest() throws Exception {
174175
// request is made to 127.0.0.1, but cert presented for localhost - this should fail
175176
final AsyncHttpClient client = getAsyncHttpClient(new Builder().setHostnameVerifier(new CheckHost("localhost")).setSSLContext(createSSLContext(new AtomicBoolean(true))).build());
176177
try {
177-
client.preparePost(getTargetUrl()).setBody(SIMPLE_TEXT_FILE).setHeader("Content-Type", "text/html").execute().get();
178+
client.preparePost(getTargetUrl()).setBody(SIMPLE_TEXT_FILE).setHeader("Content-Type", "text/html").execute().get(5, TimeUnit.SECONDS);
178179
fail("ConnectException expected");
179180
} catch (ExecutionException ex) {
180181
assertEquals(ex.getCause().getClass(), ConnectException.class);
@@ -188,7 +189,7 @@ public void remotePosHostnameVerifierTest() throws Exception {
188189

189190
final AsyncHttpClient client = getAsyncHttpClient(new Builder().setHostnameVerifier(new CheckHost("127.0.0.1")).setSSLContext(createSSLContext(new AtomicBoolean(true))).build());
190191
try {
191-
Response resp = client.preparePost(getTargetUrl()).setBody(SIMPLE_TEXT_FILE).setHeader("Content-Type", "text/html").execute().get();
192+
Response resp = client.preparePost(getTargetUrl()).setBody(SIMPLE_TEXT_FILE).setHeader("Content-Type", "text/html").execute().get(5, TimeUnit.SECONDS);
192193
assertNotNull(resp);
193194
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
194195
assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING);

0 commit comments

Comments
 (0)