|
| 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 static java.util.UUID.randomUUID; |
| 24 | +import static org.junit.Assert.assertEquals; |
| 25 | +import static org.junit.Assert.assertTrue; |
| 26 | + |
| 27 | +import io.temporal.api.common.v1.Link; |
| 28 | +import io.temporal.api.common.v1.Payloads; |
| 29 | +import io.temporal.api.common.v1.WorkflowExecution; |
| 30 | +import io.temporal.api.common.v1.WorkflowType; |
| 31 | +import io.temporal.api.enums.v1.EventType; |
| 32 | +import io.temporal.api.history.v1.HistoryEvent; |
| 33 | +import io.temporal.api.history.v1.WorkflowExecutionSignaledEventAttributes; |
| 34 | +import io.temporal.api.taskqueue.v1.TaskQueue; |
| 35 | +import io.temporal.api.workflowservice.v1.*; |
| 36 | +import io.temporal.client.WorkflowStub; |
| 37 | +import io.temporal.testing.internal.SDKTestWorkflowRule; |
| 38 | +import io.temporal.workflow.WorkflowInterface; |
| 39 | +import io.temporal.workflow.WorkflowMethod; |
| 40 | +import org.junit.Rule; |
| 41 | +import org.junit.Test; |
| 42 | + |
| 43 | +public class SignalLinksTest { |
| 44 | + @Rule |
| 45 | + public SDKTestWorkflowRule testWorkflowRule = |
| 46 | + SDKTestWorkflowRule.newBuilder().setWorkflowTypes(TestWorkflowImpl.class).build(); |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testSignalWithLinks() { |
| 50 | + WorkflowStub stub = testWorkflowRule.newUntypedWorkflowStubTimeoutOptions("TestWorkflow"); |
| 51 | + WorkflowExecution execution = stub.start(); |
| 52 | + |
| 53 | + Link testLink = createTestLink(execution.getRunId()); |
| 54 | + SignalWorkflowExecutionRequest signalRequest = |
| 55 | + SignalWorkflowExecutionRequest.newBuilder() |
| 56 | + .setNamespace(testWorkflowRule.getWorkflowClient().getOptions().getNamespace()) |
| 57 | + .setWorkflowExecution(execution) |
| 58 | + .setSignalName("test-signal") |
| 59 | + .setInput(Payloads.newBuilder().build()) |
| 60 | + .addLinks(testLink) |
| 61 | + .build(); |
| 62 | + |
| 63 | + testWorkflowRule |
| 64 | + .getWorkflowServiceStubs() |
| 65 | + .blockingStub() |
| 66 | + .signalWorkflowExecution(signalRequest); |
| 67 | + |
| 68 | + stub.getResult(Void.class); |
| 69 | + |
| 70 | + verifySignalLink(execution, testLink); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testSignalWithStartLinks() { |
| 75 | + String workflowId = "test-workflow-id"; |
| 76 | + Link testLink = createTestLink("some-run-id"); |
| 77 | + |
| 78 | + SignalWithStartWorkflowExecutionRequest signalWithStartRequest = |
| 79 | + SignalWithStartWorkflowExecutionRequest.newBuilder() |
| 80 | + .setTaskQueue(TaskQueue.newBuilder().setName(testWorkflowRule.getTaskQueue()).build()) |
| 81 | + .setNamespace(testWorkflowRule.getWorkflowClient().getOptions().getNamespace()) |
| 82 | + .setWorkflowType(WorkflowType.newBuilder().setName("TestWorkflow").build()) |
| 83 | + .setSignalInput(Payloads.newBuilder().build()) |
| 84 | + .setRequestId(randomUUID().toString()) |
| 85 | + .setSignalName("test-signal") |
| 86 | + .setWorkflowId(workflowId) |
| 87 | + .addLinks(testLink) |
| 88 | + .build(); |
| 89 | + |
| 90 | + SignalWithStartWorkflowExecutionResponse response = |
| 91 | + testWorkflowRule |
| 92 | + .getWorkflowServiceStubs() |
| 93 | + .blockingStub() |
| 94 | + .signalWithStartWorkflowExecution(signalWithStartRequest); |
| 95 | + |
| 96 | + WorkflowExecution execution = |
| 97 | + WorkflowExecution.newBuilder() |
| 98 | + .setWorkflowId(workflowId) |
| 99 | + .setRunId(response.getRunId()) |
| 100 | + .build(); |
| 101 | + |
| 102 | + verifySignalLink(execution, testLink); |
| 103 | + verifyStartEventLink(execution, testLink); |
| 104 | + } |
| 105 | + |
| 106 | + private Link createTestLink(String runId) { |
| 107 | + return Link.newBuilder() |
| 108 | + .setWorkflowEvent( |
| 109 | + Link.WorkflowEvent.newBuilder() |
| 110 | + .setWorkflowId("someWorkflow") |
| 111 | + .setNamespace("default") |
| 112 | + .setRunId(runId) |
| 113 | + .build()) |
| 114 | + .build(); |
| 115 | + } |
| 116 | + |
| 117 | + private void verifySignalLink(WorkflowExecution execution, Link expectedLink) { |
| 118 | + GetWorkflowExecutionHistoryResponse history = getHistory(execution); |
| 119 | + boolean foundSignalWithLink = false; |
| 120 | + |
| 121 | + for (HistoryEvent event : history.getHistory().getEventsList()) { |
| 122 | + if (event.getEventType() == EventType.EVENT_TYPE_WORKFLOW_EXECUTION_SIGNALED) { |
| 123 | + WorkflowExecutionSignaledEventAttributes attrs = |
| 124 | + event.getWorkflowExecutionSignaledEventAttributes(); |
| 125 | + if ("test-signal".equals(attrs.getSignalName())) { |
| 126 | + assertEquals(1, event.getLinksCount()); |
| 127 | + assertEquals(expectedLink, event.getLinks(0)); |
| 128 | + foundSignalWithLink = true; |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + assertTrue("Should have found signal event with link", foundSignalWithLink); |
| 135 | + } |
| 136 | + |
| 137 | + private void verifyStartEventLink(WorkflowExecution execution, Link expectedLink) { |
| 138 | + GetWorkflowExecutionHistoryResponse history = getHistory(execution); |
| 139 | + boolean foundStartWithLink = false; |
| 140 | + |
| 141 | + for (HistoryEvent event : history.getHistory().getEventsList()) { |
| 142 | + if (event.getEventType() == EventType.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED) { |
| 143 | + assertEquals("Link should be present on start event", 1, event.getLinksCount()); |
| 144 | + assertEquals("Link in start event should match", expectedLink, event.getLinks(0)); |
| 145 | + foundStartWithLink = true; |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + assertTrue("Should have found start event with link", foundStartWithLink); |
| 151 | + } |
| 152 | + |
| 153 | + private GetWorkflowExecutionHistoryResponse getHistory(WorkflowExecution execution) { |
| 154 | + return testWorkflowRule |
| 155 | + .getWorkflowServiceStubs() |
| 156 | + .blockingStub() |
| 157 | + .getWorkflowExecutionHistory( |
| 158 | + GetWorkflowExecutionHistoryRequest.newBuilder() |
| 159 | + .setNamespace(testWorkflowRule.getWorkflowClient().getOptions().getNamespace()) |
| 160 | + .setExecution(execution) |
| 161 | + .build()); |
| 162 | + } |
| 163 | + |
| 164 | + @WorkflowInterface |
| 165 | + public interface TestWorkflow { |
| 166 | + @WorkflowMethod(name = "TestWorkflow") |
| 167 | + void run(); |
| 168 | + } |
| 169 | + |
| 170 | + public static class TestWorkflowImpl implements TestWorkflow { |
| 171 | + @Override |
| 172 | + public void run() { |
| 173 | + // Empty workflow that completes quickly |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments