Files
scadalink-design/tests/ScadaLink.NotificationOutbox.Tests/NotificationOutboxActorPurgeTests.cs
Joseph Doherty b31747a632 feat(notif): NotificationOutboxActor + CentralAuditWriter wired (#23 M4)
M4 Bundle B (B1) — add the central-only ICentralAuditWriter implementation
and inject it into NotificationOutboxActor so subsequent tasks (B2/B3) can
route attempt + terminal lifecycle events through the direct-write audit path.

- CentralAuditWriter: thin wrapper around IAuditLogRepository.InsertIfNotExistsAsync;
  scope-per-call (matches AuditLogIngestActor / NotificationOutboxActor pattern);
  stamps IngestedAtUtc; swallows all internal failures (alog.md §13).
- Registered as a singleton in AddAuditLog.
- NotificationOutboxActor ctor takes ICentralAuditWriter (validated non-null).
- Host wiring resolves the writer once from the root provider and passes it
  into the singleton's Props.Create call.
- Existing TestKit fixtures updated with a NoOpCentralAuditWriter helper so
  tests that don't exercise audit emission still compile and pass.
2026-05-20 16:04:01 -04:00

109 lines
4.3 KiB
C#

using Akka.Actor;
using Akka.TestKit.Xunit2;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
using ScadaLink.Commons.Entities.Notifications;
using ScadaLink.Commons.Interfaces.Repositories;
using ScadaLink.Commons.Types.Enums;
using ScadaLink.NotificationOutbox.Delivery;
using ScadaLink.NotificationOutbox.Messages;
using ScadaLink.NotificationOutbox.Tests.TestSupport;
namespace ScadaLink.NotificationOutbox.Tests;
/// <summary>
/// Task 16: 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 =
Substitute.For<INotificationOutboxRepository>();
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)));
}
[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>()));
}
[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>()));
}
}