diff --git a/src/Stateless/EntryActionBehaviour.cs b/src/Stateless/EntryActionBehaviour.cs index 5fb38275..8023cebc 100644 --- a/src/Stateless/EntryActionBehaviour.cs +++ b/src/Stateless/EntryActionBehaviour.cs @@ -95,7 +95,10 @@ public AsyncFrom(TTriggerType trigger, Func action, public override void Execute(Transition transition, object[] args) { - ExecuteAsync(transition, args); + if (transition.Trigger.Equals(Trigger)) + { + base.Execute(transition, args); + } } public override Task ExecuteAsync(Transition transition, object[] args) diff --git a/test/Stateless.Tests/AsyncActionsFixture.cs b/test/Stateless.Tests/AsyncActionsFixture.cs index f44eac22..e8dd5dba 100644 --- a/test/Stateless.Tests/AsyncActionsFixture.cs +++ b/test/Stateless.Tests/AsyncActionsFixture.cs @@ -473,6 +473,19 @@ public void VerifyNotEnterSuperstateWhenDoingInitialTransition() Assert.Equal(State.D, sm.State); } + [Fact] + public void OnEntryFromAsync_WhenTriggeredSynchronously_Throws() + { + var sm = new StateMachine(State.A); + + sm.Configure(State.A).Permit(Trigger.X, State.B); + + sm.Configure(State.B) + .OnEntryFromAsync(Trigger.X, async () => await Task.Run(() => { })); + + Assert.Throws(() => sm.Fire(Trigger.X)); + } + [Fact] public async Task OnEntryFromAsync_WhenTriggered_InvokesAction() { @@ -490,6 +503,25 @@ public async Task OnEntryFromAsync_WhenTriggered_InvokesAction() Assert.True(wasInvoked); } + [Fact] + public void OnEntryFromAsync_WhenEnteringByAnotherTriggerSynchronously_DoesNotThrow() + { + bool wasInvoked = false; + + var sm = new StateMachine(State.A); + + sm.Configure(State.A) + .Permit(Trigger.X, State.B) + .Permit(Trigger.Y, State.B); + + sm.Configure(State.B) + .OnEntryFromAsync(Trigger.X, async () => await Task.Run(() => { wasInvoked = true; })); + + sm.Fire(Trigger.Y); + + Assert.False(wasInvoked); + } + [Fact] public async Task OnEntryFromAsync_WhenEnteringByAnotherTrigger_InvokesAction() { @@ -499,7 +531,7 @@ public async Task OnEntryFromAsync_WhenEnteringByAnotherTrigger_InvokesAction() sm.Configure(State.A) .Permit(Trigger.X, State.B) - .Permit(Trigger.Y, State.B); ; + .Permit(Trigger.Y, State.B); sm.Configure(State.B) .OnEntryFromAsync(Trigger.X, async () => await Task.Run(() => { wasInvoked = true; }));