Files
lmxopcua/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.Sql.Common.Tests/AlarmBatchWriteTests.cs
T
Joseph Doherty 07ee9e4888 fix(historian): #976 a historian outage no longer dead-letters alarm history, and an outbox ack never truncates an un-indexed value
G-16 / K-2 (LocalDbStoreAndForwardSink): a RetryPlease counts toward MaxAttempts only on a
drain where some event was Ack'd. A drain with no Ack is an outage: RetryPlease rows are left
untouched (no attempt, no dead letter), the backoff climbs to its 60 s cap, LastError reports
the deferral; PermanentFail rows still dead-letter. An outage longer than ~6.5 min used to
dead-letter the oldest queued alarm events, 100 more every ~10 min.

G-8 (GatewayAlarmHistorianWriter): only InvalidArgument/FailedPrecondition/OutOfRange/
NotFound/AlreadyExists/Unimplemented and unmappable events are PermanentFail; Cancelled,
Unknown, DataLoss, every unlisted code and every non-RPC transport exception retry (the
default used to be dead-letter, so a gateway restart dead-lettered a whole drained batch).

G-16 secondary (AlarmBatchWrite, both SQL writers): a multi-event chunk failing with an
error that is neither a recognised data error nor a known transient is re-sent event by
event, so one unrecognised poison event no longer drags its chunk-mates with it.

G-7 (FasterLogHistorizationOutbox): RemoveAsync of the newest indexed entry truncates to
that record's own end (recorded by the peek, else a one-record scan), never to the live
tail that may already hold an enqueued-but-unindexed append; peeks return only indexed
records so every peeked entry can be acked.

Tests: simulated multi-hour outage (fake clock, 200 drain cycles) loses 0 events and
drains in order on reconnect; positive controls for PermanentFail and a RetryPlease-forever
poison row getting exactly its MaxAttempts budget; gRPC/transport classification theory;
AlarmBatchWrite isolation + known-outage control; G-7 parked-append race + peek-only-indexed.
2026-12-03 10:03:25 -05:00

137 lines
6.1 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian;
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Sql.Common.Tests;
/// <summary>
/// The shared batched alarm write: one statement per chunk instead of one round trip per event, with the per-event
/// outcomes the store-and-forward drain needs kept intact (a poison event is isolated, an outage retries everything,
/// a cancellation never dead-letters).
/// </summary>
public sealed class AlarmBatchWriteTests
{
private sealed class PoisonException : Exception;
private sealed class OutageException : Exception;
private sealed class UnrecognisedException : Exception;
private static HistorianWriteOutcome Classify(Exception ex) =>
ex is PoisonException ? HistorianWriteOutcome.PermanentFail : HistorianWriteOutcome.RetryPlease;
private static bool IsTransient(Exception ex) => ex is OutageException;
private static HistorianWriteOutcome?[] Undecided(int n) => new HistorianWriteOutcome?[n];
[Fact]
public async Task A_clean_batch_is_one_statement_per_chunk_and_all_Ack()
{
var statements = new List<int[]>();
var r = await AlarmBatchWrite.RunAsync(Undecided(25), 10,
(ix, _) => { statements.Add(ix.ToArray()); return Task.CompletedTask; }, Classify, IsTransient, null, CancellationToken.None);
r.ShouldAllBe(o => o == HistorianWriteOutcome.Ack);
statements.Select(s => s.Length).ShouldBe([10, 10, 5]);
statements.SelectMany(s => s).ShouldBe(Enumerable.Range(0, 25));
}
[Fact]
public async Task A_poison_event_is_isolated_and_its_siblings_still_Ack()
{
var statements = 0;
var r = await AlarmBatchWrite.RunAsync(Undecided(6), 10, (ix, _) =>
{
statements++;
if (ix.Contains(3)) throw new PoisonException();
return Task.CompletedTask;
}, Classify, IsTransient, null, CancellationToken.None);
r.ShouldBe([HistorianWriteOutcome.Ack, HistorianWriteOutcome.Ack, HistorianWriteOutcome.Ack,
HistorianWriteOutcome.PermanentFail, HistorianWriteOutcome.Ack, HistorianWriteOutcome.Ack]);
statements.ShouldBe(1 + 6, "the failed chunk, then one statement per event");
}
[Fact]
public async Task An_outage_retries_the_whole_chunk_without_re_sending_event_by_event()
{
var statements = 0;
var r = await AlarmBatchWrite.RunAsync(Undecided(6), 10,
(_, _) => { statements++; throw new OutageException(); }, Classify, IsTransient, null, CancellationToken.None);
r.ShouldAllBe(o => o == HistorianWriteOutcome.RetryPlease);
statements.ShouldBe(1);
}
[Fact]
public async Task Pre_decided_slots_are_kept_and_never_sent()
{
var sent = new List<int>();
var outcomes = Undecided(4);
outcomes[1] = HistorianWriteOutcome.PermanentFail; // e.g. a pre-1970 time on QuestDB
var r = await AlarmBatchWrite.RunAsync(outcomes, 10,
(ix, _) => { sent.AddRange(ix); return Task.CompletedTask; }, Classify, IsTransient, null, CancellationToken.None);
r.ShouldBe([HistorianWriteOutcome.Ack, HistorianWriteOutcome.PermanentFail, HistorianWriteOutcome.Ack, HistorianWriteOutcome.Ack]);
sent.ShouldBe([0, 2, 3]);
}
[Fact]
public async Task A_cancellation_mid_batch_retries_everything_undecided_and_dead_letters_nothing()
{
using var cts = new CancellationTokenSource();
var r = await AlarmBatchWrite.RunAsync(Undecided(5), 2, (ix, ct) =>
{
if (ix.Contains(2)) { cts.Cancel(); ct.ThrowIfCancellationRequested(); }
return Task.CompletedTask;
}, Classify, IsTransient, null, cts.Token);
r.ShouldBe([HistorianWriteOutcome.Ack, HistorianWriteOutcome.Ack,
HistorianWriteOutcome.RetryPlease, HistorianWriteOutcome.RetryPlease, HistorianWriteOutcome.RetryPlease]);
}
[Fact]
public async Task A_cancellation_during_poison_isolation_retries_the_rest()
{
using var cts = new CancellationTokenSource();
var r = await AlarmBatchWrite.RunAsync(Undecided(4), 10, (ix, ct) =>
{
if (ix.Count > 1) throw new PoisonException();
if (ix[0] == 1) { cts.Cancel(); ct.ThrowIfCancellationRequested(); }
return Task.CompletedTask;
}, Classify, IsTransient, null, cts.Token);
r.ShouldBe([HistorianWriteOutcome.Ack, HistorianWriteOutcome.RetryPlease, HistorianWriteOutcome.RetryPlease,
HistorianWriteOutcome.RetryPlease]);
}
[Fact]
public async Task An_unrecognised_chunk_failure_is_isolated_so_its_siblings_still_Ack()
{
// G-16 secondary effect (#976): an error the classifier does not recognise is RetryPlease — not proven permanent —
// but it is not a known outage either. Retrying the whole chunk tied every sibling to the poison event; isolating it
// lets the siblings land, and the store-and-forward sink (which counts a RetryPlease only when the same drain shows
// the historian reachable) dead-letters the poison event alone once it keeps failing.
var statements = 0;
var r = await AlarmBatchWrite.RunAsync(Undecided(3), 10, (ix, _) =>
{
statements++;
if (ix.Contains(1)) throw new UnrecognisedException();
return Task.CompletedTask;
}, Classify, IsTransient, null, CancellationToken.None);
r.ShouldBe([HistorianWriteOutcome.Ack, HistorianWriteOutcome.RetryPlease, HistorianWriteOutcome.Ack]);
statements.ShouldBe(1 + 3, "the failed chunk, then one statement per event");
}
[Fact]
public async Task A_known_outage_is_never_isolated_even_though_an_unrecognised_failure_is()
{
// Control for the test above: isolation keys on "not a KNOWN transient", not on RetryPlease.
var statements = 0;
var r = await AlarmBatchWrite.RunAsync(Undecided(3), 10,
(_, _) => { statements++; throw new OutageException(); }, Classify, IsTransient, null, CancellationToken.None);
r.ShouldAllBe(o => o == HistorianWriteOutcome.RetryPlease);
statements.ShouldBe(1);
}
}