|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | + * |
| 6 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this material except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +package io.temporal.workflow.updateTest; |
| 22 | + |
| 23 | +import static org.junit.Assert.assertEquals; |
| 24 | +import static org.junit.Assert.assertThrows; |
| 25 | +import static org.junit.Assume.assumeTrue; |
| 26 | + |
| 27 | +import io.temporal.activity.ActivityOptions; |
| 28 | +import io.temporal.api.common.v1.WorkflowExecution; |
| 29 | +import io.temporal.client.*; |
| 30 | +import io.temporal.testing.internal.SDKTestOptions; |
| 31 | +import io.temporal.testing.internal.SDKTestWorkflowRule; |
| 32 | +import io.temporal.workflow.Async; |
| 33 | +import io.temporal.workflow.CompletablePromise; |
| 34 | +import io.temporal.workflow.Workflow; |
| 35 | +import io.temporal.workflow.shared.TestActivities; |
| 36 | +import io.temporal.workflow.shared.TestWorkflows.WorkflowWithUpdate; |
| 37 | +import java.time.Duration; |
| 38 | +import java.util.Optional; |
| 39 | +import java.util.Random; |
| 40 | +import java.util.UUID; |
| 41 | +import org.junit.Rule; |
| 42 | +import org.junit.Test; |
| 43 | + |
| 44 | +public class SpeculativeUpdateTest { |
| 45 | + @Rule |
| 46 | + public SDKTestWorkflowRule testWorkflowRule = |
| 47 | + SDKTestWorkflowRule.newBuilder() |
| 48 | + .setWorkflowTypes(TestUpdateWorkflowImpl.class) |
| 49 | + .setActivityImplementations(new TestActivities.TestActivitiesImpl()) |
| 50 | + .build(); |
| 51 | + |
| 52 | + @Test(timeout = 60000) |
| 53 | + public void speculativeUpdateRejected() { |
| 54 | + assumeTrue( |
| 55 | + "Test Server doesn't support speculative update yet", |
| 56 | + SDKTestWorkflowRule.useExternalService); |
| 57 | + |
| 58 | + String workflowId = UUID.randomUUID().toString(); |
| 59 | + WorkflowClient workflowClient = testWorkflowRule.getWorkflowClient(); |
| 60 | + WorkflowOptions options = |
| 61 | + SDKTestOptions.newWorkflowOptionsWithTimeouts(testWorkflowRule.getTaskQueue()).toBuilder() |
| 62 | + .setWorkflowId(workflowId) |
| 63 | + .build(); |
| 64 | + WorkflowWithUpdate workflow = workflowClient.newWorkflowStub(WorkflowWithUpdate.class, options); |
| 65 | + WorkflowExecution execution = WorkflowClient.start(workflow::execute); |
| 66 | + |
| 67 | + workflow.update(3, "test value"); |
| 68 | + // This update is going to be rejected, the resulting workflow task will not appear in history |
| 69 | + assertThrows(WorkflowUpdateException.class, () -> workflow.update(0, "reject")); |
| 70 | + |
| 71 | + assertThrows(WorkflowUpdateException.class, () -> workflow.update(0, "reject")); |
| 72 | + // Create more events to make sure the server persists the workflow tasks |
| 73 | + workflow.update(12, "test value"); |
| 74 | + // This update is going to be rejected, the resulting workflow task will appear in history |
| 75 | + assertThrows(WorkflowUpdateException.class, () -> workflow.update(0, "reject")); |
| 76 | + |
| 77 | + assertThrows(WorkflowUpdateException.class, () -> workflow.update(0, "reject")); |
| 78 | + |
| 79 | + workflow.complete(); |
| 80 | + String result = |
| 81 | + testWorkflowRule |
| 82 | + .getWorkflowClient() |
| 83 | + .newUntypedWorkflowStub(execution, Optional.empty()) |
| 84 | + .getResult(String.class); |
| 85 | + assertEquals("", result); |
| 86 | + } |
| 87 | + |
| 88 | + public static class TestUpdateWorkflowImpl implements WorkflowWithUpdate { |
| 89 | + String state = "initial"; |
| 90 | + CompletablePromise<Void> promise = Workflow.newPromise(); |
| 91 | + |
| 92 | + private final TestActivities.VariousTestActivities activities = |
| 93 | + Workflow.newActivityStub( |
| 94 | + TestActivities.VariousTestActivities.class, |
| 95 | + ActivityOptions.newBuilder() |
| 96 | + .setScheduleToCloseTimeout(Duration.ofSeconds(200)) |
| 97 | + .build()); |
| 98 | + |
| 99 | + @Override |
| 100 | + public String execute() { |
| 101 | + promise.get(); |
| 102 | + return ""; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public String getState() { |
| 107 | + return state; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String update(Integer index, String value) { |
| 112 | + Random random = Workflow.newRandom(); |
| 113 | + for (int i = 0; i <= index; i++) { |
| 114 | + int choice = random.nextInt(3); |
| 115 | + if (choice == 0) { |
| 116 | + Async.function(activities::sleepActivity, new Long(10000), 0); |
| 117 | + } else if (choice == 1) { |
| 118 | + Workflow.getVersion("test version " + i, Workflow.DEFAULT_VERSION, 1); |
| 119 | + } else { |
| 120 | + Workflow.newTimer(Duration.ofMillis(10)); |
| 121 | + } |
| 122 | + } |
| 123 | + return value; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void updateValidator(Integer index, String value) { |
| 128 | + if (value.equals("reject")) { |
| 129 | + throw new RuntimeException("Rejecting update"); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void complete() { |
| 135 | + promise.complete(null); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void completeValidator() {} |
| 140 | + } |
| 141 | +} |
0 commit comments