Skip to content

Commit 8af4a26

Browse files
ReadOnly Exception in update validator fail WFT (#1918)
ReadOnly Exception in update validator fail WFT
1 parent abb1deb commit 8af4a26

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.internal.sync;
22+
23+
/**
24+
* This exception is thrown when a workflow tries to perform an operation that could generate a new
25+
* command while it is in a read only context.
26+
*/
27+
public class ReadOnlyException extends IllegalStateException {
28+
public ReadOnlyException(String action) {
29+
super("While in read-only function, action attempted: " + action);
30+
}
31+
}

temporal-sdk/src/main/java/io/temporal/internal/sync/SyncWorkflow.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ public void handleUpdate(
163163
try {
164164
workflowContext.setReadOnly(true);
165165
workflowProc.handleValidateUpdate(updateName, input, eventId, header);
166+
} catch (ReadOnlyException r) {
167+
// Rethrow instead on rejecting the update to fail the WFT
168+
throw r;
166169
} catch (Exception e) {
167170
callbacks.reject(this.dataConverter.exceptionToFailure(e));
168171
return;

temporal-sdk/src/main/java/io/temporal/internal/sync/WorkflowInternal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ static boolean isReadOnly() {
744744

745745
static void assertNotReadOnly(String action) {
746746
if (isReadOnly()) {
747-
throw new IllegalStateException("While in read-only function, action attempted:" + action);
747+
throw new ReadOnlyException(action);
748748
}
749749
}
750750

temporal-sdk/src/test/java/io/temporal/workflow/shared/TestWorkflows.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public String execute(String arg) {
285285
}
286286
}
287287

288-
public static String[] illegalCallCases = {
288+
public static final String[] illegalCallCases = {
289289
"start_activity",
290290
"start_local_activity",
291291
"upsert_search_attribute",
@@ -312,7 +312,7 @@ public static void illegalCalls(String testCase) {
312312
Workflow.newLocalActivityStub(
313313
TestActivities.TestActivity1.class,
314314
LocalActivityOptions.newBuilder()
315-
.setScheduleToStartTimeout(Duration.ofHours(1))
315+
.setScheduleToCloseTimeout(Duration.ofHours(1))
316316
.build())
317317
.execute("test");
318318
break;

temporal-sdk/src/test/java/io/temporal/workflow/updateTest/UpdateBadValidator.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
package io.temporal.workflow.updateTest;
2222

2323
import static org.junit.Assert.assertEquals;
24-
import static org.junit.Assert.assertThrows;
2524

2625
import io.temporal.activity.*;
2726
import io.temporal.api.common.v1.WorkflowExecution;
@@ -33,6 +32,7 @@
3332
import io.temporal.workflow.Workflow;
3433
import io.temporal.workflow.shared.TestActivities;
3534
import io.temporal.workflow.shared.TestWorkflows;
35+
import java.time.Duration;
3636
import java.util.Optional;
3737
import java.util.UUID;
3838
import org.junit.Rule;
@@ -41,6 +41,7 @@
4141
import org.slf4j.LoggerFactory;
4242

4343
public class UpdateBadValidator {
44+
private static int testWorkflowTaskFailureReplayCount;
4445

4546
private static final Logger log = LoggerFactory.getLogger(UpdateTest.class);
4647

@@ -51,13 +52,14 @@ public class UpdateBadValidator {
5152
.setWorkflowTypes(TestUpdateWithBadValidatorWorkflowImpl.class)
5253
.build();
5354

54-
@Test
55+
@Test(timeout = 30000)
5556
public void testBadUpdateValidator() {
5657
String workflowId = UUID.randomUUID().toString();
5758
WorkflowClient workflowClient = testWorkflowRule.getWorkflowClient();
5859
WorkflowOptions options =
5960
SDKTestOptions.newWorkflowOptionsWithTimeouts(testWorkflowRule.getTaskQueue()).toBuilder()
6061
.setWorkflowId(workflowId)
62+
.setWorkflowTaskTimeout(Duration.ofSeconds(1))
6163
.build();
6264
TestWorkflows.WorkflowWithUpdate workflow =
6365
workflowClient.newWorkflowStub(TestWorkflows.WorkflowWithUpdate.class, options);
@@ -69,9 +71,9 @@ public void testBadUpdateValidator() {
6971
assertEquals("initial", workflow.getState());
7072

7173
assertEquals(workflowId, execution.getWorkflowId());
72-
7374
for (String testCase : TestWorkflows.illegalCallCases) {
74-
assertThrows(WorkflowUpdateException.class, () -> workflow.update(0, testCase));
75+
assertEquals("2", workflow.update(0, testCase));
76+
testWorkflowTaskFailureReplayCount = 0;
7577
}
7678

7779
workflow.complete();
@@ -102,12 +104,15 @@ public String getState() {
102104

103105
@Override
104106
public String update(Integer index, String value) {
105-
return "";
107+
return String.valueOf(testWorkflowTaskFailureReplayCount);
106108
}
107109

108110
@Override
109111
public void updateValidator(Integer index, String testCase) {
110-
TestWorkflows.illegalCalls(testCase);
112+
if (testWorkflowTaskFailureReplayCount < 2) {
113+
testWorkflowTaskFailureReplayCount += 1;
114+
TestWorkflows.illegalCalls(testCase);
115+
}
111116
}
112117

113118
@Override

0 commit comments

Comments
 (0)