mbproxy: fix the dashboard's C2/M-series review findings
Closes the on-demand-capture leak cluster from the code review. The capture's armed state was driven off SignalR's ConnectionId, which changes on every transport reconnect, so a reconnect-during-view leaked a subscriber and left the capture armed forever with no viewer. PlcSubscriptionTracker now keys on a stable per-page-load tabId, and StatusBroadcaster reconciles capture arm state from the live viewer set each push cycle — making arming single-threaded and reconnect-safe. Also fixes the TagValueCapture disarm-vs-record race, the bind-failure broadcaster/listener leak, removes dead JSON-context code, and reworks the frontend cold-start retry plus an unknown-PLC watchdog. Adds tracker / broadcaster-loop / race / wire-shape test coverage. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -7,8 +7,9 @@ using Xunit;
|
||||
namespace Mbproxy.Tests.Proxy;
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for <see cref="TagCaptureRegistry"/> — the shared seam that arms and
|
||||
/// disarms per-PLC <see cref="TagValueCapture"/> instances.
|
||||
/// Unit tests for <see cref="TagCaptureRegistry"/> — the shared seam holding per-PLC
|
||||
/// <see cref="TagValueCapture"/> instances. Arm state is reconciled in bulk against the
|
||||
/// live viewer set (not toggled per PLC) so the broadcaster is the single authority.
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class TagCaptureRegistryTests
|
||||
@@ -25,48 +26,69 @@ public sealed class TagCaptureRegistryTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetOrCreate_ReturnsSameInstance_OnRepeatCall_WhenTagSetUnchanged()
|
||||
public void GetOrCreate_ReturnsLiveInstance_OnRepeatCall()
|
||||
{
|
||||
var registry = new TagCaptureRegistry();
|
||||
var first = registry.GetOrCreate("plc-1", Map((100, 16)));
|
||||
registry.GetOrCreate("plc-1", Map((100, 16)));
|
||||
var second = registry.GetOrCreate("plc-1", Map((100, 16)));
|
||||
|
||||
// AddOrUpdate's update path rebuilds; both must be live and consistent.
|
||||
second.TagCount.ShouldBe(1);
|
||||
registry.TryGet("plc-1", out var current).ShouldBeTrue();
|
||||
current.ShouldBeSameAs(second);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetOrCreate_Rebuild_PreservesArmedFlag()
|
||||
public void GetOrCreate_Rebuild_ProducesDisarmedCapture_AndReconcileReArms()
|
||||
{
|
||||
// The rebuilt capture is intentionally disarmed: ReconcileArmed re-arms it within
|
||||
// one push cycle if the PLC still has a viewer, so arm state is never carried
|
||||
// across the rebuild — which removes any arm-vs-rebuild race.
|
||||
var registry = new TagCaptureRegistry();
|
||||
var capture = registry.GetOrCreate("plc-1", Map((100, 16)));
|
||||
capture.Arm();
|
||||
registry.GetOrCreate("plc-1", Map((100, 16)));
|
||||
registry.ReconcileArmed(["plc-1"]);
|
||||
registry.TryGet("plc-1", out var armed).ShouldBeTrue();
|
||||
armed.IsArmed.ShouldBeTrue();
|
||||
|
||||
// Hot-reload reseat: same PLC, changed tag set.
|
||||
var rebuilt = registry.GetOrCreate("plc-1", Map((100, 16), (200, 32)));
|
||||
|
||||
rebuilt.ShouldNotBeSameAs(capture);
|
||||
rebuilt.IsArmed.ShouldBeTrue("a rebuilt capture must keep capturing for an open detail page");
|
||||
rebuilt.ShouldNotBeSameAs(armed);
|
||||
rebuilt.IsArmed.ShouldBeFalse("a rebuilt capture starts disarmed");
|
||||
rebuilt.TagCount.ShouldBe(2);
|
||||
|
||||
// The next reconcile re-arms it because the PLC is still viewed.
|
||||
registry.ReconcileArmed(["plc-1"]);
|
||||
rebuilt.IsArmed.ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Arm_And_Disarm_ReachTheRightCapture()
|
||||
public void ReconcileArmed_ArmsActivePlcs_DisarmsTheRest()
|
||||
{
|
||||
var registry = new TagCaptureRegistry();
|
||||
registry.GetOrCreate("plc-1", Map((100, 16)));
|
||||
registry.GetOrCreate("plc-2", Map((100, 16)));
|
||||
|
||||
registry.Arm("plc-1");
|
||||
|
||||
registry.ReconcileArmed(["plc-1"]);
|
||||
registry.TryGet("plc-1", out var c1).ShouldBeTrue();
|
||||
registry.TryGet("plc-2", out var c2).ShouldBeTrue();
|
||||
c1.IsArmed.ShouldBeTrue();
|
||||
c2.IsArmed.ShouldBeFalse();
|
||||
|
||||
registry.Disarm("plc-1");
|
||||
// plc-1's viewer leaves, plc-2 gains one.
|
||||
registry.ReconcileArmed(["plc-2"]);
|
||||
c1.IsArmed.ShouldBeFalse();
|
||||
c2.IsArmed.ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReconcileArmed_EmptyActiveSet_DisarmsEverything()
|
||||
{
|
||||
var registry = new TagCaptureRegistry();
|
||||
registry.GetOrCreate("plc-1", Map((100, 16)));
|
||||
registry.ReconcileArmed(["plc-1"]);
|
||||
|
||||
registry.ReconcileArmed(Array.Empty<string>());
|
||||
|
||||
registry.TryGet("plc-1", out var c1).ShouldBeTrue();
|
||||
c1.IsArmed.ShouldBeFalse();
|
||||
}
|
||||
|
||||
@@ -76,8 +98,7 @@ public sealed class TagCaptureRegistryTests
|
||||
var registry = new TagCaptureRegistry();
|
||||
registry.GetOrCreate("plc-1", Map((100, 16)));
|
||||
registry.GetOrCreate("plc-2", Map((100, 16)));
|
||||
registry.Arm("plc-1");
|
||||
registry.Arm("plc-2");
|
||||
registry.ReconcileArmed(["plc-1", "plc-2"]);
|
||||
|
||||
registry.DisarmAll();
|
||||
|
||||
@@ -92,8 +113,7 @@ public sealed class TagCaptureRegistryTests
|
||||
{
|
||||
var registry = new TagCaptureRegistry();
|
||||
|
||||
Should.NotThrow(() => registry.Arm("ghost"));
|
||||
Should.NotThrow(() => registry.Disarm("ghost"));
|
||||
Should.NotThrow(() => registry.ReconcileArmed(["ghost"]));
|
||||
Should.NotThrow(() => registry.Remove("ghost"));
|
||||
registry.TryGet("ghost", out _).ShouldBeFalse();
|
||||
}
|
||||
|
||||
@@ -174,4 +174,38 @@ public sealed class TagValueCaptureTests
|
||||
await Task.WhenAll([.. writers, reader]);
|
||||
tornObserved.ShouldBeFalse("Snapshot must never observe a torn (half-updated) slot");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ConcurrentRecordAndDisarm_LeavesNoStaleObservation()
|
||||
{
|
||||
// M7 regression: Record() checks _armed then writes; Disarm() flips _armed then
|
||||
// clears the slots. A Record that passes the check while armed, then has Disarm
|
||||
// run, then writes, would strand a stale observation on a disarmed capture —
|
||||
// breaking the "reopened page shows no stale data" contract. Record's re-check
|
||||
// after the write must undo that. The capture ends disarmed (the toggler's last
|
||||
// op is Disarm), so a clean Snapshot is a deterministic post-condition of the fix.
|
||||
var capture = Make((100, 16));
|
||||
var ct = TestContext.Current.CancellationToken;
|
||||
|
||||
var recorder = Task.Run(() =>
|
||||
{
|
||||
for (int i = 0; i < 400_000; i++)
|
||||
capture.Record(100, 0x1234, 0, 1234, CaptureDirection.Read);
|
||||
}, ct);
|
||||
|
||||
var toggler = Task.Run(() =>
|
||||
{
|
||||
for (int i = 0; i < 80_000; i++)
|
||||
{
|
||||
capture.Arm();
|
||||
capture.Disarm();
|
||||
}
|
||||
}, ct);
|
||||
|
||||
await Task.WhenAll(recorder, toggler);
|
||||
|
||||
capture.IsArmed.ShouldBeFalse();
|
||||
capture.Snapshot().ShouldAllBe(s => s.UpdatedAtUtc == null,
|
||||
"a disarmed capture must never retain a recorded observation");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user