private async Task <bool> CallMessageHandlers(Message message) { var handlerFuncs = _handlerMap.Get(message.GetType()); if ((handlerFuncs == null) || (handlerFuncs.Count == 0)) { return(true); } bool allHandlersSucceeded; var watch = System.Diagnostics.Stopwatch.StartNew(); if (handlerFuncs.Count == 1) { // a shortcut for the usual case allHandlersSucceeded = await handlerFuncs[0](message); } else { var handlerTasks = handlerFuncs.Select(func => func(message)); var handlerResults = await Task.WhenAll(handlerTasks); allHandlersSucceeded = handlerResults.All(x => x); } watch.Stop(); Log.Trace("Handled message - MessageType: {0}", message.GetType().Name); _messagingMonitor.HandleTime(watch.ElapsedMilliseconds); return(allHandlersSucceeded); }
private bool CallMessageHandlers(Message message) { var handlerFuncs = _handlerMap.Get(message.GetType()); if (handlerFuncs == null) { return(true); } var allHandlersSucceeded = true; foreach (var handlerFunc in handlerFuncs) { var watch = new System.Diagnostics.Stopwatch(); watch.Start(); var thisHandlerSucceeded = handlerFunc(message); allHandlersSucceeded = allHandlersSucceeded && thisHandlerSucceeded; watch.Stop(); Log.Trace("Handled message - MessageType: {0}", message.GetType().Name); _messagingMonitor.HandleTime(watch.ElapsedMilliseconds); } return(allHandlersSucceeded); }
private async Task <bool> CallMessageHandler(Message message) { var messageType = message.GetType(); var handler = _handlerMap.Get(messageType); if (handler == null) { return(true); } var watch = System.Diagnostics.Stopwatch.StartNew(); var handlerSucceeded = await handler(message).ConfigureAwait(false); watch.Stop(); _logger.LogTrace( "Handled message with Id '{MessageId}' of type {MessageType} in {TimeToHandle}.", message.Id, messageType, watch.Elapsed); _messagingMonitor.HandleTime(watch.Elapsed); return(handlerSucceeded); }
public void EmptyMapReturnsNullHandlers() { var map = new HandlerMap(); var handlers = map.Get(typeof (GenericMessage)); Assert.That(handlers, Is.Null); }
public void HandlerIsNotReturnedForNonMatchingType() { var map = new HandlerMap(); map.Add(typeof(GenericMessage), m => Task.FromResult(true)); var handlers = map.Get(typeof(AnotherGenericMessage)); Assert.That(handlers, Is.Null); }
public void MultipleHandlersForATypeAreSupported() { Func<Message, Task<bool>> fn1 = m => Task.FromResult(true); Func<Message, Task<bool>> fn2 = m => Task.FromResult(true); var map = new HandlerMap(); map.Add(typeof(GenericMessage), fn1); map.Add(typeof(GenericMessage), fn2); var handlers1 = map.Get(typeof(GenericMessage)); Assert.That(handlers1, Is.Not.Null); Assert.That(handlers1.Count, Is.EqualTo(2)); Assert.That(handlers1[0], Is.EqualTo(fn1)); Assert.That(handlers1[1], Is.EqualTo(fn2)); var handlers2 = map.Get(typeof(AnotherGenericMessage)); Assert.That(handlers2, Is.Null); }
public void HandlerIsReturnedForMatchingType() { var map = new HandlerMap(); map.Add(typeof(GenericMessage), m => Task.FromResult(true) ); var handlers = map.Get(typeof(GenericMessage)); Assert.That(handlers, Is.Not.Null); Assert.That(handlers.Count, Is.EqualTo(1)); }
public void CorrectHandlerIsReturnedForType() { Func<Message, Task<bool>> fn1 = m => Task.FromResult(true); Func<Message, Task<bool>> fn2 = m => Task.FromResult(true); var map = new HandlerMap(); map.Add(typeof(GenericMessage), fn1); map.Add(typeof(AnotherGenericMessage), fn2); var handlers1 = map.Get(typeof(GenericMessage)); Assert.That(handlers1, Is.Not.Null); Assert.That(handlers1.Count, Is.EqualTo(1)); Assert.That(handlers1[0], Is.EqualTo(fn1)); var handlers2 = map.Get(typeof(AnotherGenericMessage)); Assert.That(handlers2, Is.Not.Null); Assert.That(handlers2.Count, Is.EqualTo(1)); Assert.That(handlers2[0], Is.EqualTo(fn2)); }
private async Task <bool> CallMessageHandler(Message message) { var handler = _handlerMap.Get(message.GetType()); if (handler == null) { return(true); } var watch = System.Diagnostics.Stopwatch.StartNew(); var handlerSucceeded = await handler(message).ConfigureAwait(false); watch.Stop(); _log.LogTrace($"Handled message - MessageType: {message.GetType().Name}"); _messagingMonitor.HandleTime(watch.ElapsedMilliseconds); return(handlerSucceeded); }