Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests/Actors/DataConnectionProbeSeedTests.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

107 lines
5.3 KiB
C#

using Akka.Actor;
using Akka.TestKit.Xunit2;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Protocol;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.DataConnection;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Actors;
using ZB.MOM.WW.ScadaBridge.HealthMonitoring;
namespace ZB.MOM.WW.ScadaBridge.DataConnectionLayer.Tests.Actors;
/// <summary>
/// The tag-resolution PROBE path did not seed the tags it
/// resolved.
///
/// <para>
/// <b>The gap this pins.</b> Both other subscribe paths read each newly advised tag's current
/// value and deliver it (<c>HandleSubscribe</c> carries the seeds back on <c>SubscribeCompleted</c>;
/// <c>ReSubscribeAll</c> re-seeds after a reconnect), because a fresh advise only produces a value
/// on the source's next CHANGE. <c>HandleRetryTagResolution</c> did not. OPC UA masks that with an
/// initial data-change notification on subscription creation, but an MxGateway advise can
/// legitimately return <c>VT_EMPTY</c> — so a STATIC tag that resolved LATE (device still booting,
/// item created after deployment) had no value path at all and its attribute stayed Uncertain for
/// the life of the connection.
/// </para>
/// </summary>
[Collection("DataConnectionManagerActor")]
public class DataConnectionProbeSeedTests : TestKit
{
private const string Tag = "plant/static/setpoint";
private readonly ISiteHealthCollector _health = Substitute.For<ISiteHealthCollector>();
private readonly IDataConnectionFactory _factory = Substitute.For<IDataConnectionFactory>();
private readonly DataConnectionOptions _options = new()
{
ReconnectInterval = TimeSpan.FromMilliseconds(100),
TagResolutionRetryInterval = TimeSpan.FromMilliseconds(150),
WriteTimeout = TimeSpan.FromSeconds(5),
SeedReadMaxAttempts = 1,
SeedReadRetryDelay = TimeSpan.FromMilliseconds(10)
};
private IActorRef CreateConnectionActor(string name, IDataConnection adapter) =>
Sys.ActorOf(Props.Create(() =>
new DataConnectionActor(name, adapter, _options, _health, _factory, "OpcUa")), name);
private bool HasEnteredConnectedState(string connectionName) =>
_health.ReceivedCalls().Any(c =>
c.GetMethodInfo().Name == nameof(ISiteHealthCollector.UpdateConnectionHealth) &&
c.GetArguments() is [string name, ConnectionHealth.Connected, ..] &&
name == connectionName);
/// <summary>
/// A tag that fails to resolve on the initial subscribe, resolves on the first probe round,
/// and then never changes again must still reach its subscriber with a Good value.
///
/// <para>
/// The adapter here models exactly the MxGateway shape the finding calls out: the advise
/// succeeds on the retry but the subscription callback is NEVER invoked (a <c>VT_EMPTY</c>
/// advise on a static item), so the seed read is the tag's only possible value source.
/// </para>
///
/// <para><b>Fails before the fix:</b> the probe resolved the tag and stopped there — the
/// subscriber received only the Bad-quality resolution-failure push from the initial
/// subscribe, and this fish for a Good value timed out.</para>
/// </summary>
[Fact]
public void ProbeResolvedTag_IsSeeded_SoAStaticTagLeavesUncertain()
{
const string name = "probe-seed";
var subscribeCalls = 0;
var adapter = Substitute.For<IDataConnection>();
adapter.ConnectAsync(Arg.Any<IDictionary<string, string>>(), Arg.Any<CancellationToken>())
.Returns(Task.CompletedTask);
adapter.Status.Returns(ConnectionHealth.Connected);
adapter.SubscribeAsync(Tag, Arg.Any<SubscriptionCallback>(), Arg.Any<CancellationToken>())
.Returns(_ => Interlocked.Increment(ref subscribeCalls) == 1
// Initial subscribe: the device is still booting — a genuine (non
// connection-level) resolution failure, so the tag lands in _unresolvedTags.
? Task.FromException<string>(new KeyNotFoundException("node not found yet"))
// Probe round: the advise succeeds. The callback is deliberately never invoked.
: Task.FromResult("sub-1"));
adapter.ReadAsync(Tag, Arg.Any<CancellationToken>())
.Returns(new ReadResult(true, new TagValue(42, QualityCode.Good, DateTimeOffset.UtcNow), null));
var actor = CreateConnectionActor(name, adapter);
AwaitCondition(() => HasEnteredConnectedState(name),
TimeSpan.FromSeconds(5), TimeSpan.FromMilliseconds(10));
var subscriber = CreateTestProbe();
subscriber.Send(actor, new SubscribeTagsRequest("c1", "inst1", name, [Tag], DateTimeOffset.UtcNow));
// The initial subscribe fails: Bad-quality push, then the ack.
subscriber.FishForMessage<SubscribeTagsResponse>(_ => true, TimeSpan.FromSeconds(5));
// The probe round resolves the tag; the seed read is the only thing that can now
// deliver a value, because the advise callback never fires for this static item.
var seeded = subscriber.FishForMessage<TagValueUpdate>(
m => m.TagPath == Tag && m.Quality == QualityCode.Good, TimeSpan.FromSeconds(10));
Assert.Equal(42, seeded.Value);
Assert.True(Volatile.Read(ref subscribeCalls) >= 2, "the probe round must have re-subscribed the tag");
}
}