fix(opcuaclient): review fixes - drop driver-side OperationLimits chunking (SDK batches), diagnostics as OTel counter, safe discovery selection, options validation at load (#273 #275 #276 #277 #278)
Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
This commit is contained in:
+96
-76
@@ -1,111 +1,131 @@
|
||||
using System.Diagnostics.Metrics;
|
||||
using Opc.Ua;
|
||||
using Opc.Ua.Client;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Driver.OpcUaClient.Tests;
|
||||
|
||||
/// <summary>#276 — counters are thread-safe and surface through <c>DriverHealth.Diagnostics</c>.</summary>
|
||||
/// <summary>
|
||||
/// #276 — session/publish events are published on the <c>otopcua.driver.opcuaclient.events</c> OTel counter.
|
||||
/// The listener filters on a UNIQUE <c>driver_instance</c> tag: the Meter is process-static, so a
|
||||
/// name-only filter would also collect parallel tests' measurements.
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class OpcUaClientDiagnosticsTests
|
||||
{
|
||||
[Fact]
|
||||
public void Fresh_snapshot_is_all_zero_with_the_full_key_set()
|
||||
private sealed class Capture : IDisposable
|
||||
{
|
||||
var snap = new OpcUaClientDiagnostics().Snapshot();
|
||||
private readonly MeterListener _listener = new();
|
||||
private readonly object _gate = new();
|
||||
private readonly Dictionary<string, long> _byEvent = new();
|
||||
|
||||
snap.Keys.ShouldBe(
|
||||
[
|
||||
"PublishResponseCount", "NotificationCount", "DataChangeCount", "BadStatusValueCount",
|
||||
"PublishErrorCount", "KeepAliveMissCount", "ReconnectAttemptCount", "ReconnectSuccessCount",
|
||||
"ReconnectExhaustedCount", "LastReconnectUtcTicks", "ReadCallCount", "ReadFailureCount",
|
||||
"WriteCallCount", "WriteFailureCount", "ChunkedRequestCount",
|
||||
], ignoreOrder: true);
|
||||
snap.Values.ShouldAllBe(v => v == 0);
|
||||
public Capture(string instance)
|
||||
{
|
||||
_listener.InstrumentPublished = (inst, l) =>
|
||||
{
|
||||
if (inst.Name == "otopcua.driver.opcuaclient.events") l.EnableMeasurementEvents(inst);
|
||||
};
|
||||
_listener.SetMeasurementEventCallback<long>((_, value, tags, _) =>
|
||||
{
|
||||
string? driver = null, evt = null;
|
||||
foreach (var t in tags)
|
||||
{
|
||||
if (t.Key == "driver_instance") driver = t.Value as string;
|
||||
else if (t.Key == "event") evt = t.Value as string;
|
||||
}
|
||||
if (driver != instance || evt is null) return;
|
||||
lock (_gate) _byEvent[evt] = _byEvent.GetValueOrDefault(evt) + value;
|
||||
});
|
||||
_listener.Start();
|
||||
}
|
||||
|
||||
public long this[string evt] { get { lock (_gate) return _byEvent.GetValueOrDefault(evt); } }
|
||||
public void Dispose() => _listener.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Each_recorder_moves_exactly_its_own_counter()
|
||||
public void Each_event_lands_on_its_own_tagged_series()
|
||||
{
|
||||
var d = new OpcUaClientDiagnostics();
|
||||
d.RecordPublishResponse();
|
||||
d.RecordNotifications(3);
|
||||
d.RecordDataChange(bad: false);
|
||||
d.RecordDataChange(bad: true);
|
||||
d.RecordPublishError();
|
||||
d.RecordKeepAliveMiss();
|
||||
d.RecordReconnectAttempt();
|
||||
d.RecordReconnectSucceeded();
|
||||
d.RecordReconnectExhausted();
|
||||
d.RecordRead(failed: true);
|
||||
d.RecordWrite(failed: false);
|
||||
d.RecordChunkedRequest();
|
||||
var id = $"diag-{Guid.NewGuid():N}";
|
||||
using var cap = new Capture(id);
|
||||
var d = new OpcUaClientDiagnostics(id);
|
||||
|
||||
var s = d.Snapshot();
|
||||
s["PublishResponseCount"].ShouldBe(1);
|
||||
s["NotificationCount"].ShouldBe(3);
|
||||
s["DataChangeCount"].ShouldBe(2);
|
||||
s["BadStatusValueCount"].ShouldBe(1);
|
||||
s["PublishErrorCount"].ShouldBe(1);
|
||||
s["KeepAliveMissCount"].ShouldBe(1);
|
||||
s["ReconnectAttemptCount"].ShouldBe(1);
|
||||
s["ReconnectSuccessCount"].ShouldBe(1);
|
||||
s["ReconnectExhaustedCount"].ShouldBe(1);
|
||||
s["LastReconnectUtcTicks"].ShouldBeGreaterThan(0);
|
||||
s["ReadCallCount"].ShouldBe(1);
|
||||
s["ReadFailureCount"].ShouldBe(1);
|
||||
s["WriteCallCount"].ShouldBe(1);
|
||||
s["WriteFailureCount"].ShouldBe(0);
|
||||
s["ChunkedRequestCount"].ShouldBe(1);
|
||||
d.Record(OpcUaClientDiagnostics.PublishResponse);
|
||||
d.Record(OpcUaClientDiagnostics.Notification, 3);
|
||||
d.Record(OpcUaClientDiagnostics.DataChange);
|
||||
d.Record(OpcUaClientDiagnostics.BadStatusValue);
|
||||
d.Record(OpcUaClientDiagnostics.PublishError);
|
||||
d.Record(OpcUaClientDiagnostics.KeepAliveMiss);
|
||||
d.Record(OpcUaClientDiagnostics.ReconnectAttempt);
|
||||
d.Record(OpcUaClientDiagnostics.ReconnectSuccess);
|
||||
d.Record(OpcUaClientDiagnostics.ReconnectExhausted);
|
||||
|
||||
cap[OpcUaClientDiagnostics.PublishResponse].ShouldBe(1);
|
||||
cap[OpcUaClientDiagnostics.Notification].ShouldBe(3);
|
||||
cap[OpcUaClientDiagnostics.DataChange].ShouldBe(1);
|
||||
cap[OpcUaClientDiagnostics.BadStatusValue].ShouldBe(1);
|
||||
cap[OpcUaClientDiagnostics.PublishError].ShouldBe(1);
|
||||
cap[OpcUaClientDiagnostics.KeepAliveMiss].ShouldBe(1);
|
||||
cap[OpcUaClientDiagnostics.ReconnectAttempt].ShouldBe(1);
|
||||
cap[OpcUaClientDiagnostics.ReconnectSuccess].ShouldBe(1);
|
||||
cap[OpcUaClientDiagnostics.ReconnectExhausted].ShouldBe(1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Concurrent_increments_do_not_lose_updates()
|
||||
public void Another_instances_events_are_not_attributed_to_this_one()
|
||||
{
|
||||
var d = new OpcUaClientDiagnostics();
|
||||
const int threads = 8, each = 5_000;
|
||||
var mine = $"diag-{Guid.NewGuid():N}";
|
||||
using var cap = new Capture(mine);
|
||||
|
||||
// Positive control: my own event IS seen; the other instance's identical event is not.
|
||||
new OpcUaClientDiagnostics("someone-else").Record(OpcUaClientDiagnostics.KeepAliveMiss);
|
||||
new OpcUaClientDiagnostics(mine).Record(OpcUaClientDiagnostics.PublishError);
|
||||
|
||||
cap[OpcUaClientDiagnostics.PublishError].ShouldBe(1);
|
||||
cap[OpcUaClientDiagnostics.KeepAliveMiss].ShouldBe(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Concurrent_records_do_not_lose_updates()
|
||||
{
|
||||
var id = $"diag-{Guid.NewGuid():N}";
|
||||
using var cap = new Capture(id);
|
||||
var d = new OpcUaClientDiagnostics(id);
|
||||
const int threads = 8, each = 2_000;
|
||||
|
||||
Parallel.For(0, threads, _ =>
|
||||
{
|
||||
for (var i = 0; i < each; i++) { d.RecordDataChange(bad: i % 2 == 0); d.RecordNotifications(2); }
|
||||
for (var i = 0; i < each; i++) d.Record(OpcUaClientDiagnostics.DataChange);
|
||||
});
|
||||
|
||||
var s = d.Snapshot();
|
||||
s["DataChangeCount"].ShouldBe(threads * each);
|
||||
s["BadStatusValueCount"].ShouldBe(threads * each / 2);
|
||||
s["NotificationCount"].ShouldBe(threads * each * 2);
|
||||
cap[OpcUaClientDiagnostics.DataChange].ShouldBe(threads * each);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Snapshot_is_a_point_in_time_copy()
|
||||
public void Zero_or_negative_counts_are_ignored()
|
||||
{
|
||||
var d = new OpcUaClientDiagnostics();
|
||||
var before = d.Snapshot();
|
||||
d.RecordKeepAliveMiss();
|
||||
var id = $"diag-{Guid.NewGuid():N}";
|
||||
using var cap = new Capture(id);
|
||||
var d = new OpcUaClientDiagnostics(id);
|
||||
|
||||
before["KeepAliveMissCount"].ShouldBe(0);
|
||||
d.Snapshot()["KeepAliveMissCount"].ShouldBe(1);
|
||||
d.Record(OpcUaClientDiagnostics.Notification, 0);
|
||||
d.Record(OpcUaClientDiagnostics.Notification, -2);
|
||||
d.Record(OpcUaClientDiagnostics.DataChange); // positive control
|
||||
|
||||
cap[OpcUaClientDiagnostics.Notification].ShouldBe(0);
|
||||
cap[OpcUaClientDiagnostics.DataChange].ShouldBe(1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #275: the driver does not split Read/Write/CreateMonitoredItems itself because the SDK session already
|
||||
/// does — <c>Session</c> derives from <c>SessionClientBatched</c> (which batches by the server's
|
||||
/// OperationLimits, fetched on open). If an SDK bump ever drops that, this goes red and the driver needs
|
||||
/// its own chunking again.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void GetHealth_carries_the_diagnostics_and_keeps_state_fields()
|
||||
public void The_sdk_session_is_the_batching_session_type()
|
||||
{
|
||||
using var drv = new OpcUaClientDriver(new OpcUaClientDriverOptions(), "opcua-diag");
|
||||
drv.DiagnosticsForTest.RecordKeepAliveMiss();
|
||||
drv.DiagnosticsForTest.RecordKeepAliveMiss();
|
||||
|
||||
var h = drv.GetHealth();
|
||||
|
||||
h.State.ShouldBe(DriverState.Unknown);
|
||||
h.Diagnostics.ShouldNotBeNull();
|
||||
h.Diagnostics!["KeepAliveMissCount"].ShouldBe(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DriverHealth_without_diagnostics_stays_back_compatible()
|
||||
{
|
||||
var h = new DriverHealth(DriverState.Healthy, null, null);
|
||||
|
||||
h.Diagnostics.ShouldBeNull();
|
||||
h.ShouldBe(new DriverHealth(DriverState.Healthy, null, null));
|
||||
typeof(SessionClientBatched).IsAssignableFrom(typeof(Session)).ShouldBeTrue();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user