public async Task ShouldLogError_IfUpdateIsUnsuccessful() { //Arrange var service = SetupService(); _queService.UpdateMessagesAsync(Arg.Any <IEnumerable <UpdateBatchRequest> >()) .Returns(Task.FromResult(new ActionResult { IsSuccessfull = false, ErrorCode = 1, ErrorMessage = "SampleMessage" })); //Act await service.UpdateMessagesAsync(new ConcurrentBag <UpdateBatchRequest> { new UpdateBatchRequest() }); //Assert Assert.Equal(1, service.ReceivedCalls().Count(c => c.GetMethodInfo().Name == "LogErrorAsync")); Assert.Equal(1, (int)service.ReceivedCalls().Single(c => c.GetMethodInfo().Name == "LogErrorAsync").GetArguments()[0]); Assert.Equal("SampleMessage", (string)service.ReceivedCalls().Single(c => c.GetMethodInfo().Name == "LogErrorAsync") .GetArguments()[1]); }
public virtual async Task <bool> UpdateMessagesAsync(ConcurrentBag <UpdateBatchRequest> updateBatchRequests) { var updateResult = await _queService.UpdateMessagesAsync(updateBatchRequests); if (!updateResult.IsSuccessfull) { await LogErrorAsync(updateResult.ErrorCode, updateResult.ErrorMessage, "UpdateMessageBatch"); return(false); } await LogInfoAsync( $"ImplementMeService.DoWorkAsync successfully updated {updateBatchRequests.Count} messages @ {Environment.MachineName}", "UpdateMessageBatch", new { UpdatedMessages = updateBatchRequests.Count, }); return(true); }
public void DoWork() { // Pseudo-code to illustrate a naive implementation. Provide an optimal implementation. while (true) { var batch = _queService.GetMessagesFromQueAsync <RingbaUOW>(10, 30, 30).Result; foreach (var message in batch.Messages) { var result = _processService.ProccessMessageAsync(message.Body).Result; _queService.UpdateMessagesAsync(new UpdateBatchRequest[] { new UpdateBatchRequest { Id = message.Id, MessageCompleted = result.IsSuccessfull } }); } } }
public async Task DoWork() { await _logService.LogInfoAsync(_logId, "Queue processing started."); while (!_cts.IsCancellationRequested) { var sw = Stopwatch.StartNew(); await _logService.LogInfoAsync(_logId, "Getting batch from queue."); var batch = await GetMessagesBatch(); await _logService.LogInfoAsync(_logId, $"Number of messages in batch: {batch.NumberOfMessages}"); await _logService.LogInfoAsync(_logId, "Started messages processing."); var batchProcessingTasks = batch.Messages.Select(message => ProcessSingleMessage(message, _cts.Token)); UpdateBatchRequest[] updateBatchRequests = await Task.WhenAll(batchProcessingTasks); var nonNullUpdateBatchRequests = updateBatchRequests.Where(p => p != null).ToArray(); var completedMessagesCount = nonNullUpdateBatchRequests.Count(p => p.MessageCompleted); var uncompletedMessagesCount = batch.NumberOfMessages - completedMessagesCount; await _logService.LogInfoAsync(_logId, $"Number of messages dropped due to concurrency conflict: {updateBatchRequests.Count(p => p == null)}"); await _logService.LogInfoAsync(_logId, $"Finished messages processing. Completed messages: {completedMessagesCount}. Uncompleted messages: {uncompletedMessagesCount}."); await _queService.UpdateMessagesAsync(nonNullUpdateBatchRequests); sw.Stop(); await _logService.LogInfoAsync(_logId, $"Batch processing completed, took: {sw.ElapsedMilliseconds} ms."); } await _logService.LogInfoAsync(_logId, "Queue processing stopped."); _cts = GetResetCts(); }