4
4
import com .evanlennick .retry4j .config .RetryConfigBuilder ;
5
5
import com .evanlennick .retry4j .exception .RetriesExhaustedException ;
6
6
import com .evanlennick .retry4j .exception .UnexpectedException ;
7
- import org .junit .BeforeClass ;
8
7
import org .junit .Test ;
9
8
10
9
import java .time .Duration ;
14
13
15
14
public class CallExecutorTest_RetryOnCustomLogicTest {
16
15
17
- private static RetryConfig config ;
16
+ @ Test
17
+ public void verifyShouldRetryOnExceptionMessage () {
18
+ RetryConfig config = new RetryConfigBuilder ()
19
+ .retryOnCustomExceptionLogic (ex -> ex .getMessage ().contains ("should retry!" ))
20
+ .withFixedBackoff ()
21
+ .withDelayBetweenTries (Duration .ofMillis (1 ))
22
+ .withMaxNumberOfTries (3 )
23
+ .build ();
18
24
19
- @ BeforeClass
20
- public static void setup () {
21
- config = new RetryConfigBuilder ()
25
+ try {
26
+ new CallExecutor <>(config )
27
+ .execute (() -> {
28
+ throw new RuntimeException ("should retry!" );
29
+ });
30
+ fail ();
31
+ } catch (RetriesExhaustedException e ) {
32
+ assertThat (e .getStatus ().getTotalTries ()).isEqualTo (3 );
33
+ }
34
+ }
35
+
36
+ @ Test (expected = UnexpectedException .class )
37
+ public void verifyShouldNotRetryOnExceptionMessage () {
38
+ RetryConfig config = new RetryConfigBuilder ()
22
39
.retryOnCustomExceptionLogic (ex -> ex .getMessage ().contains ("should retry!" ))
23
40
.withFixedBackoff ()
24
41
.withDelayBetweenTries (Duration .ofMillis (1 ))
25
42
.withMaxNumberOfTries (3 )
26
43
.build ();
44
+
45
+ new CallExecutor <>(config )
46
+ .execute (() -> {
47
+ throw new RuntimeException ("should NOT retry!" );
48
+ });
27
49
}
28
50
29
51
@ Test
30
- public void verifyShouldRetry () {
52
+ public void verifyShouldRetryOnCustomException () {
53
+ RetryConfig config = new RetryConfigBuilder ()
54
+ .retryOnCustomExceptionLogic (ex -> ((CustomTestException ) ex ).getSomeValue () > 0 )
55
+ .withFixedBackoff ()
56
+ .withDelayBetweenTries (Duration .ofMillis (1 ))
57
+ .withMaxNumberOfTries (3 )
58
+ .build ();
59
+
31
60
try {
32
61
new CallExecutor <>(config )
33
62
.execute (() -> {
34
- throw new RuntimeException ("should retry!" );
63
+ throw new CustomTestException ("should retry!" , 100 );
35
64
});
36
65
fail ();
37
66
} catch (RetriesExhaustedException e ) {
@@ -40,10 +69,31 @@ public void verifyShouldRetry() {
40
69
}
41
70
42
71
@ Test (expected = UnexpectedException .class )
43
- public void verifyShouldNotRetry () {
72
+ public void verifyShouldNotRetryOnCustomException () {
73
+ RetryConfig config = new RetryConfigBuilder ()
74
+ .retryOnCustomExceptionLogic (ex -> ((CustomTestException ) ex ).getSomeValue () > 0 )
75
+ .withFixedBackoff ()
76
+ .withDelayBetweenTries (Duration .ofMillis (1 ))
77
+ .withMaxNumberOfTries (3 )
78
+ .build ();
79
+
44
80
new CallExecutor <>(config )
45
81
.execute (() -> {
46
- throw new RuntimeException ( "should NOT retry!" );
82
+ throw new CustomTestException ( "test message" , - 100 );
47
83
});
48
84
}
85
+
86
+ private class CustomTestException extends RuntimeException {
87
+
88
+ private int someValue ;
89
+
90
+ public CustomTestException (String message , int someValue ) {
91
+ super (message );
92
+ this .someValue = someValue ;
93
+ }
94
+
95
+ public int getSomeValue () {
96
+ return someValue ;
97
+ }
98
+ }
49
99
}
0 commit comments