Skip to content

Commit 0063e1e

Browse files
committed
Add unit test to ensure we unwrap CompletionExceptions.
Really just making the coverage checker happy.
1 parent dc27f5e commit 0063e1e

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/http/StandardHttpClient.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,26 +177,31 @@ private <V> CompletableFuture<V> retryWithExponentialBackoff(
177177
}
178178
}
179179
} else {
180-
final Throwable finalThrowable;
181-
if (throwable instanceof CompletionException) {
182-
finalThrowable = throwable.getCause();
183-
} else {
184-
finalThrowable = throwable;
185-
}
186-
builder.interceptors.forEach((s, interceptor) -> interceptor.afterConnectionFailure(request, finalThrowable));
187-
if (finalThrowable instanceof IOException) {
180+
final Throwable actualCause = unwrapCompletionException(throwable);
181+
builder.interceptors.forEach((s, interceptor) -> interceptor.afterConnectionFailure(request, actualCause));
182+
if (actualCause instanceof IOException) {
188183
// TODO: may not be specific enough - incorrect ssl settings for example will get caught here
189184
LOG.debug(
190185
String.format("HTTP operation on url: %s should be retried after %d millis because of IOException",
191186
uri, retryInterval),
192-
finalThrowable);
187+
actualCause);
193188
return true;
194189
}
195190
}
196191
return false;
197192
});
198193
}
199194

195+
static Throwable unwrapCompletionException(Throwable throwable) {
196+
final Throwable actualCause;
197+
if (throwable instanceof CompletionException) {
198+
actualCause = throwable.getCause();
199+
} else {
200+
actualCause = throwable;
201+
}
202+
return actualCause;
203+
}
204+
200205
static long retryAfterMillis(HttpResponse<?> httpResponse) {
201206
String retryAfter = httpResponse.header(StandardHttpHeaders.RETRY_AFTER);
202207
if (retryAfter != null) {

kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/http/StandardHttpClientTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.Collections;
3535
import java.util.List;
3636
import java.util.concurrent.CompletableFuture;
37+
import java.util.concurrent.CompletionException;
3738
import java.util.concurrent.ExecutionException;
3839
import java.util.concurrent.TimeUnit;
3940
import java.util.concurrent.TimeoutException;
@@ -50,6 +51,7 @@
5051

5152
class StandardHttpClientTest {
5253

54+
public static final String IO_ERROR_MESSAGE = "IO woopsie";
5355
private TestStandardHttpClient client;
5456

5557
@BeforeEach
@@ -281,4 +283,25 @@ void testDerivedIsClosed() {
281283
assertTrue(client.isClosed());
282284
}
283285

286+
@Test
287+
void shouldUnwrapCompletionException() {
288+
// Given
289+
290+
// When
291+
final Throwable throwable = StandardHttpClient.unwrapCompletionException(new CompletionException(new IOException(IO_ERROR_MESSAGE)));
292+
293+
// Then
294+
assertThat(throwable).isInstanceOf(IOException.class).hasMessage(IO_ERROR_MESSAGE);
295+
}
296+
297+
@Test
298+
void shouldNotUnwrapOtherExceptions() {
299+
// Given
300+
301+
// When
302+
final Throwable throwable = StandardHttpClient.unwrapCompletionException(new IOException(IO_ERROR_MESSAGE));
303+
304+
// Then
305+
assertThat(throwable).isInstanceOf(IOException.class).hasMessage(IO_ERROR_MESSAGE);
306+
}
284307
}

0 commit comments

Comments
 (0)