From 27712eef3a0e7e1b25e9114672cf84b34ef09b71 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Sat, 5 Dec 2026 00:11:41 -0500 Subject: [PATCH 1/2] test(harness): #1111 guard AllHostTopics against missing DPS Subscribe topics; document lazy-topic convergence wait --- .../PubSubTopicCoverageGuardTests.cs | 97 +++++++++++++++++++ .../TwoNodeClusterHarness.cs | 10 +- 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/PubSubTopicCoverageGuardTests.cs diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/PubSubTopicCoverageGuardTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/PubSubTopicCoverageGuardTests.cs new file mode 100644 index 000000000..35b786e52 --- /dev/null +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/PubSubTopicCoverageGuardTests.cs @@ -0,0 +1,97 @@ +using System.Text.RegularExpressions; +using Shouldly; +using Xunit; + +namespace ZB.MOM.WW.OtOpcUa.Host.IntegrationTests; + +/// +/// lmxopcua#1111: guards (hand-kept, #1108) against drift. A host +/// DistributedPubSub topic missing from it means StartAsync/WaitForPubSubConvergedAsync cannot wait for it +/// and a test publishing on it has the #1106 lost-publish window. +/// +/// +/// Approach (source scan, no reflection - topics live in many assemblies and some are instance constants): +/// (1) scan every src/**/*.cs for new Subscribe(<first-arg>, ; the first argument is the topic. Calls whose +/// first argument is not topic-shaped (a string literal, or a PascalCase identifier / dotted path such as +/// DeploymentsTopic, Foo.BarTopic) are skipped - this excludes the unrelated +/// new Subscribe(_desiredRefs, ...) driver message. (2) Resolve the argument to its string value by parsing +/// every const string X = "lit" | Other.Y; in src (keyed by enclosing class + name, chased transitively; +/// a bare name resolves in the file's first type, a dotted A.B.X via class B). (3) Compare the values with +/// AllHostTopics names. An unresolvable argument FAILS the test (so a new spelling cannot silently pass). +/// Non-DPS new Subscribe( uses with a topic-shaped first arg would need adding to the allowlist below. +/// +public sealed class PubSubTopicCoverageGuardTests +{ + private static readonly Regex ClassRx = new(@"\b(?:class|record|struct)\s+(\w+)", RegexOptions.Compiled); + private static readonly Regex ConstRx = new( + @"\bconst\s+string\s+(\w+)\s*=\s*(""[^""]*""|[\w.]+)\s*;", RegexOptions.Compiled); + private static readonly Regex SubscribeRx = new( + @"new\s+Subscribe\(\s*(""[^""]*""|[A-Z][\w.]*)\s*,", RegexOptions.Compiled); + + [Fact] + public void EveryDistributedPubSubSubscribeTopic_IsInHarnessAllHostTopics() + { + var srcRoot = Path.Combine(SolutionRoot(), "src"); + var files = Directory.EnumerateFiles(srcRoot, "*.cs", SearchOption.AllDirectories) + .Where(f => !f.Contains($"{Path.DirectorySeparatorChar}obj{Path.DirectorySeparatorChar}") + && !f.Contains($"{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}")) + .ToList(); + + // (class, const) -> raw RHS ; also const-name -> RHS list for fallbacks is deliberately NOT used (ambiguous). + var consts = new Dictionary<(string Cls, string Name), string>(); + var calls = new List<(string File, string Cls, string Arg)>(); + foreach (var file in files) + { + var cls = ""; + foreach (var line in File.ReadLines(file)) + { + var cm = ClassRx.Match(line); + // First type in the file only: later (nested) types must not hijack the owner of a const / call site. + if (cls.Length == 0 && cm.Success && !line.TrimStart().StartsWith("//")) cls = cm.Groups[1].Value; + var k = ConstRx.Match(line); + if (k.Success) consts[(cls, k.Groups[1].Value)] = k.Groups[2].Value; + var sm = SubscribeRx.Match(line); + if (sm.Success) calls.Add((Path.GetFileName(file), cls, sm.Groups[1].Value)); + } + } + + calls.ShouldNotBeEmpty("scan found no `new Subscribe(` call sites - the regex or src root is wrong"); + + string? Resolve(string cls, string arg, int depth = 0) + { + if (depth > 8) return null; + if (arg.StartsWith('"')) return arg.Trim('"'); + var parts = arg.Split('.'); + var name = parts[^1]; + var owner = parts.Length >= 2 ? parts[^2] : cls; + return consts.TryGetValue((owner, name), out var rhs) ? Resolve(owner, rhs, depth + 1) : null; + } + + var unresolved = new List(); + var subscribed = new SortedDictionary(); + foreach (var (file, cls, arg) in calls) + { + var value = Resolve(cls, arg); + if (value is null) unresolved.Add($"{file}: Subscribe({arg}, ...)"); + else subscribed.TryAdd(value, $"{file}: Subscribe({arg}, ...)"); + } + + unresolved.ShouldBeEmpty("could not resolve these topic arguments to a const string; teach the guard"); + + var known = TwoNodeClusterHarness.AllHostTopics.Select(t => t.Name).ToHashSet(); + var missing = subscribed.Where(kv => !known.Contains(kv.Key)) + .Select(kv => $"\"{kv.Key}\" ({kv.Value})").ToList(); + missing.ShouldBeEmpty( + "host DistributedPubSub topics subscribed in src/ but absent from TwoNodeClusterHarness.AllHostTopics " + + "(#1108/#1111): add them with the right MinNodes/AtBoot"); + } + + private static string SolutionRoot() + { + var dir = new DirectoryInfo(AppContext.BaseDirectory); + while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "ZB.MOM.WW.OtOpcUa.slnx"))) + dir = dir.Parent; + dir.ShouldNotBeNull("Could not find solution root (ZB.MOM.WW.OtOpcUa.slnx)."); + return dir!.FullName; + } +} diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/TwoNodeClusterHarness.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/TwoNodeClusterHarness.cs index 82a6a3bc4..ac139f69a 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/TwoNodeClusterHarness.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests/TwoNodeClusterHarness.cs @@ -271,7 +271,15 @@ public sealed class TwoNodeClusterHarness : IAsyncDisposable /// lmxopcua#1108: every topic the host nodes subscribe to (the Subscribe(...) call sites under /// src/). waits for the subset by default /// (measured against the harness hosts: the others have no subscriber until something creates one). A new - /// subscribed topic must be added here or a test publishing on it has the #1106 lost-publish window. + /// subscribed topic must be added here or a test publishing on it has the #1106 lost-publish window + /// (PubSubTopicCoverageGuardTests fails when one is missing). + /// + /// The AtBoot: false topics (alarm-commands, opcua-endpoint-state, driver-health, + /// driver-resilience-status, script-logs) have no subscriber until something creates one. A test that spawns + /// such a subscriber and then publishes from the OTHER node must call + /// with that topic first. (#1111: no current test does - they + /// publish from the subscriber's own node, which is delivered locally without gossip.) + /// /// public static readonly IReadOnlyList AllHostTopics = [ From 7106cf23a3d96986953d7967af627c4a6ab4da73 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Sat, 5 Dec 2026 00:11:41 -0500 Subject: [PATCH 2/2] docs(historian): #1111 note client-fabricated Unimplemented exception to PermanentFail --- .claude/rules/historian.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.claude/rules/historian.md b/.claude/rules/historian.md index 5493863f4..1608c07ae 100644 --- a/.claude/rules/historian.md +++ b/.claude/rules/historian.md @@ -89,7 +89,7 @@ writers return `PermanentFail` only for failures that prove the EVENT bad (Gatew non-RPC exceptions retry; SQL: recognised data errors) — and BOTH isolate an unrecognised batch failure per event (Gateway `SendEvents` failing `Unknown`/`Internal`/`DataLoss`/unlisted/non-transport ⇒ single sends; only known-outage codes and transport faults retry the batch whole), else one such event at the queue head Acks nothing and stalls the whole queue (#976 review) — don't -reintroduce a dead-letter default "to avoid an infinite loop". **Only the node +reintroduce a dead-letter default "to avoid an infinite loop". ⚠️ **`Unimplemented` ⇒ PermanentFail / single-send only when the GATEWAY sent it (#1103/#1109):** a client- or proxy-fabricated status (e.g. "Bad gRPC response … 404" surfacing as `Unimplemented`, or any status carrying a transport `DebugException`) is treated as a systemic/transient outage — retry, never dead-letter, and not "the server lacks the RPC" (`IsClientFabricated` in `Driver.Historian.Gateway/GatewayAlarmHistorianWriter.cs`). **Only the node holding the Primary role drains** (a gated node reports `HistorianDrainState.NotPrimary`). The `AlarmHistorian` section carries only the `Enabled` gate + the queue knobs (`DrainIntervalSeconds`, `Capacity`, `DeadLetterRetentionDays`, `BatchSize`, `MaxAttempts`) — the downstream gateway connection