44import org .junit .jupiter .api .Assertions ;
55import org .junit .jupiter .api .Test ;
66import tech .illuin .pipeline .Pipeline ;
7- import tech .illuin .pipeline .context .LocalContext ;
8- import tech .illuin .pipeline .input .uid_generator .UIDGenerator ;
9- import tech .illuin .pipeline .resilience4j .execution .wrapper .RetryWrapper ;
7+ import tech .illuin .pipeline .PipelineException ;
108import tech .illuin .pipeline .generic .TestFactory ;
119import tech .illuin .pipeline .generic .model .A ;
1210import tech .illuin .pipeline .generic .pipeline .step .TestStep ;
1311import tech .illuin .pipeline .input .indexer .SingleIndexer ;
1412import tech .illuin .pipeline .output .Output ;
13+ import tech .illuin .pipeline .resilience4j .execution .wrapper .RetryWrapper ;
1514
1615import java .time .Duration ;
1716import java .util .List ;
1817import java .util .concurrent .atomic .AtomicInteger ;
18+ import java .util .function .Predicate ;
19+ import java .util .function .Supplier ;
1920
2021import static tech .illuin .pipeline .generic .Tests .getResultTypes ;
2122
@@ -27,7 +28,24 @@ public class StepWrapperRetryTest
2728 @ Test
2829 public void testPipeline_shouldRetryException ()
2930 {
30- Pipeline <Void > pipeline = Assertions .assertDoesNotThrow (StepWrapperRetryTest ::createErrorRetryPipeline );
31+ Pipeline <Void > pipeline = Assertions .assertDoesNotThrow (() -> StepWrapperRetryTest .createErrorRetryPipeline (() -> new RuntimeException ("Some error" ), ex -> true ));
32+ Output output = Assertions .assertDoesNotThrow (() -> pipeline .run ());
33+ Assertions .assertDoesNotThrow (pipeline ::close );
34+
35+ Assertions .assertIterableEquals (List .of ("1" , "2" , "3" ), getResultTypes (output , output .payload (A .class )));
36+
37+ Assertions .assertEquals (1 , output .results ().size ());
38+ Assertions .assertEquals (3 , output .results ().current ().count ());
39+
40+ Assertions .assertTrue (output .results ().current ("1" ).isPresent ());
41+ Assertions .assertTrue (output .results ().current ("2" ).isPresent ());
42+ Assertions .assertTrue (output .results ().current ("3" ).isPresent ());
43+ }
44+
45+ @ Test
46+ public void testPipeline_shouldRetryException_withTypeCondition_shouldMatch ()
47+ {
48+ Pipeline <Void > pipeline = Assertions .assertDoesNotThrow (() -> StepWrapperRetryTest .createErrorRetryPipeline (TestException ::new , ex -> ex instanceof TestException ));
3149 Output output = Assertions .assertDoesNotThrow (() -> pipeline .run ());
3250 Assertions .assertDoesNotThrow (pipeline ::close );
3351
@@ -41,26 +59,37 @@ public void testPipeline_shouldRetryException()
4159 Assertions .assertTrue (output .results ().current ("3" ).isPresent ());
4260 }
4361
44- public static Pipeline <Void > createErrorRetryPipeline ()
62+ @ Test
63+ public void testPipeline_shouldRetryException_withTypeCondition_shouldNotMatch ()
64+ {
65+ Pipeline <Void > pipeline = Assertions .assertDoesNotThrow (() -> StepWrapperRetryTest .createErrorRetryPipeline (TestException ::new , ex -> ex instanceof RuntimeException ));
66+ PipelineException ex = Assertions .assertThrows (PipelineException .class , pipeline ::run );
67+ Assertions .assertTrue (ex .getCause () instanceof TestException );
68+ }
69+
70+ public static Pipeline <Void > createErrorRetryPipeline (Supplier <Exception > errorSupplier , Predicate <Throwable > retryPredicate )
4571 {
4672 var counter = new AtomicInteger (0 );
47- return Pipeline .of ("test-error-retry" , ( Void input , LocalContext context , UIDGenerator generator ) -> TestFactory . initializerOfEmpty ( input , context , generator ) )
73+ return Pipeline .of ("test-error-retry" , TestFactory :: initializerOfEmpty )
4874 .registerIndexer (SingleIndexer .auto ())
4975 .registerStep (new TestStep <>("1" , "ok" ))
5076 .registerStep (builder -> builder
5177 .step (new TestStep <>("2" , b -> {
5278 if (counter .getAndIncrement () < 4 )
53- throw new RuntimeException ( "Some error" );
79+ throw errorSupplier . get ( );
5480 return "ok" ;
5581 }))
5682 .withWrapper (new RetryWrapper <>(RetryConfig .custom ()
5783 .maxAttempts (5 )
5884 .waitDuration (Duration .ofMillis (500 ))
85+ .retryOnException (retryPredicate )
5986 .build ()
6087 ))
6188 )
6289 .registerStep (new TestStep <>("3" , "ok" ))
6390 .build ()
6491 ;
6592 }
93+
94+ private static class TestException extends Exception {}
6695}
0 commit comments