2727import io .temporal .api .workflowservice .v1 .DescribeWorkflowExecutionResponse ;
2828import io .temporal .client .WorkflowClient ;
2929import io .temporal .client .WorkflowOptions ;
30+ import io .temporal .common .interceptors .*;
3031import io .temporal .samples .taskinteraction .activity .ActivityTaskImpl ;
3132import io .temporal .testing .TestWorkflowRule ;
32- import java .time .Duration ;
33+ import io .temporal .worker .WorkerFactoryOptions ;
34+ import io .temporal .workflow .Workflow ;
3335import java .util .List ;
36+ import java .util .concurrent .CompletableFuture ;
3437import org .junit .Rule ;
3538import org .junit .Test ;
3639
3740public class WorkflowWithTasksImplTest {
3841
42+ private MyWorkerInterceptor myWorkerInterceptor = new MyWorkerInterceptor ();
43+
3944 @ Rule
4045 public TestWorkflowRule testWorkflowRule =
4146 TestWorkflowRule .newBuilder ()
42- // .setNamespace("default")
43- // .setUseExternalService(true)
47+ .setWorkerFactoryOptions (
48+ WorkerFactoryOptions .newBuilder ()
49+ .setWorkerInterceptors (myWorkerInterceptor )
50+ .validateAndBuildWithDefaults ())
4451 .setDoNotStart (true )
4552 .build ();
4653
@@ -56,6 +63,7 @@ public void testEnd2End() {
5663 testWorkflowRule
5764 .getWorker ()
5865 .registerActivitiesImplementations (new ActivityTaskImpl (workflowClient ));
66+
5967 testWorkflowRule .getTestEnvironment ().start ();
6068
6169 WorkflowWithTasks workflow =
@@ -70,23 +78,25 @@ public void testEnd2End() {
7078
7179 WorkflowExecution execution = WorkflowClient .start (workflow ::execute );
7280
73- // TODO
74- testWorkflowRule . getTestEnvironment (). sleep ( Duration . ofSeconds ( 2 ) );
81+ // Wait until the first two tasks from WorkflowWithTasks are created in WorkflowTaskManager
82+ myWorkerInterceptor . waitUntilTwoCreateTaskInvocations ( );
7583
7684 WorkflowTaskManager workflowManager =
7785 workflowClient .newWorkflowStub (WorkflowTaskManager .class , WorkflowTaskManager .WORKFLOW_ID );
7886
7987 final List <Task > pendingTask = getPendingTask (workflowManager );
8088 assertEquals (2 , pendingTask .size ());
8189
82- // Let's complete the two pending task. Send update to the workflow that holds and keep tasks
83- // state
90+ // Complete the two pending task created in parallel from `WorkflowWithTasks`.
91+ // Send update to the workflow that keeps tasks state, that will signal back
92+ // the `WorkflowWithTasks` execution
8493 workflowManager .completeTaskByToken (pendingTask .get (0 ).getToken ());
8594 workflowManager .completeTaskByToken (pendingTask .get (1 ).getToken ());
8695
87- // TODO
88- testWorkflowRule . getTestEnvironment (). sleep ( Duration . ofSeconds ( 2 ) );
96+ // Wait until the last task in WorkflowWithTasks is created in WorkflowTaskManager
97+ myWorkerInterceptor . waitUntilThreeInvocationsOfCreateTask ( );
8998
99+ // Complete the last task in `WorkflowWithTasks`
90100 assertEquals (1 , getPendingTask (workflowManager ).size ());
91101 workflowManager .completeTaskByToken (getPendingTask (workflowManager ).get (0 ).getToken ());
92102
@@ -115,4 +125,59 @@ private DescribeWorkflowExecutionResponse getDescribeWorkflowExecutionResponse(
115125 private static List <Task > getPendingTask (final WorkflowTaskManager workflowManager ) {
116126 return workflowManager .getPendingTask ();
117127 }
128+
129+ private class MyWorkerInterceptor extends WorkerInterceptorBase {
130+
131+ private int createTaskInvocations = 0 ;
132+
133+ private CompletableFuture <Void > waitUntilTwoInvocationsOfCreateTask ;
134+
135+ private CompletableFuture <Void > waitUntilThreeInvocationsOfCreateTask ;
136+
137+ public MyWorkerInterceptor () {
138+ waitUntilTwoInvocationsOfCreateTask = new CompletableFuture <>();
139+ waitUntilThreeInvocationsOfCreateTask = new CompletableFuture <>();
140+ }
141+
142+ public Void waitUntilTwoCreateTaskInvocations () {
143+ return getFromCompletableFuture (waitUntilTwoInvocationsOfCreateTask );
144+ }
145+
146+ public Void waitUntilThreeInvocationsOfCreateTask () {
147+ return getFromCompletableFuture (waitUntilThreeInvocationsOfCreateTask );
148+ }
149+
150+ private Void getFromCompletableFuture (final CompletableFuture <Void > completableFuture ) {
151+ try {
152+ return completableFuture .get ();
153+ } catch (Exception e ) {
154+ throw new RuntimeException (e );
155+ }
156+ }
157+
158+ @ Override
159+ public WorkflowInboundCallsInterceptor interceptWorkflow (
160+ final WorkflowInboundCallsInterceptor next ) {
161+ return new WorkflowInboundCallsInterceptorBase (next ) {
162+ @ Override
163+ public UpdateOutput executeUpdate (final UpdateInput input ) {
164+ if (input .getUpdateName ().equals ("createTask" )
165+ && Workflow .getInfo ()
166+ .getWorkflowType ()
167+ .equals (WorkflowTaskManager .class .getSimpleName ())) {
168+ createTaskInvocations ++;
169+ if (createTaskInvocations == 2 ) {
170+ waitUntilTwoInvocationsOfCreateTask .complete (null );
171+ }
172+
173+ if (createTaskInvocations == 3 ) {
174+ waitUntilThreeInvocationsOfCreateTask .complete (null );
175+ }
176+ }
177+
178+ return super .executeUpdate (input );
179+ }
180+ };
181+ }
182+ }
118183}
0 commit comments