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; /// #1000 S-12: subscribing N nodes (and replaying them after a failover) costs one server round trip, not N. 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 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); } /// 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). [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); } /// The failover replay re-creates every recorded subscription in one batched apply. [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); } }