Skip to content

Commit

Permalink
Add unit test to ensure we unwrap CompletionExceptions.
Browse files Browse the repository at this point in the history
Really just making the coverage checker happy.
  • Loading branch information
SamBarker committed Aug 8, 2024
1 parent dc27f5e commit e9e4b2c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,26 +177,31 @@ private <V> CompletableFuture<V> retryWithExponentialBackoff(
}
}
} else {
final Throwable finalThrowable;
if (throwable instanceof CompletionException) {
finalThrowable = throwable.getCause();
} else {
finalThrowable = throwable;
}
builder.interceptors.forEach((s, interceptor) -> interceptor.afterConnectionFailure(request, finalThrowable));
if (finalThrowable instanceof IOException) {
final Throwable actualCause = unwrapCompletionException(throwable);
builder.interceptors.forEach((s, interceptor) -> interceptor.afterConnectionFailure(request, actualCause));
if (actualCause instanceof IOException) {
// TODO: may not be specific enough - incorrect ssl settings for example will get caught here
LOG.debug(
String.format("HTTP operation on url: %s should be retried after %d millis because of IOException",
uri, retryInterval),
finalThrowable);
actualCause);
return true;
}
}
return false;
});
}

static Throwable unwrapCompletionException(Throwable throwable) {
final Throwable actualCause;
if (throwable instanceof CompletionException) {
actualCause = throwable.getCause();
} else {
actualCause = throwable;
}
return actualCause;
}

static long retryAfterMillis(HttpResponse<?> httpResponse) {
String retryAfter = httpResponse.header(StandardHttpHeaders.RETRY_AFTER);
if (retryAfter != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand All @@ -50,6 +51,7 @@

class StandardHttpClientTest {

public static final String IO_ERROR_MESSAGE = "IO woopsie";
private TestStandardHttpClient client;

@BeforeEach
Expand Down Expand Up @@ -281,4 +283,26 @@ void testDerivedIsClosed() {
assertTrue(client.isClosed());
}

@Test
void shouldUnwrapCompletionException() {
// Given

// When
final Throwable throwable = StandardHttpClient
.unwrapCompletionException(new CompletionException(new IOException(IO_ERROR_MESSAGE)));

// Then
assertThat(throwable).isInstanceOf(IOException.class).hasMessage(IO_ERROR_MESSAGE);
}

@Test
void shouldNotUnwrapOtherExceptions() {
// Given

// When
final Throwable throwable = StandardHttpClient.unwrapCompletionException(new IOException(IO_ERROR_MESSAGE));

// Then
assertThat(throwable).isInstanceOf(IOException.class).hasMessage(IO_ERROR_MESSAGE);
}
}

0 comments on commit e9e4b2c

Please sign in to comment.