using Google.Protobuf.WellKnownTypes; using Microsoft.Extensions.Logging.Abstractions; using Shouldly; using Xunit; using ZB.MOM.WW.HistorianGateway.Contracts.Grpc; namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Tests; /// /// lmxopcua#746: a capped Gateway events read sends cap + 1 as the server-side ReadEventsRequest.max_events, so a /// current gateway completes the stream normally at the cap (no cancelled RPC, no evicted historian session) and the /// (cap + 1)-th event is the "more exist" signal. An ambiguous capped answer (foreign rows filtered out of a stream the /// server cut) and a refused cap (InvalidArgument) are re-read without a server cap. The fake honours /// max_events the way the gateway does (stop at it, complete normally). /// public sealed class GatewayServerCappedEventsTests { private static readonly DateTime T0 = new(2026, 11, 12, 8, 0, 0, DateTimeKind.Utc); private static CancellationToken Ct => TestContext.Current.CancellationToken; [Fact] public async Task A_capped_read_asks_the_server_for_cap_plus_one_and_never_abandons_the_stream() { // Budget 0 = abandon at the cap on the drain path: only the server cap keeps this stream from being abandoned. var fake = new FakeHistorianGatewayClient { Events = Series(50) }; var ds = new GatewayHistorianDataSource(fake, NullLogger.Instance, eventDrainBudget: 0); var r = await ds.ReadEventsAsync(null, T0, T0.AddHours(1), maxEvents: 10, Ct); r.Events.Select(e => e.EventId).ShouldBe(Enumerable.Range(0, 10).Select(i => $"E{i:000}")); r.ContinuationPoint.ShouldNotBeNull("the 11th event is the 'more exist' signal"); fake.EventReadCaps.ShouldBe([11]); fake.EventsYieldedCount.ShouldBe(11, "the server stops at max_events — the window's other 39 events never cross the wire"); fake.EventStreamsCompleted.ShouldBe(1); fake.EventStreamsAbandoned.ShouldBe(0); ds.EventStreamsAbandoned.ShouldBe(0); ds.EventServerCapReReads.ShouldBe(0); } [Fact] public async Task A_window_holding_exactly_the_cap_is_complete_in_one_read() { var fake = new FakeHistorianGatewayClient { Events = Series(10) }; var ds = new GatewayHistorianDataSource(fake, NullLogger.Instance); var r = await ds.ReadEventsAsync(null, T0, T0.AddHours(1), maxEvents: 10, Ct); r.Events.Count.ShouldBe(10); r.ContinuationPoint.ShouldBeNull(); fake.EventReadCaps.ShouldBe([11]); fake.ReadEventsCallCount.ShouldBe(1); } [Fact] public async Task Foreign_rows_inside_the_server_cut_are_re_read_without_the_cap() { // The server filter let foreign rows through (the client-side filter drops them): the capped stream ends at // cap + 1 rows without a matching overflow row, so whether more matching events exist is unknown. var events = Series(3, "Pump1").Concat(Series(5, "Pump2", offsetSeconds: 10)).Append(Ev(99, "Pump1")).ToArray(); var fake = new FakeHistorianGatewayClient { Events = events }; var ds = new GatewayHistorianDataSource(fake, NullLogger.Instance); var r = await ds.ReadEventsAsync("Pump1", T0, T0.AddHours(1), maxEvents: 3, Ct); r.Events.Count.ShouldBe(3); r.ContinuationPoint.ShouldNotBeNull("the late Pump1 event exists past the server's cut"); fake.EventReadCaps.ShouldBe([4, 0]); ds.EventServerCapReReads.ShouldBe(1); // Negative: without the late matching event the re-read answers complete. fake.Events = events[..^1]; fake.EventReadCaps.Clear(); (await ds.ReadEventsAsync("Pump1", T0, T0.AddHours(1), maxEvents: 3, Ct)).ContinuationPoint.ShouldBeNull(); fake.EventReadCaps.ShouldBe([4, 0]); } [Fact] public async Task A_refused_server_cap_is_re_read_uncapped_and_is_not_a_backend_failure() { var fake = new FakeHistorianGatewayClient { Events = Series(20), MaxEventsCeiling = 5 }; var ds = new GatewayHistorianDataSource(fake, NullLogger.Instance); var r = await ds.ReadEventsAsync(null, T0, T0.AddHours(1), maxEvents: 10, Ct); r.Events.Count.ShouldBe(10); r.ContinuationPoint.ShouldNotBeNull(); fake.EventReadCaps.ShouldBe([11, 0]); ds.GetHealthSnapshot().TotalFailures.ShouldBe(0); // Positive control: a cap under the ceiling goes out as the server cap and is not re-read. fake.EventReadCaps.Clear(); (await ds.ReadEventsAsync(null, T0, T0.AddHours(1), maxEvents: 3, Ct)).Events.Count.ShouldBe(3); fake.EventReadCaps.ShouldBe([4]); } [Theory] [InlineData(0)] [InlineData(int.MaxValue)] [InlineData(GatewayHistorianDataSource.MaxServerCappedEvents + 1)] public async Task Uncapped_and_over_ceiling_reads_send_no_server_cap(int maxEvents) { var fake = new FakeHistorianGatewayClient { Events = Series(5) }; var ds = new GatewayHistorianDataSource(fake, NullLogger.Instance); (await ds.ReadEventsAsync(null, T0, T0.AddHours(1), maxEvents, Ct)).Events.Count.ShouldBe(5); fake.EventReadCaps.ShouldBe([0]); } [Fact] public async Task The_largest_server_capped_page_sends_the_gateway_default_ceiling() { var fake = new FakeHistorianGatewayClient { Events = Series(5), MaxEventsCeiling = 1_000_000 }; var ds = new GatewayHistorianDataSource(fake, NullLogger.Instance); await ds.ReadEventsAsync(null, T0, T0.AddHours(1), GatewayHistorianDataSource.MaxServerCappedEvents, Ct); fake.EventReadCaps.ShouldBe([1_000_000], "cap + 1 must stay within the gateway's default MaxEventsCeiling"); } private static HistorianEvent[] Series(int count, string source = "Pump1", int offsetSeconds = 0) => Enumerable.Range(0, count).Select(i => Ev(i + offsetSeconds, source, $"E{i:000}")).ToArray(); private static HistorianEvent Ev(int seconds, string source, string? id = null) => new() { Id = id ?? $"X{seconds}", SourceName = source, EventTime = Timestamp.FromDateTime(T0.AddSeconds(seconds)), ReceivedTime = Timestamp.FromDateTime(T0.AddSeconds(seconds)), }; }