Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.StoreAndForward.Tests/ParkedOperationRelayTests.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

187 lines
8.0 KiB
C#

using Akka.Actor;
using Akka.TestKit.Xunit2;
using Microsoft.Extensions.Logging.Abstractions;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.RemoteQuery;
using ZB.MOM.WW.ScadaBridge.Commons.Types;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.TestSupport;
namespace ZB.MOM.WW.ScadaBridge.StoreAndForward.Tests;
/// <summary>
/// Retry/Discard relay: tests the site-side execution of a
/// central→site <see cref="RetryParkedOperation"/> / <see cref="DiscardParkedOperation"/>
/// relay command on the <see cref="ParkedMessageHandlerActor"/>. The cached
/// call's S&amp;F buffer message id is the <see cref="TrackedOperationId"/>, so
/// the handler resolves the parked row directly from the tracked id and reuses
/// the existing parked-message Retry/Discard primitive. A non-parked operation
/// must be a safe no-op (<c>Applied=false</c>), never a corruption.
/// </summary>
public class ParkedOperationRelayTests : TestKit, IAsyncLifetime, IDisposable
{
private readonly TestLocalDb _localDb;
private readonly StoreAndForwardStorage _storage;
private readonly StoreAndForwardService _service;
/// <summary>Initializes a new instance of the <c>ParkedOperationRelayTests</c> class.</summary>
public ParkedOperationRelayTests()
{
_localDb = TestLocalDb.CreateTemp("RelayTests");
_storage = new StoreAndForwardStorage(_localDb.Db, NullLogger<StoreAndForwardStorage>.Instance);
var options = new StoreAndForwardOptions
{
DefaultRetryInterval = TimeSpan.Zero,
DefaultMaxRetries = 1,
RetryTimerInterval = TimeSpan.FromMinutes(10),
};
_service = new StoreAndForwardService(
_storage, options, NullLogger<StoreAndForwardService>.Instance);
}
/// <summary>Initializes the storage before the tests run.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public async Task InitializeAsync() => await _storage.InitializeAsync();
/// <summary>Does nothing; storage is released in <c>Dispose</c>.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
public Task DisposeAsync() => Task.CompletedTask;
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
if (disposing)
{
var path = _localDb.Path;
_localDb.Dispose();
TestLocalDb.DeleteFiles(path);
}
base.Dispose(disposing);
}
/// <summary>
/// Enqueues a cached-call message whose S&amp;F id is the supplied
/// <see cref="TrackedOperationId"/> and parks it via the retry sweep.
/// </summary>
private async Task ParkCachedCallAsync(TrackedOperationId id)
{
_service.RegisterDeliveryHandler(
StoreAndForwardCategory.ExternalSystem, _ => throw new HttpRequestException("always fails"));
await _service.EnqueueAsync(
StoreAndForwardCategory.ExternalSystem, "ERP.GetOrder", """{}""",
maxRetries: 1, messageId: id.ToString());
await _service.RetryPendingMessagesAsync();
}
/// <summary>Verifies that retrying a parked cached call resets it to pending and reports the change as applied.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task RetryParkedOperation_ParkedCachedCall_ResetsToPendingAndApplied()
{
var id = TrackedOperationId.New();
await ParkCachedCallAsync(id);
var actor = Sys.ActorOf(Props.Create(() => new ParkedMessageHandlerActor(_service, "site-1")));
actor.Tell(new RetryParkedOperation("corr-1", id));
var ack = ExpectMsg<ParkedOperationActionAck>();
Assert.True(ack.Applied);
Assert.Equal("corr-1", ack.CorrelationId);
Assert.Null(ack.ErrorMessage);
// The parked row was reset back to Pending so the retry sweep picks it up.
var msg = await _storage.GetMessageByIdAsync(id.ToString());
Assert.NotNull(msg);
Assert.Equal(StoreAndForwardMessageStatus.Pending, msg!.Status);
}
/// <summary>Verifies that discarding a parked cached call removes its row and reports the change as applied.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task DiscardParkedOperation_ParkedCachedCall_RemovesRowAndApplied()
{
var id = TrackedOperationId.New();
await ParkCachedCallAsync(id);
var actor = Sys.ActorOf(Props.Create(() => new ParkedMessageHandlerActor(_service, "site-1")));
actor.Tell(new DiscardParkedOperation("corr-2", id));
var ack = ExpectMsg<ParkedOperationActionAck>();
Assert.True(ack.Applied);
Assert.Equal("corr-2", ack.CorrelationId);
var msg = await _storage.GetMessageByIdAsync(id.ToString());
Assert.Null(msg);
}
/// <summary>Verifies that retrying an unknown operation is a safe no-op.</summary>
[Fact]
public void RetryParkedOperation_UnknownOperation_IsSafeNoOp()
{
var actor = Sys.ActorOf(Props.Create(() => new ParkedMessageHandlerActor(_service, "site-1")));
actor.Tell(new RetryParkedOperation("corr-3", TrackedOperationId.New()));
var ack = ExpectMsg<ParkedOperationActionAck>();
// No parked row matched — definitive "nothing to do", not an error.
Assert.False(ack.Applied);
Assert.Equal("corr-3", ack.CorrelationId);
Assert.Null(ack.ErrorMessage);
}
/// <summary>Verifies that retrying a non-parked operation is a safe no-op that does not corrupt the row.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task RetryParkedOperation_NonParkedOperation_IsSafeNoOpAndDoesNotCorrupt()
{
// Enqueue a cached call but DO NOT park it — it stays Pending.
var id = TrackedOperationId.New();
_service.RegisterDeliveryHandler(
StoreAndForwardCategory.ExternalSystem, _ => throw new HttpRequestException("fails"));
await _service.EnqueueAsync(
StoreAndForwardCategory.ExternalSystem, "ERP.GetOrder", """{}""",
maxRetries: 5, messageId: id.ToString());
var before = await _storage.GetMessageByIdAsync(id.ToString());
Assert.Equal(StoreAndForwardMessageStatus.Pending, before!.Status);
var actor = Sys.ActorOf(Props.Create(() => new ParkedMessageHandlerActor(_service, "site-1")));
actor.Tell(new RetryParkedOperation("corr-4", id));
var ack = ExpectMsg<ParkedOperationActionAck>();
// The row is Pending, not Parked — Retry must be a no-op, not a mutation.
Assert.False(ack.Applied);
var after = await _storage.GetMessageByIdAsync(id.ToString());
Assert.NotNull(after);
Assert.Equal(StoreAndForwardMessageStatus.Pending, after!.Status);
// retry_count untouched — a Parked-only Retry must not reset a live row.
Assert.Equal(before.RetryCount, after.RetryCount);
}
/// <summary>Verifies that discarding a non-parked operation is a safe no-op.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task DiscardParkedOperation_NonParkedOperation_IsSafeNoOp()
{
var id = TrackedOperationId.New();
_service.RegisterDeliveryHandler(
StoreAndForwardCategory.ExternalSystem, _ => throw new HttpRequestException("fails"));
await _service.EnqueueAsync(
StoreAndForwardCategory.ExternalSystem, "ERP.GetOrder", """{}""",
maxRetries: 5, messageId: id.ToString());
var actor = Sys.ActorOf(Props.Create(() => new ParkedMessageHandlerActor(_service, "site-1")));
actor.Tell(new DiscardParkedOperation("corr-5", id));
var ack = ExpectMsg<ParkedOperationActionAck>();
Assert.False(ack.Applied);
// The Pending row must NOT have been deleted by a Parked-only Discard.
var after = await _storage.GetMessageByIdAsync(id.ToString());
Assert.NotNull(after);
}
}