public void EventStubHandlerCountTests() { var eventStub = new EventStub(typeof(ISampleInterface)); var sampleService = new SampleService(); eventStub.WireTo(sampleService); Assert.AreEqual(0, sampleService.SimpleEventHandlerCount); eventStub.AddHandler("SimpleEvent", new EventHandler((s, e) => { })); Assert.AreEqual(1, sampleService.SimpleEventHandlerCount); eventStub.AddHandler("SimpleEvent", new EventHandler((s, e) => { })); Assert.AreEqual(2, sampleService.SimpleEventHandlerCount); }
public void EventStub_WireUnwireTests() { var eventStub = new EventStub(typeof(ISampleInterface)); var simpleEventFired = false; var cancelEventFired = false; var actionFired = false; var funcFired = false; // add event handlers eventStub.AddHandler("SimpleEvent", new EventHandler((sender, args) => simpleEventFired = true)); eventStub.AddHandler("CancelEvent", new EventHandler <CancelEventArgs>((sender, args) => cancelEventFired = true)); eventStub.AddHandler("ActionDelegate", new Action(() => actionFired = true)); eventStub.AddHandler("FuncDelegate", new Func <int, string>(a => { funcFired = true; return(a.ToString()); })); eventStub.AddHandler("FuncDelegate", new Func <int, string>(a => { return((a * 2).ToString()); })); // wire up events var component = new SampleService(); eventStub.WireTo(component); // test if it works var result = component.FireHandlers(102030); Assert.AreEqual("204060", result); Assert.IsTrue(simpleEventFired); Assert.IsTrue(cancelEventFired); Assert.IsTrue(actionFired); Assert.IsTrue(funcFired); // unwire simpleEventFired = false; cancelEventFired = false; actionFired = false; funcFired = false; eventStub.UnwireFrom(component); // test if it works result = component.FireHandlers(123); Assert.IsNull(result); Assert.IsFalse(simpleEventFired); Assert.IsFalse(cancelEventFired); Assert.IsFalse(actionFired); Assert.IsFalse(funcFired); }
/// <summary> /// Creates wires between client component and server component. /// </summary> /// <param name="type">Implementation type of the server component.</param> /// <param name="eventStub"><see cref="EventStub"/> with cached subscriptions.</param> /// <param name="delegateCorrelationSet">Correlation set (say how to wire)</param> /// <param name="wiringList">Collection of built wires</param> private void CreateClientServerWires(Type type, EventStub eventStub, List <DelegateCorrelationInfo> delegateCorrelationSet, Dictionary <Guid, Delegate> wiringList) { if (delegateCorrelationSet == null) { return; } foreach (var correlationInfo in delegateCorrelationSet) { if (wiringList.ContainsKey(correlationInfo.CorrelationID)) { continue; } if (ServerSession.CurrentSession == null) { throw new InvalidSessionException(string.Format(LanguageResource.InvalidSessionException_SessionIDInvalid, "(null)")); } var dynamicWire = DynamicWireFactory.CreateDynamicWire(type, correlationInfo.DelegateMemberName, correlationInfo.IsEvent); dynamicWire.Interceptor = correlationInfo.ClientDelegateInterceptor; if (correlationInfo.IsEvent) { var dynamicEventWire = (DynamicEventWireBase)dynamicWire; dynamicEventWire.EventFilter = correlationInfo.EventFilter; // add session validation handler and unsubscription callback var sessionId = ServerSession.CurrentSession.SessionID; var sessionManager = _host.SessionManager; dynamicEventWire.ValidateSession = () => sessionManager.ExistSession(sessionId); dynamicEventWire.CancelSubscription = () => eventStub.RemoveHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate); eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate); wiringList.Add(correlationInfo.CorrelationID, dynamicEventWire.InDelegate); } else { eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicWire.InDelegate); wiringList.Add(correlationInfo.CorrelationID, dynamicWire.InDelegate); } } }
public void EventStub_FuncDelegateTests() { // add the first handler var eventStub = new EventStub(typeof(ISampleInterface)); var fired = false; eventStub.AddHandler("FuncDelegate", new Func <int, string>(a => { fired = true; return(a.ToString()); })); // check if it is called var handler = (Func <int, string>)eventStub["FuncDelegate"]; var result = handler(123); Assert.IsTrue(fired); Assert.AreEqual("123", result); // add the second handler fired = false; var firedAgain = false; var tempHandler = new Func <int, string>(a => { firedAgain = true; return(a.ToString()); }); eventStub.AddHandler("FuncDelegate", tempHandler); // check if it is called result = handler(321); Assert.IsTrue(fired); Assert.IsTrue(firedAgain); Assert.AreEqual("321", result); // remove the second handler fired = false; firedAgain = false; eventStub.RemoveHandler("FuncDelegate", tempHandler); // check if it is not called result = handler(0); Assert.IsTrue(fired); Assert.IsFalse(firedAgain); Assert.AreEqual("0", result); }
public void EventStub_ActionDelegateTests() { // add the first handler var eventStub = new EventStub(typeof(ISampleInterface)); var fired = false; eventStub.AddHandler("ActionDelegate", new Action(() => fired = true)); // check if it is called var handler = (Action)eventStub["ActionDelegate"]; handler(); Assert.IsTrue(fired); // add the second handler fired = false; var firedAgain = false; var tempHandler = new Action(() => firedAgain = true); eventStub.AddHandler("ActionDelegate", tempHandler); // check if it is called handler(); Assert.IsTrue(fired); Assert.IsTrue(firedAgain); // remove the second handler fired = false; firedAgain = false; eventStub.RemoveHandler("ActionDelegate", tempHandler); // check if it is not called handler(); Assert.IsTrue(fired); Assert.IsFalse(firedAgain); }
public void EventStub_CancelEventTests() { // add the first handler var eventStub = new EventStub(typeof(ISampleInterface)); var fired = false; eventStub.AddHandler("CancelEvent", new EventHandler <CancelEventArgs>((sender, args) => fired = true)); // check if it is called var handler = (EventHandler <CancelEventArgs>)eventStub["CancelEvent"]; handler(this, new CancelEventArgs()); Assert.IsTrue(fired); // add the second handler fired = false; var firedAgain = false; var tempHandler = new EventHandler <CancelEventArgs>((sender, args) => firedAgain = true); eventStub.AddHandler("CancelEvent", tempHandler); // check if it is called handler(this, new CancelEventArgs()); Assert.IsTrue(fired); Assert.IsTrue(firedAgain); // remove the second handler fired = false; firedAgain = false; eventStub.RemoveHandler("CancelEvent", tempHandler); // check if it is not called handler(this, new CancelEventArgs()); Assert.IsTrue(fired); Assert.IsFalse(firedAgain); }
/// <summary> /// Creates wires between client component and server component. /// </summary> /// <param name="type">Implementation type of the server component.</param> /// <param name="eventStub"><see cref="EventStub"/> with cached subscriptions.</param> /// <param name="delegateCorrelationSet">Correlation set (say how to wire)</param> /// <param name="wiringList">Collection of built wires</param> private void CreateClientServerWires(Type type, EventStub eventStub, IEnumerable <DelegateCorrelationInfo> delegateCorrelationSet, Dictionary <Guid, Delegate> wiringList) { if (delegateCorrelationSet == null) { return; } var currentSession = ServerSession.CurrentSession; if (currentSession == null) { throw new InvalidSessionException(string.Format(LanguageResource.InvalidSessionException_SessionIDInvalid, "(null)")); } foreach (var correlationInfo in delegateCorrelationSet) { if (wiringList.ContainsKey(correlationInfo.CorrelationID)) { continue; } var dynamicWire = DynamicWireFactory.CreateDynamicWire(type, correlationInfo.DelegateMemberName, correlationInfo.IsEvent); dynamicWire.Interceptor = correlationInfo.ClientDelegateInterceptor; lock (wiringList) { if (wiringList.ContainsKey(correlationInfo.CorrelationID)) { continue; } if (correlationInfo.IsEvent) { var dynamicEventWire = (DynamicEventWireBase)dynamicWire; dynamicEventWire.EventFilter = correlationInfo.EventFilter; // add session validation handler and unsubscription callback var sessionId = currentSession.SessionID; var sessionManager = _host.SessionManager; dynamicEventWire.ValidateSession = () => sessionManager.ExistSession(sessionId); dynamicEventWire.CancelSubscription = ex => { eventStub.RemoveHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate); wiringList.Remove(correlationInfo.CorrelationID); currentSession.DecrementRemoteSubscriptionCounter(); _host.OnSubscriptionCanceled(new SubscriptionEventArgs { ComponentType = type, DelegateMemberName = correlationInfo.DelegateMemberName, CorrelationID = correlationInfo.CorrelationID, Exception = ex }); }; eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate); wiringList.Add(correlationInfo.CorrelationID, dynamicEventWire.InDelegate); } else { eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicWire.InDelegate); wiringList.Add(correlationInfo.CorrelationID, dynamicWire.InDelegate); } } currentSession.IncrementRemoteSubscriptionCounter(); _host.OnSubscriptionAdded(new SubscriptionEventArgs { ComponentType = type, DelegateMemberName = correlationInfo.DelegateMemberName, CorrelationID = correlationInfo.CorrelationID, }); } }
/// <summary> /// Creates wires between client component and server component. /// </summary> /// <param name="type">Implementation type of the server component.</param> /// <param name="eventStub"><see cref="EventStub"/> with cached subscriptions.</param> /// <param name="delegateCorrelationSet">Correlation set (say how to wire)</param> /// <param name="wiringList">Collection of built wires</param> private void CreateClientServerWires(Type type, EventStub eventStub, List<DelegateCorrelationInfo> delegateCorrelationSet, Dictionary<Guid, Delegate> wiringList) { if (delegateCorrelationSet == null) return; foreach (var correlationInfo in delegateCorrelationSet) { if (wiringList.ContainsKey(correlationInfo.CorrelationID)) continue; if (ServerSession.CurrentSession == null) throw new InvalidSessionException(string.Format(LanguageResource.InvalidSessionException_SessionIDInvalid, "(null)")); var dynamicWire = DynamicWireFactory.CreateDynamicWire(type, correlationInfo.DelegateMemberName, correlationInfo.IsEvent); dynamicWire.Interceptor = correlationInfo.ClientDelegateInterceptor; if (correlationInfo.IsEvent) { var dynamicEventWire = (DynamicEventWireBase)dynamicWire; dynamicEventWire.EventFilter = correlationInfo.EventFilter; // add session validation handler and unsubscription callback var sessionId = ServerSession.CurrentSession.SessionID; var sessionManager = _host.SessionManager; dynamicEventWire.ValidateSession = () => sessionManager.ExistSession(sessionId); dynamicEventWire.CancelSubscription = () => eventStub.RemoveHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate); eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate); wiringList.Add(correlationInfo.CorrelationID, dynamicEventWire.InDelegate); } else { eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicWire.InDelegate); wiringList.Add(correlationInfo.CorrelationID, dynamicWire.InDelegate); } } }
/// <summary> /// Creates wires between client component and server component. /// </summary> /// <param name="type">Implementation type of the server component.</param> /// <param name="eventStub"><see cref="EventStub"/> with cached subscriptions.</param> /// <param name="delegateCorrelationSet">Correlation set (say how to wire)</param> /// <param name="wiringList">Collection of built wires</param> private void CreateClientServerWires(Type type, EventStub eventStub, IEnumerable<DelegateCorrelationInfo> delegateCorrelationSet, Dictionary<Guid, Delegate> wiringList) { if (delegateCorrelationSet == null) return; var currentSession = ServerSession.CurrentSession; if (currentSession == null) throw new InvalidSessionException(string.Format(LanguageResource.InvalidSessionException_SessionIDInvalid, "(null)")); foreach (var correlationInfo in delegateCorrelationSet) { if (wiringList.ContainsKey(correlationInfo.CorrelationID)) continue; var dynamicWire = DynamicWireFactory.CreateDynamicWire(type, correlationInfo.DelegateMemberName, correlationInfo.IsEvent); dynamicWire.Interceptor = correlationInfo.ClientDelegateInterceptor; lock (wiringList) { if (wiringList.ContainsKey(correlationInfo.CorrelationID)) continue; if (correlationInfo.IsEvent) { var dynamicEventWire = (DynamicEventWireBase)dynamicWire; dynamicEventWire.EventFilter = correlationInfo.EventFilter; // add session validation handler and unsubscription callback var sessionId = currentSession.SessionID; var sessionManager = _host.SessionManager; dynamicEventWire.ValidateSession = () => sessionManager.ExistSession(sessionId); dynamicEventWire.CancelSubscription = ex => { eventStub.RemoveHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate); wiringList.Remove(correlationInfo.CorrelationID); currentSession.DecrementRemoteSubscriptionCounter(); _host.OnSubscriptionCanceled(new SubscriptionEventArgs { ComponentType = type, DelegateMemberName = correlationInfo.DelegateMemberName, CorrelationID = correlationInfo.CorrelationID, Exception = ex }); }; eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicEventWire.InDelegate); wiringList.Add(correlationInfo.CorrelationID, dynamicEventWire.InDelegate); } else { eventStub.AddHandler(correlationInfo.DelegateMemberName, dynamicWire.InDelegate); wiringList.Add(correlationInfo.CorrelationID, dynamicWire.InDelegate); } } currentSession.IncrementRemoteSubscriptionCounter(); _host.OnSubscriptionAdded(new SubscriptionEventArgs { ComponentType = type, DelegateMemberName = correlationInfo.DelegateMemberName, CorrelationID = correlationInfo.CorrelationID, }); } }