79235bf593
Decided: an innermost recorder counts each attempt ending in a handled failure (not caller cancellation, not a refusal; the pipeline timeout stays counted once by OnTimeout). OnRetry only logs now. Write/AlarmAcknowledge/ Initialize failures used to never raise the /hosts count, and a retried call's last attempt was never counted. The ::non-idempotent write pipeline records failures + breaker under the caller's host (trackerHostName). Co-Authored-By: Claude <noreply@anthropic.com>
95 lines
4.9 KiB
C#
95 lines
4.9 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Resilience;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Core.Tests.Resilience;
|
|
|
|
/// <summary>
|
|
/// lmxopcua#1052 (#999 follow-up, decided): <c>ConsecutiveFailures</c> counts FAILED ATTEMPTS, every one. It used to be
|
|
/// fed only by the retry strategy's <c>OnRetry</c>, so a capability with no retry (Write, AlarmAcknowledge, Initialize)
|
|
/// never raised the /hosts failure count however often it failed, and a retried call's last attempt was never counted
|
|
/// either. And a non-idempotent write's pipeline telemetry landed on a phantom <c>host::non-idempotent</c> row.
|
|
/// </summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class TerminalFailureTrackingTests
|
|
{
|
|
private static readonly DriverResilienceOptions TierA = new() { Tier = DriverTier.A };
|
|
|
|
[Theory]
|
|
[InlineData(DriverCapability.Write)]
|
|
[InlineData(DriverCapability.AlarmAcknowledge)]
|
|
[InlineData(DriverCapability.Initialize)]
|
|
public async Task A_no_retry_capability_failure_is_counted(DriverCapability capability)
|
|
{
|
|
var tracker = new DriverResilienceStatusTracker();
|
|
var pipeline = new DriverResiliencePipelineBuilder(statusTracker: tracker).GetOrCreate("drv", "host-1", capability, TierA);
|
|
|
|
for (var i = 1; i <= 2; i++)
|
|
{
|
|
await Should.ThrowAsync<InvalidOperationException>(async () =>
|
|
await pipeline.ExecuteAsync(_ => throw new InvalidOperationException("device refused the socket"),
|
|
TestContext.Current.CancellationToken));
|
|
tracker.TryGet("drv", "host-1")!.ConsecutiveFailures.ShouldBe(i, $"failure {i} of a {capability} with no retry");
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Every_failed_attempt_of_a_retried_call_is_counted_including_the_last()
|
|
{
|
|
var tracker = new DriverResilienceStatusTracker();
|
|
var pipeline = new DriverResiliencePipelineBuilder(statusTracker: tracker).GetOrCreate("drv", "host-1", DriverCapability.Read, TierA);
|
|
var attempts = 0;
|
|
|
|
await Should.ThrowAsync<InvalidOperationException>(async () =>
|
|
await pipeline.ExecuteAsync(_ => { attempts++; throw new InvalidOperationException("down"); },
|
|
TestContext.Current.CancellationToken));
|
|
|
|
attempts.ShouldBe(TierA.Resolve(DriverCapability.Read).RetryCount + 1);
|
|
tracker.TryGet("drv", "host-1")!.ConsecutiveFailures.ShouldBe(attempts);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_refusal_is_not_a_failure()
|
|
{
|
|
var tracker = new DriverResilienceStatusTracker();
|
|
var pipeline = new DriverResiliencePipelineBuilder(statusTracker: tracker).GetOrCreate("drv", "host-1", DriverCapability.Write, TierA);
|
|
|
|
await Should.ThrowAsync<DriverOperationRefusedException>(async () =>
|
|
await pipeline.ExecuteAsync(_ => throw new DriverOperationRefusedException("not writable"),
|
|
TestContext.Current.CancellationToken));
|
|
(tracker.TryGet("drv", "host-1")?.ConsecutiveFailures ?? 0).ShouldBe(0);
|
|
|
|
// Positive control: a genuine failure on the same pipeline IS counted.
|
|
await Should.ThrowAsync<InvalidOperationException>(async () =>
|
|
await pipeline.ExecuteAsync(_ => throw new InvalidOperationException("down"), TestContext.Current.CancellationToken));
|
|
tracker.TryGet("drv", "host-1")!.ConsecutiveFailures.ShouldBe(1);
|
|
}
|
|
|
|
/// <summary>
|
|
/// A non-idempotent write runs on a pipeline cached under <c>host::non-idempotent</c>; its failures and its breaker
|
|
/// belong to the CALLER's host, where its success already resets the counter.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Non_idempotent_write_failures_land_on_the_callers_host()
|
|
{
|
|
var tracker = new DriverResilienceStatusTracker();
|
|
var invoker = new CapabilityInvoker(new DriverResiliencePipelineBuilder(statusTracker: tracker), "drv", () => TierA,
|
|
statusTracker: tracker);
|
|
var threshold = TierA.Resolve(DriverCapability.Write).BreakerFailureThreshold;
|
|
|
|
for (var i = 0; i < threshold; i++)
|
|
await Should.ThrowAsync<InvalidOperationException>(async () =>
|
|
await invoker.ExecuteWriteAsync<int>("host-1", isIdempotent: false,
|
|
_ => throw new InvalidOperationException("down"), TestContext.Current.CancellationToken));
|
|
|
|
var snap = tracker.TryGet("drv", "host-1")!;
|
|
snap.ConsecutiveFailures.ShouldBe(threshold);
|
|
snap.IsBreakerOpen.ShouldBeTrue("the write breaker opened, and it is this host's breaker");
|
|
tracker.TryGet("drv", "host-1::non-idempotent").ShouldBeNull("no phantom host row");
|
|
|
|
await invoker.ExecuteWriteAsync("host-1", isIdempotent: true, _ => ValueTask.FromResult(0), TestContext.Current.CancellationToken);
|
|
tracker.TryGet("drv", "host-1")!.ConsecutiveFailures.ShouldBe(0, "a success on the same host resets it");
|
|
}
|
|
}
|