Ships the data + runtime layer of Stream E. The SignalR hub and Blazor /hosts page refresh (E.2-E.3) are follow-up work paired with the visual-compliance review per Phase 6.4 patterns — documented as a deferred follow-up below. Configuration: - New entity DriverInstanceResilienceStatus with: DriverInstanceId, HostName (composite PK), LastCircuitBreakerOpenUtc, ConsecutiveFailures, CurrentBulkheadDepth, LastRecycleUtc, BaselineFootprintBytes, CurrentFootprintBytes, LastSampledUtc. - Separate from DriverHostStatus (per-host connectivity view) so a Running host that has tripped its breaker or is nearing its memory ceiling shows up distinctly on Admin /hosts. Admin page left-joins both for display. - OtOpcUaConfigDbContext + Fluent-API config + IX_DriverResilience_LastSampled index for the stale-sample filter query. - EF migration: 20260419124034_AddDriverInstanceResilienceStatus. Core.Resilience: - DriverResilienceStatusTracker — process-singleton in-memory tracker keyed on (DriverInstanceId, HostName). CapabilityInvoker + MemoryTracking + MemoryRecycle callers record failure/success/breaker-open/recycle/footprint events; a HostedService (Stream E.2 follow-up) samples this tracker every 5 s and persists to the DB. Pure in-memory keeps tests fast + the core free of EF/SQL dependencies. Tests: - DriverResilienceStatusTrackerTests (9 new, all pass): tryget-before-write returns null; failures accumulate; success resets; breaker/recycle/footprint fields populate; per-host isolation; snapshot returns all pairs; concurrent writes don't lose counts. - SchemaComplianceTests: expected-tables list updated to include the new DriverInstanceResilienceStatus table. Full solution dotnet test: 1042 passing (baseline 906, +136 for Phase 6.1 so far across Streams A/B/C/D/E.1). Pre-existing Client.CLI Subscribe flake unchanged. Deferred to follow-up PR (E.2/E.3): - ResilienceStatusPublisher HostedService that samples DriverResilienceStatusTracker every 5 s + upserts DriverInstanceResilienceStatus rows. - Admin FleetStatusHub SignalR hub pushing LastCircuitBreakerOpenUtc / CurrentBulkheadDepth / LastRecycleUtc on change. - Admin /hosts Blazor column additions (red badge when ConsecutiveFailures > breakerThreshold / 2). Visual-compliance reviewer signoff alongside Phase 6.4 admin-ui patterns. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
111 lines
3.3 KiB
C#
111 lines
3.3 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Resilience;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Core.Tests.Resilience;
|
|
|
|
[Trait("Category", "Unit")]
|
|
public sealed class DriverResilienceStatusTrackerTests
|
|
{
|
|
private static readonly DateTime Now = new(2026, 4, 19, 12, 0, 0, DateTimeKind.Utc);
|
|
|
|
[Fact]
|
|
public void TryGet_Returns_Null_Before_AnyWrite()
|
|
{
|
|
var tracker = new DriverResilienceStatusTracker();
|
|
|
|
tracker.TryGet("drv", "host").ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void RecordFailure_Accumulates_ConsecutiveFailures()
|
|
{
|
|
var tracker = new DriverResilienceStatusTracker();
|
|
|
|
tracker.RecordFailure("drv", "host", Now);
|
|
tracker.RecordFailure("drv", "host", Now.AddSeconds(1));
|
|
tracker.RecordFailure("drv", "host", Now.AddSeconds(2));
|
|
|
|
tracker.TryGet("drv", "host")!.ConsecutiveFailures.ShouldBe(3);
|
|
}
|
|
|
|
[Fact]
|
|
public void RecordSuccess_Resets_ConsecutiveFailures()
|
|
{
|
|
var tracker = new DriverResilienceStatusTracker();
|
|
tracker.RecordFailure("drv", "host", Now);
|
|
tracker.RecordFailure("drv", "host", Now.AddSeconds(1));
|
|
|
|
tracker.RecordSuccess("drv", "host", Now.AddSeconds(2));
|
|
|
|
tracker.TryGet("drv", "host")!.ConsecutiveFailures.ShouldBe(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void RecordBreakerOpen_Populates_LastBreakerOpenUtc()
|
|
{
|
|
var tracker = new DriverResilienceStatusTracker();
|
|
|
|
tracker.RecordBreakerOpen("drv", "host", Now);
|
|
|
|
tracker.TryGet("drv", "host")!.LastBreakerOpenUtc.ShouldBe(Now);
|
|
}
|
|
|
|
[Fact]
|
|
public void RecordRecycle_Populates_LastRecycleUtc()
|
|
{
|
|
var tracker = new DriverResilienceStatusTracker();
|
|
|
|
tracker.RecordRecycle("drv", "host", Now);
|
|
|
|
tracker.TryGet("drv", "host")!.LastRecycleUtc.ShouldBe(Now);
|
|
}
|
|
|
|
[Fact]
|
|
public void RecordFootprint_CapturesBaselineAndCurrent()
|
|
{
|
|
var tracker = new DriverResilienceStatusTracker();
|
|
|
|
tracker.RecordFootprint("drv", "host", baselineBytes: 100_000_000, currentBytes: 150_000_000, Now);
|
|
|
|
var snap = tracker.TryGet("drv", "host")!;
|
|
snap.BaselineFootprintBytes.ShouldBe(100_000_000);
|
|
snap.CurrentFootprintBytes.ShouldBe(150_000_000);
|
|
}
|
|
|
|
[Fact]
|
|
public void DifferentHosts_AreIndependent()
|
|
{
|
|
var tracker = new DriverResilienceStatusTracker();
|
|
|
|
tracker.RecordFailure("drv", "host-a", Now);
|
|
tracker.RecordFailure("drv", "host-b", Now);
|
|
tracker.RecordSuccess("drv", "host-a", Now.AddSeconds(1));
|
|
|
|
tracker.TryGet("drv", "host-a")!.ConsecutiveFailures.ShouldBe(0);
|
|
tracker.TryGet("drv", "host-b")!.ConsecutiveFailures.ShouldBe(1);
|
|
}
|
|
|
|
[Fact]
|
|
public void Snapshot_ReturnsAll_TrackedPairs()
|
|
{
|
|
var tracker = new DriverResilienceStatusTracker();
|
|
tracker.RecordFailure("drv-1", "host-a", Now);
|
|
tracker.RecordFailure("drv-1", "host-b", Now);
|
|
tracker.RecordFailure("drv-2", "host-a", Now);
|
|
|
|
var snapshot = tracker.Snapshot();
|
|
|
|
snapshot.Count.ShouldBe(3);
|
|
}
|
|
|
|
[Fact]
|
|
public void ConcurrentWrites_DoNotLose_Failures()
|
|
{
|
|
var tracker = new DriverResilienceStatusTracker();
|
|
Parallel.For(0, 500, _ => tracker.RecordFailure("drv", "host", Now));
|
|
|
|
tracker.TryGet("drv", "host")!.ConsecutiveFailures.ShouldBe(500);
|
|
}
|
|
}
|