-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable unit testing for Dapr Workflows (#1035)
* Workflow SDK changes to enable unit testing Signed-off-by: Chris Gillum <cgillum@microsoft.com> * Sample workflow unit testing project Signed-off-by: Chris Gillum <cgillum@microsoft.com> --------- Signed-off-by: Chris Gillum <cgillum@microsoft.com>
- Loading branch information
Showing
12 changed files
with
262 additions
and
80 deletions.
There are no files selected for viewing
This file contains 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 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
7 changes: 3 additions & 4 deletions
7
examples/Workflow/WorkflowConsoleApp/Activities/NotifyActivity.cs
This file contains 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
9 changes: 3 additions & 6 deletions
9
examples/Workflow/WorkflowConsoleApp/Activities/ProcessPaymentActivity.cs
This file contains 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 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 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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
namespace WorkflowConsoleApp.Models | ||
{ | ||
record OrderPayload(string Name, double TotalCost, int Quantity = 1); | ||
record InventoryRequest(string RequestId, string ItemName, int Quantity); | ||
record InventoryResult(bool Success, InventoryItem orderPayload); | ||
record PaymentRequest(string RequestId, string ItemName, int Amount, double Currency); | ||
record OrderResult(bool Processed); | ||
record InventoryItem(string Name, double PerItemCost, int Quantity); | ||
public record OrderPayload(string Name, double TotalCost, int Quantity = 1); | ||
public record InventoryRequest(string RequestId, string ItemName, int Quantity); | ||
public record InventoryResult(bool Success, InventoryItem orderPayload); | ||
public record PaymentRequest(string RequestId, string ItemName, int Amount, double Currency); | ||
public record OrderResult(bool Processed); | ||
public record InventoryItem(string Name, double PerItemCost, int Quantity); | ||
} |
5 changes: 2 additions & 3 deletions
5
examples/Workflow/WorkflowConsoleApp/Workflows/OrderProcessingWorkflow.cs
This file contains 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
86 changes: 86 additions & 0 deletions
86
examples/Workflow/WorkflowUnitTest/OrderProcessingTests.cs
This file contains 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,86 @@ | ||
using System.Threading.Tasks; | ||
using Dapr.Workflow; | ||
using Microsoft.DurableTask; | ||
using Moq; | ||
using WorkflowConsoleApp.Activities; | ||
using WorkflowConsoleApp.Models; | ||
using WorkflowConsoleApp.Workflows; | ||
using Xunit; | ||
|
||
namespace WorkflowUnitTest | ||
{ | ||
[Trait("Example", "true")] | ||
public class OrderProcessingTests | ||
{ | ||
[Fact] | ||
public async Task TestSuccessfulOrder() | ||
{ | ||
// Test payloads | ||
OrderPayload order = new(Name: "Paperclips", TotalCost: 99.95, Quantity: 10); | ||
PaymentRequest expectedPaymentRequest = new(It.IsAny<string>(), order.Name, order.Quantity, order.TotalCost); | ||
InventoryRequest expectedInventoryRequest = new(It.IsAny<string>(), order.Name, order.Quantity); | ||
InventoryResult inventoryResult = new(Success: true, new InventoryItem(order.Name, 9.99, order.Quantity)); | ||
|
||
// Mock the call to ReserveInventoryActivity | ||
Mock<WorkflowContext> mockContext = new(); | ||
mockContext | ||
.Setup(ctx => ctx.CallActivityAsync<InventoryResult>(nameof(ReserveInventoryActivity), It.IsAny<InventoryRequest>(), It.IsAny<TaskOptions>())) | ||
.Returns(Task.FromResult(inventoryResult)); | ||
|
||
// Run the workflow directly | ||
OrderResult result = await new OrderProcessingWorkflow().RunAsync(mockContext.Object, order); | ||
|
||
// Verify that workflow result matches what we expect | ||
Assert.NotNull(result); | ||
Assert.True(result.Processed); | ||
|
||
// Verify that ReserveInventoryActivity was called with a specific input | ||
mockContext.Verify( | ||
ctx => ctx.CallActivityAsync<InventoryResult>(nameof(ReserveInventoryActivity), expectedInventoryRequest, It.IsAny<TaskOptions>()), | ||
Times.Once()); | ||
|
||
// Verify that ProcessPaymentActivity was called with a specific input | ||
mockContext.Verify( | ||
ctx => ctx.CallActivityAsync(nameof(ProcessPaymentActivity), expectedPaymentRequest, It.IsAny<TaskOptions>()), | ||
Times.Once()); | ||
|
||
// Verify that there were two calls to NotifyActivity | ||
mockContext.Verify( | ||
ctx => ctx.CallActivityAsync(nameof(NotifyActivity), It.IsAny<Notification>(), It.IsAny<TaskOptions>()), | ||
Times.Exactly(2)); | ||
} | ||
|
||
[Fact] | ||
public async Task TestInsufficientInventory() | ||
{ | ||
// Test payloads | ||
OrderPayload order = new(Name: "Paperclips", TotalCost: 99.95, Quantity: int.MaxValue); | ||
InventoryRequest expectedInventoryRequest = new(It.IsAny<string>(), order.Name, order.Quantity); | ||
InventoryResult inventoryResult = new(Success: false, null); | ||
|
||
// Mock the call to ReserveInventoryActivity | ||
Mock<WorkflowContext> mockContext = new(); | ||
mockContext | ||
.Setup(ctx => ctx.CallActivityAsync<InventoryResult>(nameof(ReserveInventoryActivity), It.IsAny<InventoryRequest>(), It.IsAny<TaskOptions>())) | ||
.Returns(Task.FromResult(inventoryResult)); | ||
|
||
// Run the workflow directly | ||
OrderResult result = await new OrderProcessingWorkflow().RunAsync(mockContext.Object, order); | ||
|
||
// Verify that ReserveInventoryActivity was called with a specific input | ||
mockContext.Verify( | ||
ctx => ctx.CallActivityAsync<InventoryResult>(nameof(ReserveInventoryActivity), expectedInventoryRequest, It.IsAny<TaskOptions>()), | ||
Times.Once()); | ||
|
||
// Verify that ProcessPaymentActivity was never called | ||
mockContext.Verify( | ||
ctx => ctx.CallActivityAsync(nameof(ProcessPaymentActivity), It.IsAny<PaymentRequest>(), It.IsAny<TaskOptions>()), | ||
Times.Never()); | ||
|
||
// Verify that there were two calls to NotifyActivity | ||
mockContext.Verify( | ||
ctx => ctx.CallActivityAsync(nameof(NotifyActivity), It.IsAny<Notification>(), It.IsAny<TaskOptions>()), | ||
Times.Exactly(2)); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
examples/Workflow/WorkflowUnitTest/WorkflowUnitTest.csproj
This file contains 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,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>10</LangVersion> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> | ||
<PackageReference Include="Moq" Version="4.18.4" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\WorkflowConsoleApp\WorkflowConsoleApp.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains 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,98 @@ | ||
// ------------------------------------------------------------------------ | ||
// 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. | ||
// ------------------------------------------------------------------------ | ||
|
||
namespace Dapr.Workflow | ||
{ | ||
using System; | ||
using Microsoft.DurableTask; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
|
||
class DaprWorkflowContext : WorkflowContext | ||
{ | ||
readonly TaskOrchestrationContext innerContext; | ||
|
||
internal DaprWorkflowContext(TaskOrchestrationContext innerContext) | ||
{ | ||
this.innerContext = innerContext ?? throw new ArgumentNullException(nameof(innerContext)); | ||
} | ||
|
||
public override string Name => this.innerContext.Name; | ||
|
||
public override string InstanceId => this.innerContext.InstanceId; | ||
|
||
public override DateTime CurrentUtcDateTime => this.innerContext.CurrentUtcDateTime; | ||
|
||
public override bool IsReplaying => this.innerContext.IsReplaying; | ||
|
||
public override Task CallActivityAsync(string name, object? input = null, TaskOptions? options = null) | ||
{ | ||
return this.innerContext.CallActivityAsync(name, input, options); | ||
} | ||
|
||
public override Task<T> CallActivityAsync<T>(string name, object? input = null, TaskOptions? options = null) | ||
{ | ||
return this.innerContext.CallActivityAsync<T>(name, input, options); | ||
} | ||
|
||
public override Task CreateTimer(TimeSpan delay, CancellationToken cancellationToken = default) | ||
{ | ||
return this.innerContext.CreateTimer(delay, cancellationToken); | ||
} | ||
|
||
public override Task CreateTimer(DateTime fireAt, CancellationToken cancellationToken) | ||
{ | ||
return this.innerContext.CreateTimer(fireAt, cancellationToken); | ||
} | ||
|
||
public override Task<T> WaitForExternalEventAsync<T>(string eventName, CancellationToken cancellationToken = default) | ||
{ | ||
return this.innerContext.WaitForExternalEvent<T>(eventName, cancellationToken); | ||
} | ||
|
||
public override Task<T> WaitForExternalEventAsync<T>(string eventName, TimeSpan timeout) | ||
{ | ||
return this.innerContext.WaitForExternalEvent<T>(eventName, timeout); | ||
} | ||
|
||
public override void SendEvent(string instanceId, string eventName, object payload) | ||
{ | ||
this.innerContext.SendEvent(instanceId, eventName, payload); | ||
} | ||
|
||
public override void SetCustomStatus(object? customStatus) | ||
{ | ||
this.innerContext.SetCustomStatus(customStatus); | ||
} | ||
|
||
public override Task<TResult> CallChildWorkflowAsync<TResult>(string workflowName, object? input = null, TaskOptions? options = null) | ||
{ | ||
return this.innerContext.CallSubOrchestratorAsync<TResult>(workflowName, input, options); | ||
} | ||
|
||
public override Task CallChildWorkflowAsync(string workflowName, object? input = null, TaskOptions? options = null) | ||
{ | ||
return this.innerContext.CallSubOrchestratorAsync(workflowName, input, options); | ||
} | ||
|
||
public override void ContinueAsNew(object? newInput = null, bool preserveUnprocessedEvents = true) | ||
{ | ||
this.innerContext.ContinueAsNew(newInput!, preserveUnprocessedEvents); | ||
} | ||
|
||
public override Guid NewGuid() | ||
{ | ||
return this.innerContext.NewGuid(); | ||
} | ||
} | ||
} |
Oops, something went wrong.