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 Reinitialization( AsyncPassiveStateMachine <int, int> machine, Exception receivedException) { "establish an initialized state machine".x(async() => { machine = new AsyncPassiveStateMachine <int, int>(); await machine.Initialize(TestState); }); "when state machine is initialized again".x(async() => { try { await machine.Initialize(TestState); } catch (Exception e) { receivedException = e; } }); "should throw an invalid operation exception".x(() => { receivedException .Should().BeAssignableTo <InvalidOperationException>(); receivedException.Message .Should().Be(ExceptionMessages.StateMachineIsAlreadyInitialized); }); }
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 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 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 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 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 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 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 LoadingAnInitializedStateMachine( IAsyncStateMachine <string, int> machine, Exception receivedException) { "establish an initialized state machine"._(() => { machine = new AsyncPassiveStateMachine <string, int>(); machine.Initialize("initial"); }); "when state machine is loaded"._(async() => { receivedException = await Catch.Exception(async() => await machine.Load(A.Fake <IAsyncStateMachineLoader <string> >())); }); "it should throw invalid operation exception"._(() => { receivedException.Should().BeOfType <InvalidOperationException>(); receivedException.Message.Should().Be(ExceptionMessages.StateMachineIsAlreadyInitialized); }); }
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); }); }
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 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(); }