33package software .amazon .lambda .durable .execution ;
44
55import static org .junit .jupiter .api .Assertions .assertEquals ;
6+ import static org .junit .jupiter .api .Assertions .assertFalse ;
67import static org .junit .jupiter .api .Assertions .assertTrue ;
78
89import java .util .List ;
1112import java .util .concurrent .ExecutorService ;
1213import java .util .concurrent .Executors ;
1314import java .util .concurrent .TimeUnit ;
15+ import java .util .concurrent .atomic .AtomicInteger ;
1416import java .util .concurrent .atomic .AtomicReference ;
15- import java .util .function .Function ;
17+ import java .util .function .BiFunction ;
18+ import org .junit .jupiter .api .Test ;
1619import org .junit .jupiter .params .ParameterizedTest ;
1720import org .junit .jupiter .params .provider .CsvSource ;
1821import software .amazon .awssdk .services .lambda .model .OperationStatus ;
2831
2932class InvocationDrainingIntegrationTest {
3033 @ ParameterizedTest
31- @ CsvSource ({"false, FLAT" , "false, NESTED" , "true, FLAT" , "true, NESTED" })
32- void handlerReturnDrainsQueuedBranches (boolean parallel , NestingType nestingType ) throws Exception {
34+ @ CsvSource ({
35+ "false, FLAT, false" , "false, NESTED, false" , "true, FLAT, false" , "true, NESTED, false" ,
36+ "false, FLAT, true" , "false, NESTED, true" , "true, FLAT, true" , "true, NESTED, true"
37+ })
38+ void handlerReturnDrainsQueuedBranches (boolean parallel , NestingType nestingType , boolean nestedSteps )
39+ throws Exception {
3340 var users = Executors .newCachedThreadPool ();
3441 var runtime = Executors .newSingleThreadExecutor ();
3542 var firstStarted = new CountDownLatch (1 );
@@ -41,11 +48,17 @@ void handlerReturnDrainsQueuedBranches(boolean parallel, NestingType nestingType
4148 String .class ,
4249 (input , context ) -> {
4350 manager .set (((DurableContextImpl ) context ).getExecutionManager ());
44- startQueuedWork (context , parallel , nestingType , item -> {
51+ startQueuedWork (context , parallel , nestingType , ( item , child ) -> {
4552 if (item .equals ("a" )) {
4653 firstStarted .countDown ();
4754 await (releaseFirst );
4855 }
56+ if (nestedSteps ) {
57+ return child .step ("nested-step" , String .class , step -> {
58+ completed .add (item );
59+ return item ;
60+ });
61+ }
4962 completed .add (item );
5063 return item ;
5164 });
@@ -66,15 +79,89 @@ void handlerReturnDrainsQueuedBranches(boolean parallel, NestingType nestingType
6679 assertEquals (
6780 OperationStatus .SUCCEEDED ,
6881 result .getOperation ("queued-work" ).getStatus ());
82+ if (nestedSteps ) {
83+ var steps = result .getOperations ().stream ()
84+ .filter (op -> "nested-step" .equals (op .getName ()))
85+ .toList ();
86+ assertEquals (3 , steps .size ());
87+ assertTrue (steps .stream ().allMatch (op -> op .getStatus () == OperationStatus .SUCCEEDED ));
88+ var replay = runner .runUntilComplete ("input" );
89+ assertEquals (ExecutionStatus .SUCCEEDED , replay .getStatus ());
90+ assertEquals (List .of ("a" , "b" , "c" ), completed , "Replay must not repeat nested step bodies" );
91+ assertEquals (
92+ steps .stream ().map (op -> op .getId ()).toList (),
93+ replay .getOperations ().stream ()
94+ .filter (op -> "nested-step" .equals (op .getName ()))
95+ .map (op -> op .getId ())
96+ .toList ());
97+ }
6998 } finally {
7099 releaseFirst .countDown ();
71100 stop (runtime );
72101 stop (users );
73102 }
74103 }
75104
105+ @ Test
106+ void drainsAsyncDescendantsCreatedAfterTheRootReturns () throws Exception {
107+ var users = Executors .newCachedThreadPool ();
108+ var runtime = Executors .newSingleThreadExecutor ();
109+ var childEntered = new CountDownLatch (1 );
110+ var releaseChild = new CountDownLatch (1 );
111+ var leafEntered = new CountDownLatch (1 );
112+ var releaseLeaf = new CountDownLatch (1 );
113+ var effects = new AtomicInteger ();
114+ var manager = new AtomicReference <ExecutionManager >();
115+ var runner = LocalDurableTestRunner .create (
116+ String .class ,
117+ (input , context ) -> {
118+ manager .set (((DurableContextImpl ) context ).getExecutionManager ());
119+ context .runInChildContextAsync ("child" , String .class , child -> {
120+ childEntered .countDown ();
121+ await (releaseChild );
122+ child .runInChildContextAsync ("grandchild" , String .class , grandchild -> {
123+ grandchild .stepAsync ("leaf" , String .class , step -> {
124+ leafEntered .countDown ();
125+ await (releaseLeaf );
126+ effects .incrementAndGet ();
127+ return "leaf-result" ;
128+ });
129+ return "grandchild-result" ;
130+ });
131+ return "child-result" ;
132+ });
133+ await (childEntered );
134+ return "done" ;
135+ },
136+ DurableConfig .builder ().withExecutorService (users ).build ());
137+ try {
138+ var invocation = runtime .submit (() -> runner .runUntilComplete ("input" ));
139+ await (childEntered );
140+ awaitDraining (manager .get ());
141+ releaseChild .countDown ();
142+ await (leafEntered );
143+ assertFalse (invocation .isDone (), "Shutdown must wait for the newly registered async leaf" );
144+ releaseLeaf .countDown ();
145+ var result = invocation .get (10 , TimeUnit .SECONDS );
146+ assertEquals (ExecutionStatus .SUCCEEDED , result .getStatus ());
147+ assertEquals (OperationStatus .SUCCEEDED , result .getOperation ("leaf" ).getStatus ());
148+ assertEquals ("leaf-result" , result .getOperation ("leaf" ).getStepResult (String .class ));
149+ var replay = runner .runUntilComplete ("input" );
150+ assertEquals (ExecutionStatus .SUCCEEDED , replay .getStatus ());
151+ assertEquals (1 , effects .get (), "The checkpointed descendant must not repeat on replay" );
152+ } finally {
153+ releaseChild .countDown ();
154+ releaseLeaf .countDown ();
155+ stop (runtime );
156+ stop (users );
157+ }
158+ }
159+
76160 private static void startQueuedWork (
77- DurableContext context , boolean parallel , NestingType nestingType , Function <String , String > action ) {
161+ DurableContext context ,
162+ boolean parallel ,
163+ NestingType nestingType ,
164+ BiFunction <String , DurableContext , String > action ) {
78165 // Default allCompleted requires a caller to join. Use an explicit threshold to complete without joining.
79166 var completion = CompletionConfig .minSuccessful (3 );
80167 if (parallel ) {
@@ -86,14 +173,14 @@ private static void startQueuedWork(
86173 .completionConfig (completion )
87174 .build ());
88175 for (var item : List .of ("a" , "b" , "c" )) {
89- operation .branch (item , String .class , child -> action .apply (item ));
176+ operation .branch (item , String .class , child -> action .apply (item , child ));
90177 }
91178 } else {
92179 context .mapAsync (
93180 "queued-work" ,
94181 List .of ("a" , "b" , "c" ),
95182 String .class ,
96- (item , index , child ) -> action .apply (item ),
183+ (item , index , child ) -> action .apply (item , child ),
97184 MapConfig .builder ()
98185 .maxConcurrency (1 )
99186 .nestingType (nestingType )
0 commit comments