public void CustomTypesForStatesAndEvents( AsyncPassiveStateMachine <MyState, MyEvent> machine, bool arrivedInStateB) { "establish a state machine with custom types for states and events"._(async() => { machine = new AsyncPassiveStateMachine <MyState, MyEvent>(); machine.In(new MyState("A")) .On(new MyEvent(1)).Goto(new MyState("B")); machine.In(new MyState("B")) .ExecuteOnEntry(() => arrivedInStateB = true); await machine.Initialize(new MyState("A")); await machine.Start(); }); "when using the state machine"._(() => machine.Fire(new MyEvent(1))); "it should use equals to compare states and events"._(() => arrivedInStateB.Should().BeTrue("state B should be current state")); }
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(() => { machine = new AsyncPassiveStateMachine <int, int>(); machine.In(State) .ExecuteOnExit((int a) => passedArgument = a) .On(Event).Goto(AnotherState); machine.In(AnotherState) .ExecuteOnEntry((int a) => passedArgument = a); }); "when leaving the state".x(async() => { await machine.Initialize(State); await machine.Start(); await machine.Fire(Event, argument); }); "it should pass event argument to exit action".x(() => passedArgument.Should().Be(argument)); }
public void EventsQueueing( IAsyncStateMachine <string, int> machine) { const int firstEvent = 0; const int secondEvent = 1; bool arrived = false; "establish a passive state machine with transitions".x(() => { machine = new AsyncPassiveStateMachine <string, int>(); machine.In("A").On(firstEvent).Goto("B"); machine.In("B").On(secondEvent).Goto("C"); machine.In("C").ExecuteOnEntry(() => arrived = true); machine.Initialize("A"); }); "when firing an event onto the state machine".x(() => { machine.Fire(firstEvent); machine.Fire(secondEvent); machine.Start(); }); "it should queue event at the end".x(() => arrived.Should().BeTrue("state machine should arrive at destination state")); }
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; bool commonAncestorStateLeft = false; "establish a hierarchical state machine"._(async() => { machine = new AsyncPassiveStateMachine <int, int>(); machine.DefineHierarchyOn(commonAncestorState) .WithHistoryType(HistoryType.None) .WithInitialSubState(parentOfSourceState) .WithSubState(parentOfDestinationState); machine.DefineHierarchyOn(parentOfSourceState) .WithHistoryType(HistoryType.None) .WithInitialSubState(sourceState) .WithSubState(siblingOfSourceState); machine.DefineHierarchyOn(parentOfDestinationState) .WithHistoryType(HistoryType.None) .WithInitialSubState(destinationState) .WithSubState(siblingOfDestinationState); machine.In(sourceState) .On(Event).Goto(destinationState); machine.In(commonAncestorState) .ExecuteOnExit(() => commonAncestorStateLeft = true); await machine.Initialize(sourceState); await machine.Start(); }); "when firing an event resulting in a transition with a common ancestor"._(() => machine.Fire(Event)); "the state machine should remain inside common ancestor state"._(() => commonAncestorStateLeft .Should().BeFalse()); }
public void ExitAction( AsyncPassiveStateMachine <int, int> machine, bool exitActionExecuted, bool asyncExitActionExecuted) { "establish a state machine with exit action on a state".x(() => { machine = new AsyncPassiveStateMachine <int, int>(); machine.In(State) .ExecuteOnExit(() => exitActionExecuted = true) .ExecuteOnExit(async() => { asyncExitActionExecuted = true; await Task.Yield(); }) .On(Event).Goto(AnotherState); }); "when leaving the state".x(async() => { await machine.Initialize(State); 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 OtherwiseGuard( AsyncPassiveStateMachine <int, int> machine, CurrentStateExtension currentStateExtension) { "establish a state machine with otherwise guard and no machting other guard".x(async() => { machine = new AsyncPassiveStateMachine <int, int>(); currentStateExtension = new CurrentStateExtension(); machine.AddExtension(currentStateExtension); machine.In(SourceState) .On(Event) .If(() => Task.FromResult(false)).Goto(ErrorState) .Otherwise().Goto(DestinationState); await machine.Initialize(SourceState); 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 NoMatchingGuard( AsyncPassiveStateMachine <int, int> machine) { bool declined = false; "establish state machine with no matching guard".x(async() => { machine = new AsyncPassiveStateMachine <int, int>(); var currentStateExtension = new CurrentStateExtension(); machine.AddExtension(currentStateExtension); machine.In(SourceState) .On(Event) .If(() => Task.FromResult(false)).Goto(ErrorState); machine.TransitionDeclined += (sender, e) => declined = true; await machine.Initialize(SourceState); 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 MatchingGuard( AsyncPassiveStateMachine <int, int> machine, CurrentStateExtension currentStateExtension) { "establish a state machine with guarded transitions".x(async() => { machine = new AsyncPassiveStateMachine <int, int>(); currentStateExtension = new CurrentStateExtension(); machine.AddExtension(currentStateExtension); machine.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); await machine.Initialize(SourceState); 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 EntryAction( AsyncPassiveStateMachine <int, int> machine, bool entryActionExecuted, bool asyncEntryActionExecuted) { "establish a state machine with entry action on a state".x(() => { machine = new AsyncPassiveStateMachine <int, int>(); machine.In(State) .ExecuteOnEntry(() => entryActionExecuted = true) .ExecuteOnEntry(async() => { asyncEntryActionExecuted = true; await Task.Yield(); }); }); "when entering the state".x(async() => { await machine.Initialize(State); await machine.Start(); }); "it should execute the synchronous entry action".x(() => entryActionExecuted.Should().BeTrue()); "it should execute the asynchronous entry action".x(() => asyncEntryActionExecuted.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 () => { machine = new AsyncPassiveStateMachine<string, int>(Name); machine.AddExtension(extension); machine.In("0") .On(1).Goto("1"); await machine.Initialize("0"); 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 == "1"), A<IState<string, int>>.That.Matches(x => x.Id == "1"), A<ITransitionContext<string, int>>.That.Matches(x => x.EventId.Value == 1))) .MustHaveHappened()); }
public void Start( AsyncPassiveStateMachine <int, int> machine, bool entryActionExecuted) { "establish an initialized state machine".x(() => { machine = new AsyncPassiveStateMachine <int, int>(); machine.AddExtension(this.testExtension); machine.In(TestState) .ExecuteOnEntry(() => entryActionExecuted = true); machine.Initialize(TestState); }); "when starting the state machine".x(() => machine.Start()); "should set current state of state machine to state to which it is initialized".x(() => this.testExtension.CurrentState.Should().Be(TestState)); "should execute entry action of state to which state machine is initialized".x(() => entryActionExecuted.Should().BeTrue()); }
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(() => { machine = new AsyncPassiveStateMachine <int, int>(); machine.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.TransitionExceptionThrown += (s, e) => receivedException.Add(e.Exception); }); "when entering the state".x(async() => { await machine.Initialize(State); 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 CustomFactory( StandardFactory <string, int> factory) { "establish a custom factory".x(() => factory = A.Fake <StandardFactory <string, int> >()); "when creating a passive state machine".x(() => { var machine = new AsyncPassiveStateMachine <string, int>("_", factory); machine.In("initial").On(42).Goto("answer"); }); "it should use custom factory to create internal instances".x(() => A.CallTo(factory).MustHaveHappened()); }
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(() => { machine = new AsyncPassiveStateMachine <int, int>(); machine.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); }); "when leaving the state".x(async() => { await machine.Initialize(State); 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(() => { machine = new AsyncPassiveStateMachine <int, int>(); machine.In(State) .ExecuteOnExitParametrized(p => receivedParameter = p, parameter) .ExecuteOnExitParametrized( async p => { asyncReceivedParameter = p; await Task.Yield(); }, parameter) .On(Event).Goto(AnotherState); }); "when leaving the state".x(async() => { await machine.Initialize(State); 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 Initialize( AsyncPassiveStateMachine <int, int> machine, bool entryActionExecuted) { "establish a state machine".x(() => { machine = new AsyncPassiveStateMachine <int, int>(); machine.AddExtension(this.testExtension); machine.In(TestState) .ExecuteOnEntry(() => entryActionExecuted = true); }); "when state machine is initialized".x(() => machine.Initialize(TestState)); "should not yet execute any entry actions".x(() => entryActionExecuted.Should().BeFalse()); }
public void BeforeExecutingEntryActionsHierarchical( AsyncPassiveStateMachine<string, string> machine, IExtension<string, string> extension) { "establish an extension".x(() => extension = A.Fake<IExtension<string, string>>()); "establish a hierarchical state machine using the extension".x(async () => { machine = new AsyncPassiveStateMachine<string, string>(Name); machine.AddExtension(extension); machine.DefineHierarchyOn("A") .WithHistoryType(HistoryType.None) .WithInitialSubState("A0"); machine.In("0") .On("A0").Goto("A0"); await machine.Initialize("0"); await machine.Start(); }); "when firing an event onto the state machine".x(() => machine.Fire("A0")); "it should call EnteringState on registered extensions for entered super states of target state".x(() => A.CallTo(() => extension.EnteringState( A<IStateMachineInformation<string, string>>.That.Matches(x => x.Name == Name && x.CurrentStateId == "A0"), A<IState<string, string>>.That.Matches(x => x.Id == "A"), A<ITransitionContext<string, string>>.That.Matches(x => x.EventId.Value == "A0"))) .MustHaveHappened()); "it should call EnteringState on registered extensions for entered leaf target state".x(() => A.CallTo(() => extension.EnteringState( A<IStateMachineInformation<string, string>>.That.Matches(x => x.Name == Name && x.CurrentStateId == "A0"), A<IState<string, string>>.That.Matches(x => x.Id == "A0"), A<ITransitionContext<string, string>>.That.Matches(x => x.EventId.Value == "A0"))) .MustHaveHappened()); }
public void MultipleEntryActions( AsyncPassiveStateMachine <int, int> machine, bool entryAction1Executed, bool asyncEntryAction1Executed, bool entryAction2Executed, bool asyncEntryAction2Executed) { "establish a state machine with several entry actions on a state".x(() => { machine = new AsyncPassiveStateMachine <int, int>(); machine.In(State) .ExecuteOnEntry(() => entryAction1Executed = true) .ExecuteOnEntry(async() => { asyncEntryAction1Executed = true; await Task.Yield(); }) .ExecuteOnEntry(() => entryAction2Executed = true) .ExecuteOnEntry(async() => { asyncEntryAction2Executed = true; await Task.Yield(); }); }); "when entering the state".x(async() => { await machine.Initialize(State); await machine.Start(); }); "it should execute all entry actions".x(() => new[] { entryAction1Executed, asyncEntryAction1Executed, entryAction2Executed, asyncEntryAction2Executed }.Should().Equal(true, true, true, true)); }
public void EntryActionWithParameter( AsyncPassiveStateMachine <int, int> machine, string receivedParameter, string asyncReceivedParameter) { const string parameter = "parameter"; "establish a state machine with entry action with parameter on a state"._(() => { machine = new AsyncPassiveStateMachine <int, int>(); machine.In(State) .ExecuteOnEntryParametrized(p => receivedParameter = p, parameter) .ExecuteOnEntryParametrized( async p => { asyncReceivedParameter = p; await Task.Yield(); }, parameter); }); "when entering the state"._(async() => { await machine.Initialize(State); await machine.Start(); }); "it should execute the entry synchronous action"._(() => receivedParameter.Should().NotBeNull()); "it should pass parameter to the synchronous entry action"._(() => receivedParameter.Should().Be(parameter)); "it should execute the asynchronous entry action"._(() => asyncReceivedParameter.Should().NotBeNull()); "it should pass parameter to the asynchronous entry action"._(() => asyncReceivedParameter.Should().Be(parameter)); }
public GossipManager(GossipNode gossipNode) { _gossipNode = gossipNode; _cts = new CancellationTokenSource(); _currentStateExtension = new CurrentStateExtension(); _requests = new ConcurrentDictionary <string, BaseMessage>(); _helloTimer = new Timer(new TimerCallback(HelloTimerHandle)); _heartbeatTimer = new Timer(new TimerCallback(HeartbeatTimerHandle)); _fsm = new AsyncPassiveStateMachine <NodeState, GossipEvent>("GossipManager"); _fsm.AddExtension(new ConsoleLogExtension()); _fsm.AddExtension(_currentStateExtension); _fsm.Initialize(NodeState.Initialized); _fsm.In(NodeState.Initialized) .On(GossipEvent.HelloSend) .Goto(NodeState.HelloSent) .Execute(async() => { // set hello timer await SendHello(); }); _fsm.In(NodeState.HelloSent) .On(GossipEvent.HelloAnswer) .Goto(NodeState.Infected) .Execute <HelloResponse>((msg) => { ReceiveHelloAnswer(msg); }) .On(GossipEvent.HelloExpired) .Goto(NodeState.Initialized) .Execute(async() => { // hello timer re-set await SendHello(); }); _fsm.In(NodeState.Infected) .On(GossipEvent.HeartbeatExpired) .Goto(NodeState.Susceptible) .Execute(async() => { await SendHeartbeat(); }) .On(GossipEvent.HeartbeatAnswer) .Goto(NodeState.Infected) .Execute <HeartbeatResponse>((msg) => { IGossipPeer senderPeer = msg.Members?.FirstOrDefault(); MergeLists(senderPeer, msg.Members); }); _fsm.In(NodeState.Infected) .On(GossipEvent.HelloReceive) .Execute <HelloRequest>(async(msg) => { await SendHelloAnswer(msg); }); _fsm.In(NodeState.Susceptible) .On(GossipEvent.HelloReceive) .Execute <HelloRequest>(async(msg) => { await SendHelloAnswer(msg); }) .On(GossipEvent.HeartbeatReceive) .If <HeartbeatRequest>((msg) => { return(_gossipNode.GossipPeer.Heartbeat < msg.Peer.Heartbeat); }) .Goto(NodeState.Infected) .Execute <HeartbeatRequest>(async(msg) => { await ReceiveHeartbeat(msg); }) .On(GossipEvent.HeartbeatAnswer) .If <HeartbeatResponse>((msg) => { return(msg.Members[0].Heartbeat > _gossipNode.GossipPeer.Heartbeat); }).Goto(NodeState.Infected); _fsm.Start(); }
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"._(async() => { machine = new AsyncPassiveStateMachine <int, int>(); machine.AddExtension(CurrentStateExtension); machine.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(); }); machine.In(DestinationState) .ExecuteOnEntry(() => entryActionExecuted = true) .ExecuteOnEntry(async() => { asyncEntryActionExecuted = true; await Task.Yield(); }); await machine.Initialize(SourceState); await machine.Start(); }); "when firing an event onto the state machine"._(() => machine.Fire(Event, Parameter)); "it should execute transition by switching state"._(() => CurrentStateExtension.CurrentState.Should().Be(DestinationState)); "it should execute synchronous transition actions"._(() => actualParameter.Should().NotBeNull()); "it should execute asynchronous transition actions"._(() => asyncActualParameter.Should().NotBeNull()); "it should pass parameters to transition action"._(() => actualParameter.Should().Be(Parameter)); "it should execute synchronous exit action of source state"._(() => exitActionExecuted.Should().BeTrue()); "it should execute asynchronous exit action of source state"._(() => asyncExitActionExecuted.Should().BeTrue()); "it should execute synchronous entry action of destination state"._(() => entryActionExecuted.Should().BeTrue()); "it should execute asynchronous entry action of destination state"._(() => asyncEntryActionExecuted.Should().BeTrue()); }
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"._(async() => { machine = new AsyncPassiveStateMachine <string, int>(); machine.DefineHierarchyOn(parentOfSourceState) .WithHistoryType(HistoryType.None) .WithInitialSubState(sourceState) .WithSubState(siblingOfSourceState); machine.DefineHierarchyOn(parentOfDestinationState) .WithHistoryType(HistoryType.None) .WithInitialSubState(destinationState) .WithSubState(siblingOfDestinationState); machine.DefineHierarchyOn(grandParentOfSourceState) .WithHistoryType(HistoryType.None) .WithInitialSubState(parentOfSourceState); machine.DefineHierarchyOn(grandParentOfDestinationState) .WithHistoryType(HistoryType.None) .WithInitialSubState(parentOfDestinationState); machine.In(sourceState) .ExecuteOnExit(() => log += "exit" + sourceState) .On(Event).Goto(destinationState); machine.In(parentOfSourceState) .ExecuteOnExit(() => log += "exit" + parentOfSourceState); machine.In(destinationState) .ExecuteOnEntry(() => log += "enter" + destinationState); machine.In(parentOfDestinationState) .ExecuteOnEntry(() => log += "enter" + parentOfDestinationState); machine.In(grandParentOfSourceState) .ExecuteOnExit(() => log += "exit" + grandParentOfSourceState); machine.In(grandParentOfDestinationState) .ExecuteOnEntry(() => log += "enter" + grandParentOfDestinationState); await machine.Initialize(sourceState); await machine.Start(); }); "when firing an event resulting in a transition without a common ancestor"._(() => machine.Fire(Event)); "it should execute exit action of source state"._(() => log.Should().Contain("exit" + sourceState)); "it should execute exit action of parents of source state (recursively)"._(() => log .Should().Contain("exit" + parentOfSourceState) .And.Contain("exit" + grandParentOfSourceState)); "it should execute entry action of parents of destination state (recursively)"._(() => log .Should().Contain("enter" + parentOfDestinationState) .And.Contain("enter" + grandParentOfDestinationState)); "it should execute entry action of destination state"._(() => log.Should().Contain("enter" + destinationState)); "it should execute actions from source upwards and then downwards to destination state"._(() => { 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); }); }