The sibling mxaccessgw repo (clients/dotnet/) restored a proper client library + contracts under the new ZB.MOM.WW.MxGateway namespace, so the binary-vendoring stopgap from PR Driver.Galaxy-016 can unwind via plan #1 of libs/README.md. - csproj: replace <Reference HintPath="libs\MxGateway.*.dll"> with a ProjectReference into ..\..\..\..\mxaccessgw\clients\dotnet ZB.MOM.WW.MxGateway.Client\. The five backfill PackageReference shims (Google.Protobuf, Grpc.Core.Api, Grpc.Net.Client, Polly.Core, Microsoft.Extensions.Logging.Abstractions) are now transitive again. - Source: 'using MxGateway.X' -> 'using ZB.MOM.WW.MxGateway.X' across 19 driver files + 14 test files. No fully-qualified MxGateway.* usages in code, so no behavioural changes — purely a using-prefix flip. - libs/: deleted MxGateway.Client.dll, MxGateway.Contracts.dll, README.md (orphan after the unwind). Verified: dotnet build clean (Release), all 245 Driver.Galaxy unit tests pass, OtOpcUa service running with the new client DLL loaded (opc.tcp://localhost:4840/OtOpcUa, no FileNotFound/TypeLoad/ MissingMethod in startup logs). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
228 lines
9.5 KiB
C#
228 lines
9.5 KiB
C#
using ZB.MOM.WW.MxGateway.Contracts.Proto;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Health;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Runtime;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests.Health;
|
|
|
|
/// <summary>
|
|
/// Tests for <see cref="PerPlatformProbeWatcher"/> — the per-platform probe state
|
|
/// machine. Uses a fake <see cref="IGalaxySubscriber"/> to control SubscribeBulk
|
|
/// results and assert the watcher subscribes the right addresses + decodes ScanState
|
|
/// values correctly.
|
|
/// </summary>
|
|
public sealed class PerPlatformProbeWatcherTests
|
|
{
|
|
private sealed class FakeSubscriber : IGalaxySubscriber
|
|
{
|
|
public List<List<string>> Subscribes { get; } = [];
|
|
public List<int> SubscribeIntervalsMs { get; } = [];
|
|
public List<List<int>> Unsubscribes { get; } = [];
|
|
private int _nextHandle = 1;
|
|
public Dictionary<string, int> HandleByAddress { get; } = new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
public Task<IReadOnlyList<SubscribeResult>> SubscribeBulkAsync(
|
|
IReadOnlyList<string> fullReferences, int bufferedUpdateIntervalMs, CancellationToken cancellationToken)
|
|
{
|
|
Subscribes.Add([.. fullReferences]);
|
|
SubscribeIntervalsMs.Add(bufferedUpdateIntervalMs);
|
|
var results = new List<SubscribeResult>(fullReferences.Count);
|
|
foreach (var addr in fullReferences)
|
|
{
|
|
var handle = Interlocked.Increment(ref _nextHandle);
|
|
HandleByAddress[addr] = handle;
|
|
results.Add(new SubscribeResult
|
|
{
|
|
TagAddress = addr,
|
|
ItemHandle = handle,
|
|
WasSuccessful = true,
|
|
});
|
|
}
|
|
return Task.FromResult<IReadOnlyList<SubscribeResult>>(results);
|
|
}
|
|
|
|
public Task UnsubscribeBulkAsync(IReadOnlyList<int> itemHandles, CancellationToken cancellationToken)
|
|
{
|
|
Unsubscribes.Add([.. itemHandles]);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public IAsyncEnumerable<MxEvent> StreamEventsAsync(CancellationToken cancellationToken)
|
|
=> Empty();
|
|
|
|
private static async IAsyncEnumerable<MxEvent> Empty()
|
|
{
|
|
await Task.CompletedTask;
|
|
yield break;
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SyncPlatformsAsync_SubscribesScanStateAddressForEachPlatform()
|
|
{
|
|
var subscriber = new FakeSubscriber();
|
|
var aggregator = new HostStatusAggregator();
|
|
using var watcher = new PerPlatformProbeWatcher(subscriber, aggregator);
|
|
|
|
await watcher.SyncPlatformsAsync(["PlatformA", "PlatformB"], CancellationToken.None);
|
|
|
|
subscriber.Subscribes.Count.ShouldBe(1);
|
|
subscriber.Subscribes[0].ShouldBe(new[] { "PlatformA.ScanState", "PlatformB.ScanState" });
|
|
watcher.WatchedPlatforms.OrderBy(x => x).ShouldBe(new[] { "PlatformA", "PlatformB" });
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SyncPlatformsAsync_DefaultBufferedIntervalIsZero_GwCadence()
|
|
{
|
|
// PR 6.3 — without an override, the watcher passes 0 (gw default cadence) so
|
|
// existing deployments don't see a behavior change.
|
|
var subscriber = new FakeSubscriber();
|
|
using var watcher = new PerPlatformProbeWatcher(subscriber, new HostStatusAggregator());
|
|
await watcher.SyncPlatformsAsync(["PlatformA"], CancellationToken.None);
|
|
subscriber.SubscribeIntervalsMs.ShouldHaveSingleItem().ShouldBe(0);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SyncPlatformsAsync_ConfiguredBufferedInterval_IsForwardedToGw()
|
|
{
|
|
// PR 6.3 — when a deployment dials down MxAccess.PublishingIntervalMs for
|
|
// tighter health visibility, the probe watcher must forward that interval
|
|
// through SubscribeBulk so the gw publishes ScanState changes at the
|
|
// configured cadence.
|
|
var subscriber = new FakeSubscriber();
|
|
using var watcher = new PerPlatformProbeWatcher(
|
|
subscriber, new HostStatusAggregator(),
|
|
bufferedUpdateIntervalMs: 250);
|
|
await watcher.SyncPlatformsAsync(["PlatformA"], CancellationToken.None);
|
|
subscriber.SubscribeIntervalsMs.ShouldHaveSingleItem().ShouldBe(250);
|
|
}
|
|
|
|
[Fact]
|
|
public void Constructor_RejectsNegativeBufferedInterval()
|
|
{
|
|
var subscriber = new FakeSubscriber();
|
|
Should.Throw<ArgumentOutOfRangeException>(() =>
|
|
new PerPlatformProbeWatcher(subscriber, new HostStatusAggregator(), bufferedUpdateIntervalMs: -1));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SyncPlatformsAsync_SameSetTwice_DoesNotResubscribe()
|
|
{
|
|
var subscriber = new FakeSubscriber();
|
|
var aggregator = new HostStatusAggregator();
|
|
using var watcher = new PerPlatformProbeWatcher(subscriber, aggregator);
|
|
|
|
await watcher.SyncPlatformsAsync(["PlatformA"], CancellationToken.None);
|
|
await watcher.SyncPlatformsAsync(["PlatformA"], CancellationToken.None);
|
|
|
|
subscriber.Subscribes.Count.ShouldBe(1);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SyncPlatformsAsync_RemovedPlatforms_AreUnsubscribed_AndDroppedFromAggregator()
|
|
{
|
|
var subscriber = new FakeSubscriber();
|
|
var aggregator = new HostStatusAggregator();
|
|
using var watcher = new PerPlatformProbeWatcher(subscriber, aggregator);
|
|
|
|
await watcher.SyncPlatformsAsync(["A", "B"], CancellationToken.None);
|
|
var bHandle = subscriber.HandleByAddress["B.ScanState"];
|
|
|
|
// Push a value so B is in the aggregator before we remove it.
|
|
watcher.OnProbeValueChanged("B.ScanState", true, qualityByte: 192);
|
|
aggregator.Snapshot().Any(s => s.HostName == "B").ShouldBeTrue();
|
|
|
|
await watcher.SyncPlatformsAsync(["A"], CancellationToken.None);
|
|
|
|
subscriber.Unsubscribes.Count.ShouldBe(1);
|
|
subscriber.Unsubscribes[0].ShouldBe(new[] { bHandle });
|
|
watcher.WatchedPlatforms.ShouldBe(new[] { "A" });
|
|
aggregator.Snapshot().Any(s => s.HostName == "B").ShouldBeFalse();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(true, (byte)192, HostState.Running)]
|
|
[InlineData(false, (byte)192, HostState.Stopped)]
|
|
[InlineData(1, (byte)192, HostState.Running)]
|
|
[InlineData(0, (byte)192, HostState.Stopped)]
|
|
[InlineData("Running", (byte)192, HostState.Running)]
|
|
[InlineData("Stopped", (byte)192, HostState.Stopped)]
|
|
[InlineData("running", (byte)192, HostState.Running)]
|
|
[InlineData(2, (byte)192, HostState.Faulted)] // unknown int
|
|
[InlineData("Whatever", (byte)192, HostState.Faulted)] // unknown string
|
|
[InlineData(true, (byte)64, HostState.Unknown)] // bad quality wins
|
|
[InlineData(true, (byte)0, HostState.Unknown)]
|
|
public void DecodeState_TablePins(object? value, byte qualityByte, HostState expected)
|
|
{
|
|
PerPlatformProbeWatcher.DecodeState(value, qualityByte).ShouldBe(expected);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task OnProbeValueChanged_Running_RoutesToAggregator()
|
|
{
|
|
var subscriber = new FakeSubscriber();
|
|
var aggregator = new HostStatusAggregator();
|
|
using var watcher = new PerPlatformProbeWatcher(subscriber, aggregator);
|
|
|
|
await watcher.SyncPlatformsAsync(["PlatformA"], CancellationToken.None);
|
|
watcher.OnProbeValueChanged("PlatformA.ScanState", true, qualityByte: 192);
|
|
|
|
var snap = aggregator.Snapshot().Single(s => s.HostName == "PlatformA");
|
|
snap.State.ShouldBe(HostState.Running);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task OnProbeValueChanged_BadQuality_RoutesUnknown()
|
|
{
|
|
var subscriber = new FakeSubscriber();
|
|
var aggregator = new HostStatusAggregator();
|
|
using var watcher = new PerPlatformProbeWatcher(subscriber, aggregator);
|
|
|
|
await watcher.SyncPlatformsAsync(["PlatformA"], CancellationToken.None);
|
|
watcher.OnProbeValueChanged("PlatformA.ScanState", true, qualityByte: 0);
|
|
|
|
aggregator.Snapshot().Single(s => s.HostName == "PlatformA").State.ShouldBe(HostState.Unknown);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task OnProbeValueChanged_ForeignReference_IsSilentlyDropped()
|
|
{
|
|
var subscriber = new FakeSubscriber();
|
|
var aggregator = new HostStatusAggregator();
|
|
using var watcher = new PerPlatformProbeWatcher(subscriber, aggregator);
|
|
|
|
await watcher.SyncPlatformsAsync(["PlatformA"], CancellationToken.None);
|
|
|
|
// Reference doesn't end with .ScanState — silently dropped.
|
|
watcher.OnProbeValueChanged("PlatformA.SomethingElse", true, qualityByte: 192);
|
|
aggregator.Snapshot().Any(s => s.HostName == "PlatformA").ShouldBeFalse();
|
|
|
|
// Unknown platform — silently dropped.
|
|
watcher.OnProbeValueChanged("Stranger.ScanState", true, qualityByte: 192);
|
|
aggregator.Snapshot().Any(s => s.HostName == "Stranger").ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Dispose_UnsubscribesAllTrackedPlatforms()
|
|
{
|
|
var subscriber = new FakeSubscriber();
|
|
var aggregator = new HostStatusAggregator();
|
|
var watcher = new PerPlatformProbeWatcher(subscriber, aggregator);
|
|
|
|
await watcher.SyncPlatformsAsync(["A", "B", "C"], CancellationToken.None);
|
|
var expectedHandles = new[]
|
|
{
|
|
subscriber.HandleByAddress["A.ScanState"],
|
|
subscriber.HandleByAddress["B.ScanState"],
|
|
subscriber.HandleByAddress["C.ScanState"],
|
|
};
|
|
|
|
watcher.Dispose();
|
|
|
|
subscriber.Unsubscribes.Count.ShouldBe(1);
|
|
subscriber.Unsubscribes[0].OrderBy(x => x).ShouldBe(expectedHandles.OrderBy(x => x));
|
|
}
|
|
}
|