using System.Reflection; using System.Threading.Channels; using Opc.Ua; using Opc.Ua.Client; using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; using ZB.MOM.WW.OtOpcUa.OpcUaServer; using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections; namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections; public sealed partial class GrpcDataConfiguredDiscoveryWireTests { [Fact(Timeout = 90000), Trait("Phase", "P11Wire")] public async Task Suppressed_sdk_item_receives_fresh_snapshot_after_held_metadata_renewal_without_another_revalidation() { var ct = TestContext.Current.CancellationToken; LocalDataFixture? peer = null; var subscriptions = new FakeSubscriptions(() => peer!.Serving); using var origin = new LocalDataFixture(servingGeneration: 1, targetSubscriptions: (_, _) => subscriptions); peer = origin; subscriptions.SampleClock = origin.Clock; var published = Channel.CreateUnbounded(); var renewalEntered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); using var releaseRenewal = new ManualResetEventSlim(); LocalCatalogCoordinator? coordinator = null; try { await WithComposedServer(origin, origin.Raw, async (session, sink, _, _, _, placements, anchor) => { var manager = (OtOpcUaNodeManager)typeof(SdkAddressSpaceSink) .GetField("_nodeManager", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(sink)!; var suppressed = typeof(OtOpcUaNodeManager).GetField("_suppressedItems", BindingFlags.Instance | BindingFlags.NonPublic)!; var client = (SessionClient)session; await BrowseAll(client, anchor, 1); var subscription = (await client.CreateSubscriptionAsync(new RequestHeader(), 100, 300, 1, 0, true, 0, ct)).SubscriptionId; await AddComposedItem(client, subscription, anchor, 1, 100, ct); await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct); var original = await NextPublished(published.Reader, x => x.IsCompleteSnapshot && Equals(x.Snapshot.Value, 7), ct); (await PublishComposedValue(client, 1, ct)).StatusCode.ShouldBe(StatusCodes.Good); var replacement = Prepare(coordinator!); manager.RevalidateMonitoredItems(); ((int)suppressed.GetValue(manager)!).ShouldBe(1, "the old authorized item must be detached while its proof is withdrawn"); original.SourceProof.IsValid.ShouldBeFalse(); var denied = await PublishComposedValue(client, 1, ct); StatusCode.IsBad(denied.StatusCode).ShouldBeTrue(); var mapping = Mapping(origin.Raw); mapping.AllowedOperations |= DataOperations.Read | DataOperations.Metadata; placements.Stage(replacement, [mapping], [Connection()]); coordinator!.Complete(replacement, true).ShouldBeTrue(); subscriptions.SnapshotVersion = 2; subscriptions.SnapshotValue = 8; origin.Clock.Advance(TimeSpan.FromSeconds(5)); await renewalEntered.Task.WaitAsync(TimeSpan.FromSeconds(10), ct); ((int)suppressed.GetValue(manager)!).ShouldBe(1, "restored authority alone precedes the held metadata renewal"); releaseRenewal.Set(); var fresh = await NextPublished(published.Reader, x => x.IsCompleteSnapshot && Equals(x.Snapshot.Value, 8), ct); fresh.SourceProof.IsValid.ShouldBeTrue(); original.SourceProof.IsValid.ShouldBeFalse(); // No explicit revalidation or browse after restoration. The original item must resume. origin.Clock.Advance(TimeSpan.FromMilliseconds(100)); var recovered = await PublishComposedValue(client, 1, ct); recovered.StatusCode.ShouldBe(StatusCodes.Good); recovered.WrappedValue.AsBoxedObject(Variant.BoxingBehavior.Legacy).ShouldBe(8); }, onPublished: update => published.Writer.TryWrite(update), onCoordinator: value => coordinator = value, renewalSinkFactory: inner => new HeldRenewalSink(inner, renewalEntered, releaseRenewal)); } finally { releaseRenewal.Set(); } } private sealed class HeldRenewalSink(IProjectedNamespacePageSink inner, TaskCompletionSource entered, ManualResetEventSlim release) : IProjectedNamespacePageSink { public bool TryApply(NodeId parent, IReadOnlyList nodes, Func commit) => inner.TryApply(parent, nodes, commit); public bool TryRemove(NodeId node, ProjectedVariableDefinition expected) => inner.TryRemove(node, expected); public bool TryApplyRootLeaf(NodeId node, ProjectedVariableDefinition definition, Func commit) => inner.TryApplyRootLeaf(node, definition, commit); public bool TryRenewExact(NodeId node, ProjectedVariableDefinition expected, ProjectedVariableDefinition replacement, Func commit) { entered.TrySetResult(); if (!release.Wait(TimeSpan.FromSeconds(15))) return false; return inner.TryRenewExact(node, expected, replacement, commit); } } }