Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ jobs:
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
- uses: actions/setup-dotnet@c2fa09f4bde5ebb9d1777cf28262a3eb3db3ced7 # v5
- run: dotnet build
- run: dotnet test
- run: dotnet test tests/dotnet/Temporalio.Features.Harness.Tests.csproj

build-ruby:
strategy:
Expand Down
3 changes: 2 additions & 1 deletion dotnet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<ItemGroup>
<Using Include="Microsoft.Extensions.Logging" />
<Using Include="Xunit" />
<InternalsVisibleTo Include="Temporalio.Features.Harness.Tests" />
</ItemGroup>

</Project>
</Project>
37 changes: 26 additions & 11 deletions harness/dotnet/Temporalio.Features.Harness/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,35 @@ public async Task RunAsync(CancellationToken cancellationToken)
Feature.ConfigureWorker(this, WorkerOptions);
StartWorker();

var run = await Feature.ExecuteAsync(this);
if (run == null)
var featureFailed = false;
try
{
Logger.LogInformation("Feature {Feature} returned null", PreparedFeature.Dir);
return;
}

Logger.LogInformation("Checking result of feature {Feature}", PreparedFeature.Dir);
await Feature.CheckResultAsync(this, run);
await Feature.CheckHistoryAsync(this, run);
var run = await Feature.ExecuteAsync(this);
if (run == null)
{
Logger.LogInformation("Feature {Feature} returned null", PreparedFeature.Dir);
return;
}

if (workerState != null)
Logger.LogInformation("Checking result of feature {Feature}", PreparedFeature.Dir);
await Feature.CheckResultAsync(this, run);
await Feature.CheckHistoryAsync(this, run);
}
catch
{
await workerState.StopAndWait();
featureFailed = true;
throw;
}
finally
{
try
{
await StopWorker();
}
catch (Exception e) when (featureFailed)
{
Logger.LogError(e, "Failed stopping worker for feature {Feature}", PreparedFeature.Dir);
}
}
}

Expand Down
158 changes: 158 additions & 0 deletions tests/dotnet/RunnerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
namespace Temporalio.Features.Harness.Tests;

using System.Reflection;
using Microsoft.Extensions.Logging.Abstractions;
using Temporalio.Activities;
using Temporalio.Client;
using Temporalio.Testing;
using Temporalio.Worker;
using Xunit;

public class RunnerTests : IAsyncLifetime
{
private WorkflowEnvironment environment = null!;

public async Task InitializeAsync() =>
environment = await WorkflowEnvironment.StartLocalAsync();

public async Task DisposeAsync() => await environment.DisposeAsync();

public enum Outcome { NoWorkflow, ExecuteFailure, ResultFailure, HistoryFailure, Success }
public enum WorkerLifecycle { Running, Stopped, Restarted, Faulted }

[Theory]
[InlineData(Outcome.NoWorkflow, WorkerLifecycle.Running)]
[InlineData(Outcome.ExecuteFailure, WorkerLifecycle.Running)]
[InlineData(Outcome.ResultFailure, WorkerLifecycle.Running)]
[InlineData(Outcome.HistoryFailure, WorkerLifecycle.Running)]
[InlineData(Outcome.Success, WorkerLifecycle.Running)]
[InlineData(Outcome.NoWorkflow, WorkerLifecycle.Stopped)]
[InlineData(Outcome.NoWorkflow, WorkerLifecycle.Restarted)]
[InlineData(Outcome.ExecuteFailure, WorkerLifecycle.Faulted)]
[InlineData(Outcome.Success, WorkerLifecycle.Faulted)]
public async Task RunAsyncStopsWorker(Outcome outcome, WorkerLifecycle workerLifecycle)
{
var runner = new Runner(
new(environment.Client.Connection.Options.TargetHost!)
{
Namespace = environment.Client.Options.Namespace,
},
Guid.NewGuid().ToString(),
null,
new(typeof(TestFeature)),
NullLoggerFactory.Instance,
null);
var feature = (TestFeature)runner.Feature;
feature.Outcome = outcome;
feature.WorkerLifecycle = workerLifecycle;

try
{
var error = await Record.ExceptionAsync(() => runner.RunAsync(CancellationToken.None));
if (outcome is Outcome.ExecuteFailure or Outcome.ResultFailure or Outcome.HistoryFailure)
{
Assert.Same(feature.Failure, error);
}
else if (workerLifecycle == WorkerLifecycle.Faulted)
{
Assert.Same(feature.WorkerFailure, error);
}
else
{
Assert.Null(error);
}

if (workerLifecycle == WorkerLifecycle.Faulted)
{
try
{
await feature.WorkerTask.WaitAsync(TimeSpan.FromSeconds(5));
}
catch (OperationCanceledException)
{
}
}
else
{
Assert.True(feature.WorkerTask.IsCompleted, "Feature worker is still running");
}
Assert.Equal(outcome != Outcome.NoWorkflow && outcome != Outcome.ExecuteFailure, feature.ResultChecked);
Assert.Equal(outcome is Outcome.HistoryFailure or Outcome.Success, feature.HistoryChecked);

if (workerLifecycle != WorkerLifecycle.Faulted)
{
runner.StartWorker();
}
}
finally
{
try
{
await runner.StopWorker();
}
catch (Exception e) when (e == feature.WorkerFailure)
{
}
}
}

private class TestFeature : IFeature
{
public Outcome Outcome { get; set; }
public WorkerLifecycle WorkerLifecycle { get; set; }
public Exception Failure { get; } = new InvalidOperationException("Feature failed");
public Exception WorkerFailure { get; } = new InvalidOperationException("Worker failed");
public Task WorkerTask { get; private set; } = null!;
public bool ResultChecked { get; private set; }
public bool HistoryChecked { get; private set; }

public void ConfigureWorker(Runner runner, TemporalWorkerOptions options) =>
options.AddAllActivities(this);

[Activity]
public string Echo(string value) => value;

public async Task<WorkflowHandle?> ExecuteAsync(Runner runner)
{
if (WorkerLifecycle is WorkerLifecycle.Stopped or WorkerLifecycle.Restarted)
{
await runner.StopWorker();
}
if (WorkerLifecycle == WorkerLifecycle.Restarted)
{
runner.StartWorker();
}

// Capture the task so clearing runner state alone cannot satisfy the cleanup assertion.
var state = typeof(Runner).GetField("workerState", BindingFlags.Instance | BindingFlags.NonPublic)!
.GetValue(runner);
WorkerTask = state == null ? Task.CompletedTask :
(Task)state.GetType().GetField("workerTask", BindingFlags.Instance | BindingFlags.NonPublic)!
.GetValue(state)!;
if (WorkerLifecycle == WorkerLifecycle.Faulted)
{
// Inject a worker failure without depending on a particular SDK poller failure mode.
state!.GetType().GetField("workerTask", BindingFlags.Instance | BindingFlags.NonPublic)!
.SetValue(state, Task.FromException(WorkerFailure));
}

if (Outcome == Outcome.ExecuteFailure)
{
throw Failure;
}
return Outcome == Outcome.NoWorkflow ? null : runner.Client.GetWorkflowHandle("unused");
}

public Task CheckResultAsync(Runner runner, WorkflowHandle handle)
{
ResultChecked = true;
return Outcome == Outcome.ResultFailure ? Task.FromException(Failure) : Task.CompletedTask;
}

public Task CheckHistoryAsync(Runner runner, WorkflowHandle handle)
{
HistoryChecked = true;
return Outcome == Outcome.HistoryFailure ? Task.FromException(Failure) : Task.CompletedTask;
}
}
}
16 changes: 16 additions & 0 deletions tests/dotnet/Temporalio.Features.Harness.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../../dotnet.csproj" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" PrivateAssets="all" />
</ItemGroup>
</Project>
Loading