|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved |
| 3 | + * |
| 4 | + * Copyright 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"). You may not |
| 9 | + * use this file except in compliance with the License. A copy of the License is |
| 10 | + * located at |
| 11 | + * |
| 12 | + * http://aws.amazon.com/apache2.0 |
| 13 | + * |
| 14 | + * or in the "license" file accompanying this file. This file is distributed on |
| 15 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 16 | + * express or implied. See the License for the specific language governing |
| 17 | + * permissions and limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package io.temporal.samples.hello; |
| 21 | + |
| 22 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 23 | + |
| 24 | +import io.temporal.activity.ActivityOptions; |
| 25 | +import io.temporal.api.common.v1.WorkflowExecution; |
| 26 | +import io.temporal.client.WorkflowOptions; |
| 27 | +import io.temporal.client.WorkflowStub; |
| 28 | +import io.temporal.internal.common.WorkflowExecutionHistory; |
| 29 | +import io.temporal.testing.TestWorkflowRule; |
| 30 | +import io.temporal.testing.WorkflowReplayer; |
| 31 | +import io.temporal.workflow.Workflow; |
| 32 | +import java.time.Duration; |
| 33 | +import org.hamcrest.CoreMatchers; |
| 34 | +import org.junit.Assert; |
| 35 | +import org.junit.Rule; |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +/** |
| 39 | + * Unit test for replay {@link HelloActivity.GreetingWorkflowImpl}. Doesn't use an external Temporal |
| 40 | + * service. |
| 41 | + */ |
| 42 | +public class HelloActivityReplayTest { |
| 43 | + |
| 44 | + @Rule |
| 45 | + public TestWorkflowRule testWorkflowRule = |
| 46 | + TestWorkflowRule.newBuilder().setDoNotStart(true).build(); |
| 47 | + |
| 48 | + @Test |
| 49 | + public void replayWorkflowExecution() throws Exception { |
| 50 | + |
| 51 | + final String eventHistory = executeWorkflow(HelloActivity.GreetingWorkflowImpl.class); |
| 52 | + |
| 53 | + WorkflowReplayer.replayWorkflowExecution( |
| 54 | + eventHistory, HelloActivity.GreetingWorkflowImpl.class); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void replayWorkflowExecutionNonDeterministic() { |
| 59 | + |
| 60 | + // We are executing the workflow with one implementation (GreetingWorkflowImplTest) and trying |
| 61 | + // to replay the even history with a different implementation (GreetingWorkflowImpl), |
| 62 | + // which causes an exception during the replay |
| 63 | + |
| 64 | + try { |
| 65 | + |
| 66 | + final String eventHistory = executeWorkflow(GreetingWorkflowImplTest.class); |
| 67 | + |
| 68 | + WorkflowReplayer.replayWorkflowExecution( |
| 69 | + eventHistory, HelloActivity.GreetingWorkflowImpl.class); |
| 70 | + |
| 71 | + Assert.fail("Should have thrown an Exception"); |
| 72 | + } catch (Exception e) { |
| 73 | + assertThat( |
| 74 | + e.getMessage(), |
| 75 | + CoreMatchers.containsString("error=io.temporal.worker.NonDeterministicException")); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private String executeWorkflow( |
| 80 | + Class<? extends HelloActivity.GreetingWorkflow> workflowImplementationType) { |
| 81 | + |
| 82 | + testWorkflowRule |
| 83 | + .getWorker() |
| 84 | + .registerActivitiesImplementations(new HelloActivity.GreetingActivitiesImpl()); |
| 85 | + |
| 86 | + testWorkflowRule.getWorker().registerWorkflowImplementationTypes(workflowImplementationType); |
| 87 | + |
| 88 | + testWorkflowRule.getTestEnvironment().start(); |
| 89 | + |
| 90 | + HelloActivity.GreetingWorkflow workflow = |
| 91 | + testWorkflowRule |
| 92 | + .getWorkflowClient() |
| 93 | + .newWorkflowStub( |
| 94 | + HelloActivity.GreetingWorkflow.class, |
| 95 | + WorkflowOptions.newBuilder().setTaskQueue(testWorkflowRule.getTaskQueue()).build()); |
| 96 | + WorkflowExecution execution = WorkflowStub.fromTyped(workflow).start("Hello"); |
| 97 | + // wait until workflow completes |
| 98 | + WorkflowStub.fromTyped(workflow).getResult(String.class); |
| 99 | + |
| 100 | + return new WorkflowExecutionHistory(testWorkflowRule.getHistory(execution)).toJson(true); |
| 101 | + } |
| 102 | + |
| 103 | + public static class GreetingWorkflowImplTest implements HelloActivity.GreetingWorkflow { |
| 104 | + |
| 105 | + private final HelloActivity.GreetingActivities activities = |
| 106 | + Workflow.newActivityStub( |
| 107 | + HelloActivity.GreetingActivities.class, |
| 108 | + ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(2)).build()); |
| 109 | + |
| 110 | + @Override |
| 111 | + public String getGreeting(String name) { |
| 112 | + Workflow.sleep(100); |
| 113 | + return activities.composeGreeting("Hello", name); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments