2727
2828@ Test
2929public class RetryPolicyTest {
30+ static class FooException extends Exception {
31+ String bar ;
32+
33+ FooException (String bar ) {
34+ this .bar = bar ;
35+ }
36+ }
37+
3038 public void testIsFailureNull () {
3139 RetryPolicy <Object > policy = new RetryPolicy <>();
3240 assertFalse (policy .isFailure (null , null ));
3341 }
3442
3543 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 );
3846 assertTrue (policy .isFailure ("test" , null ));
3947 // No retries needed for successful result
4048 assertFalse (policy .isFailure (0 , null ));
@@ -48,6 +56,12 @@ public void testIsFailureFailurePredicate() {
4856 assertFalse (policy .isFailure (null , new IllegalStateException ()));
4957 }
5058
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+
5165 public void testIsFailureResultPredicate () {
5266 RetryPolicy <Integer > policy = new RetryPolicy <Integer >().handleResultIf (result -> result > 100 );
5367 assertTrue (policy .isFailure (110 , null ));
@@ -93,8 +107,8 @@ public void testIsAbortableNull() {
93107 }
94108
95109 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 );
98112 assertTrue (policy .isAbortable ("test" , null ));
99113 assertFalse (policy .isAbortable (0 , null ));
100114 assertTrue (policy .isAbortable (null , new IllegalArgumentException ()));
@@ -142,19 +156,23 @@ public void testIsAbortableResult() {
142156 public void shouldRequireValidBackoff () {
143157 Asserts .assertThrows (() -> new RetryPolicy ().withBackoff (0 , 0 , null ), NullPointerException .class );
144158 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 );
150167 }
151168
152169 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 );
156174 Asserts .assertThrows (() -> new RetryPolicy ().withBackoff (1 , 2 , ChronoUnit .MILLIS ).withDelay (Duration .ofMillis (100 )),
157- IllegalStateException .class );
175+ IllegalStateException .class );
158176 Asserts .assertThrows (() -> new RetryPolicy ().withDelay (Duration .ofMillis (-1 )), IllegalArgumentException .class );
159177 }
160178
@@ -164,8 +182,8 @@ public void shouldRequireValidMaxRetries() {
164182
165183 public void shouldRequireValidMaxDuration () {
166184 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 );
169187 }
170188
171189 public void testGetMaxAttempts () {
0 commit comments