|
27 | 27 | import static org.junit.Assert.fail;
|
28 | 28 |
|
29 | 29 | import io.temporal.api.command.v1.Command;
|
| 30 | +import io.temporal.api.command.v1.StartChildWorkflowExecutionCommandAttributes; |
30 | 31 | import io.temporal.api.common.v1.ActivityType;
|
31 | 32 | import io.temporal.api.common.v1.Payloads;
|
| 33 | +import io.temporal.api.common.v1.WorkflowExecution; |
32 | 34 | import io.temporal.api.enums.v1.CommandType;
|
33 | 35 | import io.temporal.api.enums.v1.EventType;
|
34 |
| -import io.temporal.api.history.v1.MarkerRecordedEventAttributes; |
| 36 | +import io.temporal.api.history.v1.*; |
35 | 37 | import io.temporal.api.workflowservice.v1.PollActivityTaskQueueResponse;
|
36 | 38 | import io.temporal.api.workflowservice.v1.RespondActivityTaskCompletedRequest;
|
37 | 39 | import io.temporal.common.converter.DataConverter;
|
38 | 40 | import io.temporal.common.converter.DefaultDataConverter;
|
39 | 41 | import io.temporal.internal.history.LocalActivityMarkerUtils;
|
40 | 42 | import io.temporal.internal.worker.LocalActivityResult;
|
| 43 | +import io.temporal.workflow.ChildWorkflowCancellationType; |
| 44 | +import io.temporal.workflow.Functions; |
41 | 45 | import java.util.ArrayList;
|
42 | 46 | import java.util.List;
|
43 | 47 | import java.util.Optional;
|
| 48 | +import java.util.concurrent.atomic.AtomicReference; |
44 | 49 | import org.junit.AfterClass;
|
45 | 50 | import org.junit.Test;
|
46 | 51 |
|
@@ -343,4 +348,99 @@ protected void buildWorkflow(AsyncWorkflowBuilder<Void> builder) {
|
343 | 348 | List<Command> commands = stateMachines.takeCommands();
|
344 | 349 | assertTrue(commands.isEmpty());
|
345 | 350 | }
|
| 351 | + |
| 352 | + @Test |
| 353 | + public void testLocalActivityStateMachineDuplicateTask() { |
| 354 | + class TestListener extends TestEntityManagerListenerBase { |
| 355 | + @Override |
| 356 | + protected void buildWorkflow(AsyncWorkflowBuilder<Void> builder) { |
| 357 | + StartChildWorkflowExecutionParameters childRequest = |
| 358 | + new StartChildWorkflowExecutionParameters( |
| 359 | + StartChildWorkflowExecutionCommandAttributes.newBuilder(), |
| 360 | + ChildWorkflowCancellationType.WAIT_CANCELLATION_REQUESTED); |
| 361 | + ExecuteLocalActivityParameters parameters1 = |
| 362 | + new ExecuteLocalActivityParameters( |
| 363 | + PollActivityTaskQueueResponse.newBuilder() |
| 364 | + .setActivityId("id1") |
| 365 | + .setActivityType(ActivityType.newBuilder().setName("activity1")), |
| 366 | + null, |
| 367 | + System.currentTimeMillis(), |
| 368 | + null, |
| 369 | + false, |
| 370 | + null); |
| 371 | + // TODO: This is a workaround for the lack of support for child workflow in the test |
| 372 | + // framework. |
| 373 | + // The test framework has no support for state machines with multiple callbacks. |
| 374 | + AtomicReference<Functions.Proc> cc = new AtomicReference<>(); |
| 375 | + AtomicReference<Functions.Proc2<Optional<Payloads>, Exception>> completionCallback = |
| 376 | + new AtomicReference<>(); |
| 377 | + builder |
| 378 | + .<WorkflowExecution, Exception>add2( |
| 379 | + (r, c) -> |
| 380 | + cc.set( |
| 381 | + stateMachines.startChildWorkflow( |
| 382 | + childRequest, |
| 383 | + c, |
| 384 | + (r1, c1) -> { |
| 385 | + completionCallback.get().apply(r1, c1); |
| 386 | + }))) |
| 387 | + .add((r) -> cc.get().apply()) |
| 388 | + .<Optional<Payloads>, Exception>add2( |
| 389 | + (r, c) -> { |
| 390 | + completionCallback.set(c); |
| 391 | + }) |
| 392 | + .<Optional<Payloads>, LocalActivityCallback.LocalActivityFailedException>add2( |
| 393 | + (r, c) -> stateMachines.scheduleLocalActivityTask(parameters1, c)); |
| 394 | + } |
| 395 | + } |
| 396 | + /* |
| 397 | + 1: EVENT_TYPE_WORKFLOW_EXECUTION_STARTED |
| 398 | + 2: EVENT_TYPE_WORKFLOW_TASK_SCHEDULED |
| 399 | + 3: EVENT_TYPE_WORKFLOW_TASK_STARTED |
| 400 | + 4: EVENT_TYPE_WORKFLOW_TASK_COMPLETED |
| 401 | + 5: EVENT_TYPE_START_CHILD_WORKFLOW_EXECUTION_INITIATED |
| 402 | + 6: EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_STARTED |
| 403 | + 7: EVENT_TYPE_WORKFLOW_TASK_SCHEDULED |
| 404 | + 8: EVENT_TYPE_WORKFLOW_TASK_STARTED |
| 405 | + 9: EVENT_TYPE_WORKFLOW_TASK_COMPLETED |
| 406 | + 10: EVENT_TYPE_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION_INITIATED |
| 407 | + 11: EVENT_TYPE_EXTERNAL_WORKFLOW_EXECUTION_CANCEL_REQUESTED |
| 408 | + 12: EVENT_TYPE_WORKFLOW_TASK_SCHEDULED |
| 409 | + 13: EVENT_TYPE_WORKFLOW_TASK_STARTED |
| 410 | + */ |
| 411 | + TestHistoryBuilder h = |
| 412 | + new TestHistoryBuilder() |
| 413 | + .add(EventType.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED) |
| 414 | + .addWorkflowTask() |
| 415 | + .add( |
| 416 | + EventType.EVENT_TYPE_START_CHILD_WORKFLOW_EXECUTION_INITIATED, |
| 417 | + StartChildWorkflowExecutionInitiatedEventAttributes.newBuilder().build()) |
| 418 | + .add( |
| 419 | + EventType.EVENT_TYPE_CHILD_WORKFLOW_EXECUTION_STARTED, |
| 420 | + ChildWorkflowExecutionStartedEventAttributes.newBuilder() |
| 421 | + .setInitiatedEventId(5) |
| 422 | + .build()) |
| 423 | + .addWorkflowTask() |
| 424 | + .add( |
| 425 | + EventType.EVENT_TYPE_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION_INITIATED, |
| 426 | + RequestCancelExternalWorkflowExecutionInitiatedEventAttributes.newBuilder().build()) |
| 427 | + .addWorkflowTaskScheduled() |
| 428 | + .add( |
| 429 | + EventType.EVENT_TYPE_EXTERNAL_WORKFLOW_EXECUTION_CANCEL_REQUESTED, |
| 430 | + ExternalWorkflowExecutionCancelRequestedEventAttributes.newBuilder() |
| 431 | + .setInitiatedEventId(10) |
| 432 | + .build()) |
| 433 | + .addWorkflowTaskScheduled() |
| 434 | + .addWorkflowTaskStarted(); |
| 435 | + |
| 436 | + TestListener listener = new TestListener(); |
| 437 | + stateMachines = newStateMachines(listener); |
| 438 | + |
| 439 | + h.handleWorkflowTask(stateMachines); |
| 440 | + List<ExecuteLocalActivityParameters> requests = stateMachines.takeLocalActivityRequests(); |
| 441 | + assertEquals(1, requests.size()); |
| 442 | + assertEquals("id1", requests.get(0).getActivityId()); |
| 443 | + List<Command> commands = stateMachines.takeCommands(); |
| 444 | + assertTrue(commands.isEmpty()); |
| 445 | + } |
346 | 446 | }
|
0 commit comments