Exemplo n.º 1
0
 private void ConfigureSender()
 {
     _sender.Send(Arg.Any <DataToSendMock>()).Returns(c => Task.CompletedTask);
     _sender.Send(Arg.Any <ExceptionDataToSendMok>()).Returns(c => Task.FromException(new Exception("test")));
 }
Exemplo n.º 2
0
        public async Task <IProcessMessageRaport> Process()
        {
            _logger.Information("Start process message to send");
            List <IDataToSend>   dataToSends;
            ProcessMessageRaport processMessageRaport = null;

            try
            {
                dataToSends = (await _reader.ReadData()).ToList();
                _logger.Verbose("Data readed {@readedMessageCount}", dataToSends.Count);
                if (dataToSends.Count == 0)
                {
                    _logger.Verbose("No data was return by the reader");
                    return(new NotReadAnyDataToProcess());
                }

                processMessageRaport = new ProcessMessageRaport(dataToSends.Count);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error occured when read data");
                throw new ReaderDataSourceNotAvaliable(ex);
            }

            try
            {
                List <Task> tasks = new List <Task>();
                foreach (var data in dataToSends)
                {
                    _logger.Debug("Sending data @{data}", data);
                    tasks.Add(_sender.Send(data));
                }

                try
                {
                    Task.WaitAll(tasks.ToArray());
                }
                catch (AggregateException aex)
                {
                    _logger.Warning(aex, "There was an errors when sending messages");
                }

                foreach (var task in tasks)
                {
                    if (task.IsCompletedSuccessfully)
                    {
                        _logger.Debug("Add data as successful processed");
                        processMessageRaport.AddSuccesfulMessage();
                    }
                    else
                    {
                        _logger.Debug("Add data as unsuccessful processed");
                        processMessageRaport.AddUnsuccessfulMessage();
                    }
                }
            }
            catch (Exception ex) { _logger.Error(ex, "Error occured when sending messages"); }

            _logger.Information("Processed message raport: {@processMessageRaport}", processMessageRaport);
            return(processMessageRaport);
        }