-
Notifications
You must be signed in to change notification settings - Fork 199
Cancel timer when Workflow.await condition is satisfied #2799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mfateev
wants to merge
5
commits into
temporalio:master
Choose a base branch
from
mfateev:await-cancel-timer-clean
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6782c54
Cancel timer in Workflow.await(duration, condition) when condition sa…
mfateev 986e954
Disable CANCEL_AWAIT_TIMER_ON_CONDITION by default for safe rollout
mfateev 4e91e00
Move early condition check inside cancelTimerOnCondition branch
mfateev f35461c
Merge remote-tracking branch 'origin/master' into await-cancel-timer-…
mfateev a9f16d6
Merge branch 'master' into await-cancel-timer-clean
mjameswh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
228 changes: 228 additions & 0 deletions
228
.../java/io/temporal/workflow/cancellationTests/WorkflowAwaitCancelTimerOnConditionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| package io.temporal.workflow.cancellationTests; | ||
|
|
||
| import static org.junit.Assert.*; | ||
|
|
||
| import io.temporal.api.common.v1.WorkflowExecution; | ||
| import io.temporal.api.enums.v1.EventType; | ||
| import io.temporal.client.WorkflowClient; | ||
| import io.temporal.client.WorkflowStub; | ||
| import io.temporal.internal.common.SdkFlag; | ||
| import io.temporal.internal.statemachines.WorkflowStateMachines; | ||
| import io.temporal.testing.WorkflowReplayer; | ||
| import io.temporal.testing.internal.SDKTestWorkflowRule; | ||
| import io.temporal.workflow.SignalMethod; | ||
| import io.temporal.workflow.Workflow; | ||
| import io.temporal.workflow.WorkflowInterface; | ||
| import io.temporal.workflow.WorkflowMethod; | ||
| import java.time.Duration; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
|
|
||
| /** | ||
| * Tests for the CANCEL_AWAIT_TIMER_ON_CONDITION SDK flag behavior. Tests verify both old and new | ||
| * behavior by explicitly switching the SDK flag, following the Go SDK pattern. | ||
| * | ||
| * <p>Since the flag is NOT auto-enabled (uses checkSdkFlag, not tryUseSdkFlag), tests must | ||
| * explicitly add it to initialFlags to enable the new behavior. | ||
| */ | ||
| public class WorkflowAwaitCancelTimerOnConditionTest { | ||
|
|
||
| private List<SdkFlag> savedInitialFlags; | ||
|
|
||
| @Rule | ||
| public SDKTestWorkflowRule testWorkflowRule = | ||
| SDKTestWorkflowRule.newBuilder() | ||
| .setWorkflowTypes( | ||
| TestAwaitCancelTimerWorkflowImpl.class, | ||
| TestImmediateConditionWorkflowImpl.class, | ||
| TestReturnValueWorkflowImpl.class) | ||
| .build(); | ||
|
|
||
| @Before | ||
| public void setUp() { | ||
| savedInitialFlags = WorkflowStateMachines.initialFlags; | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| WorkflowStateMachines.initialFlags = savedInitialFlags; | ||
| } | ||
|
|
||
| /** | ||
| * Tests that the timer IS cancelled when the flag is explicitly enabled. With | ||
| * CANCEL_AWAIT_TIMER_ON_CONDITION in initialFlags, we expect TIMER_CANCELED in history. | ||
| */ | ||
| @Test | ||
| public void testTimerCancelledWhenFlagEnabled() { | ||
| WorkflowStateMachines.initialFlags = | ||
| Collections.unmodifiableList( | ||
| Arrays.asList( | ||
| SdkFlag.SKIP_YIELD_ON_DEFAULT_VERSION, SdkFlag.CANCEL_AWAIT_TIMER_ON_CONDITION)); | ||
|
|
||
| TestAwaitWorkflow workflow = testWorkflowRule.newWorkflowStub(TestAwaitWorkflow.class); | ||
| WorkflowExecution execution = WorkflowClient.start(workflow::execute); | ||
|
|
||
| testWorkflowRule.sleep(Duration.ofMillis(500)); | ||
| workflow.unblock(); | ||
|
|
||
| WorkflowStub untyped = WorkflowStub.fromTyped(workflow); | ||
| String result = untyped.getResult(String.class); | ||
| assertEquals("condition satisfied", result); | ||
|
|
||
| testWorkflowRule.assertHistoryEvent( | ||
| execution.getWorkflowId(), EventType.EVENT_TYPE_TIMER_STARTED); | ||
| testWorkflowRule.assertHistoryEvent( | ||
| execution.getWorkflowId(), EventType.EVENT_TYPE_TIMER_CANCELED); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that the timer is NOT cancelled when the flag is disabled (default). Without the flag in | ||
| * initialFlags, the old behavior is used: timer runs even after condition is satisfied. | ||
| */ | ||
| @Test | ||
| public void testTimerNotCancelledWhenFlagDisabled() { | ||
| // Default initialFlags do NOT include CANCEL_AWAIT_TIMER_ON_CONDITION | ||
| TestAwaitWorkflow workflow = testWorkflowRule.newWorkflowStub(TestAwaitWorkflow.class); | ||
| WorkflowExecution execution = WorkflowClient.start(workflow::execute); | ||
|
|
||
| testWorkflowRule.sleep(Duration.ofMillis(500)); | ||
| workflow.unblock(); | ||
|
|
||
| WorkflowStub untyped = WorkflowStub.fromTyped(workflow); | ||
| String result = untyped.getResult(String.class); | ||
| assertEquals("condition satisfied", result); | ||
|
|
||
| testWorkflowRule.assertHistoryEvent( | ||
| execution.getWorkflowId(), EventType.EVENT_TYPE_TIMER_STARTED); | ||
| testWorkflowRule.assertNoHistoryEvent( | ||
| execution.getWorkflowId(), EventType.EVENT_TYPE_TIMER_CANCELED); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that no timer is created when the condition is immediately true and the flag is enabled. | ||
| */ | ||
| @Test | ||
| public void testNoTimerWhenConditionImmediatelySatisfiedWithFlag() { | ||
| WorkflowStateMachines.initialFlags = | ||
| Collections.unmodifiableList( | ||
| Arrays.asList( | ||
| SdkFlag.SKIP_YIELD_ON_DEFAULT_VERSION, SdkFlag.CANCEL_AWAIT_TIMER_ON_CONDITION)); | ||
|
|
||
| TestImmediateConditionWorkflow workflow = | ||
| testWorkflowRule.newWorkflowStub(TestImmediateConditionWorkflow.class); | ||
| WorkflowExecution execution = WorkflowClient.start(workflow::execute); | ||
|
|
||
| WorkflowStub untyped = WorkflowStub.fromTyped(workflow); | ||
| String result = untyped.getResult(String.class); | ||
| assertEquals("immediate condition", result); | ||
|
|
||
| testWorkflowRule.assertNoHistoryEvent( | ||
| execution.getWorkflowId(), EventType.EVENT_TYPE_TIMER_STARTED); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that the await returns true when condition is satisfied and false when it times out. This | ||
| * verifies the return value semantics are preserved with the new flag. | ||
| */ | ||
| @Test | ||
| public void testAwaitReturnValue() { | ||
| WorkflowStateMachines.initialFlags = | ||
| Collections.unmodifiableList( | ||
| Arrays.asList( | ||
| SdkFlag.SKIP_YIELD_ON_DEFAULT_VERSION, SdkFlag.CANCEL_AWAIT_TIMER_ON_CONDITION)); | ||
|
|
||
| TestReturnValueWorkflow workflow = | ||
| testWorkflowRule.newWorkflowStub(TestReturnValueWorkflow.class); | ||
| WorkflowExecution execution = WorkflowClient.start(workflow::execute); | ||
|
|
||
| testWorkflowRule.sleep(Duration.ofMillis(500)); | ||
| workflow.unblock(); | ||
|
|
||
| WorkflowStub untyped = WorkflowStub.fromTyped(workflow); | ||
| String result = untyped.getResult(String.class); | ||
| assertEquals("conditionSatisfied=true,timedOut=true", result); | ||
|
|
||
| testWorkflowRule.assertHistoryEvent( | ||
| execution.getWorkflowId(), EventType.EVENT_TYPE_TIMER_CANCELED); | ||
| testWorkflowRule.assertHistoryEvent( | ||
| execution.getWorkflowId(), EventType.EVENT_TYPE_TIMER_FIRED); | ||
| } | ||
|
|
||
| /** | ||
| * Tests replay compatibility with old workflow histories that were recorded WITHOUT the | ||
| * CANCEL_AWAIT_TIMER_ON_CONDITION flag. | ||
| */ | ||
| @Test | ||
| public void testReplayOldHistoryWithoutFlag() throws Exception { | ||
| WorkflowReplayer.replayWorkflowExecutionFromResource( | ||
| "awaitTimerConditionOldBehavior.json", TestAwaitCancelTimerWorkflowImpl.class); | ||
| } | ||
|
|
||
| @WorkflowInterface | ||
| public interface TestAwaitWorkflow { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I believe we generally try to deduplicate workflow interfacess in our tests. |
||
| @WorkflowMethod | ||
| String execute(); | ||
|
|
||
| @SignalMethod | ||
| void unblock(); | ||
| } | ||
|
|
||
| @WorkflowInterface | ||
| public interface TestImmediateConditionWorkflow { | ||
| @WorkflowMethod | ||
| String execute(); | ||
| } | ||
|
|
||
| @WorkflowInterface | ||
| public interface TestReturnValueWorkflow { | ||
| @WorkflowMethod | ||
| String execute(); | ||
|
|
||
| @SignalMethod | ||
| void unblock(); | ||
| } | ||
|
|
||
| public static class TestAwaitCancelTimerWorkflowImpl implements TestAwaitWorkflow { | ||
| private boolean unblocked = false; | ||
|
|
||
| @Override | ||
| public String execute() { | ||
| boolean result = Workflow.await(Duration.ofHours(1), () -> unblocked); | ||
| return result ? "condition satisfied" : "timed out"; | ||
| } | ||
|
|
||
| @Override | ||
| public void unblock() { | ||
| unblocked = true; | ||
| } | ||
| } | ||
|
|
||
| public static class TestImmediateConditionWorkflowImpl implements TestImmediateConditionWorkflow { | ||
| @Override | ||
| public String execute() { | ||
| boolean result = Workflow.await(Duration.ofHours(1), () -> true); | ||
| return result ? "immediate condition" : "unexpected timeout"; | ||
| } | ||
| } | ||
|
|
||
| public static class TestReturnValueWorkflowImpl implements TestReturnValueWorkflow { | ||
| private boolean unblocked = false; | ||
|
|
||
| @Override | ||
| public String execute() { | ||
| boolean conditionSatisfied = Workflow.await(Duration.ofHours(1), () -> unblocked); | ||
| boolean timedOut = !Workflow.await(Duration.ofMillis(100), () -> false); | ||
| return "conditionSatisfied=" + conditionSatisfied + ",timedOut=" + timedOut; | ||
| } | ||
|
|
||
| @Override | ||
| public void unblock() { | ||
| unblocked = true; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about providing a reason message here. AFAICS, we're not currently providing reason messages in similar context anywhere in the Java SDK, and we don't do that either in the Go SDK, which was used as reference for this PR.
But no strong opinion either way.