Skip to content

Commit b037426

Browse files
committed
Have BodyDeferringAsyncHandler unwrap ExecutionException
1 parent fea92a5 commit b037426

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

client/src/main/java/org/asynchttpclient/handler/BodyDeferringAsyncHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,11 @@ public void close() throws IOException {
248248
// "join" async request
249249
try {
250250
getLastResponse();
251-
} catch (Exception e) {
251+
} catch (ExecutionException e) {
252+
IOException ioe = new IOException(e.getMessage());
253+
ioe.initCause(e.getCause());
254+
throw ioe;
255+
} catch (InterruptedException e) {
252256
IOException ioe = new IOException(e.getMessage());
253257
ioe.initCause(e);
254258
throw ioe;

client/src/test/java/org/asynchttpclient/handler/BodyDeferringAsyncHandlerTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package org.asynchttpclient.handler;
1414

1515
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
16+
import static io.netty.handler.codec.http.HttpHeaderValues.APPLICATION_OCTET_STREAM;
1617
import static org.apache.commons.io.IOUtils.copy;
1718
import static org.asynchttpclient.Dsl.*;
1819
import static org.asynchttpclient.test.TestUtils.findFreePort;
@@ -51,7 +52,7 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
5152

5253
httpResponse.setStatus(200);
5354
httpResponse.setContentLength(CONTENT_LENGTH_VALUE);
54-
httpResponse.setContentType("application/octet-stream");
55+
httpResponse.setContentType(APPLICATION_OCTET_STREAM.toString());
5556

5657
httpResponse.flushBuffer();
5758

@@ -121,7 +122,7 @@ public void deferredSimple() throws IOException, ExecutionException, TimeoutExce
121122
Response resp = bdah.getResponse();
122123
assertNotNull(resp);
123124
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
124-
assertEquals(resp.getHeader("content-length"), String.valueOf(CONTENT_LENGTH_VALUE));
125+
assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE));
125126
// we got headers only, it's probably not all yet here (we have BIG file
126127
// downloading)
127128
assertTrue(cos.getByteCount() <= CONTENT_LENGTH_VALUE);
@@ -179,7 +180,7 @@ public void deferredInputStreamTrick() throws IOException, ExecutionException, T
179180
Response resp = is.getAsapResponse();
180181
assertNotNull(resp);
181182
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
182-
assertEquals(resp.getHeader("content-length"), String.valueOf(CONTENT_LENGTH_VALUE));
183+
assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE));
183184
// "consume" the body, but our code needs input stream
184185
CountingOutputStream cos = new CountingOutputStream();
185186
try {
@@ -196,8 +197,8 @@ public void deferredInputStreamTrick() throws IOException, ExecutionException, T
196197
}
197198
}
198199

199-
@Test(groups = "standalone")
200-
public void deferredInputStreamTrickWithFailure() throws IOException, ExecutionException, TimeoutException, InterruptedException {
200+
@Test(groups = "standalone", expectedExceptions = RemotelyClosedException.class)
201+
public void deferredInputStreamTrickWithFailure() throws Throwable {
201202
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
202203
BoundRequestBuilder r = client.prepareGet("http://localhost:" + port1 + "/deferredInputStreamTrickWithFailure").addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString());
203204
PipedOutputStream pos = new PipedOutputStream();
@@ -211,7 +212,7 @@ public void deferredInputStreamTrickWithFailure() throws IOException, ExecutionE
211212
Response resp = is.getAsapResponse();
212213
assertNotNull(resp);
213214
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
214-
assertEquals(resp.getHeader("content-length"), String.valueOf(CONTENT_LENGTH_VALUE));
215+
assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE));
215216
// "consume" the body, but our code needs input stream
216217
CountingOutputStream cos = new CountingOutputStream();
217218
try {
@@ -221,9 +222,8 @@ public void deferredInputStreamTrickWithFailure() throws IOException, ExecutionE
221222
is.close();
222223
cos.close();
223224
}
224-
fail("InputStream consumption should fail with IOException!");
225225
} catch (IOException e) {
226-
// good!
226+
throw e.getCause();
227227
}
228228
}
229229
}

0 commit comments

Comments
 (0)