Skip to content

Commit f43d460

Browse files
Copilotdanielgerlag
andcommitted
Add test for StopAsync waiting for running steps
Co-authored-by: danielgerlag <[email protected]>
1 parent f1d7343 commit f43d460

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Threading.Tasks;
4+
using WorkflowCore.Interface;
5+
using WorkflowCore.Models;
6+
using Xunit;
7+
using FluentAssertions;
8+
using Microsoft.Extensions.DependencyInjection;
9+
10+
namespace WorkflowCore.IntegrationTests.Scenarios
11+
{
12+
public class StopAsyncWorkflow : IWorkflow
13+
{
14+
internal static DateTime? StepStartTime = null;
15+
internal static DateTime? StepEndTime = null;
16+
17+
public string Id => "StopAsyncWorkflow";
18+
public int Version => 1;
19+
public void Build(IWorkflowBuilder<Object> builder)
20+
{
21+
builder
22+
.StartWith<LongRunningStep>();
23+
}
24+
}
25+
26+
internal class LongRunningStep : StepBodyAsync
27+
{
28+
public override async Task<ExecutionResult> RunAsync(IStepExecutionContext context)
29+
{
30+
StopAsyncWorkflow.StepStartTime = DateTime.Now;
31+
await Task.Delay(5000); // 5 second delay
32+
StopAsyncWorkflow.StepEndTime = DateTime.Now;
33+
return ExecutionResult.Next();
34+
}
35+
}
36+
37+
public class StopAsyncScenario : IDisposable
38+
{
39+
protected IWorkflowHost Host;
40+
protected IPersistenceProvider PersistenceProvider;
41+
42+
public StopAsyncScenario()
43+
{
44+
//setup dependency injection
45+
IServiceCollection services = new ServiceCollection();
46+
services.AddLogging();
47+
services.AddWorkflow(options => options.UsePollInterval(TimeSpan.FromSeconds(3)));
48+
49+
var serviceProvider = services.BuildServiceProvider();
50+
51+
PersistenceProvider = serviceProvider.GetService<IPersistenceProvider>();
52+
Host = serviceProvider.GetService<IWorkflowHost>();
53+
Host.RegisterWorkflow<StopAsyncWorkflow, Object>();
54+
Host.Start();
55+
}
56+
57+
[Fact]
58+
public async Task StopAsync_should_wait_for_running_steps_to_complete()
59+
{
60+
// Arrange
61+
StopAsyncWorkflow.StepStartTime = null;
62+
StopAsyncWorkflow.StepEndTime = null;
63+
64+
// Start a workflow with a long-running step
65+
var workflowId = await Host.StartWorkflow<Object>("StopAsyncWorkflow", null);
66+
67+
// Wait for the step to start executing
68+
await Task.Delay(500);
69+
var stepStartedTime = DateTime.Now;
70+
71+
// Act - Call StopAsync which should wait for the step to complete
72+
var stopwatch = Stopwatch.StartNew();
73+
await Host.StopAsync(default);
74+
stopwatch.Stop();
75+
76+
// Assert
77+
// The step should have started
78+
StopAsyncWorkflow.StepStartTime.Should().NotBeNull("the step should have started");
79+
80+
// The step should have completed
81+
StopAsyncWorkflow.StepEndTime.Should().NotBeNull("the step should have completed before StopAsync returned");
82+
83+
// StopAsync should have taken at least 4 seconds (5 seconds delay minus the 500ms we waited)
84+
stopwatch.ElapsedMilliseconds.Should().BeGreaterThan(4000,
85+
"StopAsync should wait for the running step to complete");
86+
}
87+
88+
public void Dispose()
89+
{
90+
// Dispose is intentionally empty to avoid double-stop
91+
}
92+
}
93+
}

test/WorkflowCore.IntegrationTests/WorkflowCore.IntegrationTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
88
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
99
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
10-
<TargetFrameworks>net6.0</TargetFrameworks>
10+
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
1111
</PropertyGroup>
1212

1313
<ItemGroup>

0 commit comments

Comments
 (0)