Skip to content

Commit fea92a5

Browse files
committed
Re-enable BodyDeferringAsyncHandlerTest#deferredSimpleWithFailure
1 parent 5fed25b commit fea92a5

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package org.asynchttpclient.handler;
1414

15+
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
1516
import static org.apache.commons.io.IOUtils.copy;
1617
import static org.asynchttpclient.Dsl.*;
1718
import static org.asynchttpclient.test.TestUtils.findFreePort;
@@ -34,22 +35,22 @@
3435
import org.asynchttpclient.AsyncHttpClientConfig;
3536
import org.asynchttpclient.BoundRequestBuilder;
3637
import org.asynchttpclient.Response;
38+
import org.asynchttpclient.exception.RemotelyClosedException;
3739
import org.asynchttpclient.handler.BodyDeferringAsyncHandler.BodyDeferringInputStream;
3840
import org.eclipse.jetty.server.Request;
3941
import org.eclipse.jetty.server.handler.AbstractHandler;
4042
import org.testng.annotations.Test;
4143

4244
public class BodyDeferringAsyncHandlerTest extends AbstractBasicTest {
4345

44-
// not a half gig ;) for test shorter run's sake
45-
protected static final int HALF_GIG = 100000;
46+
protected static final int CONTENT_LENGTH_VALUE = 100000;
4647

4748
public static class SlowAndBigHandler extends AbstractHandler {
4849

4950
public void handle(String pathInContext, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException {
5051

5152
httpResponse.setStatus(200);
52-
httpResponse.setContentLength(HALF_GIG);
53+
httpResponse.setContentLength(CONTENT_LENGTH_VALUE);
5354
httpResponse.setContentType("application/octet-stream");
5455

5556
httpResponse.flushBuffer();
@@ -58,7 +59,7 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
5859
final boolean wantSlow = httpRequest.getHeader("X-SLOW") != null;
5960

6061
OutputStream os = httpResponse.getOutputStream();
61-
for (int i = 0; i < HALF_GIG; i++) {
62+
for (int i = 0; i < CONTENT_LENGTH_VALUE; i++) {
6263
os.write(i % 255);
6364

6465
if (wantSlow) {
@@ -70,9 +71,9 @@ public void handle(String pathInContext, Request request, HttpServletRequest htt
7071
}
7172

7273
if (wantFailure) {
73-
if (i > HALF_GIG / 2) {
74+
if (i > CONTENT_LENGTH_VALUE / 2) {
7475
// kaboom
75-
// yes, response is commited, but Jetty does aborts and
76+
// yes, response is committed, but Jetty does aborts and
7677
// drops connection
7778
httpResponse.sendError(500);
7879
break;
@@ -120,46 +121,45 @@ public void deferredSimple() throws IOException, ExecutionException, TimeoutExce
120121
Response resp = bdah.getResponse();
121122
assertNotNull(resp);
122123
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
123-
assertEquals(resp.getHeader("content-length"), String.valueOf(HALF_GIG));
124+
assertEquals(resp.getHeader("content-length"), String.valueOf(CONTENT_LENGTH_VALUE));
124125
// we got headers only, it's probably not all yet here (we have BIG file
125126
// downloading)
126-
assertTrue(cos.getByteCount() <= HALF_GIG);
127+
assertTrue(cos.getByteCount() <= CONTENT_LENGTH_VALUE);
127128

128129
// now be polite and wait for body arrival too (otherwise we would be
129130
// dropping the "line" on server)
130131
f.get();
131132
// it all should be here now
132-
assertEquals(cos.getByteCount(), HALF_GIG);
133+
assertEquals(cos.getByteCount(), CONTENT_LENGTH_VALUE);
133134
}
134135
}
135136

136-
@Test(groups = "standalone", enabled = false)
137-
public void deferredSimpleWithFailure() throws IOException, ExecutionException, TimeoutException, InterruptedException {
137+
@Test(groups = "standalone", expectedExceptions = RemotelyClosedException.class)
138+
public void deferredSimpleWithFailure() throws Throwable {
138139
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
139-
BoundRequestBuilder r = client.prepareGet("http://localhost:" + port1 + "/deferredSimpleWithFailure").addHeader("X-FAIL-TRANSFER",
140-
Boolean.TRUE.toString());
140+
BoundRequestBuilder r = client.prepareGet("http://localhost:" + port1 + "/deferredSimpleWithFailure").addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString());
141141

142142
CountingOutputStream cos = new CountingOutputStream();
143143
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(cos);
144144
Future<Response> f = r.execute(bdah);
145145
Response resp = bdah.getResponse();
146146
assertNotNull(resp);
147147
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
148-
assertEquals(resp.getHeader("content-length"), String.valueOf(HALF_GIG));
148+
assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE));
149149
// we got headers only, it's probably not all yet here (we have BIG file
150150
// downloading)
151-
assertTrue(cos.getByteCount() <= HALF_GIG);
151+
assertTrue(cos.getByteCount() <= CONTENT_LENGTH_VALUE);
152152

153153
// now be polite and wait for body arrival too (otherwise we would be
154154
// dropping the "line" on server)
155155
try {
156156
f.get();
157-
fail("get() should fail with IOException!");
158-
} catch (Exception e) {
157+
} catch (ExecutionException e) {
159158
// good
159+
// it's incomplete, there was an error
160+
assertNotEquals(cos.getByteCount(), CONTENT_LENGTH_VALUE);
161+
throw e.getCause();
160162
}
161-
// it's incomplete, there was an error
162-
assertNotEquals(cos.getByteCount(), HALF_GIG);
163163
}
164164
}
165165

@@ -179,7 +179,7 @@ public void deferredInputStreamTrick() throws IOException, ExecutionException, T
179179
Response resp = is.getAsapResponse();
180180
assertNotNull(resp);
181181
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
182-
assertEquals(resp.getHeader("content-length"), String.valueOf(HALF_GIG));
182+
assertEquals(resp.getHeader("content-length"), String.valueOf(CONTENT_LENGTH_VALUE));
183183
// "consume" the body, but our code needs input stream
184184
CountingOutputStream cos = new CountingOutputStream();
185185
try {
@@ -192,15 +192,14 @@ public void deferredInputStreamTrick() throws IOException, ExecutionException, T
192192
// now we don't need to be polite, since consuming and closing
193193
// BodyDeferringInputStream does all.
194194
// it all should be here now
195-
assertEquals(cos.getByteCount(), HALF_GIG);
195+
assertEquals(cos.getByteCount(), CONTENT_LENGTH_VALUE);
196196
}
197197
}
198198

199199
@Test(groups = "standalone")
200200
public void deferredInputStreamTrickWithFailure() throws IOException, ExecutionException, TimeoutException, InterruptedException {
201201
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
202-
BoundRequestBuilder r = client.prepareGet("http://localhost:" + port1 + "/deferredInputStreamTrickWithFailure").addHeader("X-FAIL-TRANSFER",
203-
Boolean.TRUE.toString());
202+
BoundRequestBuilder r = client.prepareGet("http://localhost:" + port1 + "/deferredInputStreamTrickWithFailure").addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString());
204203
PipedOutputStream pos = new PipedOutputStream();
205204
PipedInputStream pis = new PipedInputStream(pos);
206205
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pos);
@@ -212,7 +211,7 @@ public void deferredInputStreamTrickWithFailure() throws IOException, ExecutionE
212211
Response resp = is.getAsapResponse();
213212
assertNotNull(resp);
214213
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
215-
assertEquals(resp.getHeader("content-length"), String.valueOf(HALF_GIG));
214+
assertEquals(resp.getHeader("content-length"), String.valueOf(CONTENT_LENGTH_VALUE));
216215
// "consume" the body, but our code needs input stream
217216
CountingOutputStream cos = new CountingOutputStream();
218217
try {

0 commit comments

Comments
 (0)