public void SavingEventsForPassiveStateMachine( AsyncPassiveStateMachine <string, int> machine, StateMachineSaver <string, int> saver) { "establish a state machine".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <string, int>(); stateMachineDefinitionBuilder .In("A") .On(1) .Goto("B"); stateMachineDefinitionBuilder .In("B") .On(2) .Goto("C"); stateMachineDefinitionBuilder .In("C") .On(3) .Goto("A"); machine = stateMachineDefinitionBuilder .WithInitialState("A") .Build() .CreatePassiveStateMachine(); }); "when events are fired".x(async() => { await machine.Fire(1); await machine.Fire(2); await machine.FirePriority(3); await machine.FirePriority(4); }); "and it is saved".x(async() => { saver = new StateMachineSaver <string, int>(); await machine.Save(saver); }); "it should save those events".x(() => { saver .Events .Select(x => x.EventId) .Should() .HaveCount(2) .And .ContainInOrder(1, 2); saver .PriorityEvents .Select(x => x.EventId) .Should() .HaveCount(2) .And .ContainInOrder(4, 3); }); }
public void Loading( StateMachineSaver <State> saver, StateMachineLoader <State> loader, FakeExtension extension, State sourceState, State targetState) { "establish a saved state machine with history"._(async() => { var machine = new AsyncPassiveStateMachine <State, Event>(); DefineMachine(machine); await machine.Initialize(State.A); await machine.Start(); await machine.Fire(Event.S2); // set history of super state S await machine.Fire(Event.B); // set current state to B saver = new StateMachineSaver <State>(); loader = new StateMachineLoader <State>(); await machine.Save(saver); }); "when state machine is loaded"._(async() => { loader.SetCurrentState(saver.CurrentStateId); loader.SetHistoryStates(saver.HistoryStates); extension = new FakeExtension(); var loadedMachine = new AsyncPassiveStateMachine <State, Event>(); loadedMachine.AddExtension(extension); DefineMachine(loadedMachine); await loadedMachine.Load(loader); loadedMachine.TransitionCompleted += (sender, args) => { sourceState = args.StateId; targetState = args.NewStateId; }; await loadedMachine.Start(); await loadedMachine.Fire(Event.S); }); "it should reset current state"._(() => sourceState.Should().Be(State.B)); "it should reset all history states of super states"._(() => targetState.Should().Be(State.S2)); "it should notify extensions"._(() => extension.LoadedCurrentState .Should().BeEquivalentTo(State.B)); }
public void EventArgument( AsyncPassiveStateMachine <int, int> machine, int passedArgument) { const int argument = 17; "establish a state machine with an exit action taking an event argument".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(State) .ExecuteOnExit((int a) => passedArgument = a) .On(Event).Goto(AnotherState); stateMachineDefinitionBuilder .In(AnotherState) .ExecuteOnEntry((int a) => passedArgument = a); machine = stateMachineDefinitionBuilder .WithInitialState(State) .Build() .CreatePassiveStateMachine(); }); "when leaving the state".x(async() => { await machine.Start(); await machine.Fire(Event, argument); }); "it should pass event argument to exit action".x(() => passedArgument.Should().Be(argument)); }
public void ExitAction( AsyncPassiveStateMachine <int, int> machine, bool exitActionExecuted, bool asyncExitActionExecuted) { "establish a state machine with exit action on a state".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(State) .ExecuteOnExit(() => exitActionExecuted = true) .ExecuteOnExit(async() => { asyncExitActionExecuted = true; await Task.Yield(); }) .On(Event).Goto(AnotherState); machine = stateMachineDefinitionBuilder .WithInitialState(State) .Build() .CreatePassiveStateMachine(); }); "when leaving the state".x(async() => { await machine.Start(); await machine.Fire(Event); }); "it should execute the synchronous exit action".x(() => exitActionExecuted.Should().BeTrue()); "it should execute the asynchronous exit action".x(() => asyncExitActionExecuted.Should().BeTrue()); }
public void BeforeExecutingEntryActions( AsyncPassiveStateMachine <string, int> machine, IExtension <string, int> extension) { "establish an extension".x(() => extension = A.Fake <IExtension <string, int> >()); "establish a state machine using the extension".x(async() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <string, int>(); stateMachineDefinitionBuilder .In("0") .On(1) .Goto("1"); machine = stateMachineDefinitionBuilder .WithInitialState("0") .Build() .CreatePassiveStateMachine(Name); machine.AddExtension(extension); await machine.Start(); }); "when firing an event onto the state machine".x(() => machine.Fire(1)); "it should call EnteringState on registered extensions for target state".x(() => A.CallTo(() => extension.EnteringState( A <IStateMachineInformation <string, int> > .That.Matches(x => x.Name == Name && x.CurrentStateId.ExtractOrThrow() == "1"), A <IStateDefinition <string, int> > .That.Matches(x => x.Id == "1"), A <ITransitionContext <string, int> > .That.Matches(x => x.EventId.Value == 1))) .MustHaveHappened()); }
public void CustomTypesForStatesAndEvents( AsyncPassiveStateMachine <MyState, MyEvent> machine, bool arrivedInStateB) { "establish a state machine with custom types for states and events".x(async() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <MyState, MyEvent>(); stateMachineDefinitionBuilder .In(new MyState("A")) .On(new MyEvent(1)).Goto(new MyState("B")); stateMachineDefinitionBuilder .In(new MyState("B")) .ExecuteOnEntry(() => arrivedInStateB = true); machine = stateMachineDefinitionBuilder .WithInitialState(new MyState("A")) .Build() .CreatePassiveStateMachine(); await machine.Start(); }); "when using the state machine".x(() => machine.Fire(new MyEvent(1))); "it should use equals to compare states and events".x(() => arrivedInStateB.Should().BeTrue("state B should be current state")); }
public void MatchingGuard( AsyncPassiveStateMachine <int, int> machine, CurrentStateExtension currentStateExtension) { "establish a state machine with guarded transitions".x(async() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(SourceState) .On(Event) .If(() => false).Goto(ErrorState) .If(async() => await Task.FromResult(false)).Goto(ErrorState) .If(async() => await Task.FromResult(true)).Goto(DestinationState) .If(() => true).Goto(ErrorState) .Otherwise().Goto(ErrorState); machine = stateMachineDefinitionBuilder .WithInitialState(SourceState) .Build() .CreatePassiveStateMachine(); currentStateExtension = new CurrentStateExtension(); machine.AddExtension(currentStateExtension); await machine.Start(); }); "when an event is fired".x(() => machine.Fire(Event)); "it should take transition guarded with first matching guard".x(() => currentStateExtension.CurrentState.Should().Be(DestinationState)); }
public void NoMatchingGuard( AsyncPassiveStateMachine <int, int> machine) { var declined = false; "establish state machine with no matching guard".x(async() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(SourceState) .On(Event) .If(() => Task.FromResult(false)).Goto(ErrorState); machine = stateMachineDefinitionBuilder .WithInitialState(SourceState) .Build() .CreatePassiveStateMachine(); var currentStateExtension = new CurrentStateExtension(); machine.AddExtension(currentStateExtension); machine.TransitionDeclined += (sender, e) => declined = true; await machine.Start(); }); "when an event is fired".x(() => machine.Fire(Event)); "it should notify about declined transition".x(() => declined.Should().BeTrue("TransitionDeclined event should be fired")); }
public void ExitActionException(AsyncPassiveStateMachine <int, int> machine) { "establish an exit action throwing an exception".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(Values.Source) .ExecuteOnExit(() => throw Values.Exception) .On(Values.Event).Goto(Values.Destination); machine = stateMachineDefinitionBuilder .WithInitialState(Values.Source) .Build() .CreatePassiveStateMachine(); machine.TransitionExceptionThrown += (s, e) => this.receivedTransitionExceptionEventArgs = e; }); "when executing the transition".x(async() => { await machine.Start(); await machine.Fire(Values.Event, Values.Parameter); }); this.ItShouldHandleTransitionException(); }
public void OtherwiseGuard( AsyncPassiveStateMachine <int, int> machine, CurrentStateExtension currentStateExtension) { "establish a state machine with otherwise guard and no machting other guard".x(async() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(SourceState) .On(Event) .If(() => Task.FromResult(false)).Goto(ErrorState) .Otherwise().Goto(DestinationState); machine = stateMachineDefinitionBuilder .WithInitialState(SourceState) .Build() .CreatePassiveStateMachine(); currentStateExtension = new CurrentStateExtension(); machine.AddExtension(currentStateExtension); await machine.Start(); }); "when an event is fired".x(() => machine.Fire(Event)); "it should_take_transition_guarded_with_otherwise".x(() => currentStateExtension.CurrentState.Should().Be(DestinationState)); }
public void MultipleExitActions( AsyncPassiveStateMachine <int, int> machine, bool exitAction1Executed, bool exitAction2Executed, bool asyncExitAction1Executed, bool asyncExitAction2Executed) { "establish a state machine with several exit actions on a state".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(State) .ExecuteOnExit(() => exitAction1Executed = true) .ExecuteOnExit(() => exitAction2Executed = true) .ExecuteOnExit(async() => { asyncExitAction1Executed = true; await Task.Yield(); }) .ExecuteOnExit(async() => { asyncExitAction2Executed = true; await Task.Yield(); }) .On(Event).Goto(AnotherState); machine = stateMachineDefinitionBuilder .WithInitialState(State) .Build() .CreatePassiveStateMachine(); }); "when leaving the state".x(async() => { await machine.Start(); await machine.Fire(Event); }); "it should execute all exit actions".x(() => new[] { exitAction1Executed, exitAction2Executed, asyncExitAction1Executed, asyncExitAction2Executed }.Should().Equal(true, true, true, true)); }
public void ExitActionWithParameter( AsyncPassiveStateMachine <int, int> machine, string receivedParameter, string asyncReceivedParameter) { const string parameter = "parameter"; "establish a state machine with exit action with parameter on a state".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(State) .ExecuteOnExitParametrized(p => receivedParameter = p, parameter) .ExecuteOnExitParametrized( async p => { asyncReceivedParameter = p; await Task.Yield(); }, parameter) .On(Event).Goto(AnotherState); machine = stateMachineDefinitionBuilder .WithInitialState(State) .Build() .CreatePassiveStateMachine(); }); "when leaving the state".x(async() => { await machine.Start(); await machine.Fire(Event); }); "it should execute the synchronous exit action".x(() => receivedParameter.Should().NotBeNull()); "it should pass parameter to the synchronous exit action".x(() => receivedParameter.Should().Be(parameter)); "it should execute the asynchronous exit action".x(() => asyncReceivedParameter.Should().NotBeNull()); "it should pass parameter to the asynchronous exit action".x(() => asyncReceivedParameter.Should().Be(parameter)); }
public void NoCommonAncestor( AsyncPassiveStateMachine <string, int> machine) { const string sourceState = "SourceState"; const string parentOfSourceState = "ParentOfSourceState"; const string siblingOfSourceState = "SiblingOfSourceState"; const string destinationState = "DestinationState"; const string parentOfDestinationState = "ParentOfDestinationState"; const string siblingOfDestinationState = "SiblingOfDestinationState"; const string grandParentOfSourceState = "GrandParentOfSourceState"; const string grandParentOfDestinationState = "GrandParentOfDestinationState"; const int Event = 0; var log = string.Empty; "establish a hierarchical state machine".x(async() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <string, int>(); stateMachineDefinitionBuilder .DefineHierarchyOn(parentOfSourceState) .WithHistoryType(HistoryType.None) .WithInitialSubState(sourceState) .WithSubState(siblingOfSourceState); stateMachineDefinitionBuilder .DefineHierarchyOn(parentOfDestinationState) .WithHistoryType(HistoryType.None) .WithInitialSubState(destinationState) .WithSubState(siblingOfDestinationState); stateMachineDefinitionBuilder .DefineHierarchyOn(grandParentOfSourceState) .WithHistoryType(HistoryType.None) .WithInitialSubState(parentOfSourceState); stateMachineDefinitionBuilder .DefineHierarchyOn(grandParentOfDestinationState) .WithHistoryType(HistoryType.None) .WithInitialSubState(parentOfDestinationState); stateMachineDefinitionBuilder .In(sourceState) .ExecuteOnExit(() => log += "exit" + sourceState) .On(Event).Goto(destinationState); stateMachineDefinitionBuilder .In(parentOfSourceState) .ExecuteOnExit(() => log += "exit" + parentOfSourceState); stateMachineDefinitionBuilder .In(destinationState) .ExecuteOnEntry(() => log += "enter" + destinationState); stateMachineDefinitionBuilder .In(parentOfDestinationState) .ExecuteOnEntry(() => log += "enter" + parentOfDestinationState); stateMachineDefinitionBuilder .In(grandParentOfSourceState) .ExecuteOnExit(() => log += "exit" + grandParentOfSourceState); stateMachineDefinitionBuilder .In(grandParentOfDestinationState) .ExecuteOnEntry(() => log += "enter" + grandParentOfDestinationState); machine = stateMachineDefinitionBuilder .WithInitialState(sourceState) .Build() .CreatePassiveStateMachine(); await machine.Start(); }); "when firing an event resulting in a transition without a common ancestor".x(() => machine.Fire(Event)); "it should execute exit action of source state".x(() => log.Should().Contain("exit" + sourceState)); "it should execute exit action of parents of source state (recursively)".x(() => log .Should().Contain("exit" + parentOfSourceState) .And.Contain("exit" + grandParentOfSourceState)); "it should execute entry action of parents of destination state (recursively)".x(() => log .Should().Contain("enter" + parentOfDestinationState) .And.Contain("enter" + grandParentOfDestinationState)); "it should execute entry action of destination state".x(() => log.Should().Contain("enter" + destinationState)); "it should execute actions from source upwards and then downwards to destination state".x(() => { string[] states = { sourceState, parentOfSourceState, grandParentOfSourceState, grandParentOfDestinationState, parentOfDestinationState, destinationState }; var statesInOrderOfAppearanceInLog = states .OrderBy(s => log.IndexOf(s.ToString(CultureInfo.InvariantCulture), StringComparison.Ordinal)); statesInOrderOfAppearanceInLog .Should().Equal(states); }); }
public void NoExceptionHandlerRegistered( AsyncPassiveStateMachine <int, int> machine, Exception catchedException) { "establish an exception throwing state machine without a registered exception handler".x(async() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(Values.Source) .On(Values.Event).Execute(() => throw Values.Exception); machine = stateMachineDefinitionBuilder .WithInitialState(Values.Source) .Build() .CreatePassiveStateMachine(); await machine.Start(); }); "when an exception occurs".x(async() => catchedException = await Catch.Exception(async() => await machine.Fire(Values.Event))); "should (re-)throw exception".x(() => catchedException.InnerException .Should().BeSameAs(Values.Exception)); }
private async void HeartbeatTimerHandle(object state) { await _fsm.Fire(GossipEvent.HeartbeatElapsed); }
public void ExceptionHandling( AsyncPassiveStateMachine <int, int> machine, bool exitAction1Executed, bool exitAction2Executed, bool exitAction3Executed, bool exitAction4Executed) { var exception2 = new Exception(); var exception3 = new Exception(); var exception4 = new Exception(); var receivedException = new List <Exception>(); "establish a state machine with several exit actions on a state and some of them throw an exception".x(() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(State) .ExecuteOnExit(() => exitAction1Executed = true) .ExecuteOnExit(() => { exitAction2Executed = true; throw exception2; }) .ExecuteOnExit(() => { exitAction3Executed = true; throw exception3; }) .ExecuteOnExit(async() => { exitAction4Executed = true; await Task.Yield(); throw exception4; }) .On(Event).Goto(AnotherState); machine = stateMachineDefinitionBuilder .WithInitialState(State) .Build() .CreatePassiveStateMachine(); machine.TransitionExceptionThrown += (s, e) => receivedException.Add(e.Exception); }); "when entering the state".x(async() => { await machine.Start(); await machine.Fire(Event); }); "it should execute all entry actions on entry".x(() => new[] { exitAction1Executed, exitAction2Executed, exitAction3Executed, exitAction4Executed }.Should().Equal(true, true, true, true)); "it should handle all exceptions of all throwing entry actions by firing the TransitionExceptionThrown event".x(() => receivedException .Should().BeEquivalentTo(exception2, exception3, exception4)); }
public void CommonAncestor( AsyncPassiveStateMachine <int, int> machine) { const int commonAncestorState = 0; const int sourceState = 1; const int parentOfSourceState = 2; const int siblingOfSourceState = 3; const int destinationState = 4; const int parentOfDestinationState = 5; const int siblingOfDestinationState = 6; const int Event = 0; var commonAncestorStateLeft = false; "establish a hierarchical state machine".x(async() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .DefineHierarchyOn(commonAncestorState) .WithHistoryType(HistoryType.None) .WithInitialSubState(parentOfSourceState) .WithSubState(parentOfDestinationState); stateMachineDefinitionBuilder .DefineHierarchyOn(parentOfSourceState) .WithHistoryType(HistoryType.None) .WithInitialSubState(sourceState) .WithSubState(siblingOfSourceState); stateMachineDefinitionBuilder .DefineHierarchyOn(parentOfDestinationState) .WithHistoryType(HistoryType.None) .WithInitialSubState(destinationState) .WithSubState(siblingOfDestinationState); stateMachineDefinitionBuilder .In(sourceState) .On(Event).Goto(destinationState); stateMachineDefinitionBuilder .In(commonAncestorState) .ExecuteOnExit(() => commonAncestorStateLeft = true); machine = stateMachineDefinitionBuilder .WithInitialState(sourceState) .Build() .CreatePassiveStateMachine(); await machine.Start(); }); "when firing an event resulting in a transition with a common ancestor".x(() => machine.Fire(Event)); "the state machine should remain inside common ancestor state".x(() => commonAncestorStateLeft .Should().BeFalse()); }
public void ExecutingTransition( AsyncPassiveStateMachine <int, int> machine, string actualParameter, string asyncActualParameter, bool exitActionExecuted, bool entryActionExecuted, bool asyncExitActionExecuted, bool asyncEntryActionExecuted) { "establish a state machine with transitions".x(async() => { var stateMachineDefinitionBuilder = new StateMachineDefinitionBuilder <int, int>(); stateMachineDefinitionBuilder .In(SourceState) .ExecuteOnExit(() => exitActionExecuted = true) .ExecuteOnExit(async() => { asyncExitActionExecuted = true; await Task.Yield(); }) .On(Event).Goto(DestinationState) .Execute((string p) => actualParameter = p) .Execute(async(string p) => { asyncActualParameter = p; await Task.Yield(); }); stateMachineDefinitionBuilder .In(DestinationState) .ExecuteOnEntry(() => entryActionExecuted = true) .ExecuteOnEntry(async() => { asyncEntryActionExecuted = true; await Task.Yield(); }); machine = stateMachineDefinitionBuilder .WithInitialState(SourceState) .Build() .CreatePassiveStateMachine(); machine.AddExtension(CurrentStateExtension); await machine.Start(); }); "when firing an event onto the state machine".x(() => machine.Fire(Event, Parameter)); "it should execute transition by switching state".x(() => CurrentStateExtension.CurrentState.Should().Be(DestinationState)); "it should execute synchronous transition actions".x(() => actualParameter.Should().NotBeNull()); "it should execute asynchronous transition actions".x(() => asyncActualParameter.Should().NotBeNull()); "it should pass parameters to transition action".x(() => actualParameter.Should().Be(Parameter)); "it should execute synchronous exit action of source state".x(() => exitActionExecuted.Should().BeTrue()); "it should execute asynchronous exit action of source state".x(() => asyncExitActionExecuted.Should().BeTrue()); "it should execute synchronous entry action of destination state".x(() => entryActionExecuted.Should().BeTrue()); "it should execute asynchronous entry action of destination state".x(() => asyncEntryActionExecuted.Should().BeTrue()); }
public async Task PerformAction(SubmissionAction action, bool isAdmin) { await _machine.Fire(action, isAdmin); }