dd1d02e25f
G-9: a gateway-answered InvalidArgument/OutOfRange/NotFound/Unimplemented read is a connectivity observation (open, streak ends), not a failure; outages still degrade. G-17: capped reverse/end-only QuestDB events reads find the n+1-th latest ts with an early-exit backward scan, then read ts >= cut ordered (ts DESC, event_id DESC). G-18: Distribute takes a bridged/nonGood flag instead of per-sample delegates (O(buckets) allocation). QuestDB integration fixture waits up to 60 s for WAL visibility (shared host is CPU-saturated; the 5 s production default made alarm-writer tests read back fewer rows). Co-Authored-By: Claude <noreply@anthropic.com>
113 lines
4.8 KiB
C#
113 lines
4.8 KiB
C#
using Grpc.Core;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.HistorianGateway.Client;
|
|
using ZB.MOM.WW.HistorianGateway.Contracts.Grpc;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Tests;
|
|
|
|
/// <summary>
|
|
/// G-9 (review 2026-12-02, #993): a request-level refusal (the gateway ANSWERED "bad argument") must not mark the
|
|
/// connection closed or bump the consecutive-failure count; a real outage still must. The health snapshot is the
|
|
/// latest connectivity observation, and a refusal proves the gateway is reachable.
|
|
/// </summary>
|
|
public sealed class GatewayReadRefusalHealthTests
|
|
{
|
|
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
|
private static readonly DateTime T0 = new(2026, 12, 1, 8, 0, 0, DateTimeKind.Utc);
|
|
|
|
private static Exception Wrapped(StatusCode code) =>
|
|
new HistorianGatewayException(code.ToString(), new RpcException(new Status(code, code.ToString())));
|
|
|
|
private static GatewayHistorianDataSource Source(FakeHistorianGatewayClient fake) =>
|
|
new(fake, NullLogger<GatewayHistorianDataSource>.Instance);
|
|
|
|
public static TheoryData<StatusCode> RefusalCodes =>
|
|
[StatusCode.InvalidArgument, StatusCode.OutOfRange, StatusCode.NotFound, StatusCode.Unimplemented];
|
|
|
|
public static TheoryData<StatusCode> OutageCodes =>
|
|
[StatusCode.Unavailable, StatusCode.DeadlineExceeded];
|
|
|
|
private static async Task Read(GatewayHistorianDataSource ds, string kind)
|
|
{
|
|
switch (kind)
|
|
{
|
|
case "Raw": await ds.ReadRawAsync("T", T0, T0.AddHours(1), 10, Ct); break;
|
|
case "Events": await ds.ReadEventsAsync(null, T0, T0.AddHours(1), 10, Ct); break;
|
|
case "Processed":
|
|
await ds.ReadProcessedAsync("T", T0, T0.AddHours(1), TimeSpan.FromMinutes(10), HistoryAggregateType.Average, Ct); break;
|
|
case "AtTime": await ds.ReadAtTimeAsync("T", [T0], Ct); break;
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("Raw")]
|
|
[InlineData("Events")]
|
|
[InlineData("Processed")]
|
|
[InlineData("AtTime")]
|
|
public async Task A_refusal_keeps_the_connection_open_and_counts_no_consecutive_failure(string kind)
|
|
{
|
|
var fake = new FakeHistorianGatewayClient { RawSamples = [], Events = [] };
|
|
var ds = Source(fake);
|
|
await Read(ds, kind); // a good read: the connection is observed open
|
|
ds.GetHealthSnapshot().ProcessConnectionOpen.ShouldBeTrue();
|
|
|
|
fake.ThrowOnRead = Wrapped(StatusCode.InvalidArgument);
|
|
await Should.ThrowAsync<Exception>(() => Read(ds, kind));
|
|
|
|
var h = ds.GetHealthSnapshot();
|
|
h.ProcessConnectionOpen.ShouldBeTrue("the gateway answered — a refusal is not an outage");
|
|
h.EventConnectionOpen.ShouldBeTrue();
|
|
h.ConsecutiveFailures.ShouldBe(0);
|
|
h.TotalFailures.ShouldBe(0);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(RefusalCodes))]
|
|
public async Task Every_request_level_status_is_a_refusal(StatusCode code)
|
|
{
|
|
var fake = new FakeHistorianGatewayClient { RawSamples = [], ThrowOnRead = Wrapped(code) };
|
|
var ds = Source(fake);
|
|
// NotFound is the "tag missing" shape on some arms and is served as no-data; either way no health damage.
|
|
try { await ds.ReadRawAsync("T", T0, T0.AddHours(1), 10, Ct); } catch (Exception) { /* refusal rethrown */ }
|
|
var h = ds.GetHealthSnapshot();
|
|
h.ConsecutiveFailures.ShouldBe(0);
|
|
h.ProcessConnectionOpen.ShouldBeTrue();
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(OutageCodes))]
|
|
public async Task Positive_control_a_real_outage_still_degrades_health(StatusCode code)
|
|
{
|
|
var fake = new FakeHistorianGatewayClient { RawSamples = [] };
|
|
var ds = Source(fake);
|
|
await ds.ReadRawAsync("T", T0, T0.AddHours(1), 10, Ct);
|
|
|
|
fake.ThrowOnRead = Wrapped(code);
|
|
await Should.ThrowAsync<Exception>(() => ds.ReadRawAsync("T", T0, T0.AddHours(1), 10, Ct));
|
|
|
|
var h = ds.GetHealthSnapshot();
|
|
h.ProcessConnectionOpen.ShouldBeFalse();
|
|
h.ConsecutiveFailures.ShouldBe(1);
|
|
h.TotalFailures.ShouldBe(1);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_refusal_after_an_outage_proves_the_gateway_is_back_and_clears_the_streak()
|
|
{
|
|
var fake = new FakeHistorianGatewayClient { RawSamples = [], ThrowOnRead = Wrapped(StatusCode.Unavailable) };
|
|
var ds = Source(fake);
|
|
await Should.ThrowAsync<Exception>(() => ds.ReadRawAsync("T", T0, T0.AddHours(1), 10, Ct));
|
|
ds.GetHealthSnapshot().ConsecutiveFailures.ShouldBe(1);
|
|
|
|
fake.ThrowOnRead = Wrapped(StatusCode.InvalidArgument);
|
|
await Should.ThrowAsync<Exception>(() => ds.ReadRawAsync("T", T0, T0.AddHours(1), 10, Ct));
|
|
|
|
var h = ds.GetHealthSnapshot();
|
|
h.ProcessConnectionOpen.ShouldBeTrue();
|
|
h.ConsecutiveFailures.ShouldBe(0);
|
|
}
|
|
}
|