Files
lmxopcua/tests/Client/ZB.MOM.WW.OtOpcUa.Client.Shared.Tests/SubscribeManyTests.cs
T
Joseph Doherty 291be3c55c fix(client): #1000 failover retries with backoff, reconnect clears the subscription map, subscriptions batch into one round trip
S-4: a failover that finds every endpoint down stays Reconnecting and cycles the endpoints with a
1s..15s capped backoff until it connects or the connection epoch moves on; the backoff is woken by
the epoch so it never pins the failover slot. S-3: ConnectAsync/DisconnectAsync/landing Disconnected
clear the recorded subscriptions, so a re-subscribe of the same node creates a monitored item.
S-12: SubscribeManyAsync + ISubscriptionAdapter batch add (one CreateMonitoredItems), used by the CLI
subscribe and the failover replay. CLI subscribe/alarms print connection transitions.
2026-12-03 10:46:13 -05:00

112 lines
4.5 KiB
C#

using Opc.Ua;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Client.Shared.Models;
using ZB.MOM.WW.OtOpcUa.Client.Shared.Tests.Fakes;
namespace ZB.MOM.WW.OtOpcUa.Client.Shared.Tests;
/// <summary>#1000 S-12: subscribing N nodes (and replaying them after a failover) costs one server round trip, not N.</summary>
public sealed class SubscribeManyTests : IDisposable
{
private const string Primary = "opc.tcp://primary:4840";
private const string Backup = "opc.tcp://backup:4840";
private readonly FakeSessionFactory _factory = new();
private readonly OpcUaClientService _service;
public SubscribeManyTests()
{
_service = new OpcUaClientService(new FakeApplicationConfigurationFactory(), new FakeEndpointDiscovery(), _factory);
}
public void Dispose() => _service.Dispose();
private static List<NodeId> Nodes(int count) =>
Enumerable.Range(0, count).Select(i => NodeId.Parse($"ns=2;s=Plant/Dev/Tag{i}")).ToList();
private static ConnectionSettings Settings() => new()
{
EndpointUrl = Primary, FailoverUrls = [Backup], SessionTimeoutSeconds = 60
};
[Fact]
public async Task Subscribing_100_nodes_through_the_batch_path_makes_one_apply()
{
var ct = TestContext.Current.CancellationToken;
var session = new FakeSessionAdapter();
_factory.EnqueueSession(session);
await _service.ConnectAsync(Settings(), ct);
var results = await _service.SubscribeManyAsync(Nodes(100), 250, ct);
results.ShouldAllBe(e => e == null);
var sub = session.CreatedSubscriptions.ShouldHaveSingleItem();
sub.BatchApplyCount.ShouldBe(1);
sub.AddDataChangeCount.ShouldBe(100);
sub.ActiveHandles.Count.ShouldBe(100);
}
/// <summary>A per-item server rejection is reported for that node only; the rest are subscribed and recorded, and
/// the rejected node can be subscribed again later (it was not recorded).</summary>
[Fact]
public async Task A_per_item_rejection_is_reported_for_that_node_only()
{
var ct = TestContext.Current.CancellationToken;
var nodes = Nodes(5);
var sub = new FakeSubscriptionAdapter();
sub.RejectedNodes.Add(nodes[2].ToString());
_factory.EnqueueSession(new FakeSessionAdapter { NextSubscription = sub });
await _service.ConnectAsync(Settings(), ct);
var results = await _service.SubscribeManyAsync(nodes, 250, ct);
results.Select((e, i) => (e, i)).Where(t => t.e != null).Select(t => t.i).ShouldBe([2]);
sub.AddDataChangeCount.ShouldBe(4);
// Positive control: the accepted nodes are recorded as subscribed (a repeat makes no new apply) ...
await _service.SubscribeManyAsync(nodes.Where(n => n != nodes[2]).ToList(), 250, ct);
sub.BatchApplyCount.ShouldBe(1);
// ... while the rejected one was not recorded and is retried.
sub.RejectedNodes.Clear();
(await _service.SubscribeManyAsync([nodes[2]], 250, ct)).ShouldHaveSingleItem().ShouldBeNull();
sub.BatchApplyCount.ShouldBe(2);
sub.AddDataChangeCount.ShouldBe(5);
}
[Fact]
public async Task Duplicate_and_already_subscribed_nodes_are_not_added_twice()
{
var ct = TestContext.Current.CancellationToken;
var nodes = Nodes(3);
var session = new FakeSessionAdapter();
_factory.EnqueueSession(session);
await _service.ConnectAsync(Settings(), ct);
await _service.SubscribeAsync(nodes[0], 250, ct);
await _service.SubscribeManyAsync([nodes[0], nodes[1], nodes[1], nodes[2]], 250, ct);
session.CreatedSubscriptions.ShouldHaveSingleItem().AddDataChangeCount.ShouldBe(3);
}
/// <summary>The failover replay re-creates every recorded subscription in one batched apply.</summary>
[Fact]
public async Task Failover_replay_recreates_all_subscriptions_in_one_apply()
{
var ct = TestContext.Current.CancellationToken;
var s1 = new FakeSessionAdapter();
var s2 = new FakeSessionAdapter();
_factory.EnqueueSession(s1);
_factory.EnqueueSession(s2);
await _service.ConnectAsync(Settings(), ct);
await _service.SubscribeManyAsync(Nodes(50), 250, ct);
s1.SimulateKeepAlive(false);
await _service.LastFailover.ShouldNotBeNull().WaitAsync(TimeSpan.FromSeconds(10), ct);
var replayed = s2.CreatedSubscriptions.ShouldHaveSingleItem();
replayed.BatchApplyCount.ShouldBe(1);
replayed.AddDataChangeCount.ShouldBe(50);
}
}