-
Notifications
You must be signed in to change notification settings - Fork 249
Add promise-based workflow await #3055
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
baekgyu-kim
wants to merge
1
commit into
temporalio:main
Choose a base branch
from
baekgyu-kim:1937
base: main
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.
+261
−0
Open
Changes from all commits
Commits
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
209 changes: 209 additions & 0 deletions
209
temporal-sdk/src/test/java/io/temporal/workflow/AsyncAwaitTest.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,209 @@ | ||
| package io.temporal.workflow; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import io.temporal.activity.ActivityInterface; | ||
| import io.temporal.activity.ActivityOptions; | ||
| import io.temporal.client.WorkflowClient; | ||
| import io.temporal.client.WorkflowStub; | ||
| import io.temporal.failure.CanceledFailure; | ||
| import io.temporal.testing.internal.SDKTestWorkflowRule; | ||
| import java.time.Duration; | ||
| import java.util.function.Supplier; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
|
|
||
| public class AsyncAwaitTest { | ||
|
|
||
| @Rule | ||
| public SDKTestWorkflowRule testWorkflowRule = | ||
| SDKTestWorkflowRule.newBuilder() | ||
| .setWorkflowTypes(TestAsyncAwaitWorkflowImpl.class) | ||
| .setActivityImplementations(new TestAwaitActivityImpl()) | ||
| .build(); | ||
|
|
||
| @Test | ||
| public void testAlreadySatisfiedConditionCompletesWithTrue() { | ||
| TestAsyncAwaitWorkflow workflow = newWorkflowStub(); | ||
|
|
||
| assertEquals("true", workflow.execute("alreadySatisfied")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnsatisfiedConditionCompletesWithFalseAfterTimeout() { | ||
| TestAsyncAwaitWorkflow workflow = newWorkflowStub(); | ||
|
|
||
| assertEquals("false:true", workflow.execute("timeout")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAwaitDoesNotBlockCallingWorkflowThread() { | ||
| TestAsyncAwaitWorkflow workflow = newWorkflowStub(); | ||
|
|
||
| assertEquals("true", workflow.execute("nonBlocking")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSignalConditionComposesWithActivityPromise() { | ||
| TestAsyncAwaitWorkflow workflow = newWorkflowStub(); | ||
| WorkflowClient.start(workflow::execute, "signalAndActivity"); | ||
|
|
||
| workflow.unblock(); | ||
|
|
||
| assertEquals("true:activity", WorkflowStub.fromTyped(workflow).getResult(String.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCancellationFailsPromise() { | ||
| TestAsyncAwaitWorkflow workflow = newWorkflowStub(); | ||
|
|
||
| assertEquals("CanceledFailure", workflow.execute("cancellation")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPredicateExceptionFailsPromise() { | ||
| TestAsyncAwaitWorkflow workflow = newWorkflowStub(); | ||
|
|
||
| assertEquals("IllegalStateException:predicate failed", workflow.execute("predicateFailure")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAlreadySatisfiedConditionWithoutTimeoutCompletesWithNull() { | ||
| assertEquals("null", newWorkflowStub().execute("alreadySatisfiedWithoutTimeout")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAwaitWithoutTimeoutDoesNotBlockCallingWorkflowThread() { | ||
| assertEquals("null", newWorkflowStub().execute("nonBlockingWithoutTimeout")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSignalConditionWithoutTimeoutComposesWithActivityPromise() { | ||
| TestAsyncAwaitWorkflow workflow = newWorkflowStub(); | ||
| WorkflowClient.start(workflow::execute, "signalAndActivityWithoutTimeout"); | ||
|
|
||
| workflow.unblock(); | ||
|
|
||
| assertEquals("null:activity", WorkflowStub.fromTyped(workflow).getResult(String.class)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCancellationWithoutTimeoutFailsPromise() { | ||
| assertEquals("CanceledFailure", newWorkflowStub().execute("cancellationWithoutTimeout")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPredicateExceptionWithoutTimeoutFailsPromise() { | ||
| assertEquals( | ||
| "IllegalStateException:predicate failed", | ||
| newWorkflowStub().execute("predicateFailureWithoutTimeout")); | ||
| } | ||
|
|
||
| private TestAsyncAwaitWorkflow newWorkflowStub() { | ||
| return testWorkflowRule.newWorkflowStubTimeoutOptions(TestAsyncAwaitWorkflow.class); | ||
| } | ||
|
|
||
| @WorkflowInterface | ||
| public interface TestAsyncAwaitWorkflow { | ||
|
|
||
| @WorkflowMethod | ||
| String execute(String testCase); | ||
|
|
||
| @SignalMethod | ||
| void unblock(); | ||
| } | ||
|
|
||
| @ActivityInterface | ||
| public interface TestAwaitActivity { | ||
| String execute(); | ||
| } | ||
|
|
||
| public static class TestAwaitActivityImpl implements TestAwaitActivity { | ||
|
|
||
| @Override | ||
| public String execute() { | ||
| return "activity"; | ||
| } | ||
| } | ||
|
|
||
| public static class TestAsyncAwaitWorkflowImpl implements TestAsyncAwaitWorkflow { | ||
|
|
||
| private boolean unblocked; | ||
| private Promise<?> cancellationPromise; | ||
|
|
||
| @Override | ||
| public String execute(String testCase) { | ||
| switch (testCase) { | ||
| case "alreadySatisfied": | ||
| return Async.await(Duration.ofHours(1), () -> true).get().toString(); | ||
| case "alreadySatisfiedWithoutTimeout": | ||
| Promise<Void> satisfied = Async.await(() -> true); | ||
| return String.valueOf(satisfied.get()); | ||
| case "timeout": | ||
| long timeoutStart = Workflow.currentTimeMillis(); | ||
| boolean result = Async.await(Duration.ofMinutes(1), () -> false).get(); | ||
| return result | ||
| + ":" | ||
| + (Workflow.currentTimeMillis() - timeoutStart >= Duration.ofMinutes(1).toMillis()); | ||
| case "nonBlocking": | ||
| long start = Workflow.currentTimeMillis(); | ||
| Async.await(Duration.ofHours(1), () -> false); | ||
| return Boolean.toString( | ||
| Workflow.currentTimeMillis() - start < Duration.ofHours(1).toMillis()); | ||
| case "nonBlockingWithoutTimeout": | ||
| Promise<Void> pending = Async.await(() -> unblocked); | ||
| unblocked = true; | ||
| return String.valueOf(pending.get()); | ||
| case "signalAndActivity": | ||
| case "signalAndActivityWithoutTimeout": | ||
| Promise<?> condition = | ||
| testCase.equals("signalAndActivity") | ||
| ? Async.await(Duration.ofHours(1), () -> unblocked) | ||
| : Async.await(() -> unblocked); | ||
| TestAwaitActivity activity = | ||
| Workflow.newActivityStub( | ||
| TestAwaitActivity.class, | ||
| ActivityOptions.newBuilder() | ||
| .setStartToCloseTimeout(Duration.ofSeconds(10)) | ||
| .build()); | ||
| Promise<String> activityResult = Async.function(activity::execute); | ||
| Promise.allOf(condition, activityResult).get(); | ||
| return condition.get() + ":" + activityResult.get(); | ||
| case "cancellation": | ||
| case "cancellationWithoutTimeout": | ||
| CancellationScope scope = | ||
| Workflow.newCancellationScope( | ||
| () -> | ||
| cancellationPromise = | ||
| testCase.equals("cancellation") | ||
| ? Async.await(Duration.ofHours(1), () -> false) | ||
| : Async.await(() -> false)); | ||
| scope.run(); | ||
| scope.cancel(); | ||
| RuntimeException cancellationFailure = cancellationPromise.getFailure(); | ||
| assertTrue(cancellationFailure instanceof CanceledFailure); | ||
| return cancellationFailure.getClass().getSimpleName(); | ||
| case "predicateFailure": | ||
| case "predicateFailureWithoutTimeout": | ||
| Supplier<Boolean> predicate = | ||
| () -> { | ||
| throw new IllegalStateException("predicate failed"); | ||
| }; | ||
| Promise<?> failed = | ||
| testCase.equals("predicateFailure") | ||
| ? Async.await(Duration.ofHours(1), predicate) | ||
| : Async.await(predicate); | ||
| RuntimeException predicateFailure = failed.getFailure(); | ||
| return predicateFailure.getClass().getSimpleName() + ":" + predicateFailure.getMessage(); | ||
| default: | ||
| throw new IllegalArgumentException("Unknown test case: " + testCase); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void unblock() { | ||
| unblocked = true; | ||
| } | ||
| } | ||
| } |
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.
Can we also add a overload that take no duration to be consisiten with
Workflow.awaitUh oh!
There was an error while loading. Please reload this page.
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.
Hi @Quinn-With-Two-Ns, thanks for taking a look at this issue.
I added a no-timeout overload to match
Workflow.await, along with documentation and tests.Please take a look when you have a chance. Thanks!