fix(adminui): snapshot already old at dialog open is stale immediately (#857)
The Controls panel primed its per-node view from the snapshot store with ReceivedUtc = now, so a node that stopped publishing before the dialog opened looked live for the first 30 s stale window. InMemoryDriverStatusSnapshotStore now records each snapshot's LOCAL receive time (TimeProvider-injectable) and exposes it via GetAllWithReceiveTime(); the panel primes through the pure seam DriverStatusByNode.Prime/PrimedReceiveTime, which keeps the store's receive time (clamped to now; still skew-proof). A store that does not track one (default interface impl -> null) falls back to the publish time only when that is already older than the stale window, else now. Claude-Session: https://claude.ai/code/session_01GT67AD99zQQqpLCVm3p7xk
This commit is contained in:
@@ -129,7 +129,9 @@ public sealed class DriverStatusByNodeTests
|
||||
var src = AdminUiSource.Read("Components", "Shared", "Drivers", "DriverStatusPanel.razor");
|
||||
|
||||
src.ShouldContain("new DriverStatusByNode(DriverInstanceId)");
|
||||
src.ShouldContain("StatusStore.GetAll()");
|
||||
src.ShouldContain("_byNode.Prime(StatusStore.GetAllWithReceiveTime(), DateTime.UtcNow, StaleAfter)");
|
||||
// Priming must not stamp the store's snapshots "received now" (#857).
|
||||
src.ShouldNotContain("StatusStore.GetAll()");
|
||||
src.ShouldNotContain("StatusStore.TryGet(");
|
||||
src.ShouldContain("@foreach (var n in nodes)");
|
||||
src.ShouldContain("@n.Label");
|
||||
@@ -137,4 +139,117 @@ public sealed class DriverStatusByNodeTests
|
||||
// The old single-snapshot field must be gone, or a half-migrated panel could still render one node.
|
||||
src.ShouldNotContain("_snapshot");
|
||||
}
|
||||
|
||||
// ---- lmxopcua#857: a snapshot already old when the dialog opens is stale immediately ----
|
||||
|
||||
private static readonly TimeSpan StaleAfter = TimeSpan.FromSeconds(30);
|
||||
|
||||
/// <summary>A hand-driven clock — the test project has no <c>FakeTimeProvider</c> package reference.</summary>
|
||||
private sealed class ManualTimeProvider(DateTime startUtc) : TimeProvider
|
||||
{
|
||||
public DateTime UtcNow { get; set; } = startUtc;
|
||||
|
||||
public override DateTimeOffset GetUtcNow() => new(UtcNow, TimeSpan.Zero);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_store_records_its_local_receive_time_per_node()
|
||||
{
|
||||
var clock = new ManualTimeProvider(T0);
|
||||
var store = new InMemoryDriverStatusSnapshotStore(time: clock);
|
||||
store.Upsert(Snap("drv", "Healthy", T0.AddHours(-3), "central-1")); // peer clock far behind: irrelevant
|
||||
clock.UtcNow = T0.AddSeconds(7);
|
||||
store.Upsert(Snap("drv", "Healthy", T0.AddHours(-3), "central-2"));
|
||||
|
||||
var all = store.GetAllWithReceiveTime();
|
||||
all.Single(e => e.Snapshot.ReportingNodeId == "central-1").ReceivedUtc.ShouldBe(T0);
|
||||
all.Single(e => e.Snapshot.ReportingNodeId == "central-2").ReceivedUtc.ShouldBe(T0.AddSeconds(7));
|
||||
store.GetAll().Count.ShouldBe(2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_snapshot_the_store_received_long_before_the_dialog_opened_is_stale_at_once()
|
||||
{
|
||||
// The docker-dev repro: central-2 stopped 1m30s before the panel was reopened. It must dim immediately,
|
||||
// while central-1 (still publishing) stays live.
|
||||
var clock = new ManualTimeProvider(T0);
|
||||
var store = new InMemoryDriverStatusSnapshotStore(time: clock);
|
||||
store.Upsert(Snap("drv", "Healthy", T0, "central-2"));
|
||||
clock.UtcNow = T0.AddSeconds(88);
|
||||
store.Upsert(Snap("drv", "Healthy", T0.AddSeconds(88), "central-1"));
|
||||
|
||||
var openedAt = T0.AddSeconds(90);
|
||||
var view = new DriverStatusByNode("drv");
|
||||
view.Prime(store.GetAllWithReceiveTime(), openedAt, StaleAfter).ShouldBeTrue();
|
||||
|
||||
var entries = view.Entries;
|
||||
entries.Single(e => e.NodeId == "central-2").IsStale(openedAt, StaleAfter).ShouldBeTrue();
|
||||
entries.Single(e => e.NodeId == "central-1").IsStale(openedAt, StaleAfter).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_fresh_snapshot_loaded_on_open_is_not_stale_even_when_the_peer_clock_is_far_behind()
|
||||
{
|
||||
// Positive control + skew-proofing: the age comes from the store's LOCAL clock, so a publisher whose clock
|
||||
// is an hour behind does not dim a snapshot this process received a second ago.
|
||||
var clock = new ManualTimeProvider(T0);
|
||||
var store = new InMemoryDriverStatusSnapshotStore(time: clock);
|
||||
store.Upsert(Snap("drv", "Healthy", T0.AddHours(-1), "central-1"));
|
||||
|
||||
var openedAt = T0.AddSeconds(1);
|
||||
var view = new DriverStatusByNode("drv");
|
||||
view.Prime(store.GetAllWithReceiveTime(), openedAt, StaleAfter);
|
||||
|
||||
var entry = view.Entries.ShouldHaveSingleItem();
|
||||
entry.ReceivedUtc.ShouldBe(T0);
|
||||
entry.IsStale(openedAt, StaleAfter).ShouldBeFalse();
|
||||
entry.IsStale(T0.AddSeconds(31), StaleAfter).ShouldBeTrue(); // and it does age out on the normal schedule
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_live_update_after_priming_un_stales_the_node()
|
||||
{
|
||||
var view = new DriverStatusByNode("drv");
|
||||
view.Prime([new StoredDriverHealth(Snap("drv", "Healthy", T0, "central-2"), T0)], T0.AddSeconds(90), StaleAfter);
|
||||
view.Entries.Single().IsStale(T0.AddSeconds(90), StaleAfter).ShouldBeTrue();
|
||||
|
||||
view.Upsert(Snap("drv", "Healthy", T0.AddSeconds(91), "central-2"), T0.AddSeconds(91)).ShouldBeTrue();
|
||||
view.Entries.Single().IsStale(T0.AddSeconds(92), StaleAfter).ShouldBeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_store_receive_time_in_the_future_is_clamped_to_now()
|
||||
{
|
||||
var stored = new StoredDriverHealth(Snap("drv", "Healthy", T0, "central-1"), T0.AddMinutes(5));
|
||||
DriverStatusByNode.PrimedReceiveTime(stored, T0, StaleAfter).ShouldBe(T0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Without_a_store_receive_time_an_old_publish_seeds_the_age_and_a_recent_one_is_now()
|
||||
{
|
||||
var old = new StoredDriverHealth(Snap("drv", "Healthy", T0.AddSeconds(-90), "central-2"), null);
|
||||
DriverStatusByNode.PrimedReceiveTime(old, T0, StaleAfter).ShouldBe(T0.AddSeconds(-90));
|
||||
|
||||
// Positive control: a recent publish (or a peer clock running ahead) is treated as received now.
|
||||
var recent = new StoredDriverHealth(Snap("drv", "Healthy", T0.AddSeconds(-5), "central-1"), null);
|
||||
DriverStatusByNode.PrimedReceiveTime(recent, T0, StaleAfter).ShouldBe(T0);
|
||||
var ahead = new StoredDriverHealth(Snap("drv", "Healthy", T0.AddMinutes(10), "central-1"), null);
|
||||
DriverStatusByNode.PrimedReceiveTime(ahead, T0, StaleAfter).ShouldBe(T0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_default_interface_receive_time_is_null_for_a_store_that_does_not_track_one()
|
||||
{
|
||||
IDriverStatusSnapshotStore store = new GetAllOnlyStore([Snap("drv", "Healthy", T0, "central-1")]);
|
||||
store.GetAllWithReceiveTime().ShouldHaveSingleItem().ReceivedUtc.ShouldBeNull();
|
||||
}
|
||||
|
||||
private sealed class GetAllOnlyStore(IReadOnlyCollection<DriverHealthChanged> all) : IDriverStatusSnapshotStore
|
||||
{
|
||||
public event Action<DriverHealthChanged>? SnapshotChanged { add { } remove { } }
|
||||
public void Upsert(DriverHealthChanged snapshot) { }
|
||||
public bool TryGet(string driverInstanceId, out DriverHealthChanged snapshot) { snapshot = null!; return false; }
|
||||
public IReadOnlyCollection<DriverHealthChanged> GetAll() => all;
|
||||
public int EvictUnconfiguredStale(IReadOnlySet<string> configuredInstanceIds, DateTime olderThanUtc) => 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user