mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 08:11:10 +00:00
Guards structured logging with IsEnabled checks
This commit is contained in:
1 parent
ccbd653ee5
commit
2e53449ada
44 files changed
+448
-136
No files matched your search
+8
@@ -23,6 +23,7 @@ using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@@ -120,6 +121,7 @@ public class PluginDetectionSyncJobTests
|
||||
public async Task StartAsync_WhenUpsertFails_ShouldLogErrorAndContinueWithOtherPlugins()
|
||||
{
|
||||
// Arrange
|
||||
_mockLogger.IsEnabled(Arg.Any<LogLevel>()).Returns(true);
|
||||
IPlugin failingPlugin = CreatePlugin(Guid.NewGuid(), "Failing Plugin", new Version(1, 0, 0));
|
||||
IPlugin successfulPlugin = CreatePlugin(Guid.NewGuid(), "Successful Plugin", new Version(1, 0, 0));
|
||||
_mockPluginManager.GetPlugins().Returns([failingPlugin, successfulPlugin]);
|
||||
@@ -139,6 +141,12 @@ public class PluginDetectionSyncJobTests
|
||||
// Assert
|
||||
await _mockPluginRepository.Received(1).UpsertAsync(Arg.Is<PluginEntity>(plugin => plugin.Id == successfulPlugin.Id), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
_mockLogger.Received(1).Log(
|
||||
LogLevel.Error,
|
||||
Arg.Any<EventId>(),
|
||||
Arg.Is<object>(state => state.ToString()!.Contains("Failed to persist the detection of plugin")),
|
||||
Arg.Any<Exception?>(),
|
||||
Arg.Any<Func<object, Exception?, string>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
+8
@@ -9,6 +9,7 @@ using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
@@ -96,6 +97,7 @@ public class PluginSettingsStoreTests
|
||||
public async Task GetSettingsAsync_WhenRepositoryReturnsFailure_ShouldReturnNullAndLogWarning()
|
||||
{
|
||||
// Arrange
|
||||
_mockLogger.IsEnabled(Arg.Any<LogLevel>()).Returns(true);
|
||||
Guid pluginId = Guid.NewGuid();
|
||||
_mockPluginRepository.GetByIdAsync(pluginId, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure("Database.Error", "Failed to retrieve the plugin"));
|
||||
@@ -105,5 +107,11 @@ public class PluginSettingsStoreTests
|
||||
|
||||
// Assert
|
||||
Assert.Null(result);
|
||||
_mockLogger.Received(1).Log(
|
||||
LogLevel.Warning,
|
||||
Arg.Any<EventId>(),
|
||||
Arg.Is<object>(state => state.ToString()!.Contains("Failed to read the settings of the plugin")),
|
||||
Arg.Any<Exception?>(),
|
||||
Arg.Any<Func<object, Exception?, string>>());
|
||||
}
|
||||
}
|
||||
+18
-2
@@ -25,6 +25,7 @@ using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@@ -49,6 +50,7 @@ public class ScheduledJobSchedulerJobTests
|
||||
private readonly IDomainEventPublisher _mockDomainEventPublisher;
|
||||
private readonly IScheduledTaskExecutorFactory _mockTaskExecutorFactory;
|
||||
private readonly IScheduledTaskExecutor _mockTaskExecutor;
|
||||
private readonly ILogger<ScheduledJobSchedulerJob> _mockLogger;
|
||||
private readonly ScheduledJobRuntimeRegistry _runtimeRegistry;
|
||||
private readonly ScheduledJobSchedulerJob _sut;
|
||||
private readonly Dictionary<Type, object> _services;
|
||||
@@ -95,8 +97,8 @@ public class ScheduledJobSchedulerJobTests
|
||||
.Returns(callInfo => _services.TryGetValue((Type)callInfo[0]!, out object? service) ? service : null);
|
||||
|
||||
_runtimeRegistry = new ScheduledJobRuntimeRegistry();
|
||||
ILogger<ScheduledJobSchedulerJob> logger = Substitute.For<ILogger<ScheduledJobSchedulerJob>>();
|
||||
_sut = new ScheduledJobSchedulerJob(_runtimeRegistry, logger, _mockServiceScopeFactory);
|
||||
_mockLogger = Substitute.For<ILogger<ScheduledJobSchedulerJob>>();
|
||||
_sut = new ScheduledJobSchedulerJob(_runtimeRegistry, _mockLogger, _mockServiceScopeFactory);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -555,6 +557,7 @@ public class ScheduledJobSchedulerJobTests
|
||||
public async Task ExecuteScheduledJobRunAsync_WhenTheTaskPayloadFails_ShouldLogTheFailureAndStillCompleteTheExecution()
|
||||
{
|
||||
// Arrange
|
||||
_mockLogger.IsEnabled(Arg.Any<LogLevel>()).Returns(true);
|
||||
ScheduledJobEntity addedJob = _scheduledJobEntityFixture.Create(status: ScheduledJobStatus.Added, scheduleType: ScheduleType.WithIntervalInMinutes, intervalMinutes: 60);
|
||||
ScheduledJobId scheduledJobId = _scheduledJobIdFixture.Create(addedJob.Id);
|
||||
_mockScheduledJobRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>()).Returns(addedJob);
|
||||
@@ -568,12 +571,19 @@ public class ScheduledJobSchedulerJobTests
|
||||
// Assert
|
||||
await _mockTaskExecutor.Received(1).ExecutePayloadAsync(Arg.Any<ScheduledJob>(), Arg.Any<CancellationToken>());
|
||||
await _mockDomainEventPublisher.Received(1).PublishAsync(Arg.Is<ScheduledJobExecutionCompletedDomainEvent>(domainEvent => !domainEvent.IsCycleRun), Arg.Any<CancellationToken>());
|
||||
_mockLogger.Received(1).Log(
|
||||
LogLevel.Warning,
|
||||
Arg.Any<EventId>(),
|
||||
Arg.Is<object>(state => state.ToString()!.Contains("failed:")),
|
||||
Arg.Any<Exception?>(),
|
||||
Arg.Any<Func<object, Exception?, string>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecuteScheduledJobRunAsync_WhenTheTaskPayloadThrows_ShouldLogTheExceptionAndStillCompleteTheExecution()
|
||||
{
|
||||
// Arrange
|
||||
_mockLogger.IsEnabled(Arg.Any<LogLevel>()).Returns(true);
|
||||
ScheduledJobEntity addedJob = _scheduledJobEntityFixture.Create(status: ScheduledJobStatus.Added, scheduleType: ScheduleType.WithIntervalInMinutes, intervalMinutes: 60);
|
||||
ScheduledJobId scheduledJobId = _scheduledJobIdFixture.Create(addedJob.Id);
|
||||
_mockScheduledJobRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>()).Returns(addedJob);
|
||||
@@ -586,6 +596,12 @@ public class ScheduledJobSchedulerJobTests
|
||||
|
||||
// Assert
|
||||
await _mockDomainEventPublisher.Received(1).PublishAsync(Arg.Is<ScheduledJobExecutionCompletedDomainEvent>(domainEvent => !domainEvent.IsCycleRun), Arg.Any<CancellationToken>());
|
||||
_mockLogger.Received(1).Log(
|
||||
LogLevel.Error,
|
||||
Arg.Any<EventId>(),
|
||||
Arg.Any<object>(),
|
||||
Arg.Is<Exception>(exception => exception is InvalidOperationException && exception.Message == "The task threw."),
|
||||
Arg.Any<Func<object, Exception?, string>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
@@ -52,6 +52,7 @@ public class ThemeSynchronizerTests : IDisposable
|
||||
_mockUnitOfWork.ThemeRepository.Returns(_mockThemeRepository);
|
||||
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
|
||||
_mockLogger = Substitute.For<ILogger>();
|
||||
_mockLogger.IsEnabled(Arg.Any<LogLevel>()).Returns(true);
|
||||
_mockThemeService.GetBundledThemeArchivePaths().Returns([]);
|
||||
_mockThemeService.HasThemePack(Arg.Any<string>()).Returns(true);
|
||||
_mockThemeService.DefaultThemeId.Returns("lumina-default");
|
||||
@@ -79,6 +80,28 @@ public class ThemeSynchronizerTests : IDisposable
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SynchronizeAsync_WhenLoggingIsDisabledAndTheInstalledThemesCannotBeRead_ShouldNotLogAWarning()
|
||||
{
|
||||
// Arrange
|
||||
_mockLogger.IsEnabled(Arg.Any<LogLevel>()).Returns(false);
|
||||
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Error.Failure("Themes.NotFound", "Failed to read the installed themes"));
|
||||
_mockThemeService.GetBundledThemeArchivePaths().Returns(["bundled.zip"]);
|
||||
|
||||
// Act
|
||||
await ThemeSynchronizer.SynchronizeAsync(_mockThemeService, _mockUnitOfWork, _mockLogger, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
_mockLogger.DidNotReceive().Log(
|
||||
LogLevel.Warning,
|
||||
Arg.Any<EventId>(),
|
||||
Arg.Any<object>(),
|
||||
Arg.Any<Exception?>(),
|
||||
Arg.Any<Func<object, Exception?, string>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SynchronizeAsync_WhenABundledArchiveManifestCannotBeRead_ShouldSkipThatArchive()
|
||||
{
|
||||
@@ -235,6 +258,12 @@ public class ThemeSynchronizerTests : IDisposable
|
||||
// Assert
|
||||
await _mockThemeRepository.Received(1).InsertAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>());
|
||||
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
||||
_mockLogger.Received(1).Log(
|
||||
LogLevel.Warning,
|
||||
Arg.Any<EventId>(),
|
||||
Arg.Is<object>(state => state.ToString()!.Contains("Failed to persist the detection of theme")),
|
||||
Arg.Any<Exception?>(),
|
||||
Arg.Any<Func<object, Exception?, string>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
Reference in new issue
Block a user