public void MsgDispatcherPhysical_ExplicitHandlers() { MsgDispatcher dispatcher; dispatcher = new MsgDispatcher(); dispatcher.AddPhysical(new MsgHandlerDelegate(OnExplicit1), typeof(_DispatchMsg1), null); dispatcher.AddPhysical(new MsgHandlerDelegate(OnExplicit2), typeof(_DispatchMsg2), null); dispatcher.AddPhysical(new MsgHandlerDelegate(OnExplicit3), typeof(_DispatchMsg3), null); Clear(); dispatcher.Dispatch(new _DispatchMsg1()); Thread.Sleep(DispatchWait); Assert.IsTrue(onExplicit1); Assert.IsFalse(onExplicit2); Assert.IsFalse(onExplicit3); Clear(); dispatcher.Dispatch(new _DispatchMsg2()); Thread.Sleep(DispatchWait); Assert.IsFalse(onExplicit1); Assert.IsTrue(onExplicit2); Assert.IsFalse(onExplicit3); Clear(); dispatcher.Dispatch(new _DispatchMsg3()); Thread.Sleep(DispatchWait); Assert.IsFalse(onExplicit1); Assert.IsFalse(onExplicit2); Assert.IsTrue(onExplicit3); }
public void MsgDispatcherLogical_Multi_Handlers_Grouped() { Target4A target1 = new Target4A(); Target4B target2 = new Target4B(); MsgDispatcher dispatcher = new MsgDispatcher(); // I'm going to test whether the handlers for the two targets // were grouped properly in the routing table by sending // instances of the two message types to logical://Foo. // // The message receive counts should match the number // of messages sent if grouping worked properly. If // grouping didn't work, we'd expect to see messages // routed randomly to one target or the other with // messages that aren't handled by each target being // dropped. dispatcher.AddTarget(target1, null, null, target1); dispatcher.AddTarget(target2, null, null, target1); for (int i = 0; i < 10; i++) { dispatcher.Dispatch(new _DispatchMsg1("logical://Foo")); dispatcher.Dispatch(new _DispatchMsg2("logical://Foo")); } Thread.Sleep(1000); Assert.AreEqual(10, target1.Count); Assert.AreEqual(10, target2.Count); }
public void MsgDispatcherPhysical_NoHandlers() { Target1 target = new Target1(); MsgDispatcher dispatcher = new MsgDispatcher(); dispatcher.AddTarget(target); dispatcher.Dispatch(new _DispatchMsg1()); dispatcher.Dispatch(new _DispatchMsg2()); Thread.Sleep(DispatchWait); Assert.IsFalse(target.dispatch1); Assert.IsFalse(target.dispatch2); }
public void MsgDispatcherPhysical_ValidHandlers() { Target2 target = new Target2(); MsgDispatcher dispatcher = new MsgDispatcher(); dispatcher.AddTarget(target); dispatcher.Dispatch(new _DispatchMsg1()); dispatcher.Dispatch(new _DispatchMsg2()); dispatcher.Dispatch(new _DispatchMsg3()); Thread.Sleep(DispatchWait); Assert.IsTrue(target.dispatch1); Assert.IsTrue(target.dispatch2); Assert.IsTrue(target.dispatch3); }
private int DispatchWait = 250; // # of milliseconds to wait for messages // dispatches to be handled on worker threads /// <summary> /// Handles the dispatching of a message via a dispatcher and then /// waits a bit of time to give the background threads to actually /// process the message. /// </summary> /// <param name="dispatcher"></param> /// <param name="msg"></param> /// <returns></returns> private bool Dispatch(MsgDispatcher dispatcher, Msg msg) { bool result; result = dispatcher.Dispatch(msg); Thread.Sleep(DispatchWait); return(result); }
public void MsgDispatcherPhysical_DefaultHandler() { Target3 target = new Target3(); MsgDispatcher dispatcher = new MsgDispatcher(); dispatcher.AddTarget(target); dispatcher.Dispatch(new _DispatchMsg1()); dispatcher.Dispatch(new _DispatchMsg2()); Thread.Sleep(DispatchWait); Assert.IsTrue(target.dispatch1); Assert.IsTrue(target.dispatch2); Assert.IsFalse(target.defHandler); dispatcher.Dispatch(new _DispatchMsg3()); Thread.Sleep(DispatchWait); Assert.IsTrue(target.defHandler); }
public void MsgDispatcherLogical_Multi_Handlers_Send() { Target3 target1 = new Target3(); Target3 target2 = new Target3(); MsgDispatcher dispatcher = new MsgDispatcher(); dispatcher.AddTarget(target1); dispatcher.AddTarget(target2); dispatcher.Dispatch(new _DispatchMsg1("logical://Dispatch1")); Thread.Sleep(DispatchWait); Assert.IsTrue(target1.dispatch1 || target2.dispatch1); for (int i = 0; i < 100; i++) { dispatcher.Dispatch(new _DispatchMsg1("logical://Dispatch1")); } Thread.Sleep(DispatchWait); Assert.IsTrue(target1.dispatch1 && target2.dispatch1); }
public void MsgDispatcherLogical_Multi_Handlers_Broadcast() { Target3 target1 = new Target3(); Target3 target2 = new Target3(); MsgDispatcher dispatcher = new MsgDispatcher(); _DispatchMsg1 msg; dispatcher.AddTarget(target1); dispatcher.AddTarget(target2); msg = new _DispatchMsg1("logical://Dispatch1"); msg._Flags |= MsgFlag.Broadcast; dispatcher.Dispatch(msg); Thread.Sleep(DispatchWait); Assert.IsTrue(target1.dispatch1 && target2.dispatch1); }