|
| 1 | +package io.temporal.activity; |
| 2 | + |
| 3 | +import static org.junit.Assume.assumeTrue; |
| 4 | + |
| 5 | +import io.temporal.api.common.v1.WorkflowExecution; |
| 6 | +import io.temporal.api.workflow.v1.PendingActivityInfo; |
| 7 | +import io.temporal.api.workflowservice.v1.ResetActivityRequest; |
| 8 | +import io.temporal.client.ActivityResetException; |
| 9 | +import io.temporal.client.WorkflowStub; |
| 10 | +import io.temporal.common.converter.GlobalDataConverter; |
| 11 | +import io.temporal.testing.internal.SDKTestOptions; |
| 12 | +import io.temporal.testing.internal.SDKTestWorkflowRule; |
| 13 | +import io.temporal.workflow.Async; |
| 14 | +import io.temporal.workflow.Workflow; |
| 15 | +import io.temporal.workflow.shared.TestActivities; |
| 16 | +import io.temporal.workflow.shared.TestWorkflows; |
| 17 | +import java.time.Duration; |
| 18 | +import java.util.concurrent.atomic.AtomicInteger; |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.Rule; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +public class ActivityResetTest { |
| 24 | + |
| 25 | + @Rule |
| 26 | + public SDKTestWorkflowRule testWorkflowRule = |
| 27 | + SDKTestWorkflowRule.newBuilder() |
| 28 | + .setWorkflowTypes(TestWorkflowImpl.class) |
| 29 | + .setActivityImplementations(new HeartBeatingActivityImpl()) |
| 30 | + .build(); |
| 31 | + |
| 32 | + @Test |
| 33 | + public void activityReset() { |
| 34 | + assumeTrue( |
| 35 | + "Test Server doesn't support activity pause", SDKTestWorkflowRule.useExternalService); |
| 36 | + |
| 37 | + TestWorkflows.TestWorkflowReturnString workflow = |
| 38 | + testWorkflowRule.newWorkflowStub(TestWorkflows.TestWorkflowReturnString.class); |
| 39 | + Assert.assertEquals("I am stopped after reset", workflow.execute()); |
| 40 | + Assert.assertEquals( |
| 41 | + 1, |
| 42 | + WorkflowStub.fromTyped(workflow) |
| 43 | + .describe() |
| 44 | + .getRawDescription() |
| 45 | + .getPendingActivitiesCount()); |
| 46 | + PendingActivityInfo activityInfo = |
| 47 | + WorkflowStub.fromTyped(workflow).describe().getRawDescription().getPendingActivities(0); |
| 48 | + Assert.assertEquals( |
| 49 | + "1", |
| 50 | + GlobalDataConverter.get() |
| 51 | + .fromPayload( |
| 52 | + activityInfo.getHeartbeatDetails().getPayloads(0), String.class, String.class)); |
| 53 | + } |
| 54 | + |
| 55 | + public static class TestWorkflowImpl implements TestWorkflows.TestWorkflowReturnString { |
| 56 | + |
| 57 | + private final TestActivities.TestActivity1 activities = |
| 58 | + Workflow.newActivityStub( |
| 59 | + TestActivities.TestActivity1.class, |
| 60 | + SDKTestOptions.newActivityOptions20sScheduleToClose()); |
| 61 | + |
| 62 | + @Override |
| 63 | + public String execute() { |
| 64 | + Async.function(activities::execute, ""); |
| 65 | + Workflow.sleep(Duration.ofSeconds(1)); |
| 66 | + return activities.execute("CompleteOnPause"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public static class HeartBeatingActivityImpl implements TestActivities.TestActivity1 { |
| 71 | + private final AtomicInteger resetCounter = new AtomicInteger(0); |
| 72 | + |
| 73 | + @Override |
| 74 | + public String execute(String arg) { |
| 75 | + ActivityInfo info = Activity.getExecutionContext().getInfo(); |
| 76 | + // Have the activity pause itself |
| 77 | + Activity.getExecutionContext() |
| 78 | + .getWorkflowClient() |
| 79 | + .getWorkflowServiceStubs() |
| 80 | + .blockingStub() |
| 81 | + .resetActivity( |
| 82 | + ResetActivityRequest.newBuilder() |
| 83 | + .setNamespace(info.getNamespace()) |
| 84 | + .setExecution( |
| 85 | + WorkflowExecution.newBuilder() |
| 86 | + .setWorkflowId(info.getWorkflowId()) |
| 87 | + .setRunId(info.getRunId()) |
| 88 | + .build()) |
| 89 | + .setId(info.getActivityId()) |
| 90 | + .build()); |
| 91 | + while (true) { |
| 92 | + try { |
| 93 | + Thread.sleep(1000); |
| 94 | + // Check if the activity has been reset, and the activity info shows we are on the 1st |
| 95 | + // attempt. |
| 96 | + if (resetCounter.get() >= 1 |
| 97 | + && Activity.getExecutionContext().getInfo().getAttempt() == 1) { |
| 98 | + return "I am stopped after reset"; |
| 99 | + } |
| 100 | + // Heartbeat and verify that the correct exception is thrown |
| 101 | + Activity.getExecutionContext().heartbeat("1"); |
| 102 | + } catch (ActivityResetException pe) { |
| 103 | + // Counter is incremented to track the number of resets |
| 104 | + resetCounter.addAndGet(1); |
| 105 | + // This will fail the attempt, and the activity will be retried. |
| 106 | + throw pe; |
| 107 | + } catch (InterruptedException e) { |
| 108 | + throw new RuntimeException(e); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments