|
| 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.testserver.functional; |
| 22 | + |
| 23 | +import io.temporal.api.common.v1.WorkflowExecution; |
| 24 | +import io.temporal.client.WorkflowOptions; |
| 25 | +import io.temporal.client.WorkflowStub; |
| 26 | +import io.temporal.common.WorkflowExecutionHistory; |
| 27 | +import io.temporal.common.interceptors.*; |
| 28 | +import io.temporal.testing.internal.SDKTestWorkflowRule; |
| 29 | +import io.temporal.testserver.functional.common.TestWorkflows; |
| 30 | +import io.temporal.worker.WorkerFactoryOptions; |
| 31 | +import io.temporal.workflow.ContinueAsNewOptions; |
| 32 | +import io.temporal.workflow.Workflow; |
| 33 | +import java.time.Duration; |
| 34 | +import org.junit.Assert; |
| 35 | +import org.junit.Rule; |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +public class ContinueAsNewTest { |
| 39 | + |
| 40 | + @Rule |
| 41 | + public SDKTestWorkflowRule testWorkflowRule = |
| 42 | + SDKTestWorkflowRule.newBuilder() |
| 43 | + .setWorkerFactoryOptions( |
| 44 | + WorkerFactoryOptions.newBuilder() |
| 45 | + .setWorkerInterceptors(new StripsTqFromCanInterceptor()) |
| 46 | + .build()) |
| 47 | + .setWorkflowTypes(TestWorkflow.class) |
| 48 | + .build(); |
| 49 | + |
| 50 | + @Test |
| 51 | + public void repeatedFailure() { |
| 52 | + WorkflowOptions options = |
| 53 | + WorkflowOptions.newBuilder() |
| 54 | + .setWorkflowTaskTimeout(Duration.ofSeconds(1)) |
| 55 | + .setTaskQueue(testWorkflowRule.getTaskQueue()) |
| 56 | + .build(); |
| 57 | + |
| 58 | + TestWorkflows.WorkflowTakesBool workflowStub = |
| 59 | + testWorkflowRule |
| 60 | + .getWorkflowClient() |
| 61 | + .newWorkflowStub(TestWorkflows.WorkflowTakesBool.class, options); |
| 62 | + workflowStub.execute(true); |
| 63 | + |
| 64 | + WorkflowExecution execution = WorkflowStub.fromTyped(workflowStub).getExecution(); |
| 65 | + // Verify the latest history is from being CaN'd |
| 66 | + WorkflowExecutionHistory lastHist = |
| 67 | + testWorkflowRule.getExecutionHistory(execution.getWorkflowId()); |
| 68 | + Assert.assertFalse( |
| 69 | + lastHist |
| 70 | + .getEvents() |
| 71 | + .get(0) |
| 72 | + .getWorkflowExecutionStartedEventAttributes() |
| 73 | + .getFirstExecutionRunId() |
| 74 | + .isEmpty()); |
| 75 | + } |
| 76 | + |
| 77 | + public static class TestWorkflow implements TestWorkflows.WorkflowTakesBool { |
| 78 | + @Override |
| 79 | + public void execute(boolean doContinue) { |
| 80 | + if (doContinue) { |
| 81 | + Workflow.continueAsNew(false); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Verify that we can strip the TQ name and test server continues onto same TQ |
| 87 | + private static class StripsTqFromCanInterceptor extends WorkerInterceptorBase { |
| 88 | + @Override |
| 89 | + public WorkflowInboundCallsInterceptor interceptWorkflow(WorkflowInboundCallsInterceptor next) { |
| 90 | + return new WorkflowInboundCallsInterceptorBase(next) { |
| 91 | + @Override |
| 92 | + public void init(WorkflowOutboundCallsInterceptor outboundCalls) { |
| 93 | + next.init(new StripsTqFromCanCmd(outboundCalls)); |
| 94 | + } |
| 95 | + }; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static class StripsTqFromCanCmd extends WorkflowOutboundCallsInterceptorBase { |
| 100 | + |
| 101 | + private final WorkflowOutboundCallsInterceptor next; |
| 102 | + |
| 103 | + public StripsTqFromCanCmd(WorkflowOutboundCallsInterceptor next) { |
| 104 | + super(next); |
| 105 | + this.next = next; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void continueAsNew(ContinueAsNewInput input) { |
| 110 | + next.continueAsNew( |
| 111 | + new ContinueAsNewInput( |
| 112 | + input.getWorkflowType(), |
| 113 | + ContinueAsNewOptions.newBuilder(input.getOptions()).setTaskQueue("").build(), |
| 114 | + input.getArgs(), |
| 115 | + input.getHeader())); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments