| 759 | } |
| 760 | |
| 761 | @Disabled("FIXME random test failures when building on ubuntu, need to investigate further") |
| 762 | // @ParameterizedTest |
| 763 | // @ValueSource(ints = {1, 5, 10, 100, 1000}) |
| 764 | public void cancelRetry(final int expectedTryCount) throws Throwable { |
| 765 | // Arrange |
| 766 | final CompletableFuture<Boolean> maximumTryCompleted = new CompletableFuture<>(); |
| 767 | final AtomicInteger actualTryCount = new AtomicInteger(); |
| 768 | final AtomicBoolean isCancelled = new AtomicBoolean(true); |
| 769 | |
| 770 | final int RUNNING_TIME_MILLIS = 100; |
| 771 | final ExecutorService execs = Executors.newSingleThreadExecutor(); |
| 772 | final AsyncClient<Void> clientMock = |
| 773 | (_, _, _) -> |
| 774 | CompletableFuture.supplyAsync( |
| 775 | () -> { |
| 776 | final int tryCount = actualTryCount.addAndGet(1); |
| 777 | if (tryCount < expectedTryCount) { |
| 778 | throw new CompletionException(new IOException()); |
| 779 | } |
| 780 | |
| 781 | if (tryCount > expectedTryCount) { |
| 782 | isCancelled.set(false); |
| 783 | throw new CompletionException(new IOException()); |
| 784 | } |
| 785 | |
| 786 | maximumTryCompleted.complete(true); |
| 787 | try { |
| 788 | Thread.sleep(RUNNING_TIME_MILLIS); |
| 789 | throw new IOException(); |
| 790 | } catch (Throwable e) { |
| 791 | throw new CompletionException(e); |
| 792 | } |
| 793 | }, |
| 794 | execs); |
| 795 | final TestInterfaceAsync sut = |
| 796 | AsyncFeign.<Void>builder() |
| 797 | .client(clientMock) |
| 798 | .retryer(new DefaultRetryer(0, Long.MAX_VALUE, expectedTryCount * 2)) |
| 799 | .target(TestInterfaceAsync.class, "http://localhost:" + server.getPort()); |
| 800 | |
| 801 | // Act |
| 802 | final CompletableFuture<String> actual = sut.post(); |
| 803 | maximumTryCompleted.join(); |
| 804 | actual.cancel(true); |
| 805 | Thread.sleep(RUNNING_TIME_MILLIS * 5); |
| 806 | |
| 807 | // Assert |
| 808 | assertThat(actualTryCount.get()).isEqualTo(expectedTryCount); |
| 809 | assertThat(isCancelled.get()).isTrue(); |
| 810 | execs.shutdown(); |
| 811 | } |
| 812 | |
| 813 | private static class MockRetryer implements Retryer { |
| 814 | |