Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataSuppressedTombstoneWireTests.cs
T

67 lines
4.2 KiB
C#

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 Tombstoning_a_suppressed_projected_item_retires_health_and_demand_before_client_delete()
{
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<ProjectedValueUpdate>();
LocalCatalogCoordinator? coordinator = null;
await WithComposedServer(origin, origin.Raw, async (session, sink, _, _, _, _, anchor) =>
{
var client = (SessionClient)session;
await BrowseAll(client, anchor, 1);
var sub = (await client.CreateSubscriptionAsync(new Opc.Ua.RequestHeader(), 100, 300, 1, 0, true, 0, ct)).SubscriptionId;
await AddComposedItem(client, sub, anchor, 1, 100, ct);
await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
await NextPublished(published.Reader, x => x.IsCompleteSnapshot, ct);
(await PublishComposedValue(client, 1, ct)).StatusCode.ShouldBe(Opc.Ua.StatusCodes.Good);
const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
var manager = (OtOpcUaNodeManager)typeof(SdkAddressSpaceSink).GetField("_nodeManager", flags)!.GetValue(sink)!;
var live = manager.GetType().GetField("_liveItems", flags)!.GetValue(manager)!;
var keys = (System.Collections.IEnumerable)live.GetType().GetProperty("Keys")!.GetValue(live)!;
var item = keys.Cast<object>().OfType<Opc.Ua.Server.MonitoredItem>().Single(x => x.ClientHandle == 1);
var health = (System.Collections.IDictionary)manager.GetType().GetField("_projectedHealthIndex", flags)!.GetValue(manager)!;
health.Contains(item).ShouldBeTrue();
var demandField = item.GetType().GetField("_demand", flags).ShouldNotBeNull();
var originalDemand = (IProjectedDemandLease)demandField.GetValue(item).ShouldNotBeNull();
var slotsAtDisposal = -1;
demandField.SetValue(item, new ObserveDemandDisposal(originalDemand, () => slotsAtDisposal = health.Count));
var anchors = (System.Collections.IDictionary)manager.GetType().GetField("_projectedAnchors", flags)!.GetValue(manager)!;
var entry = anchors[anchor].ShouldNotBeNull();
var definition = (ProjectedAnchorDefinition)entry.GetType().GetField("Item1")!.GetValue(entry)!;
_ = Prepare(coordinator!);
manager.RevalidateMonitoredItems();
((int)manager.GetType().GetField("_suppressedItems", flags)!.GetValue(manager)!).ShouldBe(1,
"the projected item must be detached before its anchor is removed");
var before = origin.Clock.GetTimestamp();
((IProjectedAnchorSink)sink).TryRemoveProjectedAnchor(anchor, definition).ShouldBeTrue();
origin.Clock.GetTimestamp().ShouldBe(before, "tombstone cleanup cannot depend on a later health timer");
var remaining = (System.Collections.IEnumerable)live.GetType().GetProperty("Keys")!.GetValue(live)!;
remaining.Cast<object>().ShouldNotContain(item);
health.Contains(item).ShouldBeFalse("a removed item cannot retain a health slot");
slotsAtDisposal.ShouldBe(0, "the health slot must be released before demand capacity is returned");
demandField.GetValue(item).ShouldBeNull();
// The client subscription remains alive and has not issued DeleteMonitoredItems.
}, onPublished: update => published.Writer.TryWrite(update), onCoordinator: value => coordinator = value);
}
}