Skip to content

Commit 71e89f9

Browse files
Block invalid calls in Await (#2225)
1 parent 7525c65 commit 71e89f9

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,13 +498,34 @@ public static <R> R executeActivity(
498498
public static void await(String reason, Supplier<Boolean> unblockCondition)
499499
throws DestroyWorkflowThreadError {
500500
assertNotReadOnly(reason);
501-
getWorkflowOutboundInterceptor().await(reason, unblockCondition);
501+
getWorkflowOutboundInterceptor()
502+
.await(
503+
reason,
504+
() -> {
505+
getRootWorkflowContext().setReadOnly(true);
506+
try {
507+
return unblockCondition.get();
508+
} finally {
509+
getRootWorkflowContext().setReadOnly(false);
510+
}
511+
});
502512
}
503513

504514
public static boolean await(Duration timeout, String reason, Supplier<Boolean> unblockCondition)
505515
throws DestroyWorkflowThreadError {
506516
assertNotReadOnly(reason);
507-
return getWorkflowOutboundInterceptor().await(timeout, reason, unblockCondition);
517+
return getWorkflowOutboundInterceptor()
518+
.await(
519+
timeout,
520+
reason,
521+
() -> {
522+
getRootWorkflowContext().setReadOnly(true);
523+
try {
524+
return unblockCondition.get();
525+
} finally {
526+
getRootWorkflowContext().setReadOnly(false);
527+
}
528+
});
508529
}
509530

510531
public static <R> R sideEffect(Class<R> resultClass, Type resultType, Func<R> func) {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.workflow;
22+
23+
import static org.junit.Assert.assertThrows;
24+
25+
import io.temporal.client.WorkflowFailedException;
26+
import io.temporal.testing.internal.SDKTestWorkflowRule;
27+
import io.temporal.worker.WorkflowImplementationOptions;
28+
import io.temporal.workflow.shared.TestWorkflows;
29+
import io.temporal.workflow.shared.TestWorkflows.TestWorkflow1;
30+
import org.junit.Rule;
31+
import org.junit.Test;
32+
33+
public class BadAwaitTest {
34+
35+
@Rule
36+
public SDKTestWorkflowRule testWorkflowRule =
37+
SDKTestWorkflowRule.newBuilder()
38+
.setWorkflowTypes(
39+
WorkflowImplementationOptions.newBuilder()
40+
.setFailWorkflowExceptionTypes(RuntimeException.class)
41+
.build(),
42+
TestAwait.class)
43+
.build();
44+
45+
@Test
46+
public void testBadAwait() {
47+
for (String testCase : TestWorkflows.illegalCallCases) {
48+
TestWorkflows.TestWorkflow1 workflowStub =
49+
testWorkflowRule.newWorkflowStubTimeoutOptions(TestWorkflows.TestWorkflow1.class);
50+
assertThrows(WorkflowFailedException.class, () -> workflowStub.execute(testCase));
51+
}
52+
}
53+
54+
public static class TestAwait implements TestWorkflow1 {
55+
@Override
56+
public String execute(String testCase) {
57+
Workflow.await(
58+
() -> {
59+
TestWorkflows.illegalCalls(testCase);
60+
return true;
61+
});
62+
return "fail";
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)