using Opc.Ua; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Core.Abstractions; using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections; namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections; /// /// H4 — the remote-history diagnostics follow real traffic: a real consumer OPC UA session reads a projected node through /// the real projection stack and a real in-process origin; both sides' counts by status class and open continuations /// move with each page, resume and release. /// public sealed partial class GrpcDataConfiguredDiscoveryWireTests { [Fact(Timeout = 90000)] [Trait("Phase", "H4")] public async Task History_diagnostics_count_answers_by_status_class_and_track_open_continuations_on_both_sides() { await using var originServer = await HistoryOrigin.StartAsync(7); originServer.Source.Aggregates = new HashSet { HistoryAggregateType.Average }; using var origin = new LocalDataFixture(servingGeneration: 1, history: originServer.Port); ProjectedHistoryAccess? history = null; await WithComposedServer(origin, origin.Raw, async (session, _, _, _, _, _, anchor) => { var client = (SessionClient)session; await BrowseAll(client, anchor, 1); var consumer = history!.Diagnostics; // Before any history traffic: nothing counted, nothing open; the origin reports its backend. consumer.CaptureConsumer("link").ShouldBe(new ConsumerHistoryCounts(0, 0, 0, 0)); var idle = origin.HistoryDiagnostics.CaptureOrigin().ShouldNotBeNull(); idle.Available.ShouldBeTrue(); (idle.ReadsGood, idle.ReadsBad, idle.OpenContinuations).ShouldBe((0L, 0L, 0)); // Page 1 of 7 at 2 per page: one Good answer on each side, one continuation open on each side. var page = await RawHistory(client, anchor, 2, default); page.ContinuationPoint.IsEmpty.ShouldBeFalse(); consumer.CaptureConsumer("link").ShouldBe(new ConsumerHistoryCounts(1, 0, 0, 1)); consumer.CaptureConsumer("LINK").Good.ShouldBe(1, "connection ids are case-insensitive"); origin.HistoryDiagnostics.CaptureOrigin()!.OpenContinuations.ShouldBe(1); origin.HistoryDiagnostics.CaptureOrigin()!.ReadsGood.ShouldBe(1); // Resume: the claimed cursor settles, its successor opens — still one open, two Good answers. var next = await RawHistory(client, anchor, 2, page.ContinuationPoint); next.ContinuationPoint.IsEmpty.ShouldBeFalse(); consumer.CaptureConsumer("link").ShouldBe(new ConsumerHistoryCounts(2, 0, 0, 1)); consumer.OpenConsumerContinuations.ShouldBe(1); // releaseContinuationPoints: the consumer cursor settles at once, the origin's after the coalesced release. await client.HistoryReadAsync(new RequestHeader(), new ExtensionObject(RawDetails(2)), TimestampsToReturn.Both, true, [new HistoryReadValueId { NodeId = anchor, ContinuationPoint = next.ContinuationPoint }], TestContext.Current.CancellationToken); consumer.CaptureConsumer("link").OpenContinuations.ShouldBe(0); await AwaitReleases(history, () => origin.Services.HistoryOpenContinuations == 0); origin.HistoryDiagnostics.CaptureOrigin()!.OpenContinuations.ShouldBe(0); // An aggregate the origin refuses: one Bad answer on each side. var refused = await ProcessedHistory(client, anchor, ObjectIds.AggregateFunction_Count); refused.StatusCode.Code.ShouldBe(StatusCodes.BadAggregateNotSupported.Code); consumer.CaptureConsumer("link").ShouldBe(new ConsumerHistoryCounts(2, 0, 1, 0)); var after = origin.HistoryDiagnostics.CaptureOrigin()!; (after.ReadsGood, after.ReadsUncertain, after.ReadsBad).ShouldBe((2L, 0L, 1L)); }, localPermissions: 15, allowHistory: true, connectionSource: new PersistentConnections(origin), onHistory: (h, _) => history = h); } [Fact(Timeout = 90000)] [Trait("Phase", "H4")] public async Task Consumer_session_close_settles_its_open_consumer_continuations() { await using var originServer = await HistoryOrigin.StartAsync(7); using var origin = new LocalDataFixture(servingGeneration: 1, history: originServer.Port); ProjectedHistoryAccess? history = null; await WithComposedServer(origin, origin.Raw, async (session, _, _, _, _, _, anchor) => { var client = (SessionClient)session; await BrowseAll(client, anchor, 1); (await RawHistory(client, anchor, 2, default)).ContinuationPoint.IsEmpty.ShouldBeFalse(); history!.Diagnostics.OpenConsumerContinuations.ShouldBe(1); await session.CloseAsync(TestContext.Current.CancellationToken); await AwaitReleases(history, () => origin.Services.HistoryOpenContinuations == 0); history.Diagnostics.OpenConsumerContinuations.ShouldBe(0, "the SDK disposed the Remote point on session close"); origin.HistoryDiagnostics.CaptureOrigin()!.OpenContinuations.ShouldBe(0); }, localPermissions: 15, allowHistory: true, connectionSource: new PersistentConnections(origin), onHistory: (h, _) => history = h); } }