fix(client): #1048 Client.UI recursive subscribe + restore batch through SubscribeManyAsync; CLI subscribe/alarms exit 3 when a failover never recovers

- SubscriptionsViewModel collects the subtree's variables first (deduped, already-subscribed
  skipped) and creates them in one SubscribeManyAsync; per-node failures are named in
  StatusMessage, a whole-batch failure adds nothing. RestoreSubscriptionsAsync batches too.
- CLI: ConnectionLossWatch ends subscribe/alarms when the service lands Disconnected on its
  own, or stays out of Connected past --reconnect-timeout (default 300 s, 0 = no limit):
  WARNING line, summary (subscribe adds 'Connection: LOST'), exit code 3. A recovery inside
  the window re-arms the watch.
This commit is contained in:
Joseph Doherty
2026-12-03 15:07:30 -05:00
parent 65212dd471
commit 9496db9357
10 changed files with 471 additions and 37 deletions
@@ -272,7 +272,10 @@ public class SubscriptionsViewModelTests
await _vm.RestoreSubscriptionsAsync(["ns=2;s=A", "ns=2;s=B"]);
_vm.ActiveSubscriptions.Count.ShouldBe(2);
_service.SubscribeCallCount.ShouldBe(2);
// #1048: one batched round trip for the whole saved set.
_service.SubscribeManyCallCount.ShouldBe(1);
_service.SubscribeManyBatches[0].ShouldBe(["ns=2;s=A", "ns=2;s=B"]);
_service.SubscribeCallCount.ShouldBe(0);
}
/// <summary>Verifies that ValidateAndWriteAsync returns true on successful write.</summary>
@@ -359,7 +362,54 @@ public class SubscriptionsViewModelTests
await _vm.AddSubscriptionRecursiveAsync("ns=2;s=Folder", "Object");
_vm.ActiveSubscriptions.Count.ShouldBe(2);
_service.SubscribeCallCount.ShouldBe(2);
// #1048: the whole collected set is one batched SubscribeManyAsync, not a round trip per node.
_service.SubscribeManyCallCount.ShouldBe(1);
_service.SubscribeManyBatches[0].ShouldBe(["ns=2;s=Child1", "ns=2;s=Child2"]);
_service.SubscribeCallCount.ShouldBe(0);
}
/// <summary>
/// #1048: a per-node rejection inside the batch adds only the nodes that subscribed and names the failure;
/// nodes already subscribed and duplicates reached twice in the walk are not sent again.
/// </summary>
[Fact]
public async Task AddSubscriptionRecursiveAsync_BatchReportsPerNodeFailuresAndSkipsAlreadySubscribed()
{
_vm.IsConnected = true;
await _vm.AddSubscriptionForNodeAsync("ns=2;s=Already");
_service.BrowseResultsByParent["ns=2;s=Root"] =
[
new BrowseResult("ns=2;s=Already", "Already", "Variable", false),
new BrowseResult("ns=2;s=Good", "Good", "Variable", false),
new BrowseResult("ns=2;s=Bad", "Bad", "Variable", false),
new BrowseResult("ns=2;s=Sub", "Sub", "Object", true),
];
_service.BrowseResultsByParent["ns=2;s=Sub"] = [new BrowseResult("ns=2;s=Good", "Good", "Variable", false)];
_service.SubscribeManyNodeFailures["ns=2;s=Bad"] = new Exception("BadNodeIdUnknown");
await _vm.AddSubscriptionRecursiveAsync("ns=2;s=Root", "Object");
_service.SubscribeManyBatches.ShouldHaveSingleItem().ShouldBe(["ns=2;s=Good", "ns=2;s=Bad"]);
_vm.ActiveSubscriptions.Select(s => s.NodeId).ShouldBe(["ns=2;s=Already", "ns=2;s=Good"]);
_vm.SubscriptionCount.ShouldBe(2);
_vm.StatusMessage.ShouldNotBeNull();
_vm.StatusMessage.ShouldContain("1 of 2");
_vm.StatusMessage.ShouldContain("ns=2;s=Bad");
_vm.StatusMessage.ShouldContain("BadNodeIdUnknown");
}
/// <summary>#1048: a batch that fails as a whole adds nothing and surfaces the error.</summary>
[Fact]
public async Task AddSubscriptionRecursiveAsync_WholeBatchFailure_SurfacesStatusMessage()
{
_vm.IsConnected = true;
_service.BrowseResultsByParent["ns=2;s=Root"] = [new BrowseResult("ns=2;s=V", "V", "Variable", false)];
_service.SubscribeException = new Exception("session closed");
await _vm.AddSubscriptionRecursiveAsync("ns=2;s=Root", "Object");
_vm.ActiveSubscriptions.ShouldBeEmpty();
_vm.StatusMessage.ShouldNotBeNull().ShouldContain("session closed");
}
/// <summary>