27
27
28
28
@ Test
29
29
public class RetryPolicyTest {
30
+ static class FooException extends Exception {
31
+ String bar ;
32
+
33
+ FooException (String bar ) {
34
+ this .bar = bar ;
35
+ }
36
+ }
37
+
30
38
public void testIsFailureNull () {
31
39
RetryPolicy <Object > policy = new RetryPolicy <>();
32
40
assertFalse (policy .isFailure (null , null ));
33
41
}
34
42
35
43
public void testIsFailureCompletionPredicate () {
36
- RetryPolicy <Object > policy = new RetryPolicy <>()
37
- . handleIf ( (result , failure ) -> result == "test" || failure instanceof IllegalArgumentException );
44
+ RetryPolicy <Object > policy = new RetryPolicy <>(). handleIf (
45
+ (result , failure ) -> result == "test" || failure instanceof IllegalArgumentException );
38
46
assertTrue (policy .isFailure ("test" , null ));
39
47
// No retries needed for successful result
40
48
assertFalse (policy .isFailure (0 , null ));
@@ -48,6 +56,12 @@ public void testIsFailureFailurePredicate() {
48
56
assertFalse (policy .isFailure (null , new IllegalStateException ()));
49
57
}
50
58
59
+ public void testIsFailureTypedFailurePredicate () {
60
+ RetryPolicy <Boolean > policy = new RetryPolicy <Boolean >().<FooException >handleIf ((result , failure ) -> failure .bar .equals ("bar" ));
61
+ assertTrue (policy .isFailure (null , new FooException ("bar" )));
62
+ assertFalse (policy .isFailure (null , new IllegalStateException ()));
63
+ }
64
+
51
65
public void testIsFailureResultPredicate () {
52
66
RetryPolicy <Integer > policy = new RetryPolicy <Integer >().handleResultIf (result -> result > 100 );
53
67
assertTrue (policy .isFailure (110 , null ));
@@ -93,8 +107,8 @@ public void testIsAbortableNull() {
93
107
}
94
108
95
109
public void testIsAbortableCompletionPredicate () {
96
- RetryPolicy <Object > policy = new RetryPolicy <>()
97
- . abortIf ( (result , failure ) -> result == "test" || failure instanceof IllegalArgumentException );
110
+ RetryPolicy <Object > policy = new RetryPolicy <>(). abortIf (
111
+ (result , failure ) -> result == "test" || failure instanceof IllegalArgumentException );
98
112
assertTrue (policy .isAbortable ("test" , null ));
99
113
assertFalse (policy .isAbortable (0 , null ));
100
114
assertTrue (policy .isAbortable (null , new IllegalArgumentException ()));
@@ -142,19 +156,23 @@ public void testIsAbortableResult() {
142
156
public void shouldRequireValidBackoff () {
143
157
Asserts .assertThrows (() -> new RetryPolicy ().withBackoff (0 , 0 , null ), NullPointerException .class );
144
158
Asserts .assertThrows (
145
- () -> new RetryPolicy ().withMaxDuration (Duration .ofMillis (1 )).withBackoff (100 , 120 , ChronoUnit .MILLIS ),
146
- IllegalStateException .class );
147
- Asserts .assertThrows (() -> new RetryPolicy ().withBackoff (-3 , 10 , ChronoUnit .MILLIS ), IllegalArgumentException .class );
148
- Asserts .assertThrows (() -> new RetryPolicy ().withBackoff (100 , 10 , ChronoUnit .MILLIS ), IllegalArgumentException .class );
149
- Asserts .assertThrows (() -> new RetryPolicy ().withBackoff (5 , 10 , ChronoUnit .MILLIS , .5 ), IllegalArgumentException .class );
159
+ () -> new RetryPolicy ().withMaxDuration (Duration .ofMillis (1 )).withBackoff (100 , 120 , ChronoUnit .MILLIS ),
160
+ IllegalStateException .class );
161
+ Asserts .assertThrows (() -> new RetryPolicy ().withBackoff (-3 , 10 , ChronoUnit .MILLIS ),
162
+ IllegalArgumentException .class );
163
+ Asserts .assertThrows (() -> new RetryPolicy ().withBackoff (100 , 10 , ChronoUnit .MILLIS ),
164
+ IllegalArgumentException .class );
165
+ Asserts .assertThrows (() -> new RetryPolicy ().withBackoff (5 , 10 , ChronoUnit .MILLIS , .5 ),
166
+ IllegalArgumentException .class );
150
167
}
151
168
152
169
public void shouldRequireValidDelay () {
153
- Asserts .assertThrows (() -> new RetryPolicy ().withDelay ((Duration )null ), NullPointerException .class );
154
- Asserts .assertThrows (() -> new RetryPolicy ().withMaxDuration (Duration .ofMillis (1 )).withDelay (Duration .ofMillis (100 )),
155
- IllegalStateException .class );
170
+ Asserts .assertThrows (() -> new RetryPolicy ().withDelay ((Duration ) null ), NullPointerException .class );
171
+ Asserts .assertThrows (
172
+ () -> new RetryPolicy ().withMaxDuration (Duration .ofMillis (1 )).withDelay (Duration .ofMillis (100 )),
173
+ IllegalStateException .class );
156
174
Asserts .assertThrows (() -> new RetryPolicy ().withBackoff (1 , 2 , ChronoUnit .MILLIS ).withDelay (Duration .ofMillis (100 )),
157
- IllegalStateException .class );
175
+ IllegalStateException .class );
158
176
Asserts .assertThrows (() -> new RetryPolicy ().withDelay (Duration .ofMillis (-1 )), IllegalArgumentException .class );
159
177
}
160
178
@@ -164,8 +182,8 @@ public void shouldRequireValidMaxRetries() {
164
182
165
183
public void shouldRequireValidMaxDuration () {
166
184
Asserts .assertThrows (
167
- () -> new RetryPolicy ().withDelay (Duration .ofMillis (100 )).withMaxDuration (Duration .ofMillis (100 )),
168
- IllegalStateException .class );
185
+ () -> new RetryPolicy ().withDelay (Duration .ofMillis (100 )).withMaxDuration (Duration .ofMillis (100 )),
186
+ IllegalStateException .class );
169
187
}
170
188
171
189
public void testGetMaxAttempts () {
0 commit comments