Exemplo n.º 1
0
        public void HandleMessage_PassMessageToHandler_ForExistingMessageType()
        {
            // Arrange
            mockedHandler.CanHandle(Arg.Any <MessageType>()).ReturnsForAnyArgs(true);
            var message = CreateTestMessage();

            // Act
            messageCoordinator.HandleMessage(message);

            // Assert
            mockedHandler.Received(1).Handle(Arg.Is(message.Data));
        }
Exemplo n.º 2
0
        private async Task HandleMessageAsync(
            ISocketConnection connection,
            OperationMessage message,
            CancellationToken cancellationToken)
        {
            for (int i = 0; i < _messageHandlers.Length; i++)
            {
                IMessageHandler handler = _messageHandlers[i];

                if (handler.CanHandle(message))
                {
                    await handler.HandleAsync(
                        connection,
                        message,
                        cancellationToken)
                    .ConfigureAwait(false);

                    // the message is handled and we are done.
                    return;
                }
            }

            // TODO : resources
            throw new NotSupportedException(
                      "The specified message type is not supported.");
        }
Exemplo n.º 3
0
 public IEnumerable <IMessageHandler> GetHandlersForType(Type type)
 {
     foreach (Type handlerType in this)
     {
         IMessageHandler handler = _factory.Create(handlerType);
         if (handler.CanHandle(type))
         {
             yield return(handler);
         }
     }
 }
Exemplo n.º 4
0
        private async Task <(bool IsSuccess, IMessageHandler Handler)> CanHandlingTask(IMessageHandler handler,
                                                                                       Update update)
        {
            var isAnonymous = handler is IAllowAnonymous;

            if (!isAnonymous && _chatSession.ChatAdministrators.All(c => c.ChatId != update.Message.Chat.Id))
            {
                return(false, null);
            }

            var result = await handler.CanHandle(update);

            return(result, handler);
        }
Exemplo n.º 5
0
        private bool SafelyHandles(IMessageHandler handler, EventMessageBase message, string originalMessage)
        {
            var handles = false;

            try
            {
                handles = handler.CanHandle(message);
            }
            catch (Exception ex)
            {
                this.Logger.Error($"{handler.GetType().Name} threw exception in CanHandle() with message: {originalMessage}", ex);
            }

            return(handles);
        }
Exemplo n.º 6
0
        public async Task Process(IEnumerable <EventData> events)
        {
            if (events is null)
            {
                throw new ArgumentNullException(nameof(events));
            }

            List <Exception>?exceptions = default;

            foreach (EventData eventData in events)
            {
                try
                {
                    if (_converter.TryConvertToMessage(eventData) is Message message)
                    {
                        if (_handler.CanHandle(message))
                        {
                            await _handler.Handle(message).ConfigureAwait(continueOnCapturedContext: false);
                        }
                    }
                }
                catch (Exception exception)
                {
                    switch (exceptions)
                    {
                    case null:
                        exceptions = new List <Exception> {
                            exception
                        };
                        break;

                    default:
                        exceptions.Add(exception);
                        break;
                    }
                }
            }

            if (exceptions != null)
            {
                throw new AggregateException(innerExceptions: exceptions);
            }
        }
Exemplo n.º 7
0
 public bool CanHandle(Message message) => _handler.CanHandle(message);
Exemplo n.º 8
0
        private bool SafelyHandles(IMessageHandler handler, EventMessageBase message, string originalMessage)
        {
            var handles = false;

            try
            {
                handles = handler.CanHandle(message);
            }
            catch (Exception ex)
            {
                this.Logger.Error($"{handler.GetType().Name} threw exception in CanHandle() with message: {originalMessage}", ex);
            }

            return handles;
        }