public void SendSynchronously_should_not_hang_when_raising_BeginDispach_event() { var finishedEvent = new AutoResetEvent(false); EventHandler onLoad = ((sender, e) => { using (var machine = new TestMachine <State, Event, EventArgs>()) { machine.AddTransition(State.S1, Event.S1_to_S2, State.S2); machine.BeginDispatch += (sender1, e1) => { }; machine.Start(State.S1); machine.SendSynchronously(Event.S1_to_S2); finishedEvent.Set(); } }); Form form = null; ThreadPool.QueueUserWorkItem(state => { // The form must be instantiated in the future ui thread. // Otherwise, a WindowsFormSynchronizationContext would be installed // on the test thread (which has no message-pump), and will cause hangups. form = new Form(); form.Load += onLoad; Application.Run(form); }); var finished = finishedEvent.WaitOne(TimeSpan.FromSeconds(10)); Assert.IsTrue(finished); form.Invoke((MethodInvoker)form.Dispose); }
public void Superstate_should_handle_event_when_guard_of_substate_does_not_pass() { using (var machine = new TestMachine <State, Event, EventArgs>()) { machine.SetupSubstates(State.S1, HistoryType.None, State.S1_1, State.S1_2); machine.AddTransition(State.S1_1, Event.E1, State.S1_2); machine.AddTransition(State.S1_2, Event.E1, (sender, args) => false, State.S1_2); machine.AddTransition(State.S1, Event.E1, State.S2); IActiveStateMachine <State, Event, EventArgs> m = machine; registerMachineEvents(m); machine.Start(State.S1); m.SendSynchronously(Event.E1); Assert.AreEqual(State.S1_2, m.CurrentStateID); machine.SendSynchronously(Event.E1); Assert.AreEqual(State.S2, m.CurrentStateID); m.WaitForPendingEvents(); assertMachineEvents(true, false, true, false); } }