Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions client/src/test/java/org/asynchttpclient/LargeResponseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.io.FileUtils;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.AfterAll;
Expand All @@ -38,6 +40,8 @@
import org.slf4j.LoggerFactory;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;


@ExtendWith(NettyLeakDetectorExtension.class)
Expand Down Expand Up @@ -94,7 +98,9 @@ static void stopServer() {
@Test
void handleLargeResponse() throws Throwable {
AtomicInteger status = new AtomicInteger(-1);
AtomicInteger throwableCount = new AtomicInteger();
AtomicReference<Throwable> failure = new AtomicReference<>();
AtomicBoolean headersReceived = new AtomicBoolean();
AtomicBoolean completed = new AtomicBoolean();
AtomicLong bytesReceived = new AtomicLong();
AtomicLong bodyPartCount = new AtomicLong();

Expand All @@ -113,6 +119,7 @@ public State onStatusReceived(HttpResponseStatus responseStatus)
@Override
public State onHeadersReceived(HttpHeaders headers)
throws Exception {
headersReceived.set(true);
return State.CONTINUE;
}

Expand All @@ -122,32 +129,29 @@ public State onBodyPartReceived(HttpResponseBodyPart bodyPart)
bodyPartCount.incrementAndGet();
bytesReceived.addAndGet(bodyPart.length());

/*
LOG.info("onBodyPartReceived: "
+ "bodyPartCount=" + bodyPartCount.get()
+ " bodyPartLength: "
+ FileUtils.byteCountToDisplaySize(bodyPart.length()));
*/

return State.CONTINUE;
}

@Override
public void onThrowable(Throwable t) {
throwableCount.incrementAndGet();
failure.compareAndSet(null, t);
}

@Override
public @Nullable Object onCompleted()
throws Exception {
completed.set(true);
return null;
}
});

future.get(1, TimeUnit.MINUTES);

assertEquals(200, status.get());
assertEquals(0, throwableCount.get());
assertNull(failure.get(), "unexpected failure: " + failure.get());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can never fail. Every path that calls onThrowable also fails the future, so future.get throws ExecutionException before we get here. I think we should drop failure and let the ExecutionException carry the cause, JUnit already prints it.

assertTrue(headersReceived.get(), "headers were not received");
assertTrue(completed.get(), "onCompleted was not called");
assertTrue(bodyPartCount.get() > 0, "no body parts received");
Comment on lines +153 to +154

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both are always true once get() returns. onCompleted is what completes the future, and bodyPartCount > 0 is implied by the byte count check below. Can we drop them? Otherwise an empty body fails here with a worse message than the assertEquals on bytesReceived would give.

assertEquals(responseSize, bytesReceived.get());

LOG.info("Body part count: " + bodyPartCount);
Expand Down
Loading