Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataSdkItemDiagnosticsTests.cs
T

97 lines
5.4 KiB
C#

using System.Collections.Concurrent;
using System.Reflection;
using System.Text.Json;
using GrpcDataNodeProbe;
using Opc.Ua;
using Opc.Ua.Client;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
using ZB.MOM.WW.OtOpcUa.OpcUaServer;
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
public sealed partial class GrpcDataConfiguredDiscoveryWireTests
{
[Fact(Timeout = 90000), Trait("Phase", "P11Wire")]
public async Task Fixture_sdk_item_diagnostics_report_live_suppressed_and_retired_states()
{
var ct = TestContext.Current.CancellationToken;
LocalDataFixture? peer = null;
var subscriptions = new FakeSubscriptions(() => peer!.Serving);
using var origin = new LocalDataFixture(servingGeneration: 1, targetSubscriptions: (_, _) => subscriptions);
peer = origin;
subscriptions.SampleClock = origin.Clock;
LocalCatalogCoordinator? coordinator = null;
await WithComposedServer(origin, origin.Raw, async (session, sink, _, _, _, _, anchor) =>
{
var client = (SessionClient)session;
await BrowseAll(client, anchor, 1);
var sub = (await client.CreateSubscriptionAsync(new RequestHeader(), 100, 300, 1, 0, true, 0, ct)).SubscriptionId;
await AddComposedItem(client, sub, anchor, 1, 100, ct);
await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
(await PublishComposedValue(client, 1, ct)).StatusCode.ShouldBe(StatusCodes.Good);
const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
var manager = (OtOpcUaNodeManager)typeof(SdkAddressSpaceSink).GetField("_nodeManager", flags)!.GetValue(sink)!;
var initial = SdkItemDiagnostics.Capture(manager, anchor);
initial.Available.ShouldBeTrue();
initial.Truncated.ShouldBeFalse();
initial.ProcessTimestamp.ShouldBeGreaterThan(0);
initial.ProcessFrequency.ShouldBeGreaterThan(0);
var live = initial.Items.ShouldHaveSingleItem();
live.Retired.ShouldBe(false); live.Suppressed.ShouldBe(false); live.HealthTracked.ShouldBe(true);
live.DemandPresent.ShouldBe(true); live.DemandValid.ShouldBe(true);
live.DefinitionLifetimeActive.ShouldBe(true); live.DefinitionAuthorityCurrent.ShouldBe(true);
live.SourceBindingValid.ShouldBe(true); live.LatestProofValid.ShouldBe(true);
_ = Prepare(coordinator!);
manager.RevalidateMonitoredItems();
var suppressed = SdkItemDiagnostics.Capture(manager, anchor).Items.ShouldHaveSingleItem();
suppressed.Suppressed.ShouldBe(true); suppressed.HealthTracked.ShouldBe(true);
suppressed.DemandValid.ShouldBe(false);
var items = manager.GetType().GetField("_liveItems", flags)!.GetValue(manager)!;
var keys = (System.Collections.IEnumerable)items.GetType().GetProperty("Keys")!.GetValue(items)!;
var item = keys.Cast<object>().OfType<Opc.Ua.Server.MonitoredItem>().Single(x => x.ClientHandle == 1);
item.GetType().GetMethod("Retire", flags)!.Invoke(item, null);
var retired = SdkItemDiagnostics.Capture(manager, anchor).Items.ShouldHaveSingleItem();
retired.Retired.ShouldBe(true); retired.HealthTracked.ShouldBe(false); retired.DemandPresent.ShouldBe(false);
}, onCoordinator: value => coordinator = value);
}
[Fact, Trait("Phase", "P11Wire")]
public void Fixture_sdk_item_diagnostics_bound_results_and_do_not_serialize_item_objects()
{
var target = new NodeId("target", 2);
var fake = new DiagnosticManager();
for (var i = 0; i < 20; i++) fake.Add(target);
var captured = SdkItemDiagnostics.Capture(fake, target);
captured.Available.ShouldBeTrue(); captured.Truncated.ShouldBeTrue(); captured.Items.Count.ShouldBe(16);
var json = JsonSerializer.Serialize(captured);
json.ShouldNotContain("target");
json.ShouldNotContain("private-payload");
SdkItemDiagnostics.Capture(fake, new NodeId("other", 2)).Items.ShouldBeEmpty();
SdkItemDiagnostics.Capture(null, target).Available.ShouldBeFalse();
}
private sealed class DiagnosticManager
{
private readonly ConcurrentDictionary<DiagnosticItem, DiagnosticTracked> _liveItems = new();
private readonly object _projectedHealthGate = new();
private readonly Dictionary<DiagnosticItem, object> _projectedHealthIndex = new();
public void Add(NodeId node) { var item = new DiagnosticItem(node); item.Keep(); _liveItems[item] = new(); _projectedHealthIndex[item] = new(); }
}
private sealed class DiagnosticTracked { public bool Suppressed => false; }
private sealed class DiagnosticItem(NodeId node)
{
private NodeId ProjectedNodeId => node;
private bool HealthRetired => false;
private readonly object _bindingSync = new();
private readonly object? _demand = null, _binding = null, _latestEvidence = null, _definition = null;
private readonly List<object> _candidates = [];
public int MonitoringMode => 1;
public string PrivatePayload => "private-payload";
public void Keep() { GC.KeepAlive(_demand); GC.KeepAlive(_binding); GC.KeepAlive(_latestEvidence); GC.KeepAlive(_definition); }
}
}