Files
lmxopcua/tests/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests/Health/HostConnectivityForwarderTests.cs
Joseph Doherty dae520b9c0 PR 4.7 — Host-connectivity probes (IHostConnectivityProbe scaffold)
HostStatusAggregator merges transport + per-platform host entries with
change-event diffing (re-asserting same state is a no-op so a stable
ScanState=Running burst doesn't fan out duplicates). PerPlatformProbeWatcher
ports the legacy GalaxyRuntimeProbeManager state machine onto the gw
subscription path: SubscribeBulk for `<tag>.ScanState`, idempotent
SyncPlatformsAsync (subscribe new, unsubscribe dropped), and a
DecodeState helper pinning bool/int/string ScanState values + bad-quality
fallback. HostConnectivityForwarder is the skeleton for the gw-6
StreamSessionHealth signal — until that mxaccessgw RPC ships, PR 4.5's
ReconnectSupervisor pushes transport state by calling SetTransport on
session connect/disconnect.

GalaxyDriver wiring (implement IHostConnectivityProbe, route OnDataChange
to PerPlatformProbeWatcher, expose GetHostStatuses() / OnHostStatusChanged,
push transport from supervisor) is deferred to PR 4.W to avoid conflict
with the rest of the Phase 4 deferred wiring (4.5 supervisor + 4.6
DeployWatcher).

Tests: 19 new
- HostStatusAggregatorTests (9): empty snapshot, new-host change with
  Unknown predecessor, same-state silence, transition diff, snapshot
  reflects every host, case-insensitive host names, Remove returns true
  for tracked, Remove false for unknown, concurrent updates don't corrupt.
- HostConnectivityForwarderTests (5): SetTransport routes under client
  name, transitions fire change, repeated same-state silent, empty client
  name throws, post-dispose throws.
- PerPlatformProbeWatcherTests (5 + theory pinning DecodeState's full
  truth table): subscribe N platforms, idempotent re-sync, removed
  platforms unsubscribed + dropped from aggregator, OnProbeValueChanged
  routing for Running/Stopped/bad-quality/foreign-ref, Dispose
  unsubscribes everything.

NOTE: build is currently broken because mxaccessgw/clients/dotnet/ has
been removed from C:\Users\dohertj2\Desktop\mxaccessgw — this PR's source
is internally consistent and isolated from the missing dependency, but the
existing Driver.Galaxy code (PRs 4.1–4.6) can't compile until the .NET
client is restored. Once it is, expect 116 + 19 = 135 tests in the
Driver.Galaxy.Tests project.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-29 15:47:13 -04:00

84 lines
2.9 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Health;
namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests.Health;
/// <summary>
/// Tests for <see cref="HostConnectivityForwarder"/>'s push path. The forwarder is a
/// thin shim over <see cref="HostStatusAggregator"/>; the only invariants worth pinning
/// are that SetTransport routes correctly under the configured client name and that
/// repeated identical pushes don't produce duplicate change events (the aggregator's
/// dedup carries that — this test asserts the forwarder doesn't re-introduce them).
/// </summary>
public sealed class HostConnectivityForwarderTests
{
[Fact]
public void SetTransport_Running_PushesUnderClientName()
{
var agg = new HostStatusAggregator();
var captured = new List<HostStatusChangedEventArgs>();
agg.OnHostStatusChanged += (_, e) => captured.Add(e);
var fwd = new HostConnectivityForwarder("OtOpcUa-A", agg);
fwd.SetTransport(HostState.Running);
captured.Count.ShouldBe(1);
captured[0].HostName.ShouldBe("OtOpcUa-A");
captured[0].NewState.ShouldBe(HostState.Running);
agg.Snapshot()[0].HostName.ShouldBe("OtOpcUa-A");
}
[Fact]
public void SetTransport_StateTransition_FiresChange()
{
var agg = new HostStatusAggregator();
var fwd = new HostConnectivityForwarder("OtOpcUa-A", agg);
fwd.SetTransport(HostState.Running);
var captured = new List<HostStatusChangedEventArgs>();
agg.OnHostStatusChanged += (_, e) => captured.Add(e);
fwd.SetTransport(HostState.Stopped);
captured.Count.ShouldBe(1);
captured[0].OldState.ShouldBe(HostState.Running);
captured[0].NewState.ShouldBe(HostState.Stopped);
}
[Fact]
public void SetTransport_RepeatedSameState_DoesNotFire()
{
var agg = new HostStatusAggregator();
var fwd = new HostConnectivityForwarder("OtOpcUa-A", agg);
fwd.SetTransport(HostState.Running);
var captured = new List<HostStatusChangedEventArgs>();
agg.OnHostStatusChanged += (_, e) => captured.Add(e);
fwd.SetTransport(HostState.Running);
fwd.SetTransport(HostState.Running);
fwd.SetTransport(HostState.Running);
captured.ShouldBeEmpty();
}
[Fact]
public void Constructor_RejectsEmptyClientName()
{
var agg = new HostStatusAggregator();
Should.Throw<ArgumentException>(() => new HostConnectivityForwarder("", agg));
Should.Throw<ArgumentException>(() => new HostConnectivityForwarder(" ", agg));
}
[Fact]
public void SetTransport_AfterDispose_Throws()
{
var agg = new HostStatusAggregator();
var fwd = new HostConnectivityForwarder("OtOpcUa-A", agg);
fwd.Dispose();
Should.Throw<ObjectDisposedException>(() => fwd.SetTransport(HostState.Running));
}
}