-
Notifications
You must be signed in to change notification settings - Fork 224
Initial Workflow SDK #857
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
Merged
Merged
Initial Workflow SDK #857
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ccd29fc
Init for workflows
bderusha 22aacbd
Updating some javadocs and Years.
cf3f60f
Add missing Header
c6e3b5e
respond to PR feedback
bderusha 3cfd8c1
Update workflow example README
bderusha a6cfda9
Address PR feedback
bderusha 1ed1940
fixup deprecated pom.xml variable
bderusha 452a4d9
Updates based on PR feedback
bderusha 9b2881b
Update pom files per feedback
bderusha 522f1ef
Merge branch 'dapr:master' into master
bderusha edecaf6
Add unit testing example
bderusha 42b61ef
Merge branch 'dapr:master' into master
bderusha 234ab44
update pom
bderusha 2fbc805
Fix dependency conflict
bderusha 888b6e5
Merge branch 'dapr:master' into master
bderusha 28898e4
Merge branch 'master' into master
bderusha 1cfae5e
Use Mono and refactor GRPC managed channel builder
bderusha c9f6d1d
Fix example unit tests
bderusha 2aba320
Implement PR feedback
bderusha 6af50b5
PR Feedback: Revert Mono usage + pom.xml changes
bderusha 4a53c4e
pom.xml version fixes
bderusha 904e41e
Remove Mono entirely from workflows
bderusha 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| apiVersion: dapr.io/v1alpha1 | ||
| kind: Component | ||
| metadata: | ||
| name: statestore | ||
| spec: | ||
| type: state.redis | ||
| version: v1 | ||
| metadata: | ||
| - name: redisHost | ||
| value: localhost:6379 | ||
| - name: redisPassword | ||
| value: "" | ||
| - name: actorStateStore | ||
| value: "true" |
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
109 changes: 109 additions & 0 deletions
109
examples/src/main/java/io/dapr/examples/unittesting/DaprWorkflowExampleTest.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,109 @@ | ||
| /* | ||
| * Copyright 2023 The Dapr Authors | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package io.dapr.examples.unittesting; | ||
|
|
||
| import com.microsoft.durabletask.Task; | ||
| import com.microsoft.durabletask.TaskCanceledException; | ||
| import io.dapr.workflows.Workflow; | ||
| import io.dapr.workflows.WorkflowContext; | ||
| import io.dapr.workflows.WorkflowStub; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mockito; | ||
| import org.slf4j.Logger; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.ArgumentMatchers.startsWith; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| /** | ||
| * 1. Build and install jars: | ||
| * mvn clean install | ||
| * 2. cd [repo root]/examples | ||
| * 3. Run the test code: | ||
| * java -jar target/dapr-java-sdk-examples-exec.jar \ | ||
| * org.junit.platform.console.ConsoleLauncher --select-class=io.dapr.examples.unittesting.DaprWorkflowExampleTest | ||
| */ | ||
| public class DaprWorkflowExampleTest { | ||
| private static final String timeoutWorkflow = "DemoWorkflowTimeout"; | ||
| private static final String noTimeoutWorkflow = "DemoWorkflowNoTimeout"; | ||
| private static final String workflowDefaultId = "demo-workflow-123"; | ||
|
|
||
| private class DemoWorkflow extends Workflow { | ||
|
|
||
| @Override | ||
| public WorkflowStub create() { | ||
| return ctx -> { | ||
| String name = ctx.getName(); | ||
| String id = ctx.getInstanceId(); | ||
| try { | ||
| ctx.waitForExternalEvent(name, Duration.ofMillis(100)).await(); | ||
| } catch (TaskCanceledException e) { | ||
| ctx.getLogger().warn("Timed out"); | ||
| } | ||
| String output = name + ":" + id; | ||
| ctx.complete(output); | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testWorkflow() { | ||
| String name = noTimeoutWorkflow; | ||
| String id = workflowDefaultId; | ||
| WorkflowContext mockContext = createMockContext(name, id); | ||
|
|
||
| new DemoWorkflow().run(mockContext); | ||
|
|
||
| String expectedOutput = name + ":" + id; | ||
| Mockito.verify(mockContext, Mockito.times(1)).complete(expectedOutput); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWorkflowWaitForEventTimeout() { | ||
| WorkflowContext mockContext = createMockContext(timeoutWorkflow, workflowDefaultId); | ||
| Logger mockLogger = mock(Logger.class); | ||
| Mockito.doReturn(mockLogger).when(mockContext).getLogger(); | ||
|
|
||
| new DemoWorkflow().run(mockContext); | ||
|
|
||
| Mockito.verify(mockLogger, Mockito.times(1)).warn("Timed out"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWorkflowWaitForEventNoTimeout() { | ||
| WorkflowContext mockContext = createMockContext(noTimeoutWorkflow, workflowDefaultId); | ||
| Logger mockLogger = mock(Logger.class); | ||
| Mockito.doReturn(mockLogger).when(mockContext).getLogger(); | ||
|
|
||
| new DemoWorkflow().run(mockContext); | ||
|
|
||
| Mockito.verify(mockLogger, Mockito.times(0)).warn(anyString()); | ||
| } | ||
|
|
||
| private WorkflowContext createMockContext(String name, String id) { | ||
| WorkflowContext mockContext = mock(WorkflowContext.class); | ||
|
|
||
| Mockito.doReturn(name).when(mockContext).getName(); | ||
| Mockito.doReturn(id).when(mockContext).getInstanceId(); | ||
| Mockito.doReturn(mock(Task.class)) | ||
| .when(mockContext).waitForExternalEvent(startsWith(noTimeoutWorkflow), any(Duration.class)); | ||
| Mockito.doThrow(TaskCanceledException.class) | ||
| .when(mockContext).waitForExternalEvent(startsWith(timeoutWorkflow), any(Duration.class)); | ||
|
|
||
| return mockContext; | ||
| } | ||
| } |
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
43 changes: 43 additions & 0 deletions
43
examples/src/main/java/io/dapr/examples/workflows/DemoWorkflow.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,43 @@ | ||
| /* | ||
| * Copyright 2023 The Dapr Authors | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package io.dapr.examples.workflows; | ||
|
|
||
| import com.microsoft.durabletask.TaskCanceledException; | ||
| import io.dapr.workflows.Workflow; | ||
| import io.dapr.workflows.WorkflowStub; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| /** | ||
| * Implementation of the DemoWorkflow for the server side. | ||
| */ | ||
| public class DemoWorkflow extends Workflow { | ||
|
|
||
| @Override | ||
| public WorkflowStub create() { | ||
| return ctx -> { | ||
| ctx.getLogger().info("Starting Workflow: " + ctx.getName()); | ||
| ctx.getLogger().info("Instance ID: " + ctx.getInstanceId()); | ||
| ctx.getLogger().info("Waiting for event: 'myEvent'..."); | ||
| try { | ||
| ctx.waitForExternalEvent("myEvent", Duration.ofSeconds(10)).await(); | ||
| ctx.getLogger().info("Received!"); | ||
| } catch (TaskCanceledException e) { | ||
| ctx.getLogger().warn("Timed out"); | ||
| ctx.getLogger().warn(e.getMessage()); | ||
| } | ||
| ctx.complete("finished"); | ||
| }; | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
examples/src/main/java/io/dapr/examples/workflows/DemoWorkflowClient.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,55 @@ | ||
| /* | ||
| * Copyright 2023 The Dapr Authors | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package io.dapr.examples.workflows; | ||
|
|
||
| import io.dapr.workflows.client.DaprWorkflowClient; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * For setup instructions, see the README. | ||
| */ | ||
| public class DemoWorkflowClient { | ||
|
|
||
| /** | ||
| * The main method. | ||
| * @param args Input arguments (unused). | ||
| * @throws InterruptedException If program has been interrupted. | ||
| */ | ||
| public static void main(String[] args) throws InterruptedException { | ||
| DaprWorkflowClient client = new DaprWorkflowClient(); | ||
|
|
||
| try (client) { | ||
| System.out.println("*****"); | ||
| String instanceId = client.scheduleNewWorkflow(DemoWorkflow.class); | ||
| System.out.printf("Started new workflow instance with random ID: %s%n", instanceId); | ||
|
|
||
| System.out.println("Sleep and allow this workflow instance to timeout..."); | ||
| TimeUnit.SECONDS.sleep(10); | ||
|
|
||
| System.out.println("*****"); | ||
| String instanceToTerminateId = "terminateMe"; | ||
| client.scheduleNewWorkflow(DemoWorkflow.class, null, instanceToTerminateId); | ||
| System.out.printf("Started new workflow instance with specified ID: %s%n", instanceToTerminateId); | ||
|
|
||
| TimeUnit.SECONDS.sleep(5); | ||
| System.out.println("Terminate this workflow instance manually before the timeout is reached"); | ||
| client.terminateWorkflow(instanceToTerminateId, null); | ||
| System.out.println("*****"); | ||
| } | ||
|
|
||
| System.out.println("Exiting DemoWorkflowClient."); | ||
| System.exit(0); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.