public async Task TestNextState() { var context = StateMachineContext.Create <BasicEnum, TestTransitionEnum, object>(null, new StateMachineState <BasicEnum, TestTransitionEnum, object>(BasicEnum.Start, null, null, null), null); await Task.Delay(100); context.SetState(new StateMachineState <BasicEnum, TestTransitionEnum, object>(BasicEnum.Stop, null, null, null)); Assert.IsTrue(context.TotalElapsedTime > context.StateElapsedTime); Assert.AreEqual(BasicEnum.Stop, context.CurrentState.State); }
public async Task TestNewContext() { var context = StateMachineContext.Create <BasicEnum, TestTransitionEnum, object>(null, new StateMachineState <BasicEnum, TestTransitionEnum, object>(BasicEnum.Start, null, null, null), null); Assert.IsNotNull(context); Assert.AreEqual(BasicEnum.Start, context.CurrentState.State); await Task.Delay(100); Assert.IsTrue(context.StateElapsedTime > TimeSpan.Zero); Assert.IsTrue(context.TotalElapsedTime > TimeSpan.Zero); }
/// <summary> /// Starts a state machine. /// </summary> /// <param name="startState">The start state.</param> /// <param name="payload">The payload.</param> /// <param name="executeStageEntry">True executes the OnEnter function of the first state, if it exists.</param> /// <returns>The state machine context.</returns> public StateMachineContext <TState, TTransition, TPayload> StartStateMachine(TState startState, TPayload payload, bool executeStageEntry) { var currentState = this.states[startState]; var context = StateMachineContext.Create(this, currentState, payload); lock (this.contexts) { this.contexts.Add(context); } // If true, fire off the first stage. if (true == executeStageEntry) { currentState.InvokeEnter(context); } return(context); }
public void TestNextState_Throws() { var context = StateMachineContext.Create <BasicEnum, TestTransitionEnum, object>(null, new StateMachineState <BasicEnum, TestTransitionEnum, object>(BasicEnum.Start, null, null, null), null); Assert.ThrowsException <ArgumentException>(() => context.SetState(new StateMachineState <BasicEnum, TestTransitionEnum, object>(BasicEnum.Start, null, null, null))); }