mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 19:11:10 +00:00
Renames project paths from Lumina to Nemera
This commit is contained in:
1 parent
ce6fddb4c9
commit
0f659a4877
6450 files changed
No files matched your search
+218
@@ -0,0 +1,218 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects;
|
||||
using Lumina.Infrastructure.Core.Scheduling.Execution;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Core.Scheduling.Execution;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="ScheduledJobRuntimeRegistry"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ScheduledJobRuntimeRegistryTests
|
||||
{
|
||||
private readonly ScheduledJobRuntimeRegistry _sut = new();
|
||||
private readonly ScheduledJobIdFixture _scheduledJobIdFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void TryStartCycle_WhenNoCycleIsRunning_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJobId scheduledJobId = _scheduledJobIdFixture.Create();
|
||||
using CancellationTokenSource cycleCancellationTokenSource = new();
|
||||
|
||||
// Act
|
||||
bool result = _sut.TryStartCycle(scheduledJobId, cycleCancellationTokenSource);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryStartCycle_WhenCycleIsAlreadyRunning_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJobId scheduledJobId = _scheduledJobIdFixture.Create();
|
||||
using CancellationTokenSource firstCancellationTokenSource = new();
|
||||
using CancellationTokenSource secondCancellationTokenSource = new();
|
||||
_sut.TryStartCycle(scheduledJobId, firstCancellationTokenSource);
|
||||
|
||||
// Act
|
||||
bool result = _sut.TryStartCycle(scheduledJobId, secondCancellationTokenSource);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StopCycle_WhenCycleIsRunning_ShouldCancelTheCycleAndAllowRestartingIt()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJobId scheduledJobId = _scheduledJobIdFixture.Create();
|
||||
using CancellationTokenSource cycleCancellationTokenSource = new();
|
||||
_sut.TryStartCycle(scheduledJobId, cycleCancellationTokenSource);
|
||||
|
||||
// Act
|
||||
_sut.StopCycle(scheduledJobId);
|
||||
|
||||
// Assert
|
||||
Assert.True(cycleCancellationTokenSource.IsCancellationRequested);
|
||||
using CancellationTokenSource restartedCycleCancellationTokenSource = new();
|
||||
Assert.True(_sut.TryStartCycle(scheduledJobId, restartedCycleCancellationTokenSource));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StopCycle_WhenNoCycleIsRunning_ShouldNotThrow()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJobId scheduledJobId = _scheduledJobIdFixture.Create();
|
||||
|
||||
// Act
|
||||
_sut.StopCycle(scheduledJobId);
|
||||
|
||||
// Assert
|
||||
Assert.True(true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryAcquireRunSlot_WhenNoExecutionIsRunning_ShouldReturnTrueAndThenFalseWhileHeld()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJobId scheduledJobId = _scheduledJobIdFixture.Create();
|
||||
|
||||
// Act
|
||||
bool firstAcquire = _sut.TryAcquireRunSlot(scheduledJobId);
|
||||
bool secondAcquire = _sut.TryAcquireRunSlot(scheduledJobId);
|
||||
|
||||
// Assert
|
||||
Assert.True(firstAcquire);
|
||||
Assert.False(secondAcquire);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReleaseRunSlot_WhenSlotWasAcquired_ShouldAllowAcquiringItAgain()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJobId scheduledJobId = _scheduledJobIdFixture.Create();
|
||||
_sut.TryAcquireRunSlot(scheduledJobId);
|
||||
|
||||
// Act
|
||||
_sut.ReleaseRunSlot(scheduledJobId);
|
||||
|
||||
// Assert
|
||||
Assert.True(_sut.TryAcquireRunSlot(scheduledJobId));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryAcquireRunSlot_AfterReleasingTheSlot_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJobId scheduledJobId = _scheduledJobIdFixture.Create();
|
||||
_sut.TryAcquireRunSlot(scheduledJobId);
|
||||
_sut.ReleaseRunSlot(scheduledJobId);
|
||||
|
||||
// Act
|
||||
bool result = _sut.TryAcquireRunSlot(scheduledJobId);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HasActiveCycle_WhenCycleIsRunning_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJobId scheduledJobId = _scheduledJobIdFixture.Create();
|
||||
using CancellationTokenSource cycleCancellationTokenSource = new();
|
||||
_sut.TryStartCycle(scheduledJobId, cycleCancellationTokenSource);
|
||||
|
||||
// Act
|
||||
bool result = _sut.HasActiveCycle(scheduledJobId);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HasActiveCycle_WhenNoCycleIsRunning_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJobId scheduledJobId = _scheduledJobIdFixture.Create();
|
||||
|
||||
// Act
|
||||
bool result = _sut.HasActiveCycle(scheduledJobId);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void HasActiveCycle_AfterTheCycleWasStopped_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJobId scheduledJobId = _scheduledJobIdFixture.Create();
|
||||
using CancellationTokenSource cycleCancellationTokenSource = new();
|
||||
_sut.TryStartCycle(scheduledJobId, cycleCancellationTokenSource);
|
||||
_sut.StopCycle(scheduledJobId);
|
||||
|
||||
// Act
|
||||
bool result = _sut.HasActiveCycle(scheduledJobId);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EndCycle_WhenCalledWithTheOwnedTokenSource_ShouldCancelItAndClearTheCycle()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJobId scheduledJobId = _scheduledJobIdFixture.Create();
|
||||
using CancellationTokenSource cycleCancellationTokenSource = new();
|
||||
_sut.TryStartCycle(scheduledJobId, cycleCancellationTokenSource);
|
||||
|
||||
// Act
|
||||
_sut.EndCycle(scheduledJobId, cycleCancellationTokenSource);
|
||||
|
||||
// Assert
|
||||
Assert.True(cycleCancellationTokenSource.IsCancellationRequested);
|
||||
Assert.False(_sut.HasActiveCycle(scheduledJobId));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EndCycle_WhenTheCycleWasReplacedByANewerOne_ShouldNotCancelTheNewerCycle()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJobId scheduledJobId = _scheduledJobIdFixture.Create();
|
||||
using CancellationTokenSource oldCycleCancellationTokenSource = new();
|
||||
using CancellationTokenSource newCycleCancellationTokenSource = new();
|
||||
_sut.TryStartCycle(scheduledJobId, oldCycleCancellationTokenSource);
|
||||
_sut.StopCycle(scheduledJobId);
|
||||
_sut.TryStartCycle(scheduledJobId, newCycleCancellationTokenSource);
|
||||
|
||||
// Act
|
||||
_sut.EndCycle(scheduledJobId, oldCycleCancellationTokenSource);
|
||||
|
||||
// Assert
|
||||
Assert.True(oldCycleCancellationTokenSource.IsCancellationRequested);
|
||||
Assert.False(newCycleCancellationTokenSource.IsCancellationRequested);
|
||||
Assert.True(_sut.HasActiveCycle(scheduledJobId));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EndCycle_WhenNoCycleIsRegistered_ShouldStillCancelTheOwnedTokenSource()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJobId scheduledJobId = _scheduledJobIdFixture.Create();
|
||||
using CancellationTokenSource cycleCancellationTokenSource = new();
|
||||
|
||||
// Act
|
||||
_sut.EndCycle(scheduledJobId, cycleCancellationTokenSource);
|
||||
|
||||
// Assert
|
||||
Assert.True(cycleCancellationTokenSource.IsCancellationRequested);
|
||||
Assert.False(_sut.HasActiveCycle(scheduledJobId));
|
||||
}
|
||||
}
|
||||
+1209
File diff suppressed because it is too large.
Load diff
+123
@@ -0,0 +1,123 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Time;
|
||||
using Lumina.Domain.Common.Events;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
|
||||
using Lumina.Infrastructure.Core.Scheduling.Execution;
|
||||
using Lumina.Infrastructure.Core.Scheduling.Execution.TaskExecutors;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Core.Scheduling.Execution;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="ScheduledTaskExecutorFactory"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ScheduledTaskExecutorFactoryTests
|
||||
{
|
||||
private readonly IServiceProvider _mockServiceProvider;
|
||||
private readonly MediaLibraryScanTaskExecutor _mockMediaLibraryScanTaskExecutor;
|
||||
private readonly TemporaryFilesCleanupTaskExecutor _mockTemporaryFilesCleanupTaskExecutor;
|
||||
private readonly RepairThemesTaskExecutor _mockRepairThemesTaskExecutor;
|
||||
private readonly CleanScheduledJobExecutionHistoryTaskExecutor _mockCleanScheduledJobExecutionHistoryTaskExecutor;
|
||||
private readonly TechnicalDataCleanupTaskExecutor _mockTechnicalDataCleanupTaskExecutor;
|
||||
private readonly ScheduledTaskExecutorFactory _sut;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ScheduledTaskExecutorFactoryTests"/> class.
|
||||
/// </summary>
|
||||
public ScheduledTaskExecutorFactoryTests()
|
||||
{
|
||||
_mockServiceProvider = Substitute.For<IServiceProvider>();
|
||||
_mockMediaLibraryScanTaskExecutor = Substitute.For<MediaLibraryScanTaskExecutor>(
|
||||
Substitute.For<IDomainEventPublisher>(),
|
||||
Substitute.For<ILogger<MediaLibraryScanTaskExecutor>>(),
|
||||
Substitute.For<IUnitOfWork>());
|
||||
_mockTemporaryFilesCleanupTaskExecutor = Substitute.For<TemporaryFilesCleanupTaskExecutor>(
|
||||
Substitute.For<ILogger<TemporaryFilesCleanupTaskExecutor>>());
|
||||
_mockRepairThemesTaskExecutor = Substitute.For<RepairThemesTaskExecutor>(
|
||||
Substitute.For<Lumina.Application.Common.Infrastructure.Themes.IThemeService>(),
|
||||
Substitute.For<ILogger<RepairThemesTaskExecutor>>(),
|
||||
Substitute.For<IUnitOfWork>());
|
||||
_mockCleanScheduledJobExecutionHistoryTaskExecutor = Substitute.For<CleanScheduledJobExecutionHistoryTaskExecutor>(
|
||||
Substitute.For<IDateTimeProvider>(),
|
||||
Substitute.For<ILogger<CleanScheduledJobExecutionHistoryTaskExecutor>>(),
|
||||
Substitute.For<IUnitOfWork>());
|
||||
_mockTechnicalDataCleanupTaskExecutor = Substitute.For<TechnicalDataCleanupTaskExecutor>(
|
||||
Substitute.For<ILogger<TechnicalDataCleanupTaskExecutor>>(),
|
||||
Substitute.For<IUnitOfWork>());
|
||||
|
||||
_mockServiceProvider.GetService(typeof(MediaLibraryScanTaskExecutor)).Returns(_mockMediaLibraryScanTaskExecutor);
|
||||
_mockServiceProvider.GetService(typeof(TemporaryFilesCleanupTaskExecutor)).Returns(_mockTemporaryFilesCleanupTaskExecutor);
|
||||
_mockServiceProvider.GetService(typeof(RepairThemesTaskExecutor)).Returns(_mockRepairThemesTaskExecutor);
|
||||
_mockServiceProvider.GetService(typeof(CleanScheduledJobExecutionHistoryTaskExecutor)).Returns(_mockCleanScheduledJobExecutionHistoryTaskExecutor);
|
||||
_mockServiceProvider.GetService(typeof(TechnicalDataCleanupTaskExecutor)).Returns(_mockTechnicalDataCleanupTaskExecutor);
|
||||
|
||||
_sut = new ScheduledTaskExecutorFactory(_mockServiceProvider);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateExecutor_WhenTaskTypeIsScanMediaLibraries_ShouldReturnMediaLibraryScanTaskExecutor()
|
||||
{
|
||||
// Act
|
||||
IScheduledTaskExecutor result = _sut.CreateExecutor(ScheduledTaskType.ScanMediaLibraries);
|
||||
|
||||
// Assert
|
||||
Assert.Same(_mockMediaLibraryScanTaskExecutor, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateExecutor_WhenTaskTypeIsCleanTemporaryFiles_ShouldReturnTemporaryFilesCleanupTaskExecutor()
|
||||
{
|
||||
// Act
|
||||
IScheduledTaskExecutor result = _sut.CreateExecutor(ScheduledTaskType.CleanTemporaryFiles);
|
||||
|
||||
// Assert
|
||||
Assert.Same(_mockTemporaryFilesCleanupTaskExecutor, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateExecutor_WhenTaskTypeIsRepairThemes_ShouldReturnRepairThemesTaskExecutor()
|
||||
{
|
||||
// Act
|
||||
IScheduledTaskExecutor result = _sut.CreateExecutor(ScheduledTaskType.RepairThemes);
|
||||
|
||||
// Assert
|
||||
Assert.Same(_mockRepairThemesTaskExecutor, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateExecutor_WhenTaskTypeIsCleanScheduledJobExecutionHistory_ShouldReturnCleanScheduledJobExecutionHistoryTaskExecutor()
|
||||
{
|
||||
// Act
|
||||
IScheduledTaskExecutor result = _sut.CreateExecutor(ScheduledTaskType.CleanScheduledJobExecutionHistory);
|
||||
|
||||
// Assert
|
||||
Assert.Same(_mockCleanScheduledJobExecutionHistoryTaskExecutor, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateExecutor_WhenTaskTypeIsTechnicalDataCleanup_ShouldReturnTechnicalDataCleanupTaskExecutor()
|
||||
{
|
||||
// Act
|
||||
IScheduledTaskExecutor result = _sut.CreateExecutor(ScheduledTaskType.TechnicalDataCleanup);
|
||||
|
||||
// Assert
|
||||
Assert.Same(_mockTechnicalDataCleanupTaskExecutor, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateExecutor_WhenTaskTypeIsUnsupported_ShouldThrowArgumentException()
|
||||
{
|
||||
// Act
|
||||
ArgumentException exception = Assert.Throws<ArgumentException>(() => _sut.CreateExecutor((ScheduledTaskType)999));
|
||||
|
||||
// Assert
|
||||
Assert.Contains("Unsupported task type", exception.Message);
|
||||
Assert.Equal("taskType", exception.ParamName);
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Scheduling;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Time;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
|
||||
using Lumina.Infrastructure.Core.Scheduling.Execution.TaskExecutors;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Core.Scheduling.Execution.TaskExecutors;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="CleanScheduledJobExecutionHistoryTaskExecutor"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class CleanScheduledJobExecutionHistoryTaskExecutorTests
|
||||
{
|
||||
private readonly IDateTimeProvider _mockDateTimeProvider;
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly IScheduledJobExecutionRepository _mockScheduledJobExecutionRepository;
|
||||
private readonly CleanScheduledJobExecutionHistoryTaskExecutor _sut;
|
||||
private readonly ScheduledJobFixture _scheduledJobFixture = new();
|
||||
private readonly DateTime _fixedUtcNow = DateTime.UtcNow;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CleanScheduledJobExecutionHistoryTaskExecutorTests"/> class.
|
||||
/// </summary>
|
||||
public CleanScheduledJobExecutionHistoryTaskExecutorTests()
|
||||
{
|
||||
_mockDateTimeProvider = Substitute.For<IDateTimeProvider>();
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
|
||||
_mockScheduledJobExecutionRepository = Substitute.For<IScheduledJobExecutionRepository>();
|
||||
_mockDateTimeProvider.UtcNow.Returns(_fixedUtcNow);
|
||||
_mockUnitOfWork.ScheduledJobExecutionRepository.Returns(_mockScheduledJobExecutionRepository);
|
||||
ILogger<CleanScheduledJobExecutionHistoryTaskExecutor> logger = Substitute.For<ILogger<CleanScheduledJobExecutionHistoryTaskExecutor>>();
|
||||
_sut = new CleanScheduledJobExecutionHistoryTaskExecutor(_mockDateTimeProvider, logger, _mockUnitOfWork);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenCalled_ShouldDeleteTheExecutionsOlderThanAMonth()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create(taskType: ScheduledTaskType.CleanScheduledJobExecutionHistory);
|
||||
_mockScheduledJobExecutionRepository.DeleteOlderThanAsync(Arg.Any<DateTime>(), Arg.Any<CancellationToken>()).Returns(Result.Success);
|
||||
DateTime expectedCutoff = _fixedUtcNow.AddMonths(-1);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockScheduledJobExecutionRepository.Received(1).DeleteOlderThanAsync(Arg.Is<DateTime>(cutoff => cutoff == expectedCutoff), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenTheDeletionFails_ShouldReturnTheError()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create(taskType: ScheduledTaskType.CleanScheduledJobExecutionHistory);
|
||||
Error expectedError = Error.Failure("Database.Error", "Failed to delete the old executions");
|
||||
_mockScheduledJobExecutionRepository.DeleteOlderThanAsync(Arg.Any<DateTime>(), Arg.Any<CancellationToken>()).Returns(expectedError);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(expectedError, result.FirstError);
|
||||
}
|
||||
}
|
||||
+172
@@ -0,0 +1,172 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Domain.Common.Events;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using Lumina.Infrastructure.Core.Scheduling.Execution.TaskExecutors;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Core.Scheduling.Execution.TaskExecutors;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="MediaLibraryScanTaskExecutor"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class MediaLibraryScanTaskExecutorTests
|
||||
{
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly ILibraryRepository _mockLibraryRepository;
|
||||
private readonly IDomainEventPublisher _mockDomainEventPublisher;
|
||||
private readonly MediaLibraryScanTaskExecutor _sut;
|
||||
private readonly ScheduledJobFixture _scheduledJobFixture = new();
|
||||
private readonly LibraryEntityFixture _libraryEntityFixture = new();
|
||||
private readonly LibraryScanEntityFixture _libraryScanEntityFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MediaLibraryScanTaskExecutorTests"/> class.
|
||||
/// </summary>
|
||||
public MediaLibraryScanTaskExecutorTests()
|
||||
{
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
|
||||
_mockLibraryRepository.GetAllEnabledAndUnlockedAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<LibraryEntity>>([]));
|
||||
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
|
||||
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
|
||||
_mockDomainEventPublisher = Substitute.For<IDomainEventPublisher>();
|
||||
_sut = new MediaLibraryScanTaskExecutor(_mockDomainEventPublisher, Substitute.For<ILogger<MediaLibraryScanTaskExecutor>>(), _mockUnitOfWork);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenNoLibrariesAreEnabled_ShouldReturnSuccessWithoutPublishingEvents()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create();
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(Result.Success, result.Value);
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
await _mockDomainEventPublisher.DidNotReceive().PublishAsync(Arg.Any<IDomainEvent>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenGetAllEnabledAndUnlockedLibrariesFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create();
|
||||
Error error = Error.Failure("Database.Error", "Failed to get the enabled libraries");
|
||||
_mockLibraryRepository.GetAllEnabledAndUnlockedAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenGetPastMonthScansFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create();
|
||||
LibraryEntity library = _libraryEntityFixture.Create();
|
||||
_mockLibraryRepository.GetAllEnabledAndUnlockedAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<LibraryEntity>>([library]));
|
||||
Error error = Error.Failure("Database.Error", "Failed to get the past month scans");
|
||||
_mockUnitOfWork.LibraryScanRepository.GetPastMonthScansByLibraryIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenALibraryIsAlreadyBeingScanned_ShouldSkipThatLibrary()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create();
|
||||
LibraryEntity library = _libraryEntityFixture.Create();
|
||||
_mockLibraryRepository.GetAllEnabledAndUnlockedAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<LibraryEntity>>([library]));
|
||||
LibraryScanEntity runningScan = _libraryScanEntityFixture.Create(libraryId: library.Id, status: LibraryScanJobStatus.Running);
|
||||
_mockUnitOfWork.LibraryScanRepository.GetPastMonthScansByLibraryIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<LibraryScanEntity>>([runningScan]));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
await _mockDomainEventPublisher.DidNotReceive().PublishAsync(Arg.Any<IDomainEvent>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenAQueuedScanIsInserted_ShouldPublishItsDomainEvents()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create();
|
||||
LibraryEntity library = _libraryEntityFixture.Create();
|
||||
_mockLibraryRepository.GetAllEnabledAndUnlockedAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<LibraryEntity>>([library]));
|
||||
_mockUnitOfWork.LibraryScanRepository.GetPastMonthScansByLibraryIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<LibraryScanEntity>>([]));
|
||||
_mockUnitOfWork.LibraryScanRepository.InsertAsync(Arg.Any<LibraryScanEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.Created);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockUnitOfWork.LibraryScanRepository.Received(1).InsertAsync(Arg.Any<LibraryScanEntity>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
await _mockDomainEventPublisher.Received(1).PublishAsync(Arg.Any<IDomainEvent>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenInsertingTheQueuedScanFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create();
|
||||
LibraryEntity library = _libraryEntityFixture.Create();
|
||||
_mockLibraryRepository.GetAllEnabledAndUnlockedAsync(Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<LibraryEntity>>([library]));
|
||||
_mockUnitOfWork.LibraryScanRepository.GetPastMonthScansByLibraryIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IEnumerable<LibraryScanEntity>>([]));
|
||||
Error error = Error.Failure("Database.Error", "Failed to insert the scan");
|
||||
_mockUnitOfWork.LibraryScanRepository.InsertAsync(Arg.Any<LibraryScanEntity>(), Arg.Any<CancellationToken>())
|
||||
.Returns(error);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(error, result.FirstError);
|
||||
await _mockDomainEventPublisher.DidNotReceive().PublishAsync(Arg.Any<IDomainEvent>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Themes;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.DTO.Filtering;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
using Lumina.Application.Common.Infrastructure.Models.DTO.Themes;
|
||||
using Lumina.Application.Common.Infrastructure.Themes;
|
||||
using Lumina.Application.Fixtures.Common.DTO.Pagination;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
|
||||
using Lumina.Infrastructure.Core.Scheduling.Execution.TaskExecutors;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Core.Scheduling.Execution.TaskExecutors;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="RepairThemesTaskExecutor"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class RepairThemesTaskExecutorTests
|
||||
{
|
||||
private readonly IThemeService _mockThemeService;
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly IThemeRepository _mockThemeRepository;
|
||||
private readonly RepairThemesTaskExecutor _sut;
|
||||
private readonly ScheduledJobFixture _scheduledJobFixture = new();
|
||||
private readonly PaginatedResultDtoFixture<ThemeEntity> _paginatedResultDtoFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RepairThemesTaskExecutorTests"/> class.
|
||||
/// </summary>
|
||||
public RepairThemesTaskExecutorTests()
|
||||
{
|
||||
_mockThemeService = Substitute.For<IThemeService>();
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockThemeRepository = Substitute.For<IThemeRepository>();
|
||||
_mockUnitOfWork.ThemeRepository.Returns(_mockThemeRepository);
|
||||
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
|
||||
ILogger<RepairThemesTaskExecutor> logger = Substitute.For<ILogger<RepairThemesTaskExecutor>>();
|
||||
_sut = new RepairThemesTaskExecutor(_mockThemeService, logger, _mockUnitOfWork);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenCalled_ShouldSynchronizeTheThemes()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create(taskType: ScheduledTaskType.RepairThemes);
|
||||
_mockThemeService.GetBundledThemeArchivePaths().Returns([]);
|
||||
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From(_paginatedResultDtoFixture.Create(data: [], currentPage: 1, perPage: 0, count: 0, numberOfPages: 1)));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockThemeRepository.Received(1).GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenABundledThemeArchiveCannotBeRead_ShouldStillSucceed()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create(taskType: ScheduledTaskType.RepairThemes);
|
||||
_mockThemeService.GetBundledThemeArchivePaths().Returns(["missing.zip"]);
|
||||
_mockThemeService.ReadManifestFromArchiveAsync(Arg.Any<string>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure("Theme.Manifest", "Failed to read the theme manifest"));
|
||||
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From(_paginatedResultDtoFixture.Create(data: [], currentPage: 1, perPage: 0, count: 0, numberOfPages: 1)));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockThemeRepository.Received(1).GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenTheSynchronizationThrows_ShouldReturnFailure()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create(taskType: ScheduledTaskType.RepairThemes);
|
||||
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Task.FromException<Result<PaginatedResultDto<ThemeEntity>>>(new InvalidOperationException("The database is unavailable.")));
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(nameof(RepairThemesTaskExecutor), result.FirstError.Description);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenTheSynchronizationIsCancelled_ShouldRethrowTheCancellation()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create(taskType: ScheduledTaskType.RepairThemes);
|
||||
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Task.FromException<Result<PaginatedResultDto<ThemeEntity>>>(new OperationCanceledException()));
|
||||
|
||||
// Act
|
||||
async Task Act()
|
||||
{
|
||||
await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
}
|
||||
|
||||
// Assert
|
||||
await Assert.ThrowsAsync<OperationCanceledException>(Act);
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
|
||||
using Lumina.Infrastructure.Core.Scheduling.Execution.TaskExecutors;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Core.Scheduling.Execution.TaskExecutors;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="TechnicalDataCleanupTaskExecutor"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class TechnicalDataCleanupTaskExecutorTests
|
||||
{
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly ILibraryScanRepository _mockLibraryScanRepository;
|
||||
private readonly TechnicalDataCleanupTaskExecutor _sut;
|
||||
private readonly ScheduledJobFixture _scheduledJobFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TechnicalDataCleanupTaskExecutorTests"/> class.
|
||||
/// </summary>
|
||||
public TechnicalDataCleanupTaskExecutorTests()
|
||||
{
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockLibraryScanRepository = Substitute.For<ILibraryScanRepository>();
|
||||
_mockUnitOfWork.LibraryScanRepository.Returns(_mockLibraryScanRepository);
|
||||
ILogger<TechnicalDataCleanupTaskExecutor> logger = Substitute.For<ILogger<TechnicalDataCleanupTaskExecutor>>();
|
||||
_sut = new TechnicalDataCleanupTaskExecutor(logger, _mockUnitOfWork);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenCalled_ShouldFailTheInterruptedScans()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create(taskType: ScheduledTaskType.TechnicalDataCleanup);
|
||||
_mockLibraryScanRepository.FailInterruptedScansAsync(Arg.Any<CancellationToken>()).Returns(Result.Updated);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
await _mockLibraryScanRepository.Received(1).FailInterruptedScansAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenFailingTheInterruptedScansFails_ShouldReturnTheError()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create(taskType: ScheduledTaskType.TechnicalDataCleanup);
|
||||
Error expectedError = Error.Failure("Database.Error", "Failed to fail the interrupted scans");
|
||||
_mockLibraryScanRepository.FailInterruptedScansAsync(Arg.Any<CancellationToken>()).Returns(expectedError);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(expectedError, result.FirstError);
|
||||
}
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate;
|
||||
using Lumina.Infrastructure.Core.Scheduling.Execution.TaskExecutors;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.UnitTests.Core.Scheduling.Execution.TaskExecutors;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="TemporaryFilesCleanupTaskExecutor"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class TemporaryFilesCleanupTaskExecutorTests
|
||||
{
|
||||
private readonly TemporaryFilesCleanupTaskExecutor _sut;
|
||||
private readonly ScheduledJobFixture _scheduledJobFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TemporaryFilesCleanupTaskExecutorTests"/> class.
|
||||
/// </summary>
|
||||
public TemporaryFilesCleanupTaskExecutorTests()
|
||||
{
|
||||
ILogger<TemporaryFilesCleanupTaskExecutor> logger = Substitute.For<ILogger<TemporaryFilesCleanupTaskExecutor>>();
|
||||
_sut = new TemporaryFilesCleanupTaskExecutor(logger);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenTemporaryDirectoryExists_ShouldEmptyItAndReturnSuccess()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create();
|
||||
string temporaryDirectory = Path.Combine(AppContext.BaseDirectory, "reading-cache");
|
||||
Directory.CreateDirectory(Path.Combine(temporaryDirectory, "subdirectory"));
|
||||
File.WriteAllText(Path.Combine(temporaryDirectory, "subdirectory", "file.txt"), "content");
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(Result.Success, result.Value);
|
||||
Assert.True(Directory.Exists(temporaryDirectory));
|
||||
Assert.Empty(Directory.GetFileSystemEntries(temporaryDirectory));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenTemporaryDirectoryDoesNotExist_ShouldRecreateItAndReturnSuccess()
|
||||
{
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create();
|
||||
string temporaryDirectory = Path.Combine(AppContext.BaseDirectory, "reading-cache");
|
||||
if (Directory.Exists(temporaryDirectory))
|
||||
Directory.Delete(temporaryDirectory, recursive: true);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(Result.Success, result.Value);
|
||||
Assert.True(Directory.Exists(temporaryDirectory));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecutePayloadAsync_WhenDeletingTheDirectoryFails_ShouldReturnError()
|
||||
{
|
||||
// On non Windows platforms deleting a file that is locked open succeeds, so the failure path can only be forced on Windows.
|
||||
if (!OperatingSystem.IsWindows())
|
||||
return;
|
||||
|
||||
// Arrange
|
||||
ScheduledJob scheduledJob = _scheduledJobFixture.Create();
|
||||
string temporaryDirectory = Path.Combine(AppContext.BaseDirectory, "reading-cache");
|
||||
Directory.CreateDirectory(temporaryDirectory);
|
||||
string lockedFile = Path.Combine(temporaryDirectory, "locked.txt");
|
||||
File.WriteAllText(lockedFile, "content");
|
||||
// Lock the file with an exclusive handle so the recursive delete fails.
|
||||
using FileStream lockStream = new(lockedFile, FileMode.Open, FileAccess.Read, FileShare.None);
|
||||
|
||||
// Act
|
||||
Result<Success> result = await _sut.ExecutePayloadAsync(scheduledJob, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user