Skip to content
Open
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 src/Stateless/StateMachine.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private async Task InternalFireOneAsync(TTrigger trigger, params object[] args)

if (foundHandler == null || foundHandler.UnmetGuardConditions.Any())
{
await _unhandledTriggerAction.ExecuteAsync(representativeState.UnderlyingState, trigger, null);
await _unhandledTriggerAction.ExecuteAsync(representativeState.UnderlyingState, trigger, foundHandler?.UnmetGuardConditions);
return;
}

Expand Down
40 changes: 40 additions & 0 deletions test/Stateless.Tests/AsyncActionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,46 @@ public async Task CanInvokeOnUnhandledTriggerAsyncAction()

Assert.Equal("foo", test); // Should await action
}

[Fact]
public async Task CanInvokeOnUnhandledTriggerAsyncActionWithUnmetGuardDescriptions()
{
const string guardDescription = "Guard failed";
ICollection<string> guardDescriptions = null;
var sm = new StateMachine<State, Trigger>(State.A);

sm.Configure(State.A)
.PermitIfAsync(Trigger.X, State.B, async () => await Task.FromResult(false), guardDescription);

sm.OnUnhandledTriggerAsync((s, t, u) =>
{
guardDescriptions = u;
return TaskResult.Done;
});

await sm.FireAsync(Trigger.X).ConfigureAwait(false);

Assert.Equal(State.A, sm.State);
Assert.NotNull(guardDescriptions);
Assert.Single(guardDescriptions);
Assert.Contains(guardDescription, guardDescriptions);
}

[Fact]
public async Task FireAsyncThrowsGuardDescriptionsWhenGuardFails()
{
const string guardDescription = "Guard failed";
var sm = new StateMachine<State, Trigger>(State.A);

sm.Configure(State.A)
.PermitIfAsync(Trigger.X, State.B, async () => await Task.FromResult(false), guardDescription);

var exception = await Assert.ThrowsAsync<InvalidOperationException>(
() => sm.FireAsync(Trigger.X)).ConfigureAwait(false);

Assert.Contains(guardDescription, exception.Message);
}

[Fact]
public void WhenSyncFireOnUnhandledTriggerAsyncTask()
{
Expand Down
2 changes: 1 addition & 1 deletion test/Stateless.Tests/Stateless.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<DefineConstants>$(DefineConstants);TASKS</DefineConstants>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<TargetFrameworks>net462;net8.0;net9.0</TargetFrameworks>
<TargetFrameworks>net462;net8.0;net9.0;net10.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<AssemblyName>Stateless.Tests</AssemblyName>
<AssemblyOriginatorKeyFile>../../asset/Stateless.snk</AssemblyOriginatorKeyFile>
Expand Down