2
2
3
3
import com .evanlennick .retry4j .config .RetryConfig ;
4
4
import com .evanlennick .retry4j .config .RetryConfigBuilder ;
5
- import org . assertj . core . api . Assertions ;
5
+ import com . evanlennick . retry4j . listener . RetryListener ;
6
6
import org .mockito .Mock ;
7
7
import org .mockito .MockitoAnnotations ;
8
8
import org .testng .annotations .BeforeMethod ;
9
9
import org .testng .annotations .Test ;
10
10
11
11
import java .time .temporal .ChronoUnit ;
12
12
import java .util .concurrent .Callable ;
13
- import java .util .concurrent .CompletableFuture ;
14
13
import java .util .concurrent .Executors ;
15
14
16
15
import static org .assertj .core .api .Assertions .assertThat ;
19
18
import static org .mockito .Mockito .when ;
20
19
21
20
public class AsyncCallExecutorTest_ListenersTest {
22
-
23
21
@ Mock
24
22
private DummyMock dummyMock ;
25
-
26
23
private AsyncCallExecutor <String > executor ;
27
-
28
24
private Callable <String > callable ;
29
25
30
26
@ BeforeMethod
@@ -43,61 +39,25 @@ public void setup() {
43
39
executor = new AsyncCallExecutor <>(config , Executors .newFixedThreadPool (5 ));
44
40
}
45
41
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
42
@ Test
66
43
public void verifyOnListener_resultHasTypeOfCallExecutor () throws Exception {
67
44
when (dummyMock .callableCallThis ())
68
- .thenThrow (new RuntimeException ())
69
- .thenThrow (new RuntimeException ())
70
- .thenThrow (new IllegalStateException ())
71
45
.thenReturn ("success!" );
72
46
47
+ RetryListener <String > listener = status -> {
48
+ dummyMock .listenersCallThis ();
49
+ assertThat (status .getResult ()).isInstanceOf (String .class );
50
+ };
73
51
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 ();
87
55
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 ();
97
58
}
98
59
99
60
private class DummyMock {
100
-
101
61
public String listenersCallThis () {
102
62
return "this is to use to verify listeners call the mock" ;
103
63
}
0 commit comments