Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 40e8d7c

Browse files
committed
PR for CallExecutor does not know the type of a result #72
Add a test for AsyncCallExecutor to test listeners Use assertions for a test of CallExecutor
1 parent 417cc86 commit 40e8d7c

File tree

2 files changed

+137
-4
lines changed

2 files changed

+137
-4
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}

src/test/java/com/evanlennick/retry4j/CallExecutorTest_ListenersTest.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.evanlennick.retry4j.config.RetryConfig;
44
import com.evanlennick.retry4j.config.RetryConfigBuilder;
5+
import org.assertj.core.api.Assertions;
56
import org.mockito.Mock;
67
import org.mockito.MockitoAnnotations;
78
import org.testng.annotations.BeforeMethod;
@@ -194,10 +195,29 @@ public void verifyOnFailureListener_populatesException() {
194195
}
195196

196197
@Test
197-
public void verifyOnSuccessListener_resultHasTypeOfCallExecutor() {
198-
executor.onSuccess(status -> {
199-
String result = status.getResult(); //compile time check
200-
}).execute(callable);
198+
public void verifyOnListener_resultHasTypeOfCallExecutor() {
199+
when(dummyMock.callableCallThis())
200+
.thenThrow(new RuntimeException())
201+
.thenThrow(new RuntimeException())
202+
.thenThrow(new RuntimeException())
203+
.thenReturn("success!");
204+
205+
executor
206+
.onSuccess(status -> {
207+
dummyMock.listenersCallThis();
208+
Assertions.assertThat(status.getResult()).isInstanceOf(String.class);
209+
})
210+
.onFailure(status -> dummyMock.listenersCallThis())
211+
.onCompletion(status -> {
212+
dummyMock.listenersCallThis();
213+
Assertions.assertThat(status.getResult()).isInstanceOf(String.class);
214+
})
215+
.afterFailedTry(status -> dummyMock.listenersCallThis())
216+
.beforeNextTry(status -> dummyMock.listenersCallThis())
217+
.execute(callable);
218+
219+
//only calls success once, completion once and the retry listeners 3 times each
220+
verify(dummyMock, timeout(1000).times(8)).listenersCallThis();
201221
}
202222

203223
private class DummyMock {

0 commit comments

Comments
 (0)