|
| 1 | +package com.evanlennick.retry4j; |
| 2 | + |
| 3 | +import com.evanlennick.retry4j.config.RetryConfig; |
| 4 | +import com.evanlennick.retry4j.config.RetryConfigBuilder; |
| 5 | +import org.assertj.core.api.Assertions; |
| 6 | +import org.mockito.Mock; |
| 7 | +import org.mockito.MockitoAnnotations; |
| 8 | +import org.testng.annotations.BeforeMethod; |
| 9 | +import org.testng.annotations.Test; |
| 10 | + |
| 11 | +import java.time.temporal.ChronoUnit; |
| 12 | +import java.util.concurrent.Callable; |
| 13 | +import java.util.concurrent.CompletableFuture; |
| 14 | +import java.util.concurrent.Executors; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.mockito.Mockito.timeout; |
| 18 | +import static org.mockito.Mockito.verify; |
| 19 | +import static org.mockito.Mockito.when; |
| 20 | + |
| 21 | +public class AsyncCallExecutorTest_ListenersTest { |
| 22 | + |
| 23 | + @Mock |
| 24 | + private DummyMock dummyMock; |
| 25 | + |
| 26 | + private AsyncCallExecutor<String> executor; |
| 27 | + |
| 28 | + private Callable<String> callable; |
| 29 | + |
| 30 | + @BeforeMethod |
| 31 | + public void setup() { |
| 32 | + MockitoAnnotations.initMocks(this); |
| 33 | + |
| 34 | + callable = () -> dummyMock.callableCallThis(); |
| 35 | + |
| 36 | + RetryConfig config = new RetryConfigBuilder() |
| 37 | + .retryOnAnyException() |
| 38 | + .withMaxNumberOfTries(5) |
| 39 | + .withDelayBetweenTries(0, ChronoUnit.SECONDS) |
| 40 | + .withFixedBackoff() |
| 41 | + .build(); |
| 42 | + |
| 43 | + executor = new AsyncCallExecutor<>(config, Executors.newFixedThreadPool(5)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void verifyAfterFailedListener() throws Exception { |
| 48 | + when(dummyMock.callableCallThis()) |
| 49 | + .thenThrow(new RuntimeException()) |
| 50 | + .thenThrow(new IllegalStateException()) |
| 51 | + .thenReturn("success!"); |
| 52 | + |
| 53 | + executor.afterFailedTry(status -> dummyMock.listenersCallThis()); |
| 54 | + CompletableFuture<Status<String>> future = executor.execute(callable); |
| 55 | + |
| 56 | + Status<String> status = future.get(); |
| 57 | + assertThat(future).isDone(); |
| 58 | + assertThat(status.getLastExceptionThatCausedRetry()).isInstanceOf(IllegalStateException.class); |
| 59 | + assertThat(status.wasSuccessful()).isTrue(); |
| 60 | + assertThat(status.getTotalTries()).isEqualTo(3); |
| 61 | + assertThat(status.getResult()).isEqualTo("success!"); |
| 62 | + verify(dummyMock, timeout(1000).times(2)).listenersCallThis(); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void verifyOnListener_resultHasTypeOfCallExecutor() throws Exception { |
| 67 | + when(dummyMock.callableCallThis()) |
| 68 | + .thenThrow(new RuntimeException()) |
| 69 | + .thenThrow(new RuntimeException()) |
| 70 | + .thenThrow(new IllegalStateException()) |
| 71 | + .thenReturn("success!"); |
| 72 | + |
| 73 | + executor |
| 74 | + .onSuccess(status -> { |
| 75 | + dummyMock.listenersCallThis(); |
| 76 | + Assertions.assertThat(status.getResult()).isInstanceOf(String.class); |
| 77 | + }) |
| 78 | + .onFailure(status -> dummyMock.listenersCallThis()) |
| 79 | + .onCompletion(status -> { |
| 80 | + dummyMock.listenersCallThis(); |
| 81 | + Assertions.assertThat(status.getResult()).isInstanceOf(String.class); |
| 82 | + }) |
| 83 | + .afterFailedTry(status -> dummyMock.listenersCallThis()) |
| 84 | + .beforeNextTry(status -> dummyMock.listenersCallThis()); |
| 85 | + |
| 86 | + CompletableFuture<Status<String>> future = executor.execute(callable); |
| 87 | + |
| 88 | + Status<String> status = future.get(); |
| 89 | + assertThat(future).isDone(); |
| 90 | + assertThat(status.getLastExceptionThatCausedRetry()).isInstanceOf(IllegalStateException.class); |
| 91 | + assertThat(status.wasSuccessful()).isTrue(); |
| 92 | + assertThat(status.getTotalTries()).isEqualTo(4); |
| 93 | + assertThat(status.getResult()).isEqualTo("success!"); |
| 94 | + |
| 95 | + //only calls success once, completion once and the retry listeners 3 times each |
| 96 | + verify(dummyMock, timeout(1000).times(8)).listenersCallThis(); |
| 97 | + } |
| 98 | + |
| 99 | + private class DummyMock { |
| 100 | + |
| 101 | + public String listenersCallThis() { |
| 102 | + return "this is to use to verify listeners call the mock"; |
| 103 | + } |
| 104 | + |
| 105 | + public String listenersCallThis(Exception e) { |
| 106 | + return "this is to verify exceptions in the after failed call listener"; |
| 107 | + } |
| 108 | + |
| 109 | + public String callableCallThis() { |
| 110 | + return "this is to use for mocking the executed callable"; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments