|
| 1 | +package io.temporal.workflow.cancellationTests; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | + |
| 6 | +import io.temporal.client.WorkflowClient; |
| 7 | +import io.temporal.client.WorkflowFailedException; |
| 8 | +import io.temporal.client.WorkflowOptions; |
| 9 | +import io.temporal.client.WorkflowStub; |
| 10 | +import io.temporal.failure.CanceledFailure; |
| 11 | +import io.temporal.testing.internal.SDKTestWorkflowRule; |
| 12 | +import io.temporal.workflow.CancellationScope; |
| 13 | +import io.temporal.workflow.ExternalWorkflowStub; |
| 14 | +import io.temporal.workflow.Promise; |
| 15 | +import io.temporal.workflow.QueryMethod; |
| 16 | +import io.temporal.workflow.Workflow; |
| 17 | +import io.temporal.workflow.WorkflowInterface; |
| 18 | +import io.temporal.workflow.WorkflowMethod; |
| 19 | +import java.util.UUID; |
| 20 | +import org.junit.Rule; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +public class WorkflowCancelReasonTest { |
| 24 | + |
| 25 | + @Rule |
| 26 | + public SDKTestWorkflowRule testWorkflowRule = |
| 27 | + SDKTestWorkflowRule.newBuilder() |
| 28 | + .setUseTimeskipping(false) |
| 29 | + .setWorkflowTypes( |
| 30 | + CancellationAwareWorkflow.class, |
| 31 | + CancelExternalWorkflowImpl.class, |
| 32 | + CascadingCancelWorkflowImpl.class) |
| 33 | + .build(); |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testCancellationReasonFromClient() { |
| 37 | + String workflowId = "client-cancel-" + UUID.randomUUID(); |
| 38 | + CancellationReasonWorkflow workflow = |
| 39 | + testWorkflowRule |
| 40 | + .getWorkflowClient() |
| 41 | + .newWorkflowStub( |
| 42 | + CancellationReasonWorkflow.class, |
| 43 | + WorkflowOptions.newBuilder() |
| 44 | + .setWorkflowId(workflowId) |
| 45 | + .setTaskQueue(testWorkflowRule.getTaskQueue()) |
| 46 | + .build()); |
| 47 | + |
| 48 | + WorkflowClient.start(workflow::execute); |
| 49 | + |
| 50 | + WorkflowStub stub = WorkflowStub.fromTyped(workflow); |
| 51 | + String reason = "client-cancel-reason"; |
| 52 | + stub.cancel(reason); |
| 53 | + |
| 54 | + WorkflowFailedException exception = |
| 55 | + assertThrows(WorkflowFailedException.class, () -> stub.getResult(String.class)); |
| 56 | + assertEquals(CanceledFailure.class, exception.getCause().getClass()); |
| 57 | + |
| 58 | + assertEquals(expectedResult(reason), workflow.getRecordedReason()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testCancellationReasonFromCommand() { |
| 63 | + String targetWorkflowId = "command-cancel-" + UUID.randomUUID(); |
| 64 | + CancellationReasonWorkflow targetWorkflow = |
| 65 | + testWorkflowRule |
| 66 | + .getWorkflowClient() |
| 67 | + .newWorkflowStub( |
| 68 | + CancellationReasonWorkflow.class, |
| 69 | + WorkflowOptions.newBuilder() |
| 70 | + .setWorkflowId(targetWorkflowId) |
| 71 | + .setTaskQueue(testWorkflowRule.getTaskQueue()) |
| 72 | + .build()); |
| 73 | + |
| 74 | + WorkflowClient.start(targetWorkflow::execute); |
| 75 | + |
| 76 | + CancelExternalWorkflow canceller = |
| 77 | + testWorkflowRule.newWorkflowStub(CancelExternalWorkflow.class); |
| 78 | + String reason = "command-cancel-reason"; |
| 79 | + WorkflowClient.start(canceller::execute, targetWorkflowId, reason); |
| 80 | + |
| 81 | + WorkflowStub targetStub = WorkflowStub.fromTyped(targetWorkflow); |
| 82 | + WorkflowFailedException exception = |
| 83 | + assertThrows(WorkflowFailedException.class, () -> targetStub.getResult(String.class)); |
| 84 | + assertEquals(CanceledFailure.class, exception.getCause().getClass()); |
| 85 | + |
| 86 | + assertEquals(expectedResult(reason), targetWorkflow.getRecordedReason()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testCancellationReasonAbsent() { |
| 91 | + String workflowId = "client-cancel-null-" + UUID.randomUUID(); |
| 92 | + CancellationReasonWorkflow workflow = |
| 93 | + testWorkflowRule |
| 94 | + .getWorkflowClient() |
| 95 | + .newWorkflowStub( |
| 96 | + CancellationReasonWorkflow.class, |
| 97 | + WorkflowOptions.newBuilder() |
| 98 | + .setWorkflowId(workflowId) |
| 99 | + .setTaskQueue(testWorkflowRule.getTaskQueue()) |
| 100 | + .build()); |
| 101 | + |
| 102 | + WorkflowClient.start(workflow::execute); |
| 103 | + |
| 104 | + WorkflowStub stub = WorkflowStub.fromTyped(workflow); |
| 105 | + stub.cancel(); |
| 106 | + |
| 107 | + WorkflowFailedException exception = |
| 108 | + assertThrows(WorkflowFailedException.class, () -> stub.getResult(String.class)); |
| 109 | + assertEquals(CanceledFailure.class, exception.getCause().getClass()); |
| 110 | + |
| 111 | + assertEquals(expectedResult(""), workflow.getRecordedReason()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testCancellationReasonDerivedFromContext() { |
| 116 | + String targetWorkflowId = "context-derived-" + UUID.randomUUID(); |
| 117 | + CancellationReasonWorkflow targetWorkflow = |
| 118 | + testWorkflowRule |
| 119 | + .getWorkflowClient() |
| 120 | + .newWorkflowStub( |
| 121 | + CancellationReasonWorkflow.class, |
| 122 | + WorkflowOptions.newBuilder() |
| 123 | + .setWorkflowId(targetWorkflowId) |
| 124 | + .setTaskQueue(testWorkflowRule.getTaskQueue()) |
| 125 | + .build()); |
| 126 | + |
| 127 | + WorkflowClient.start(targetWorkflow::execute); |
| 128 | + |
| 129 | + CascadingCancelWorkflow cascaderWorkflow = |
| 130 | + testWorkflowRule.newWorkflowStub(CascadingCancelWorkflow.class); |
| 131 | + |
| 132 | + WorkflowClient.start(cascaderWorkflow::execute, targetWorkflowId); |
| 133 | + |
| 134 | + WorkflowStub cascaderStub = WorkflowStub.fromTyped(cascaderWorkflow); |
| 135 | + String reason = "context-derived-reason"; |
| 136 | + cascaderStub.cancel(reason); |
| 137 | + |
| 138 | + WorkflowStub targetStub = WorkflowStub.fromTyped(targetWorkflow); |
| 139 | + WorkflowFailedException exception = |
| 140 | + assertThrows(WorkflowFailedException.class, () -> targetStub.getResult(String.class)); |
| 141 | + assertEquals(CanceledFailure.class, exception.getCause().getClass()); |
| 142 | + |
| 143 | + assertEquals(expectedResult(reason), targetWorkflow.getRecordedReason()); |
| 144 | + } |
| 145 | + |
| 146 | + private static String expectedResult(String reason) { |
| 147 | + String part = String.valueOf(reason); |
| 148 | + return part + "|" + part; |
| 149 | + } |
| 150 | + |
| 151 | + @WorkflowInterface |
| 152 | + public interface CancellationReasonWorkflow { |
| 153 | + @WorkflowMethod |
| 154 | + String execute(); |
| 155 | + |
| 156 | + @QueryMethod |
| 157 | + String getRecordedReason(); |
| 158 | + } |
| 159 | + |
| 160 | + public static class CancellationAwareWorkflow implements CancellationReasonWorkflow { |
| 161 | + |
| 162 | + private String recordedReason; |
| 163 | + |
| 164 | + @Override |
| 165 | + public String execute() { |
| 166 | + Promise<String> cancellationRequest = CancellationScope.current().getCancellationRequest(); |
| 167 | + String requestedReason = cancellationRequest.get(); |
| 168 | + String scopeReason = CancellationScope.current().getCancellationReason(); |
| 169 | + recordedReason = expectedResultFromWorkflow(requestedReason, scopeReason); |
| 170 | + System.out.println(recordedReason); |
| 171 | + return recordedReason; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public String getRecordedReason() { |
| 176 | + return recordedReason; |
| 177 | + } |
| 178 | + |
| 179 | + private String expectedResultFromWorkflow(String requestedReason, String scopeReason) { |
| 180 | + return requestedReason + "|" + scopeReason; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + @WorkflowInterface |
| 185 | + public interface CancelExternalWorkflow { |
| 186 | + @WorkflowMethod |
| 187 | + void execute(String workflowId, String reason); |
| 188 | + } |
| 189 | + |
| 190 | + public static class CancelExternalWorkflowImpl implements CancelExternalWorkflow { |
| 191 | + |
| 192 | + @Override |
| 193 | + public void execute(String workflowId, String reason) { |
| 194 | + ExternalWorkflowStub externalWorkflow = Workflow.newUntypedExternalWorkflowStub(workflowId); |
| 195 | + externalWorkflow.cancel(reason); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + @WorkflowInterface |
| 200 | + public interface CascadingCancelWorkflow { |
| 201 | + @WorkflowMethod |
| 202 | + void execute(String workflowId); |
| 203 | + } |
| 204 | + |
| 205 | + public static class CascadingCancelWorkflowImpl implements CascadingCancelWorkflow { |
| 206 | + |
| 207 | + @Override |
| 208 | + public void execute(String workflowId) { |
| 209 | + ExternalWorkflowStub externalWorkflow = Workflow.newUntypedExternalWorkflowStub(workflowId); |
| 210 | + CancellationScope.current().getCancellationRequest().get(); |
| 211 | + externalWorkflow.cancel(); |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments