test(grpc-data): A4 review r2 NITs — F4 ungranted caller is refused without promoting the folder (fake gate honours ConditionBearing; control is BadUserAccessDenied), EventId retention across a held-set takeover

Adds the ProjectedFolderEventNotifier test observation.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-11-25 04:07:19 -05:00
parent d72f2d966d
commit 6bdca132fe
3 changed files with 67 additions and 3 deletions
@@ -709,6 +709,14 @@ public sealed partial class OtOpcUaNodeManager : IProjectedConditionSink
IsImportedConditionId(nodeId) ? AuthorizeImportedCondition(identity, operation, nodeId) : null;
/// <summary>Test observation: whether a projected folder is currently wired as a notifier of some imported condition.</summary>
/// <summary>Test observation (A4 review r2): a projected folder's <c>EventNotifier</c> (null when it is no known projected folder).</summary>
internal byte? ProjectedFolderEventNotifier(NodeId folderId)
{
lock (Lock)
return _importedConditions.FolderKeys.TryGetValue(folderId, out var key) && FindProjectedFolder(key) is { } folder
? folder.EventNotifier : null;
}
internal bool IsImportedNotifierWired(NodeId folderId)
{
lock (Lock) return _importedConditions.WiredByFolder.ContainsKey(folderId);
@@ -342,21 +342,39 @@ public sealed class ImportedConditionEventWireTests
var f = Fixture.Install(rig, access);
using var granted = await rig.ConnectAsync(anonymous: false, ct);
// Control (the defect's precondition): while the mapping is not condition-bearing the folder is no notifier — refused.
// Control (the defect's precondition): while the mapping is not condition-bearing even a granted caller is refused — by
// the mapping gate, as the real one does (review r2 NIT: BadUserAccessDenied, not the SDK's BadNotSupported).
var early = await SubscribeAsync(granted, new Events(), ct);
var refused = AddItem(early, f.AnchorB);
await early.ApplyChangesAsync(ct);
(refused.Status.Error?.StatusCode ?? StatusCodes.Good).Code.ShouldBe(StatusCodes.BadNotSupported.Code, "not condition-bearing yet");
(refused.Status.Error?.StatusCode ?? StatusCodes.Good).Code.ShouldBe(StatusCodes.BadUserAccessDenied.Code, "not condition-bearing yet");
access.ConditionBearing = true; // the authority activates after materialisation (first barrier of a restarted consumer)
// Review r2 NIT, negative case: an UNGRANTED caller is refused by the mapping gate before any promotion — the folder's
// EventNotifier is unchanged, so a refusal cannot be used to flip (or probe) notifier state.
using var ungranted = await rig.ConnectAsync(anonymous: true, ct);
var before = EventNotifier(rig, f.AnchorB);
(before & EventNotifiers.SubscribeToEvents).ShouldBe((byte)0, "precondition: not yet promoted");
var deniedSub = await SubscribeAsync(ungranted, new Events(), ct);
var denied = AddItem(deniedSub, f.AnchorB);
await deniedSub.ApplyChangesAsync(ct);
denied.Status.Error?.StatusCode.Code.ShouldBe(StatusCodes.BadUserAccessDenied.Code);
(EventNotifier(rig, f.AnchorB)).ShouldBe(before, "an ungranted caller never promotes the folder");
access.Outstanding.ShouldBe(0, "a refused caller opens no condition demand");
var events = new Events();
var sub = await SubscribeAsync(granted, events, ct);
var item = AddItem(sub, f.AnchorB);
await sub.ApplyChangesAsync(ct);
(item.Status.Error?.StatusCode ?? StatusCodes.Good).Code.ShouldBe(StatusCodes.Good.Code, "folder-first item admitted");
access.Outstanding.ShouldBe(1, "the folder-only client opened condition demand");
(EventNotifier(rig, f.AnchorB) & EventNotifiers.SubscribeToEvents).ShouldNotBe((byte)0,
"positive control: the granted caller's admission promoted it");
f.Apply(Update(Key2, version: 3, active: true, acked: false, [f.BPlacement()], message: "arrived-f4"));
await Events.WaitUntilAsync(() => events.Of(item).Any(e => Message(e) == "arrived-f4"));
static byte EventNotifier(ServerEventRig rig, NodeId folder) => rig.Nm.ProjectedFolderEventNotifier(folder).ShouldNotBeNull();
}
[Fact]
@@ -484,8 +502,9 @@ public sealed class ImportedConditionEventWireTests
public bool CanSee(RoleCarryingUserIdentity? caller, IReadOnlyList<ImportedConditionPlacement> placements) =>
caller is not null && Allowed.Contains(caller.Principal);
public long AuthorityGeneration => Interlocked.Read(ref Generation);
/// <remarks>Review r2 NIT: like the real mapping gate, a mapping that is not condition-bearing admits nobody.</remarks>
public bool CanSubscribeFolder(RoleCarryingUserIdentity? caller, string mappingId, string folderLocalSubject) =>
caller is not null && Allowed.Contains(caller.Principal);
caller is not null && ConditionBearing && Allowed.Contains(caller.Principal);
/// <summary>A4 F4: false models a mapping whose authority was not yet active when its folders materialised.</summary>
public volatile bool ConditionBearing = true;
public bool IsConditionBearingMapping(string mappingId) => ConditionBearing;
@@ -164,6 +164,43 @@ public sealed class ImportedConditionMethodWireTests
_ = refresh; // review S6: the RefreshRequired itself is pinned over the wire by A_resync_that_cannot_re_bind_…
}
/// <summary>
/// A4 review r2 NIT (F1 EventId retention across a held-set takeover, previously only live-gated by A03): the consumer
/// holds its conditions across its own deploy and the next runner re-snapshots ONTO the same nodes. What reaches the
/// node manager is a restart of the stream's versions with the same marker — and, after an origin reload restatement
/// (#935), a new record time. The condition instance, its current EventId and every older still-retained EventId stay
/// bound, no event is reported; the takeover's RefreshRequired is not a condition event either.
/// </summary>
[Fact]
public async Task R2_a_takeover_re_snapshot_with_the_same_marker_keeps_the_instance_and_every_retained_EventId()
{
var ct = TestContext.Current.CancellationToken;
await using var rig = await ServerEventRig.StartAsync("a4-r2-takeover", encrypted: false, ct);
var f = Fixture.Install(rig);
var t0 = DateTimeOffset.UtcNow.AddMinutes(-1);
f.Apply(Update(version: 3, active: true, acked: false, time: t0, message: "first"));
var older = f.CurrentEventId();
f.Apply(Update(version: 4, active: true, acked: false, time: t0.AddSeconds(1), message: "second"));
var current = f.CurrentEventId();
current.ShouldNotBe(older, "positive control: a real transition mints a new EventId");
var instance = rig.Nm.FindImportedCondition(Connection, Key1);
var reported = rig.Nm.ImportedEventsReported;
// The takeover runner's first snapshot: versions restart (new stream), same marker, a restated (newer) record time.
f.Apply(Update(version: 1, active: true, acked: false, time: t0.AddSeconds(30), message: "second"));
rig.Nm.RequireRefresh(Connection);
ReferenceEquals(rig.Nm.FindImportedCondition(Connection, Key1), instance).ShouldBeTrue("the same condition object — not removed and re-created");
f.CurrentEventId().ShouldBe(current, "no event, no new EventId");
rig.Nm.ImportedEventsReported.ShouldBe(reported, "an unchanged occurrence reports nothing");
using var session = await rig.ConnectAsync(anonymous: false, ct);
var id = f.ConditionId(session);
(await CallAsync(session, id, MethodIds.AcknowledgeableConditionType_Acknowledge, older, "older", ct)).ShouldBe(StatusCodes.Good);
(await CallAsync(session, id, MethodIds.AcknowledgeableConditionType_Acknowledge, current, "current", ct)).ShouldBe(StatusCodes.Good);
f.Port.Calls.Count.ShouldBe(2);
f.Port.Calls.ShouldAllBe(c => c.ExpectedActivation == Marker1);
}
/// <summary>
/// §5.6 step 6 / §9.3 L3 (hard gate): the origin's method handler stalls 5 s. Read, Browse and a data Publish on the
/// consumer complete in &lt; 200 ms meanwhile, the node manager's <c>Lock</c> is free, and no acknowledged state is