|
26 | 26 | import java.util.Arrays; |
27 | 27 | import java.util.Collections; |
28 | 28 | import java.util.List; |
| 29 | +import java.util.concurrent.CompletableFuture; |
| 30 | +import java.util.concurrent.ExecutionException; |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | +import java.util.concurrent.TimeoutException; |
29 | 33 | import feign.FeignException; |
30 | 34 | import feign.Headers; |
31 | 35 | import feign.Param; |
@@ -427,6 +431,66 @@ public void rxSingleListFallback() { |
427 | 431 | assertThat(testSubscriber.getOnNextEvents().get(0)).containsExactly("fallback"); |
428 | 432 | } |
429 | 433 |
|
| 434 | + @Test |
| 435 | + public void completableFutureEmptyBody() |
| 436 | + throws InterruptedException, ExecutionException, TimeoutException { |
| 437 | + server.enqueue(new MockResponse()); |
| 438 | + |
| 439 | + TestInterface api = target(); |
| 440 | + |
| 441 | + CompletableFuture<String> completable = api.completableFuture(); |
| 442 | + |
| 443 | + assertThat(completable).isNotNull(); |
| 444 | + |
| 445 | + completable.get(5, TimeUnit.SECONDS); |
| 446 | + } |
| 447 | + |
| 448 | + @Test |
| 449 | + public void completableFutureWithBody() |
| 450 | + throws InterruptedException, ExecutionException, TimeoutException { |
| 451 | + server.enqueue(new MockResponse().setBody("foo")); |
| 452 | + |
| 453 | + TestInterface api = target(); |
| 454 | + |
| 455 | + CompletableFuture<String> completable = api.completableFuture(); |
| 456 | + |
| 457 | + assertThat(completable).isNotNull(); |
| 458 | + |
| 459 | + assertThat(completable.get(5, TimeUnit.SECONDS)).isEqualTo("foo"); |
| 460 | + } |
| 461 | + |
| 462 | + @Test |
| 463 | + public void completableFutureFailWithoutFallback() throws TimeoutException, InterruptedException { |
| 464 | + server.enqueue(new MockResponse().setResponseCode(500)); |
| 465 | + |
| 466 | + TestInterface api = HystrixFeign.builder() |
| 467 | + .target(TestInterface.class, "http://localhost:" + server.getPort()); |
| 468 | + |
| 469 | + CompletableFuture<String> completable = api.completableFuture(); |
| 470 | + |
| 471 | + assertThat(completable).isNotNull(); |
| 472 | + |
| 473 | + try { |
| 474 | + completable.get(5, TimeUnit.SECONDS); |
| 475 | + } catch (ExecutionException e) { |
| 476 | + assertThat(e).hasCauseInstanceOf(HystrixRuntimeException.class); |
| 477 | + } |
| 478 | + } |
| 479 | + |
| 480 | + @Test |
| 481 | + public void completableFutureFallback() |
| 482 | + throws InterruptedException, ExecutionException, TimeoutException { |
| 483 | + server.enqueue(new MockResponse().setResponseCode(500)); |
| 484 | + |
| 485 | + TestInterface api = target(); |
| 486 | + |
| 487 | + CompletableFuture<String> completable = api.completableFuture(); |
| 488 | + |
| 489 | + assertThat(completable).isNotNull(); |
| 490 | + |
| 491 | + assertThat(completable.get(5, TimeUnit.SECONDS)).isEqualTo("fallback"); |
| 492 | + } |
| 493 | + |
430 | 494 | @Test |
431 | 495 | public void rxCompletableEmptyBody() { |
432 | 496 | server.enqueue(new MockResponse()); |
@@ -657,6 +721,9 @@ default HystrixCommand<String> defaultMethodReturningCommand() { |
657 | 721 |
|
658 | 722 | @RequestLine("GET /") |
659 | 723 | Completable completable(); |
| 724 | + |
| 725 | + @RequestLine("GET /") |
| 726 | + CompletableFuture<String> completableFuture(); |
660 | 727 | } |
661 | 728 |
|
662 | 729 | class FallbackTestInterface implements TestInterface { |
@@ -742,5 +809,10 @@ public List<String> getList() { |
742 | 809 | public Completable completable() { |
743 | 810 | return Completable.complete(); |
744 | 811 | } |
| 812 | + |
| 813 | + @Override |
| 814 | + public CompletableFuture<String> completableFuture() { |
| 815 | + return CompletableFuture.completedFuture("fallback"); |
| 816 | + } |
745 | 817 | } |
746 | 818 | } |
0 commit comments