Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.NotificationOutbox.Tests/NotificationOutboxActorPurgeTests.cs
T
Joseph Doherty 513fa1aa16 docs(tests): fix CommentChecker findings across tests/ — 13,392 → 0 (repo-wide scan clean)
Comment/XML-doc changes only:
- add summaries to ~10k test methods, fixtures, fakes and helpers
  (+ <param> on theories, <returns> on async Task methods,
  <inheritdoc/> on interface implementations the checker can resolve)
- strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N,
  #NN, Component-0NN) from comments, keeping the explanatory text;
  reword false positives in comments only
- real docs on fakes implementing BCL/library interfaces
  (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime)
- reword the last 5 src/ false positives (CWE-1236, failures-1,
  year-0001, M365)

Only non-comment edits: four one-line nested classes split across lines
so members can carry docs. Build 0 errors / 0 warnings.
2026-10-07 15:05:49 -04:00

111 lines
4.6 KiB
C#

using Akka.Actor;
using Akka.TestKit.Xunit2;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.NotificationOutbox.Delivery;
using ZB.MOM.WW.ScadaBridge.NotificationOutbox.Messages;
using ZB.MOM.WW.ScadaBridge.NotificationOutbox.Tests.TestSupport;
namespace ZB.MOM.WW.ScadaBridge.NotificationOutbox.Tests;
/// <summary>
/// Tests for the <see cref="NotificationOutboxActor"/> daily purge job — the
/// periodic sweep that bulk-deletes terminal notification rows older than
/// <see cref="NotificationOutboxOptions.TerminalRetention"/> via
/// <see cref="INotificationOutboxRepository.DeleteTerminalOlderThanAsync"/>.
/// </summary>
public class NotificationOutboxActorPurgeTests : TestKit
{
private readonly INotificationOutboxRepository _outboxRepository =
OutboxRepositorySubstitute.Healthy();
private readonly INotificationRepository _notificationRepository =
Substitute.For<INotificationRepository>();
private IServiceProvider BuildServiceProvider()
{
var services = new ServiceCollection();
services.AddScoped(_ => _outboxRepository);
services.AddScoped(_ => _notificationRepository);
return services.BuildServiceProvider();
}
/// <summary>
/// Creates the actor with both the dispatch and purge timers set to a long interval so
/// neither periodic timer fires during a test — purge ticks are sent manually instead.
/// </summary>
private IActorRef CreateActor(NotificationOutboxOptions? options = null)
{
return Sys.ActorOf(Props.Create(() => new NotificationOutboxActor(
BuildServiceProvider(),
options ?? new NotificationOutboxOptions
{
DispatchInterval = TimeSpan.FromHours(1),
PurgeInterval = TimeSpan.FromHours(1),
},
new NoOpCentralAuditWriter(),
NullLogger<NotificationOutboxActor>.Instance)));
}
/// <summary>Verifies that a purge tick deletes terminal rows using a cutoff of the current UTC time minus the terminal retention.</summary>
[Fact]
public void PurgeTick_DeletesTerminalRows_WithCutoffAtUtcNowMinusTerminalRetention()
{
var retention = TimeSpan.FromDays(30);
var options = new NotificationOutboxOptions
{
DispatchInterval = TimeSpan.FromHours(1),
PurgeInterval = TimeSpan.FromHours(1),
TerminalRetention = retention,
};
_outboxRepository
.DeleteTerminalOlderThanAsync(Arg.Any<DateTimeOffset>(), Arg.Any<CancellationToken>())
.Returns(3);
var actor = CreateActor(options);
var expectedCutoff = DateTimeOffset.UtcNow - retention;
actor.Tell(InternalMessages.PurgeTick.Instance);
AwaitAssert(() =>
_outboxRepository.Received(1).DeleteTerminalOlderThanAsync(
Arg.Is<DateTimeOffset>(cutoff =>
(cutoff - expectedCutoff).Duration() < TimeSpan.FromMinutes(1)),
Arg.Any<CancellationToken>()));
}
/// <summary>Verifies that a faulted purge does not crash the actor and a later purge still runs.</summary>
[Fact]
public void FaultedPurge_DoesNotCrashActor_AndSubsequentPurgeStillRuns()
{
var calls = 0;
_outboxRepository
.DeleteTerminalOlderThanAsync(Arg.Any<DateTimeOffset>(), Arg.Any<CancellationToken>())
.Returns(_ =>
{
calls++;
// First purge faults; the second returns normally to prove the actor lives on.
if (calls == 1)
{
throw new InvalidOperationException("db down");
}
return Task.FromResult(0);
});
var actor = CreateActor();
// First tick: the purge faults internally but must be handled and not kill the actor.
actor.Tell(InternalMessages.PurgeTick.Instance);
AwaitAssert(() => _outboxRepository.Received(1).DeleteTerminalOlderThanAsync(
Arg.Any<DateTimeOffset>(), Arg.Any<CancellationToken>()));
// Second tick: if the faulted purge had crashed the actor, this would never run.
actor.Tell(InternalMessages.PurgeTick.Instance);
AwaitAssert(() => _outboxRepository.Received(2).DeleteTerminalOlderThanAsync(
Arg.Any<DateTimeOffset>(), Arg.Any<CancellationToken>()));
}
}