fix(notifications): list sort index + CAS operator Retry/Discard (P5-3, P5-4)

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).
This commit is contained in:
Joseph Doherty
2026-08-19 16:20:18 -04:00
parent 11675285af
commit 1cea01d924
12 changed files with 2607 additions and 32 deletions
@@ -8,14 +8,16 @@ namespace ZB.MOM.WW.ScadaBridge.NotificationOutbox.Tests;
/// Factory for the outbox repository substitute used across these tests.
/// </summary>
/// <remarks>
/// <see cref="INotificationOutboxRepository.UpdateAsync"/> returns whether the
/// targeted UPDATE actually matched a row — <c>false</c> is the "this
/// notification no longer exists" signal the operator retry/discard handlers
/// turn into a not-found reply. NSubstitute's default for <c>Task&lt;bool&gt;</c>
/// is <c>false</c>, i.e. the VANISHED-row answer, so an unconfigured substitute
/// would silently put every test on the failure path. Tests that want a healthy
/// store take one from here; the vanished-row tests configure
/// <c>Returns(false)</c> for themselves.
/// <see cref="INotificationOutboxRepository.UpdateAsync"/> and
/// <see cref="INotificationOutboxRepository.TryUpdateFromParkedAsync"/> both
/// return whether the targeted UPDATE actually matched a row — <c>false</c> is
/// the "this notification no longer exists (or, for the CAS variant, is no
/// longer Parked)" signal the operator retry/discard handlers turn into a
/// not-found / already-progressed reply. NSubstitute's default for
/// <c>Task&lt;bool&gt;</c> is <c>false</c>, i.e. the failure answer, so an
/// unconfigured substitute would silently put every test on the failure path.
/// Tests that want a healthy store take one from here; the vanished-row and
/// lost-CAS tests configure <c>Returns(false)</c> for themselves.
/// </remarks>
internal static class OutboxRepositorySubstitute
{
@@ -26,6 +28,9 @@ internal static class OutboxRepositorySubstitute
repository
.UpdateAsync(Arg.Any<Notification>(), Arg.Any<CancellationToken>())
.Returns(true);
repository
.TryUpdateFromParkedAsync(Arg.Any<Notification>(), Arg.Any<CancellationToken>())
.Returns(true);
return repository;
}
}