public void ResetResetsCurrentStateOfStateMachine() { var sm = new StateMachine("sm"); var initial = sm.CreateInitialState("initial"); var state1 = sm.CreateState("state1"); var evt = sm.CreateEvent("evt"); initial.TransitionOn(evt).To(state1); evt.Fire(); sm.Reset(); Assert.AreEqual(initial, sm.CurrentState); }
public void ResettingStateMachineRemovesFault() { var exception = new Exception("foo"); var sm = new StateMachine("sm"); var initial = sm.CreateInitialState("initial").WithEntry(i => { throw exception; }); var evt = sm.CreateEvent("evt"); initial.TransitionOn(evt).To(initial); Assert.Throws<TransitionFailedException>(() => evt.TryFire()); sm.Reset(); Assert.False(sm.IsFaulted); Assert.Null(sm.Fault); }
public void ResetResetsStateOfChildStateMachines() { var parent = new StateMachine("parent"); var initialState = parent.CreateInitialState("initialState"); var state1 = parent.CreateState("state1"); var child = state1.CreateChildStateMachine(); var substate1 = child.CreateInitialState("substate1"); var substate2 = child.CreateState("substate2"); var evt = parent.CreateEvent("evt"); initialState.TransitionOn(evt).To(state1); substate1.TransitionOn(evt).To(substate2); evt.Fire(); evt.Fire(); parent.Reset(); Assert.IsNull(child.CurrentState); }
public void ResetEmptiesTransitionQueue() { var sm = new StateMachine("parent"); var state1 = sm.CreateInitialState("state1"); var state2 = sm.CreateState("state2"); var evt = new Event("evt"); state1.TransitionOn(evt).To(state2).WithHandler(i => { evt.Fire(); sm.Reset(); }); state2.TransitionOn(evt).To(state1); evt.Fire(); // Make sure that the queued event didn't get fired Assert.AreEqual(state2, sm.CurrentState); // Make sure nothing's been queued evt.Fire(); Assert.AreEqual(state1, sm.CurrentState); }
public void StateMachineDoesNotResetUntilActionIsInvoked() { // Need to transition, so we can tell when it's reset var sm = new StateMachine("sm"); var initial = sm.CreateInitialState("initial"); var state1 = sm.CreateState("state1"); var evt = new Event("evt"); initial.TransitionOn(evt).To(state1); evt.Fire(); var synchronizer = new Mock<IStateMachineSynchronizer>(); sm.Synchronizer = synchronizer.Object; Action resetAction = null; synchronizer.Setup(x => x.Reset(It.IsAny<Action>())).Callback((Action action) => resetAction = action); sm.Reset(); Assert.AreEqual(state1, sm.CurrentState); Assert.NotNull(resetAction); resetAction(); Assert.AreEqual(initial, sm.CurrentState); }
public void ResetCallsSynchronizerReset() { var synchronizer = new Mock<IStateMachineSynchronizer>(); var sm = new StateMachine("sm"); sm.Synchronizer = synchronizer.Object; sm.Reset(); synchronizer.Verify(x => x.Reset(It.IsAny<Action>())); }
public static void TransitionGuards() { var stateMachine = new StateMachine(); var stateA = stateMachine.CreateInitialState("State A"); var stateB = stateMachine.CreateState("State B"); var stateC = stateMachine.CreateState("State C"); var eventE = new Event("Event E"); /////////////////////// bool allowTransitionToStateB = false; stateA.TransitionOn(eventE).To(stateB).WithGuard(info => allowTransitionToStateB); stateA.TransitionOn(eventE).To(stateC); eventE.Fire(); Assert.AreEqual(stateC, stateMachine.CurrentState); /////////////////////// stateMachine.Reset(); /////////////////////// // Alternatively... allowTransitionToStateB = true; eventE.Fire(); Assert.AreEqual(stateB, stateMachine.CurrentState); /////////////////////// }
public static void SerializationAndDeserialization() { var stateMachine = new StateMachine(); var stateA = stateMachine.CreateInitialState("StateA"); var stateB = stateMachine.CreateState("StateB"); var evt = new Event(); stateA.TransitionOn(evt).To(stateB); // Move us out of the default state evt.Fire(); Assert.AreEqual(stateB, stateMachine.CurrentState); string serialized = stateMachine.Serialize(); // Reset the state machine stateMachine.Reset(); Assert.AreEqual(stateA, stateMachine.CurrentState); // Deserialize into it stateMachine.Deserialize(serialized); Assert.AreEqual(stateB, stateMachine.CurrentState); }
public static void DynamicTransitions() { var stateMachine = new StateMachine(); var stateA = stateMachine.CreateInitialState("State A"); var stateB = stateMachine.CreateState("State B"); var stateC = stateMachine.CreateState("State C"); var eventE = new Event("Event E"); /////////////////////// State stateToTransitionTo = stateB; stateA.TransitionOn(eventE).ToDynamic(info => stateToTransitionTo); eventE.Fire(); Assert.AreEqual(stateB, stateMachine.CurrentState); /////////////////////// stateMachine.Reset(); /////////////////////// // Alternatively... stateToTransitionTo = stateC; eventE.Fire(); Assert.AreEqual(stateC, stateMachine.CurrentState); }