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

96 lines
6.6 KiB
C#

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.Runtime.DataConnections;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
public sealed partial class GrpcDataConfiguredDiscoveryWireTests
{
[Fact(Timeout = 90000), Trait("Phase", "P11Wire")]
public async Task Same_acquisition_successor_refreshes_only_health_evidence_not_queued_or_republished_proof()
{
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>();
await WithComposedServer(origin, origin.Raw, async (session, sink, _, _, _, _, anchor) =>
{
var client = (SessionClient)session;
await BrowseAll(client, anchor, 1);
var slow = (await client.CreateSubscriptionAsync(new RequestHeader(), 200, 300, 1, 0, true, 0, ct)).SubscriptionId;
await AddComposedItem(client, slow, anchor, 1, 200, ct);
await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
var source = await NextPublished(published.Reader, x => x.IsCompleteSnapshot && Equals(x.Snapshot.Value, 7), ct);
(await PublishComposedValue(client, 1, ct)).StatusCode.ShouldBe(StatusCodes.Good);
await client.SetPublishingModeAsync(new RequestHeader(), false, [slow], ct);
var fast = (await client.CreateSubscriptionAsync(new RequestHeader(), 50, 300, 1, 0, true, 0, ct)).SubscriptionId;
await AddComposedItem(client, fast, anchor, 2, 50, ct);
await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
(await PublishComposedValue(client, 2, ct)).StatusCode.ShouldBe(StatusCodes.Good);
var predecessor = new RenewableTestWitness(source.SourceProof);
var successor = new RenewableTestWitness(source.SourceProof);
var stamp = origin.Clock.GetUtcNow().UtcDateTime;
var snapshot = source.Snapshot with { Value = 42, SourceTimestampUtc = stamp, ServerTimestampUtc = stamp };
var received = origin.Clock.GetTimestamp();
var oldBounds = new DataAgeBounds(TimeSpan.Zero, TimeSpan.Zero);
var newBounds = new DataAgeBounds(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));
var old = new ProjectedValueWrite(source.Route.Realm, source.Route.LocalIdentifier, source.Route.ProjectionGeneration,
source.Route.Serving, snapshot, oldBounds, received, predecessor, false, 2);
((IProjectedValueSink)sink).TryPublish([old]).ShouldBeTrue();
var oldNotification = await PublishComposedNotification(client, 2, ct);
oldNotification.Value.StatusCode.ShouldBe(StatusCodes.Good);
oldNotification.Value.WrappedValue.AsBoxedObject(Variant.BoxingBehavior.Legacy).ShouldBe(42);
await client.SetPublishingModeAsync(new RequestHeader(), false, [fast], ct);
var next = old with { Proof = successor, OriginAgeBounds = newBounds, IsCompleteSnapshot = true };
((IProjectedValueSink)sink).TryPublish([next]).ShouldBeTrue();
var flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;
var manager = sink.GetType().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 latest = item.GetType().GetField("_latestEvidence", flags)!.GetValue(item).ShouldNotBeNull();
latest.GetType().GetProperty("Proof")!.GetValue(latest).ShouldBeSameAs(successor,
"health needs the successor's independently validated metadata witness");
latest.GetType().GetProperty("Age")!.GetValue(latest).ShouldBe(newBounds);
latest.GetType().GetProperty("ReceivedTimestamp")!.GetValue(latest).ShouldBe(received);
var candidates = (System.Collections.IEnumerable)item.GetType().GetField("_candidates", flags)!.GetValue(item)!;
var held = candidates.Cast<object>().Select(candidate =>
(ProjectedValueWrite)candidate.GetType().GetProperty("Sample")!.GetValue(candidate)!)
.Single(candidate => Equals(candidate.Snapshot.Value, 42));
held.Proof.ShouldBeSameAs(predecessor, "the successor may not rewrite the queued candidate's proof");
held.OriginAgeBounds.ShouldBe(oldBounds);
held.ReceivedTimestamp.ShouldBe(received);
predecessor.Valid = false; // Models expiry of the old metadata lease only.
origin.Clock.Advance(TimeSpan.FromMilliseconds(100));
item.GetType().GetField("_queuedLoss", flags)!.GetValue(item).ShouldBeNull("a valid successor must keep current health Good");
await client.SetPublishingModeAsync(new RequestHeader(), true, [slow], ct);
var queued = await PublishComposedValue(client, 1, ct);
StatusCode.IsGood(queued.StatusCode).ShouldBeFalse("the held old candidate keeps its original expired witness");
queued.WrappedValue.IsNull.ShouldBeTrue();
var replay = await client.RepublishAsync(new RequestHeader(), fast, oldNotification.Sequence, ct);
var oldReplay = ComposedChanges(replay.NotificationMessage).Single(x => x.ClientHandle == 2).Value;
StatusCode.IsGood(oldReplay.StatusCode).ShouldBeFalse("the successor must not extend the old notification's metadata proof");
oldReplay.WrappedValue.IsNull.ShouldBeTrue();
}, onPublished: update => published.Writer.TryWrite(update));
}
private sealed class RenewableTestWitness(IProjectedSamplePublicationProof source) : IProjectedSamplePublicationProof
{
public bool Valid = true;
public bool IsValid => Valid && source.IsValid;
public bool IsSourceValid => source.IsSourceValid;
public object SourceIdentity => source.SourceIdentity;
public TimeProvider Clock => source.Clock;
public void Invalidate() => Valid = false;
}
}