1cea01d924
P5-3: add IX_Notifications_Created_NotificationId (CreatedAt DESC, NotificationId DESC) — the Central UI Notification Outbox list page sorts on exactly that pair, but no existing index leads unconditionally on CreatedAt, so an unfiltered/lightly filtered page fell back to a full clustered-index scan + sort on every COUNT + page request. Mirrors the Medium wave's IX_SiteCalls_Created_TrackedOperationId migration + SQL twin pattern faithfully, including the handwritten docs/plans/sql script. P5-4: operator Retry/Discard was read-check-write keyed on NotificationId only — a detached PipeTo task interleaving with the dispatcher's own write (or a second concurrent operator action) could land between the read and the write and get silently clobbered by the unconditional UpdateAsync, stamping a Delivered row Discarded with stale fields. Adds INotificationOutboxRepository.TryUpdateFromParkedAsync, a single-statement compare-and-swap (WHERE Status == Parked) mirroring SecuredWriteRepository.TryMarkApprovedAsync's CAS shape. NotificationOutboxActor's RetryAsync/DiscardAsync now use the CAS write and, on a lost race, re-read to report "notification already progressed to <Status>" instead of silently overwriting whatever is there now. Proof of failure: reverting only the actor's CAS usage while keeping the repository addition reproduces the silent-overwrite bug — 7 tests fail, including two new deterministic (non-sleep) race-proof tests that mock the exact GetByIdAsync -> lost-CAS -> re-read sequence the finding describes. P5-3 is perf-only; verified via migration/SQL-twin parity instead (no runtime proof-of-failure is meaningful for an index).
156 lines
5.8 KiB
C#
156 lines
5.8 KiB
C#
using Akka.Actor;
|
|
using Akka.TestKit.Xunit2;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using NSubstitute;
|
|
using ZB.MOM.WW.Audit;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Services;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Notification;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
|
|
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
|
|
using ZB.MOM.WW.ScadaBridge.NotificationOutbox.Delivery;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.NotificationOutbox.Tests;
|
|
|
|
/// <summary>
|
|
/// Task 13 (arch-review 04) — verifies that an operator-initiated Retry on a parked
|
|
/// notification emits a <see cref="AuditChannel.Notification"/>/<see cref="AuditKind.NotifyDeliver"/>
|
|
/// audit row with status <see cref="AuditStatus.Submitted"/> and the operator as
|
|
/// <c>Actor</c>, closing the forensic gap (previously the lifecycle read
|
|
/// <c>Parked → Attempted → Delivered</c> with no record of who un-parked it). Also
|
|
/// asserts the audit write is best-effort — a throwing writer never fails the retry.
|
|
/// </summary>
|
|
public class NotificationOutboxActorRetryEmissionTests : TestKit
|
|
{
|
|
private readonly INotificationOutboxRepository _outboxRepository =
|
|
OutboxRepositorySubstitute.Healthy();
|
|
|
|
private readonly RecordingCentralAuditWriter _auditWriter = new();
|
|
|
|
private sealed class RecordingCentralAuditWriter : ICentralAuditWriter
|
|
{
|
|
public List<AuditRowProjection.AuditRowValues> Events { get; } = new();
|
|
public Func<AuditEvent, Task>? OnWrite { get; set; }
|
|
|
|
public Task WriteAsync(AuditEvent evt, CancellationToken ct = default)
|
|
{
|
|
lock (Events)
|
|
{
|
|
Events.Add(evt.AsRow());
|
|
}
|
|
|
|
return OnWrite?.Invoke(evt) ?? Task.CompletedTask;
|
|
}
|
|
}
|
|
|
|
private IServiceProvider BuildServiceProvider()
|
|
{
|
|
var services = new ServiceCollection();
|
|
services.AddScoped(_ => _outboxRepository);
|
|
return services.BuildServiceProvider();
|
|
}
|
|
|
|
private IActorRef CreateActor()
|
|
{
|
|
return Sys.ActorOf(Props.Create(() => new NotificationOutboxActor(
|
|
BuildServiceProvider(),
|
|
new NotificationOutboxOptions { DispatchInterval = TimeSpan.FromHours(1) },
|
|
(ICentralAuditWriter)_auditWriter,
|
|
NullLogger<NotificationOutboxActor>.Instance)));
|
|
}
|
|
|
|
private static Notification MakeParked(Guid? notificationId = null)
|
|
{
|
|
return new Notification(
|
|
(notificationId ?? Guid.NewGuid()).ToString("D"),
|
|
NotificationType.Email,
|
|
"ops-team",
|
|
"Tank overflow",
|
|
"Tank 3 level critical",
|
|
"site-1")
|
|
{
|
|
Status = NotificationStatus.Parked,
|
|
RetryCount = 5,
|
|
CreatedAt = DateTimeOffset.UtcNow,
|
|
};
|
|
}
|
|
|
|
private List<AuditRowProjection.AuditRowValues> EventsByStatus(AuditStatus status)
|
|
{
|
|
lock (_auditWriter.Events)
|
|
{
|
|
return _auditWriter.Events.Where(e => e.Status == status).ToList();
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Retry_OnParked_Emits_Submitted_NotifyDeliver_AuditRow_With_Operator()
|
|
{
|
|
var notificationId = Guid.NewGuid();
|
|
var notification = MakeParked(notificationId);
|
|
_outboxRepository.GetByIdAsync(notification.NotificationId, Arg.Any<CancellationToken>())
|
|
.Returns(notification);
|
|
var actor = CreateActor();
|
|
|
|
actor.Tell(new RetryNotificationRequest(
|
|
CorrelationId: "test-corr", NotificationId: notification.NotificationId, RequestedBy: "jdoe"));
|
|
|
|
ExpectMsg<RetryNotificationResponse>(r => r.Success);
|
|
AwaitAssert(() =>
|
|
{
|
|
var submitted = EventsByStatus(AuditStatus.Submitted);
|
|
Assert.Single(submitted);
|
|
var evt = submitted[0];
|
|
Assert.Equal(AuditChannel.Notification, evt.Channel);
|
|
Assert.Equal(AuditKind.NotifyDeliver, evt.Kind);
|
|
Assert.Equal("jdoe", evt.Actor);
|
|
Assert.Equal(notificationId, evt.CorrelationId);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void Discard_OnParked_Emits_Discarded_AuditRow_With_Operator()
|
|
{
|
|
var notification = MakeParked();
|
|
_outboxRepository.GetByIdAsync(notification.NotificationId, Arg.Any<CancellationToken>())
|
|
.Returns(notification);
|
|
var actor = CreateActor();
|
|
|
|
actor.Tell(new DiscardNotificationRequest(
|
|
CorrelationId: "test-corr", NotificationId: notification.NotificationId, RequestedBy: "jdoe"));
|
|
|
|
ExpectMsg<DiscardNotificationResponse>(r => r.Success);
|
|
AwaitAssert(() =>
|
|
{
|
|
var discarded = EventsByStatus(AuditStatus.Discarded);
|
|
Assert.Single(discarded);
|
|
Assert.Equal("jdoe", discarded[0].Actor);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void Retry_AuditFailure_DoesNotFail_TheRetry()
|
|
{
|
|
// Audit is best-effort: a throwing writer must NOT abort the retry — the
|
|
// row is still flipped back to Pending and the response reports Success.
|
|
var notification = MakeParked();
|
|
_outboxRepository.GetByIdAsync(notification.NotificationId, Arg.Any<CancellationToken>())
|
|
.Returns(notification);
|
|
_auditWriter.OnWrite = _ => throw new InvalidOperationException("audit dead");
|
|
var actor = CreateActor();
|
|
|
|
actor.Tell(new RetryNotificationRequest(
|
|
CorrelationId: "test-corr", NotificationId: notification.NotificationId, RequestedBy: "jdoe"));
|
|
|
|
ExpectMsg<RetryNotificationResponse>(r => r.Success);
|
|
AwaitAssert(() =>
|
|
{
|
|
_outboxRepository.Received(1).TryUpdateFromParkedAsync(
|
|
Arg.Is<Notification>(n => n.Status == NotificationStatus.Pending),
|
|
Arg.Any<CancellationToken>());
|
|
});
|
|
}
|
|
}
|