e967042fbb
#995 follow-up 1: OpcUaPublishActor still SHA-256'd every rebuild's artifact once on its pinned thread (WithArtifactRevision). DriverHostActor already hashes the same bytes for its catalog attempt, so it now stamps RebuildAddressSpace.ArtifactRevision at all three send sites (deploy apply, boot-from-cache, bootstrap restore) from a per-blob ConditionalWeakTable memo (one hash per blob instance, no lifetime extension). The publish actor's fallback hash stays for unstamped senders and is observable through TestHooks.OnArtifactHashed. #995 follow-up 2: DependencyMuxConsumerRegistrationGuardTests scans src for every DependencyMuxActor.RegisterInterest sender; an unlisted one fails with the instruction to fold its refs into DriverHostActor.ConsumerRefs, and each listed replay-opting consumer's composition term must still appear in ConsumerRefs (mutation-checked). The historization recorder is the one exempt, non-replay registrant.
103 lines
5.7 KiB
C#
103 lines
5.7 KiB
C#
using System.Text.RegularExpressions;
|
|
using Shouldly;
|
|
using Xunit;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.Drivers;
|
|
|
|
/// <summary>
|
|
/// Gitea #1054 (#995 follow-up). Since #995 R-11 a deploy skips re-subscribing a kept driver whose seeding fingerprint
|
|
/// is unchanged, unless one of its value refs ENTERED <c>DriverHostActor.ConsumerRefs</c> — the set of refs some mux
|
|
/// value consumer reads. The mux replays a retained value only for refs a replay-opting subscriber already named, so
|
|
/// a NEW kind of consumer that registers replay interest with the mux but is not folded into <c>ConsumerRefs</c> gets
|
|
/// no first value on its first deploy: it waits for the next value change, which for a static input never comes.
|
|
/// <para>
|
|
/// This guard makes that rule structural. Every production site that sends
|
|
/// <c>DependencyMuxActor.RegisterInterest</c> must be a known consumer, and every known replay-opting consumer must
|
|
/// have its composition term in <c>ConsumerRefs</c>. A new site fails here with the instruction to do both.
|
|
/// </para>
|
|
/// </summary>
|
|
public sealed class DependencyMuxConsumerRegistrationGuardTests
|
|
{
|
|
/// <summary>
|
|
/// Every production registrant, keyed by file name, with the token its refs appear under in
|
|
/// <c>DriverHostActor.ConsumerRefs</c> — or null for a registrant that deliberately does NOT need a seed.
|
|
/// </summary>
|
|
private static readonly IReadOnlyDictionary<string, string?> KnownRegistrants = new Dictionary<string, string?>(StringComparer.Ordinal)
|
|
{
|
|
// Enabled VirtualTags' DependencyRefs.
|
|
["VirtualTagActor.cs"] = "composition.EquipmentVirtualTags",
|
|
// Enabled scripted alarms' DependencyRefs.
|
|
["ScriptedAlarmHostActor.cs"] = "composition.EquipmentScriptedAlarms",
|
|
// Every hosted IDependencyConsumer driver (Calculation) — the adapter registers on its behalf.
|
|
["DependencyConsumerMuxAdapter.cs"] = "_depConsumers",
|
|
// The historization recorder registers WITHOUT replay and records changes only: a seed would be a duplicate
|
|
// sample, and the mux retains nothing on its behalf. Exempt by design (#995 R-11).
|
|
["ContinuousHistorizationRecorder.cs"] = null,
|
|
};
|
|
|
|
private static readonly Regex Registration = new(@"new\s+(?:DependencyMuxActor\.)?RegisterInterest\s*\(", RegexOptions.Compiled);
|
|
|
|
[Fact]
|
|
public void Every_mux_registrant_is_known_and_every_replay_registrant_feeds_ConsumerRefs()
|
|
{
|
|
var src = Path.Combine(RepoRoot().FullName, "src");
|
|
var registrants = Directory.EnumerateFiles(src, "*.cs", SearchOption.AllDirectories)
|
|
.Where(f => !IsBuildOutput(f))
|
|
.Where(f => Path.GetFileName(f) != "DependencyMuxActor.cs") // the record's own declaration
|
|
.Where(f => Registration.IsMatch(StripComments(File.ReadAllText(f))))
|
|
.Select(Path.GetFileName)
|
|
.ToHashSet(StringComparer.Ordinal);
|
|
|
|
// Positive control: the scan really finds the registrants it is supposed to police.
|
|
registrants.ShouldContain("VirtualTagActor.cs", "the source scan found no registrants at all — the guard would pass vacuously");
|
|
|
|
foreach (var file in registrants)
|
|
KnownRegistrants.ContainsKey(file!).ShouldBeTrue(
|
|
$"{file} registers interest with the dependency mux but is not a known consumer. If it reads driver values " +
|
|
"and needs a first value, fold its refs into DriverHostActor.ConsumerRefs (else a deploy that skips the " +
|
|
"kept driver's re-subscribe leaves it waiting for the next value change), then list it here.");
|
|
|
|
var consumerRefs = ConsumerRefsBody();
|
|
foreach (var (file, token) in KnownRegistrants)
|
|
{
|
|
registrants.ShouldContain(file, $"{file} no longer registers with the mux — drop it from this guard's table");
|
|
if (token is not null)
|
|
consumerRefs.ShouldContain(token, Case.Sensitive,
|
|
$"{file} is a replay-opting mux consumer, but DriverHostActor.ConsumerRefs no longer reads {token}");
|
|
}
|
|
}
|
|
|
|
/// <summary>The body of <c>DriverHostActor.ConsumerRefs</c>, comments stripped.</summary>
|
|
private static string ConsumerRefsBody()
|
|
{
|
|
var path = Directory.EnumerateFiles(Path.Combine(RepoRoot().FullName, "src"), "DriverHostActor.cs", SearchOption.AllDirectories)
|
|
.Single(f => !IsBuildOutput(f));
|
|
var text = StripComments(File.ReadAllText(path));
|
|
var start = text.IndexOf("HashSet<string> ConsumerRefs(AddressSpaceComposition composition)", StringComparison.Ordinal);
|
|
start.ShouldBeGreaterThanOrEqualTo(0, "DriverHostActor.ConsumerRefs not found — update this guard to its new shape");
|
|
var open = text.IndexOf('{', start);
|
|
var depth = 0;
|
|
for (var i = open; i < text.Length; i++)
|
|
{
|
|
if (text[i] == '{') depth++;
|
|
else if (text[i] == '}' && --depth == 0) return text[open..(i + 1)];
|
|
}
|
|
throw new InvalidOperationException("unbalanced braces in DriverHostActor.ConsumerRefs");
|
|
}
|
|
|
|
private static string StripComments(string code) =>
|
|
Regex.Replace(code, @"//[^\n]*|/\*.*?\*/", string.Empty, RegexOptions.Singleline);
|
|
|
|
private static DirectoryInfo RepoRoot()
|
|
{
|
|
var root = new DirectoryInfo(AppContext.BaseDirectory);
|
|
while (root is not null && !File.Exists(Path.Combine(root.FullName, "ZB.MOM.WW.OtOpcUa.slnx"))) root = root.Parent;
|
|
root.ShouldNotBeNull("could not find the repo root");
|
|
return root;
|
|
}
|
|
|
|
private static bool IsBuildOutput(string file) =>
|
|
file.Contains($"{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}") ||
|
|
file.Contains($"{Path.DirectorySeparatorChar}obj{Path.DirectorySeparatorChar}");
|
|
}
|