From d3f379e6c509eea6b89d1bca0c0cd0c132e05d18 Mon Sep 17 00:00:00 2001 From: Andrew Yuan Date: Tue, 3 Nov 2026 10:51:08 -0500 Subject: [PATCH] Ensure .NET harness workers stop after each feature Stop workers when features return no workflow handle or fail during execution or checks. Preserve the original feature failure when worker cleanup also fails. Add nine regression cases and run the harness test project in CI. --- .github/workflows/ci.yaml | 2 +- dotnet.csproj | 3 +- .../Temporalio.Features.Harness/Runner.cs | 37 ++-- tests/dotnet/RunnerTests.cs | 158 ++++++++++++++++++ .../Temporalio.Features.Harness.Tests.csproj | 16 ++ 5 files changed, 203 insertions(+), 13 deletions(-) create mode 100644 tests/dotnet/RunnerTests.cs create mode 100644 tests/dotnet/Temporalio.Features.Harness.Tests.csproj diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index c945ec9b..1a692860 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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: diff --git a/dotnet.csproj b/dotnet.csproj index cc990406..9d4f4baa 100644 --- a/dotnet.csproj +++ b/dotnet.csproj @@ -48,6 +48,7 @@ + - \ No newline at end of file + diff --git a/harness/dotnet/Temporalio.Features.Harness/Runner.cs b/harness/dotnet/Temporalio.Features.Harness/Runner.cs index 03043e72..c27bd176 100644 --- a/harness/dotnet/Temporalio.Features.Harness/Runner.cs +++ b/harness/dotnet/Temporalio.Features.Harness/Runner.cs @@ -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); + } } } diff --git a/tests/dotnet/RunnerTests.cs b/tests/dotnet/RunnerTests.cs new file mode 100644 index 00000000..66ed263c --- /dev/null +++ b/tests/dotnet/RunnerTests.cs @@ -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 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; + } + } +} diff --git a/tests/dotnet/Temporalio.Features.Harness.Tests.csproj b/tests/dotnet/Temporalio.Features.Harness.Tests.csproj new file mode 100644 index 00000000..fd36d824 --- /dev/null +++ b/tests/dotnet/Temporalio.Features.Harness.Tests.csproj @@ -0,0 +1,16 @@ + + + net8.0 + enable + enable + true + false + true + + + + + + + +