Skip to content

Commit

Permalink
IHostApplicationLifetime for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolaj-milewski committed Aug 27, 2024
1 parent e51d3c5 commit 408460a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Core/Stateflows.Testing/StateflowsTestClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Stateflows.Common.Registration.Interfaces;
using Stateflows.Testing.StateMachines.Sequence;
using Stateflows.Common.Classes;
using Stateflows.Testing;

namespace StateMachine.IntegrationTests.Utils
{
Expand All @@ -29,15 +30,22 @@ public abstract class StateflowsTestClass

protected ExecutionSequenceObserver ExecutionSequence => ServiceProvider.GetRequiredService<ExecutionSequenceObserver>();

private TestingHost testingHost;
private TestingHost TestingHost => testingHost ??= ServiceProvider.GetRequiredService<TestingHost>();

public virtual void Initialize()
{
ServiceCollection.AddStateflows(b => InitializeStateflows(b));
ServiceCollection.AddSingleton<IExecutionSequenceBuilder, ExecutionSequence>();
ServiceCollection.AddSingleton<TestingHost>();
ServiceCollection.AddSingleton<IHostApplicationLifetime>(services => services.GetRequiredService<TestingHost>());
ServiceCollection.AddLogging(builder => builder.AddConsole());

var hostedServices = ServiceProvider.GetRequiredService<IEnumerable<IHostedService>>();
Task.WaitAll(hostedServices.Select(s => s.StartAsync(new CancellationToken())).ToArray());

TestingHost.StartApplication();

ContextValues.GlobalValues.Clear();
ContextValues.StateValues.Clear();
ContextValues.SourceStateValues.Clear();
Expand All @@ -46,6 +54,8 @@ public virtual void Initialize()

public virtual void Cleanup()
{
TestingHost.StopApplication();

_serviceCollection = null;
_serviceProvider = null;
}
Expand Down
28 changes: 28 additions & 0 deletions Core/Stateflows.Testing/TestingHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Threading;
using Microsoft.Extensions.Hosting;

namespace Stateflows.Testing
{
internal class TestingHost : IHostApplicationLifetime
{
private readonly CancellationTokenSource ApplicationStartedToken = new CancellationTokenSource();
public CancellationToken ApplicationStarted => ApplicationStartedToken.Token;

private readonly CancellationTokenSource ApplicationStoppingToken = new CancellationTokenSource();
public CancellationToken ApplicationStopping => ApplicationStoppingToken.Token;

private readonly CancellationTokenSource ApplicationStoppedToken = new CancellationTokenSource();
public CancellationToken ApplicationStopped => ApplicationStoppedToken.Token;

public void StartApplication()
{
ApplicationStartedToken.Cancel();
}

public void StopApplication()
{
ApplicationStoppingToken.Cancel();
ApplicationStoppedToken.Cancel();
}
}
}

0 comments on commit 408460a

Please sign in to comment.