public void Invoke() { EndpointId processedId = null; NotificationId registration = null; var sink = new Mock <ISendNotifications>(); { sink.Setup(s => s.UnregisterFromNotification(It.IsAny <EndpointId>(), It.IsAny <NotificationId>())) .Callback <EndpointId, NotificationId>((e, s) => { processedId = e; registration = s; }); } var action = new UnregisterFromNotificationProcessAction(sink.Object); var id = new EndpointId("id"); var reg = NotificationId.Create(typeof(InteractionExtensionsTest.IMockNotificationSetWithTypedEventHandler).GetEvent("OnMyEvent")); var msg = new UnregisterFromNotificationMessage(id, reg); action.Invoke(msg); Assert.AreEqual(id, processedId); Assert.AreEqual(reg, registration); }
public void ProxyDisconnectFromEventWithNormalEventHandler() { var local = new EndpointId("local"); UnregisterFromNotificationMessage intermediateMsg = null; SendMessage messageSender = (e, m, r) => { intermediateMsg = m as UnregisterFromNotificationMessage; }; var systemDiagnostics = new SystemDiagnostics((p, s) => { }, null); var builder = new NotificationProxyBuilder(local, messageSender, systemDiagnostics); var remoteEndpoint = new EndpointId("other"); var proxy = builder.ProxyConnectingTo <InteractionExtensionsTest.IMockNotificationSetWithEventHandler>(remoteEndpoint); object sender = null; EventArgs receivedArgs = null; EventHandler handler = (s, e) => { sender = s; receivedArgs = e; }; proxy.OnMyEvent += handler; Assert.IsNull(intermediateMsg); var notificationObj = proxy as NotificationSetProxy; Assert.IsNotNull(notificationObj); var id = NotificationId.Create(typeof(InteractionExtensionsTest.IMockNotificationSetWithEventHandler).GetEvent("OnMyEvent")); var args = new EventArgs(); notificationObj.RaiseEvent(id, args); Assert.AreSame(proxy, sender); Assert.AreSame(args, receivedArgs); sender = null; receivedArgs = null; proxy.OnMyEvent -= handler; Assert.AreEqual(id, intermediateMsg.Notification); notificationObj.RaiseEvent(id, new EventArgs()); Assert.IsNull(sender); Assert.IsNull(receivedArgs); }
public void FromMessage() { var translator = new NotificationUnregistrationConverter(); var id = NotificationId.Create(typeof(InteractionExtensionsTest.IMockNotificationSetWithTypedEventHandler).GetEvent("OnMyEvent")); var msg = new UnregisterFromNotificationMessage(new EndpointId("a"), id); var data = translator.FromMessage(msg); Assert.IsInstanceOf(typeof(NotificationUnregistrationData), data); Assert.AreSame(msg.Id, data.Id); Assert.AreSame(msg.Sender, data.Sender); Assert.AreSame(msg.InResponseTo, data.InResponseTo); Assert.AreEqual(NotificationIdExtensions.Serialize(id), ((NotificationUnregistrationData)data).NotificationId); }
/// <summary> /// Generates a proxy object for the given command set and the specified endpoint. /// </summary> /// <param name="endpoint">The endpoint for which a proxy must be made.</param> /// <param name="interfaceType">The interface of the command set for which a proxy must be made.</param> /// <returns> /// The interfaced proxy. /// </returns> public INotificationSet ProxyConnectingTo(EndpointId endpoint, Type interfaceType) { { Lokad.Enforce.Argument(() => interfaceType); Lokad.Enforce.With <ArgumentException>( typeof(INotificationSet).IsAssignableFrom(interfaceType), Resources.Exceptions_Messages_ANotificationSetTypeMustDeriveFromINotificationSet); } // We assume that the interface lives up to the demands we placed on it, i.e.: // - Derives from INotificationSet // - Has only events, no properties and no methods // - Every event is based on either the EventHandler or the EventHandler<T> delegate. // All these checks should have been done when the interface was registered // at the remote endpoint. var selfReference = new ProxySelfReferenceInterceptor(); var addEventHandler = new NotificationEventAddMethodInterceptor( interfaceType, eventInfo => { var msg = new RegisterForNotificationMessage(m_Local, eventInfo); m_SendWithoutResponse(endpoint, msg, CommunicationConstants.DefaultMaximuNumberOfRetriesForMessageSending); }, m_Diagnostics); var removeEventHandler = new NotificationEventRemoveMethodInterceptor( interfaceType, eventInfo => { var msg = new UnregisterFromNotificationMessage(m_Local, eventInfo); m_SendWithoutResponse(endpoint, msg, CommunicationConstants.DefaultMaximuNumberOfRetriesForMessageSending); }, m_Diagnostics); var options = new ProxyGenerationOptions { Selector = new NotificationSetInterceptorSelector(), BaseTypeForInterfaceProxy = typeof(NotificationSetProxy), }; var proxy = m_Generator.CreateInterfaceProxyWithoutTarget( interfaceType, options, new IInterceptor[] { selfReference, addEventHandler, removeEventHandler }); return((INotificationSet)proxy); }