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

Commit f2a4c5e

Browse files
committed
PR for CallExecutor does not know the type of a result #72
Get rid of redundant testing - simplify test cases
1 parent 2e7b7d6 commit f2a4c5e

File tree

2 files changed

+20
-67
lines changed

2 files changed

+20
-67
lines changed

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

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
import com.evanlennick.retry4j.config.RetryConfig;
44
import com.evanlennick.retry4j.config.RetryConfigBuilder;
5-
import org.assertj.core.api.Assertions;
5+
import com.evanlennick.retry4j.listener.RetryListener;
66
import org.mockito.Mock;
77
import org.mockito.MockitoAnnotations;
88
import org.testng.annotations.BeforeMethod;
99
import org.testng.annotations.Test;
1010

1111
import java.time.temporal.ChronoUnit;
1212
import java.util.concurrent.Callable;
13-
import java.util.concurrent.CompletableFuture;
1413
import java.util.concurrent.Executors;
1514

1615
import static org.assertj.core.api.Assertions.assertThat;
@@ -19,12 +18,9 @@
1918
import static org.mockito.Mockito.when;
2019

2120
public class AsyncCallExecutorTest_ListenersTest {
22-
2321
@Mock
2422
private DummyMock dummyMock;
25-
2623
private AsyncCallExecutor<String> executor;
27-
2824
private Callable<String> callable;
2925

3026
@BeforeMethod
@@ -43,61 +39,25 @@ public void setup() {
4339
executor = new AsyncCallExecutor<>(config, Executors.newFixedThreadPool(5));
4440
}
4541

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-
6542
@Test
6643
public void verifyOnListener_resultHasTypeOfCallExecutor() throws Exception {
6744
when(dummyMock.callableCallThis())
68-
.thenThrow(new RuntimeException())
69-
.thenThrow(new RuntimeException())
70-
.thenThrow(new IllegalStateException())
7145
.thenReturn("success!");
7246

47+
RetryListener<String> listener = status -> {
48+
dummyMock.listenersCallThis();
49+
assertThat(status.getResult()).isInstanceOf(String.class);
50+
};
7351
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);
52+
.onSuccess(listener)
53+
.onCompletion(listener)
54+
.execute(callable).get();
8755

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();
56+
// ensure both listeners are called
57+
verify(dummyMock, timeout(1000).times(2)).listenersCallThis();
9758
}
9859

9960
private class DummyMock {
100-
10161
public String listenersCallThis() {
10262
return "this is to use to verify listeners call the mock";
10363
}

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.evanlennick.retry4j.config.RetryConfig;
44
import com.evanlennick.retry4j.config.RetryConfigBuilder;
5-
import org.assertj.core.api.Assertions;
5+
import com.evanlennick.retry4j.listener.RetryListener;
66
import org.mockito.Mock;
77
import org.mockito.MockitoAnnotations;
88
import org.testng.annotations.BeforeMethod;
@@ -11,6 +11,7 @@
1111
import java.time.temporal.ChronoUnit;
1212
import java.util.concurrent.Callable;
1313

14+
import static org.assertj.core.api.Assertions.assertThat;
1415
import static org.mockito.Mockito.after;
1516
import static org.mockito.Mockito.isA;
1617
import static org.mockito.Mockito.timeout;
@@ -197,27 +198,19 @@ public void verifyOnFailureListener_populatesException() {
197198
@Test
198199
public void verifyOnListener_resultHasTypeOfCallExecutor() {
199200
when(dummyMock.callableCallThis())
200-
.thenThrow(new RuntimeException())
201-
.thenThrow(new RuntimeException())
202-
.thenThrow(new RuntimeException())
203201
.thenReturn("success!");
204202

203+
RetryListener<String> listener = status -> {
204+
dummyMock.listenersCallThis();
205+
assertThat(status.getResult()).isInstanceOf(String.class);
206+
};
205207
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())
208+
.onSuccess(listener)
209+
.onCompletion(listener)
217210
.execute(callable);
218211

219-
//only calls success once, completion once and the retry listeners 3 times each
220-
verify(dummyMock, timeout(1000).times(8)).listenersCallThis();
212+
// ensure both listeners are called
213+
verify(dummyMock, timeout(1000).times(2)).listenersCallThis();
221214
}
222215

223216
private class DummyMock {

0 commit comments

Comments
 (0)