|
| 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.versionTests; |
| 22 | + |
| 23 | +import static org.junit.Assert.*; |
| 24 | + |
| 25 | +import io.temporal.activity.LocalActivityOptions; |
| 26 | +import io.temporal.testing.internal.SDKTestWorkflowRule; |
| 27 | +import io.temporal.worker.WorkerOptions; |
| 28 | +import io.temporal.workflow.Workflow; |
| 29 | +import io.temporal.workflow.shared.TestActivities; |
| 30 | +import io.temporal.workflow.shared.TestActivities.TestActivitiesImpl; |
| 31 | +import io.temporal.workflow.shared.TestWorkflows.TestWorkflow1; |
| 32 | +import io.temporal.workflow.unsafe.WorkflowUnsafe; |
| 33 | +import java.time.Duration; |
| 34 | +import org.junit.Rule; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +public class GetVersionRemovalBeforeMarkerTest { |
| 38 | + private static boolean hasReplayed; |
| 39 | + |
| 40 | + @Rule |
| 41 | + public SDKTestWorkflowRule testWorkflowRule = |
| 42 | + SDKTestWorkflowRule.newBuilder() |
| 43 | + .setWorkflowTypes(TestGetVersionRemovalWorkflowImpl.class) |
| 44 | + .setActivityImplementations(new TestActivitiesImpl()) |
| 45 | + // Forcing a replay. Full history arrived from a normal queue causing a replay. |
| 46 | + .setWorkerOptions( |
| 47 | + WorkerOptions.newBuilder() |
| 48 | + .setStickyQueueScheduleToStartTimeout(Duration.ZERO) |
| 49 | + .build()) |
| 50 | + .build(); |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testSideEffectAfterGetVersion() { |
| 54 | + TestWorkflow1 workflowStub = |
| 55 | + testWorkflowRule.newWorkflowStubTimeoutOptions(TestWorkflow1.class); |
| 56 | + String result = workflowStub.execute("SideEffect"); |
| 57 | + assertTrue(hasReplayed); |
| 58 | + assertEquals("side effect", result); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testMutableSideEffectAfterGetVersion() { |
| 63 | + TestWorkflow1 workflowStub = |
| 64 | + testWorkflowRule.newWorkflowStubTimeoutOptions(TestWorkflow1.class); |
| 65 | + String result = workflowStub.execute("MutableSideEffect"); |
| 66 | + assertTrue(hasReplayed); |
| 67 | + assertEquals("mutable side effect", result); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testGetVersionAfterGetVersion() { |
| 72 | + TestWorkflow1 workflowStub = |
| 73 | + testWorkflowRule.newWorkflowStubTimeoutOptions(TestWorkflow1.class); |
| 74 | + String result = workflowStub.execute("GetVersion"); |
| 75 | + assertTrue(hasReplayed); |
| 76 | + assertEquals("6", result); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testLocalActivityAfterGetVersion() { |
| 81 | + TestWorkflow1 workflowStub = |
| 82 | + testWorkflowRule.newWorkflowStubTimeoutOptions(TestWorkflow1.class); |
| 83 | + String result = workflowStub.execute("LocalActivity"); |
| 84 | + assertTrue(hasReplayed); |
| 85 | + assertEquals("activity", result); |
| 86 | + } |
| 87 | + |
| 88 | + public static class TestGetVersionRemovalWorkflowImpl implements TestWorkflow1 { |
| 89 | + private final TestActivities.VariousTestActivities activities = |
| 90 | + Workflow.newLocalActivityStub( |
| 91 | + TestActivities.VariousTestActivities.class, |
| 92 | + LocalActivityOptions.newBuilder() |
| 93 | + .setStartToCloseTimeout(Duration.ofSeconds(5)) |
| 94 | + .build()); |
| 95 | + |
| 96 | + @Override |
| 97 | + public String execute(String action) { |
| 98 | + // Test removing a version check in replaying code with an additional thread running. |
| 99 | + if (!WorkflowUnsafe.isReplaying()) { |
| 100 | + int version = Workflow.getVersion("changeId", 1, 2); |
| 101 | + assertEquals(version, 2); |
| 102 | + } else { |
| 103 | + hasReplayed = true; |
| 104 | + } |
| 105 | + String result = ""; |
| 106 | + if (action.equals("SideEffect")) { |
| 107 | + result = Workflow.sideEffect(String.class, () -> "side effect"); |
| 108 | + } else if (action.equals("MutableSideEffect")) { |
| 109 | + result = |
| 110 | + Workflow.mutableSideEffect( |
| 111 | + "mutable-side-effect-i", |
| 112 | + String.class, |
| 113 | + (a, b) -> !a.equals(b), |
| 114 | + () -> "mutable side effect"); |
| 115 | + } else if (action.equals("GetVersion")) { |
| 116 | + int v = Workflow.getVersion("otherChangeId", 5, 6); |
| 117 | + result = String.valueOf(v); |
| 118 | + } else if (action.equals("LocalActivity")) { |
| 119 | + result = activities.activity(); |
| 120 | + } |
| 121 | + // Sleep to trigger at lest one more workflow task |
| 122 | + Workflow.sleep(Duration.ofSeconds(1)); |
| 123 | + return result; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments