Lands the pure-logic heart of Phase 6.3. OPC UA node wiring (Stream C), RedundancyCoordinator topology loader (Stream A), Admin UI + metrics (Stream E), and client interop tests (Stream F) are follow-up work — tracked as tasks #145-150. New Server.Redundancy sub-namespace: - ServiceLevelCalculator — pure 8-state matrix per decision #154. Inputs: role, selfHealthy, peerUa/HttpHealthy, applyInProgress, recoveryDwellMet, topologyValid, operatorMaintenance. Output: OPC UA Part 5 §6.3.34 Byte. Reserved bands (0=Maintenance, 1=NoData, 2=InvalidTopology) override everything; operational bands occupy 30..255. Key invariants: * Authoritative-Primary = 255, Authoritative-Backup = 100. * Isolated-Primary = 230 (retains authority with peer down). * Isolated-Backup = 80 (does NOT auto-promote — non-transparent model). * Primary-Mid-Apply = 200, Backup-Mid-Apply = 50; apply dominates peer-unreachable per Stream C.4 integration expectation. * Recovering-Primary = 180, Recovering-Backup = 30. * Standalone treats healthy as Authoritative-Primary (no peer concept). - ServiceLevelBand enum — labels every numeric band for logs + Admin UI. Values match the calculator table exactly; compliance script asserts drift detection. - RecoveryStateManager — holds Recovering band until (dwell ≥ 60s default) AND (one publish witness observed). Re-fault resets both gates so a flapping node doesn't shortcut through recovery twice. - ApplyLeaseRegistry — keyed on (ConfigGenerationId, PublishRequestId) per decision #162. BeginApplyLease returns an IAsyncDisposable so every exit path (success, exception, cancellation, dispose-twice) closes the lease. ApplyMaxDuration watchdog (10 min default) via PruneStale tick forces close after a crashed publisher so ServiceLevel can't stick at mid-apply. Tests (40 new, all pass): - ServiceLevelCalculatorTests (27): reserved bands override; self-unhealthy → NoData; invalid topology demotes both nodes to 2; authoritative primary 255; backup 100; isolated primary 230 retains authority; isolated backup 80 does not promote; http-only unreachable triggers isolated; mid-apply primary 200; mid-apply backup 50; apply dominates peer-unreachable; recovering primary 180; recovering backup 30; standalone treats healthy as 255; classify round-trips every band including Unknown sentinel. - RecoveryStateManagerTests (6): never-faulted auto-meets dwell; faulted-only returns true (semantics-doc test — coordinator short-circuits on selfHealthy=false); recovered without witness never meets; witness without dwell never meets; witness + dwell-elapsed meets; re-fault resets. - ApplyLeaseRegistryTests (7): empty registry not-in-progress; begin+dispose closes; dispose on exception still closes; dispose twice safe; concurrent leases isolated; watchdog closes stale; watchdog leaves recent alone. Full solution dotnet test: 1137 passing (Phase 6.2 shipped at 1097, Phase 6.3 B + D core = +40 = 1137). Pre-existing Client.CLI Subscribe flake unchanged. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
93 lines
3.1 KiB
C#
93 lines
3.1 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Server.Redundancy;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Server.Tests;
|
|
|
|
[Trait("Category", "Unit")]
|
|
public sealed class RecoveryStateManagerTests
|
|
{
|
|
private static readonly DateTime T0 = new(2026, 4, 19, 12, 0, 0, DateTimeKind.Utc);
|
|
|
|
private sealed class FakeTimeProvider : TimeProvider
|
|
{
|
|
public DateTime Utc { get; set; } = T0;
|
|
public override DateTimeOffset GetUtcNow() => new(Utc, TimeSpan.Zero);
|
|
}
|
|
|
|
[Fact]
|
|
public void NeverFaulted_DwellIsAutomaticallyMet()
|
|
{
|
|
var mgr = new RecoveryStateManager();
|
|
mgr.IsDwellMet().ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void AfterFault_Only_IsDwellMet_Returns_True_ButCallerDoesntQueryDuringFaulted()
|
|
{
|
|
// Documented semantics: IsDwellMet is only consulted when selfHealthy=true (i.e. the
|
|
// node has recovered into Healthy). During Faulted the coordinator short-circuits on
|
|
// the self-health check and never calls IsDwellMet. So returning true here is harmless;
|
|
// the test captures the intent so a future "return false during Faulted" tweak has to
|
|
// deliberately change this test first.
|
|
var mgr = new RecoveryStateManager();
|
|
mgr.MarkFaulted();
|
|
mgr.IsDwellMet().ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void AfterRecovery_NoWitness_DwellNotMet_EvenAfterElapsed()
|
|
{
|
|
var clock = new FakeTimeProvider();
|
|
var mgr = new RecoveryStateManager(dwellTime: TimeSpan.FromSeconds(60), timeProvider: clock);
|
|
mgr.MarkFaulted();
|
|
mgr.MarkRecovered();
|
|
clock.Utc = T0.AddSeconds(120);
|
|
|
|
mgr.IsDwellMet().ShouldBeFalse("dwell elapsed but no publish witness — must NOT escape Recovering band");
|
|
}
|
|
|
|
[Fact]
|
|
public void AfterRecovery_WitnessButTooSoon_DwellNotMet()
|
|
{
|
|
var clock = new FakeTimeProvider();
|
|
var mgr = new RecoveryStateManager(dwellTime: TimeSpan.FromSeconds(60), timeProvider: clock);
|
|
mgr.MarkFaulted();
|
|
mgr.MarkRecovered();
|
|
mgr.RecordPublishWitness();
|
|
clock.Utc = T0.AddSeconds(30);
|
|
|
|
mgr.IsDwellMet().ShouldBeFalse("witness ok but dwell 30s < 60s");
|
|
}
|
|
|
|
[Fact]
|
|
public void AfterRecovery_Witness_And_DwellElapsed_Met()
|
|
{
|
|
var clock = new FakeTimeProvider();
|
|
var mgr = new RecoveryStateManager(dwellTime: TimeSpan.FromSeconds(60), timeProvider: clock);
|
|
mgr.MarkFaulted();
|
|
mgr.MarkRecovered();
|
|
mgr.RecordPublishWitness();
|
|
clock.Utc = T0.AddSeconds(61);
|
|
|
|
mgr.IsDwellMet().ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void ReFault_ResetsWitness_AndDwellClock()
|
|
{
|
|
var clock = new FakeTimeProvider();
|
|
var mgr = new RecoveryStateManager(dwellTime: TimeSpan.FromSeconds(60), timeProvider: clock);
|
|
mgr.MarkFaulted();
|
|
mgr.MarkRecovered();
|
|
mgr.RecordPublishWitness();
|
|
clock.Utc = T0.AddSeconds(61);
|
|
mgr.IsDwellMet().ShouldBeTrue();
|
|
|
|
mgr.MarkFaulted();
|
|
mgr.MarkRecovered();
|
|
clock.Utc = T0.AddSeconds(100); // re-entered Recovering, no new witness
|
|
mgr.IsDwellMet().ShouldBeFalse("new recovery needs its own witness");
|
|
}
|
|
}
|