Skip to content

Commit

Permalink
Add test which covers future being completed exceptionally with a Com…
Browse files Browse the repository at this point in the history
…pletionException.

This happens in the Jetty implementation but  gets wrapped in an IOException by the OkHttp impl but this should be enough for the coverage checker.
  • Loading branch information
SamBarker committed Jul 22, 2024
1 parent 3d39532 commit 43f88fa
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
Expand All @@ -31,6 +32,7 @@
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -233,6 +235,37 @@ public void after(HttpRequest request, HttpResponse<?> response, Consumer<List<B
}
}

@Test
@DisplayName("afterConnectionFailure, completionExceptions are passed through")
public void afterConnectionFailureCompletionException() {
// Given
final CountDownLatch connectionFailureCallbackInvoked = new CountDownLatch(1);
final HttpClient.Builder builder = getHttpClientFactory().newBuilder()
.connectTimeout(1, TimeUnit.SECONDS)
.addOrReplaceInterceptor("test", new Interceptor() {
@Override
public void afterConnectionFailure(HttpRequest request, Throwable failure) {
connectionFailureCallbackInvoked.countDown();
}
});
// When
try (HttpClient client = builder.build()) {
final CompletableFuture<HttpResponse<String>> response = client.sendAsync(client.newHttpRequestBuilder()
.timeout(1, TimeUnit.SECONDS)
.uri(server.url("/intercepted-url"))
.method("POST", "application/json", new InputStream() {
@Override
public int read() {
throw new CompletionException("boom time", null); // gets propagated by jetty but gets wrapped by OkHttp
}
}, 1L).build(), String.class);

// Then
assertThat(response).failsWithin(Duration.of(30, ChronoUnit.SECONDS));
assertThat(connectionFailureCallbackInvoked).extracting(CountDownLatch::getCount).isEqualTo(0L);
}
}

@Test
@DisplayName("afterFailure (HTTP), replaces the HttpResponse produced by HttpClient.consumeBytes")
public void afterHttpFailureReplacesResponseInConsumeBytes() throws Exception {
Expand Down

0 comments on commit 43f88fa

Please sign in to comment.