Skip to content

Commit

Permalink
Add request unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jviau committed Mar 20, 2023
1 parent 2e8dee2 commit 96361ca
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/Abstractions/TaskActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,28 @@ public abstract class TaskActivity<TInput> : ITaskActivity
/// <returns>The output of the activity as a task.</returns>
public abstract Task RunAsync(TaskActivityContext context, TInput input);
}

/// <inheritdoc cref="TaskActivity{Unit, Unit}" />
public abstract class TaskActivity : ITaskActivity
{
/// <inheritdoc/>
Type ITaskActivity.InputType => typeof(Unit);

/// <inheritdoc/>
Type ITaskActivity.OutputType => typeof(Unit);

/// <inheritdoc/>
async Task<object?> ITaskActivity.RunAsync(TaskActivityContext context, object? input)
{
Check.NotNull(context, nameof(context));
await this.RunAsync(context);
return Unit.Value;
}

/// <summary>
/// Override to implement async (non-blocking) task activity logic.
/// </summary>
/// <param name="context">Provides access to additional context for the current activity execution.</param>
/// <returns>The output of the activity as a task.</returns>
public abstract Task RunAsync(TaskActivityContext context);
}
28 changes: 25 additions & 3 deletions src/Abstractions/TaskOrchestrator.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Logging;

namespace Microsoft.DurableTask;

/// <summary>
Expand Down Expand Up @@ -164,3 +161,28 @@ public abstract class TaskOrchestrator<TInput> : ITaskOrchestrator
/// <returns>A task that completes when the orchestration is complete.</returns>
public abstract Task RunAsync(TaskOrchestrationContext context, TInput input);
}

/// <inheritdoc cref="TaskOrchestrator{Unit, Unit}" />.
public abstract class TaskOrchestrator : ITaskOrchestrator
{
/// <inheritdoc/>
public Type InputType => typeof(Unit);

/// <inheritdoc/>
public Type OutputType => typeof(Unit);

/// <inheritdoc/>
async Task<object?> ITaskOrchestrator.RunAsync(TaskOrchestrationContext context, object? input)
{
Check.NotNull(context, nameof(context));
await this.RunAsync(context);
return Unit.Value;
}

/// <summary>
/// Override to implement task orchestrator logic.
/// </summary>
/// <param name="context">The task orchestrator's context.</param>
/// <returns>A task that completes when the orchestration is complete.</returns>
public abstract Task RunAsync(TaskOrchestrationContext context);
}
62 changes: 62 additions & 0 deletions test/Abstractions.Tests/ActivityRequestTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.


namespace Microsoft.DurableTask.Tests;

public class ActivityRequestTests
{
[Theory]
[InlineData(null)]
[InlineData(10)]
public void Create_Success(int? input)
{
TaskName name = "Test";
IActivityRequest request = ActivityRequest.Create(name, input);

request.GetInput().Should().Be(input);
request.GetTaskName().Should().Be(name);
}

[Theory]
[InlineData(null)]
[InlineData("input")]
public void Create_OfT_Success(string input)
{
TaskName name = "Test";
IActivityRequest<int> request = ActivityRequest.Create<int>(name, input);

request.GetInput().Should().Be(input);
request.GetTaskName().Should().Be(name);
}

[Theory]
[InlineData(null)]
[InlineData("input")]
public async Task RunRequest_Success(string input)
{
TaskName name = "Test";
IActivityRequest request = ActivityRequest.Create(name, input);
Mock<TaskOrchestrationContext> context = new(MockBehavior.Strict);
context.Setup(m => m.CallActivityAsync(name, input, null)).Returns(Task.CompletedTask);

await context.Object.RunAsync(request);

context.Verify(m => m.CallActivityAsync(name, input, null), Times.Once);
}

[Theory]
[InlineData(null)]
[InlineData("input")]
public async Task RunRequest_OfT_Success(string input)
{
TaskName name = "Test";
IActivityRequest<int> request = ActivityRequest.Create<int>(name, input);
Mock<TaskOrchestrationContext> context = new(MockBehavior.Strict);
context.Setup(m => m.CallActivityAsync<int>(name, input, null)).Returns(Task.FromResult(0));

await context.Object.RunAsync(request);

context.Verify(m => m.CallActivityAsync<int>(name, input, null), Times.Once);
}
}
62 changes: 62 additions & 0 deletions test/Abstractions.Tests/OrchestrationRequestTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.


namespace Microsoft.DurableTask.Tests;

public class OrchestrationRequestTests
{
[Theory]
[InlineData(null)]
[InlineData(10)]
public void Create_Success(int? input)
{
TaskName name = "Test";
IOrchestrationRequest request = OrchestrationRequest.Create(name, input);

request.GetInput().Should().Be(input);
request.GetTaskName().Should().Be(name);
}

[Theory]
[InlineData(null)]
[InlineData("input")]
public void Create_OfT_Success(string input)
{
TaskName name = "Test";
IOrchestrationRequest<int> request = OrchestrationRequest.Create<int>(name, input);

request.GetInput().Should().Be(input);
request.GetTaskName().Should().Be(name);
}

[Theory]
[InlineData(null)]
[InlineData("input")]
public async Task RunRequest_Success(string input)
{
TaskName name = "Test";
IOrchestrationRequest request = OrchestrationRequest.Create(name, input);
Mock<TaskOrchestrationContext> context = new(MockBehavior.Strict);
context.Setup(m => m.CallSubOrchestratorAsync(name, input, null)).Returns(Task.CompletedTask);

await context.Object.RunAsync(request);

context.Verify(m => m.CallSubOrchestratorAsync(name, input, null), Times.Once);
}

[Theory]
[InlineData(null)]
[InlineData("input")]
public async Task RunRequest_OfT_Success(string input)
{
TaskName name = "Test";
IOrchestrationRequest<int> request = OrchestrationRequest.Create<int>(name, input);
Mock<TaskOrchestrationContext> context = new(MockBehavior.Strict);
context.Setup(m => m.CallSubOrchestratorAsync<int>(name, input, null)).Returns(Task.FromResult(0));

await context.Object.RunAsync(request);

context.Verify(m => m.CallSubOrchestratorAsync<int>(name, input, null), Times.Once);
}
}

0 comments on commit 96361ca

Please sign in to comment.