test: validate grpc data connections with local docker and opcua cli
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
using System.Collections;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using Opc.Ua;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
||||
using ZB.MOM.WW.OtOpcUa.OpcUaServer;
|
||||
|
||||
namespace GrpcDataNodeProbe;
|
||||
|
||||
// Fixture-only inspection. All returned fields are bounded, fixed-shape state;
|
||||
// no item, proof, principal, identifier, payload or exception text escapes.
|
||||
internal static class SdkItemDiagnostics
|
||||
{
|
||||
private const BindingFlags Fields = BindingFlags.Instance | BindingFlags.NonPublic;
|
||||
private const BindingFlags Properties = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
private const int ScanLimit = 16384;
|
||||
private const int ItemLimit = 16;
|
||||
internal sealed record Item(bool? Retired, bool? Suppressed, bool? HealthTracked, bool? DemandPresent,
|
||||
bool? DemandValid, long? DefinitionGeneration, bool? DefinitionLifetimeActive, bool? DefinitionAuthorityCurrent,
|
||||
bool? SourceBindingValid, bool? LatestProofValid, int? CandidateCount, int? MonitoringMode);
|
||||
internal sealed record Snapshot(long ProcessTimestamp, long ProcessFrequency, bool Available, bool Truncated,
|
||||
int LiveScanned, int HealthScanned, IReadOnlyList<Item> Items);
|
||||
|
||||
internal static Snapshot Capture(object? manager, NodeId? target)
|
||||
{
|
||||
var timestamp = Stopwatch.GetTimestamp();
|
||||
if (manager is null || target is not { } actualTarget) return new(timestamp, Stopwatch.Frequency, false, false, 0, 0, []);
|
||||
try
|
||||
{
|
||||
var type = manager.GetType();
|
||||
var live = type.GetField("_liveItems", Fields)?.GetValue(manager) as IEnumerable;
|
||||
var health = type.GetField("_projectedHealthIndex", Fields)?.GetValue(manager) as IDictionary;
|
||||
var healthGate = type.GetField("_projectedHealthGate", Fields)?.GetValue(manager);
|
||||
if (live is null || health is null || healthGate is null)
|
||||
return new(timestamp, Stopwatch.Frequency, false, false, 0, 0, []);
|
||||
var found = new Dictionary<object, (bool? Suppressed, bool Health)>(System.Collections.Generic.ReferenceEqualityComparer.Instance);
|
||||
var liveScanned = 0; var healthScanned = 0; var truncated = false;
|
||||
foreach (var pair in live)
|
||||
{
|
||||
if (++liveScanned > ScanLimit) { truncated = true; break; }
|
||||
var item = pair?.GetType().GetProperty("Key")?.GetValue(pair);
|
||||
if (!Matches(item, actualTarget)) continue;
|
||||
if (found.Count == ItemLimit) { truncated = true; break; }
|
||||
var tracked = pair!.GetType().GetProperty("Value")?.GetValue(pair);
|
||||
found[item!] = (ReadBool(tracked, "Suppressed"), false);
|
||||
}
|
||||
// Copy only references under the index lock. Authority and proof checks
|
||||
// happen after releasing it, avoiding health-index/manager lock inversion.
|
||||
var healthItems = new List<object>();
|
||||
lock (healthGate)
|
||||
{
|
||||
foreach (var key in health.Keys)
|
||||
{
|
||||
if (++healthScanned > ScanLimit) { truncated = true; break; }
|
||||
if (key is not null) healthItems.Add(key);
|
||||
}
|
||||
}
|
||||
foreach (var item in healthItems)
|
||||
{
|
||||
if (!Matches(item, actualTarget)) continue;
|
||||
if (found.TryGetValue(item, out var current)) found[item] = (current.Suppressed, true);
|
||||
else if (found.Count < ItemLimit) found[item] = (null, true);
|
||||
else truncated = true;
|
||||
}
|
||||
var items = found.Select(pair => Describe(pair.Key, pair.Value.Suppressed,
|
||||
pair.Value.Health ? true : truncated ? null : false)).ToArray();
|
||||
return new(timestamp, Stopwatch.Frequency, true, truncated, Math.Min(liveScanned, ScanLimit),
|
||||
Math.Min(healthScanned, ScanLimit), items);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{ return new(timestamp, Stopwatch.Frequency, false, false, 0, 0, []); }
|
||||
}
|
||||
|
||||
private static bool Matches(object? item, NodeId target) =>
|
||||
item?.GetType().GetProperty("ProjectedNodeId", Properties)?.GetValue(item) is NodeId id && id == target;
|
||||
|
||||
private static Item Describe(object item, bool? suppressed, bool? healthTracked)
|
||||
{
|
||||
var type = item.GetType();
|
||||
object? demand, binding, evidence; ProjectedVariableDefinition? definition; int? candidateCount;
|
||||
var sync = type.GetField("_bindingSync", Fields)!.GetValue(item)!;
|
||||
lock (sync)
|
||||
{
|
||||
demand = type.GetField("_demand", Fields)?.GetValue(item);
|
||||
binding = type.GetField("_binding", Fields)?.GetValue(item);
|
||||
evidence = type.GetField("_latestEvidence", Fields)?.GetValue(item);
|
||||
definition = type.GetField("_definition", Fields)?.GetValue(item) as ProjectedVariableDefinition;
|
||||
candidateCount = (type.GetField("_candidates", Fields)?.GetValue(item) as ICollection)?.Count;
|
||||
}
|
||||
var proof = evidence?.GetType().GetProperty("Proof", Properties)?.GetValue(evidence);
|
||||
return new(ReadBool(item, "HealthRetired"), suppressed, healthTracked, demand is not null,
|
||||
Safe(() => (demand as IProjectedDemandLease)?.IsValid), definition?.ProjectionGeneration, Safe(() => definition?.Lifetime.IsActive),
|
||||
Safe(() => definition?.AuthorityIsCurrent()), ReadBool(binding, "IsValid"), ReadBool(proof, "IsValid"),
|
||||
candidateCount, SafeInt(() => type.GetProperty("MonitoringMode", Properties)?.GetValue(item) is { } mode
|
||||
? Convert.ToInt32(mode) : null));
|
||||
}
|
||||
private static bool? ReadBool(object? target, string property) =>
|
||||
Safe(() => target?.GetType().GetProperty(property, Properties)?.GetValue(target) as bool?);
|
||||
private static bool? Safe(Func<bool?> read)
|
||||
{ try { return read(); } catch (Exception ex) when (ex is not OutOfMemoryException) { return null; } }
|
||||
private static int? SafeInt(Func<int?> read)
|
||||
{ try { return read(); } catch (Exception ex) when (ex is not OutOfMemoryException) { return null; } }
|
||||
}
|
||||
Reference in New Issue
Block a user