/// <summary> /// Subscribe an event on the EventServer /// </summary> /// <param name="eventName">event name</param> /// <param name="s">delegate on the client side that process the event notification</param> public void SubscribeEvent(string eventName, EventProcessingHandler s) { try { //check if the event has already subscribed by the client Delegate handler = (Delegate)repeatDelegate[eventName]; //if already subscribed, chain up the delegates //otherwise, create a new delegate and add it to the repeatDelegate hashtable if (handler != null) { //chain up the delegates together handler = Delegate.Combine(handler, s); //reset the delegate object in the hashtable repeatDelegate[eventName] = handler; } else { repeatDelegate.Add(eventName, s); EventClient.EventProcessingHandler repeat = new EventClient.EventProcessingHandler(Repeat); //subscribe the "repeat" delegate to the EventServer. es.SubscribeEvent(eventName, repeat); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public void ExceptionWillBeThrowWhenConfigIsNotEmptyAndEventIsValidAndExceptionFolderIsNotEmpty() { //Arrange var fixture = new Fixture(); var eventDetails = fixture.Build <EventDetails>().Create(); var result = fixture.Build <EventReaderResult>().Create(); var mockConfig = new Mock <IConfiguration>(); var mockLogger = new Mock <ILogger>(); var mockValidator = new Mock <IValidator>(); var fakeData = new Dictionary <string, string>(); fakeData.Add("exception", "test"); var fakeConfig = new List <IConfigurationSection> { new FakeConfigSection(fakeData, "exception", "test") }; mockConfig.Setup(c => c.GetSection(result.Events[0].EventType.ToLower()).GetChildren()).Returns(fakeConfig); mockValidator.Setup(v => v.IsValidate(result.Events[0])).Returns(false); var eventProcessingHandler = new EventProcessingHandler(mockConfig.Object, mockValidator.Object, mockLogger.Object); //Assert Assert.Throws <Exception>(() => eventProcessingHandler.ProcessingEvent(eventDetails, result)); }
/// <summary> /// Subscribe an event on the EventServer /// </summary> /// <param name="eventName">event name</param> /// <param name="s">delegate on the client side that process the event notification</param> public void SubscribeEvent(string eventName, EventProcessingHandler s) { try { //check if the event has already subscribed by the client Delegate handler =(Delegate)repeatDelegate[eventName]; //if already subscribed, chain up the delegates //otherwise, create a new delegate and add it to the repeatDelegate hashtable if (handler != null) { //chain up the delegates together handler = Delegate.Combine(handler, s); //reset the delegate object in the hashtable repeatDelegate[eventName] = handler; } else { repeatDelegate.Add(eventName,s); EventProcessingHandler repeat = new EventProcessingHandler(Repeat); //subscribe the "repeat" delegate to the EventServer. es.SubscribeEvent(eventName,repeat); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public void ValidatorWillBeCalledOnceWhenConfigIsNotEmpty() { //Arrange var fixture = new Fixture(); var eventDetails = fixture.Build <EventDetails>().Create(); var result = fixture.Build <EventReaderResult>().Create(); var mockConfig = new Mock <IConfiguration>(); var mockLogger = new Mock <ILogger>(); var mockValidator = new Mock <IValidator>(); var fakeData = new Dictionary <string, string>(); fakeData.Add("test", "test value"); var fakeConfig = new List <IConfigurationSection> { new FakeConfigSection(fakeData) }; mockConfig.Setup(c => c.GetSection("test").GetChildren()).Returns(fakeConfig); var eventProcessingHandler = new EventProcessingHandler(mockConfig.Object, mockValidator.Object, mockLogger.Object); //Act eventProcessingHandler.ProcessingEvent(eventDetails, result); //Assert mockValidator.Verify(v => v.IsValidate(result.Events[0]), Times.Once); }
/// <summary> /// 订阅服务器上的事件 /// </summary> /// <param name="eventName">事件名</param> /// <param name="s">回调的委托</param> public void SubscribeEvent(string eventName, EventProcessingHandler s) { try { //检查事件是否已由客户订阅 Delegate handler = (Delegate)repeatDelegate[eventName]; //如果已经订阅, 加入订阅链 //否则创建新的委托并添加到订阅的Hash表 if (handler != null) { //接入委托链 handler = Delegate.Combine(handler, s); ////在哈希表中重置委托链 repeatDelegate[eventName] = handler; } else { repeatDelegate.Add(eventName, s); EventClient.EventProcessingHandler repeat = new EventClient.EventProcessingHandler(Repeat); //订阅服务器上的事件 es.SubscribeEvent(eventName, repeat); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public void LoggerWillBeCalledOnceWhenConfigIsNotEmptyAndEventIsValidAndCuratedFolderIsEmpty() { //Arrange var fixture = new Fixture(); var eventDetails = fixture.Build <EventDetails>().Create(); var result = fixture.Build <EventReaderResult>().Create(); var mockConfig = new Mock <IConfiguration>(); var mockLogger = new Mock <ILogger>(); var mockValidator = new Mock <IValidator>(); var fakeData = new Dictionary <string, string>(); fakeData.Add("curated", ""); var fakeConfig = new List <IConfigurationSection> { new FakeConfigSection(fakeData) }; mockConfig.Setup(c => c.GetSection(result.Events[0].EventType.ToLower()).GetChildren()).Returns(fakeConfig); mockValidator.Setup(v => v.IsValidate(result.Events[0])).Returns(true); var eventProcessingHandler = new EventProcessingHandler(mockConfig.Object, mockValidator.Object, mockLogger.Object); //Act eventProcessingHandler.ProcessingEvent(eventDetails, result); //Assert mockLogger.Verify(l => l.LogInfoToConsole($"{result.Events[0].EventType.ToLower()} has no curated folder setup in appsettings.json."), Times.Once); }
public void LoggerWillBeCalledOnceWhenConfigIsEmpty() { //Arrange var fixture = new Fixture(); var eventDetails = fixture.Build <EventDetails>().Create(); var result = fixture.Build <EventReaderResult>().Create(); var flightEventTypeName = result.Events[0].EventType.ToLower(); var mockConfig = new Mock <IConfiguration>(); var mockLogger = new Mock <ILogger>(); var mockValidator = new Mock <IValidator>(); mockConfig.Setup(c => c.GetSection("test").GetChildren()).Returns(new List <IConfigurationSection>()); var eventProcessingHandler = new EventProcessingHandler(mockConfig.Object, mockValidator.Object, mockLogger.Object); //Act eventProcessingHandler.ProcessingEvent(eventDetails, result); //Assert mockLogger.Verify( l => l.LogInfoToConsole($"{flightEventTypeName} has no folder path setup in appsettings.json"), Times.Once); }
public void Constructor() { Mock <IDateTimeService> iDateTime = new Mock <IDateTimeService>(); Mock <IRead> iRead = new Mock <IRead>(); Mock <ISelector> iSelector = new Mock <ISelector>(); var eventHandler = new EventProcessingHandler(iDateTime.Object, iRead.Object, iSelector.Object); Assert.IsInstanceOfType(eventHandler, typeof(EventProcessingHandler)); }
/// <summary> /// 作为一个客户端通知的调度者. 被服务端执行来推送新的通知并负责调度的通知所有在客户端上的处理程序 /// </summary> /// <param name="eventName">事件名</param> /// <param name="content">通知的内容</param> public void Repeat(string eventName, object content) { //从哈希表中获取委托 EventProcessingHandler eph = (EventProcessingHandler)repeatDelegate[eventName]; if (eph != null) { //执行委托 eph(eventName, content); } }
static void Main() { string pathFile = @"C:\Users\jose.ek\Desktop\tarea.txt"; IRead reader = new TextReaderService(pathFile); IDateTimeService dtService = new DateTimeService(); ISelector selectorMessage = new MessageService(); EventProcessingHandler eventHandler = new EventProcessingHandler(dtService, reader, selectorMessage); DisplayService display = new DisplayService(); display.ShowMessages(eventHandler.ListOfMessages()); }
/// <summary> /// Repeat method functions as a repeater of the event notification /// on the client side. Repeat method is invoke by the EventServer upon the /// arrival of new notificatioin and is responsible for dispatch the notification /// on all the "handlers" on the client side /// </summary> /// <param name="eventName">event name</param> /// <param name="content">the content of the notification</param> public void Repeat(string eventName, object content) { //Retrieve the delegate from the hashtable. EventProcessingHandler eph = (EventProcessingHandler)repeatDelegate[eventName]; if (eph != null) { //invoke the delegate eph(eventName, content); } }
/// <summary> /// 取消订阅事件 /// </summary> /// <param name="eventName">事件名</param> /// <param name="s">事件回调委托</param> public void UnSubscribeEvent(string eventName, EventProcessingHandler s) { //从hash表里移除订阅 Delegate handler = (Delegate)repeatDelegate[eventName]; //如果不为空,服务端订阅的移除由服务端检查通知不可用时自动移除 if (handler != null) { handler = Delegate.Remove(handler, s); //重新分配链的哈希表 repeatDelegate[eventName] = handler; } }
/// <summary> /// Unsubscribe a event /// </summary> /// <param name="eventName">event name</param> /// <param name="s">delegate on the client side that process the event notification</param> public void UnSubscribeEvent(string eventName, EventProcessingHandler s) { //retrieve the delegate handler from the hashtable Delegate handler =(Delegate)repeatDelegate[eventName]; //check if the handler is null //if not null, remove the event processing handler from the chain if (handler != null) { handler = Delegate.Remove(handler, s); //reassign the chain back to the hashtable. repeatDelegate[eventName] = handler; } }
/// <summary> /// Unsubscribe a event /// </summary> /// <param name="eventName">event name</param> /// <param name="s">delegate on the client side that process the event notification</param> public void UnSubscribeEvent(string eventName, EventProcessingHandler s) { //retrieve the delegate handler from the hashtable Delegate handler = (Delegate)repeatDelegate[eventName]; //check if the handler is null //if not null, remove the event processing handler from the chain if (handler != null) { handler = Delegate.Remove(handler, s); //reassign the chain back to the hashtable. repeatDelegate[eventName] = handler; } }
/// <summary> /// UnsubscribeEvent remove an client side delegate from delegate chain /// </summary> /// <param name="eventName">event name</param> /// <param name="handler"></param> public void UnSubscribeEvent(string eventName, EventProcessingHandler handler) { //ensure that only one thread modify the delegate chain at a time. lock (this) { Delegate delegateChain = (Delegate)delegateMap[eventName]; //check if the delegate chain is null. if not null, remove the //client side delegate from the delegate chain. if (delegateChain != null) { //remove the delegate from the chain delegateChain = Delegate.Remove(delegateChain, handler); //reset the delegate chain in the hashtable delegateMap[eventName] = delegateChain; } } }
/// <summary> /// SubscribeEvent add the client delegate to delegate chain /// for a given event name /// </summary> /// <param name="eventName">event name</param> /// <param name="handler">client side delegate</param> public void SubscribeEvent(string eventName, EventProcessingHandler handler) { //ensure that only one thread modify the delegate chain at a time. lock (this) { Delegate delegateChain = (Delegate)delegateMap[eventName]; //check if the delegate chain is null. if null, add the //client side delegate as the initial handler. Otherwise, //add delegate to the chain. if (delegateChain == null) { delegateMap.Add(eventName, handler); } else { //add the delegate to the chain delegateChain = Delegate.Combine(delegateChain, handler); //reset the delegate chain in the hashtable delegateMap[eventName] = delegateChain; } } }
public void ListOfMessages_BuildMessage_CorrectMessage() { EventInformation eventInfo = new EventInformation { Name = "Event1", OriginalDateTime = "2020/02/01" }; Mock <IDateTimeService> iDateTime = new Mock <IDateTimeService>(); Mock <IRead> iRead = new Mock <IRead>(); Mock <ISelector> iSelector = new Mock <ISelector>(); iRead.Setup(m => m.GetListEvent()).Returns(new List <IDatos> { eventInfo }); iDateTime.Setup(m => m.DateIsAfterCurrent(It.IsAny <IDatos>())).Returns(true); iDateTime.Setup(m => m.GetElapsedTime(It.IsAny <IDatos>(), It.IsAny <bool>())).Returns(new TimeSpan(2, 0, 0, 0)); iSelector.Setup(m => m.GetInstance(It.IsAny <TimeSpan>())).Returns(new MessageDay()); var eventHandler = new EventProcessingHandler(iDateTime.Object, iRead.Object, iSelector.Object); string actual = eventHandler.ListOfMessages().FirstOrDefault(); string expected = $"{eventInfo.Name},{eventInfo.OriginalDateTime} ocurrirá dentro de 2 día(s)"; Assert.AreEqual(expected, actual); }