Files
monorepo/LCU.API.Internal.Dispatcher/LCU.Api.Internal.DispatcherTests/ServiceTests/DispatcherOperationServices/BatchErroredJobsServiceTest.cs
T
2026-03-24 15:26:03 -05:00

101 lines
3.6 KiB
C#

using LCU.Api.Internal.Dispatcher.Services;
using LCU.Api.Internal.Dispatcher.Services.DispatcherOperationServices;
using LCU.Api.Internal.Dispatcher.Services.QueueServices;
using LCU.Library.Internal.DispatcherModels.Enums;
using LCU.Library.Internal.DispatcherModels.Models.Requests;
using System.Collections.Generic;
using System.Threading;
namespace LCU.Api.Internal.DispatcherTests.ServiceTests.DispatcherOperationServices;
public class BatchErroredJobsServiceTest
{
private readonly BatchErroredJobsService _service;
private readonly Mock<IJobService> _mockJobService;
private readonly Mock<IQueueSendErroredJobAlertService> _mockQueueSendErroredJobAlertService;
public BatchErroredJobsServiceTest()
{
_mockJobService = new Mock<IJobService>();
_mockQueueSendErroredJobAlertService = new Mock<IQueueSendErroredJobAlertService>();
_service = new BatchErroredJobsService(_mockJobService.Object,
_mockQueueSendErroredJobAlertService.Object);
}
[Fact]
public void Basic_Construct_Test()
{
Assert.NotNull(_service);
}
[Fact]
public async Task PerformBatchErroredJobs_NoJobs_DoesNotQueue()
{
// Arrange
_mockJobService.Setup(x => x.GetErrorMonitoringJobs())
.ReturnsAsync(new List<Job>());
// Act
await _service.PerformBatchErroredJobs(CancellationToken.None);
// Assert
_mockQueueSendErroredJobAlertService.Verify(
x => x.QueueSendErroredJobAlert(It.IsAny<BatchedErroredJobAlertRequest>()),
Times.Never);
}
[Fact]
public async Task PerformBatchErroredJobs_JobsWithFailedWorkItems_QueuesBatch()
{
// Arrange
var failedExecution = new WorkItemExecution
{
Status = StatusType.Failed
};
var workItem = new WorkItem
{
WorkItemId = 1,
WorkItemExecutions = new List<WorkItemExecution> { failedExecution }
};
var job = new Job
{
JobId = 10,
WorkItems = new List<WorkItem> { workItem }
};
_mockJobService.Setup(x => x.GetErrorMonitoringJobs())
.ReturnsAsync(new List<Job> { job });
BatchedErroredJobAlertRequest capturedBatch = null;
_mockQueueSendErroredJobAlertService
.Setup(x => x.QueueSendErroredJobAlert(It.IsAny<BatchedErroredJobAlertRequest>()))
.Callback<BatchedErroredJobAlertRequest>(batch => capturedBatch = batch)
.Returns(Task.CompletedTask);
// Act
await _service.PerformBatchErroredJobs(CancellationToken.None);
// Assert
_mockQueueSendErroredJobAlertService.Verify(
x => x.QueueSendErroredJobAlert(It.IsAny<BatchedErroredJobAlertRequest>()),
Times.Once);
Assert.NotNull(capturedBatch);
Assert.Single(capturedBatch.ErroredJobAlertRequests);
Assert.Equal(job, capturedBatch.ErroredJobAlertRequests[0].CurrentJob);
Assert.Equal(workItem, capturedBatch.ErroredJobAlertRequests[0].CurrentWorkItem);
}
[Fact]
public async Task PerformBatchErroredJobs_Exception_IsHandledAndLogged()
{
// Arrange
_mockJobService.Setup(x => x.GetErrorMonitoringJobs())
.ThrowsAsync(new System.Exception("Test exception"));
// Act & Assert
await _service.PerformBatchErroredJobs(CancellationToken.None);
_mockQueueSendErroredJobAlertService.Verify(
x => x.QueueSendErroredJobAlert(It.IsAny<BatchedErroredJobAlertRequest>()),
Times.Never);
}
}