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

73 lines
4.2 KiB
C#

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 Lost_cadence_retains_only_original_freshness_while_another_cadence_covers_the_source()
{
var ct = TestContext.Current.CancellationToken;
LocalDataFixture? peer = null;
var subscriptions = new FakeSubscriptions(() => peer!.Serving);
var gated = new GateLaterSubscriptions(subscriptions);
using var origin = new LocalDataFixture(servingGeneration: 1, targetSubscriptions: (_, _) => gated);
peer = origin; subscriptions.SampleClock = origin.Clock;
try
{
await WithComposedServer(origin, origin.Raw, async (session, _, _, _, _, _, 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);
var slowStream = await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
var initialSlow = await PublishComposedNotification(client, 1, ct);
initialSlow.Value.StatusCode.ShouldBe(StatusCodes.Good);
var fast = (await client.CreateSubscriptionAsync(new RequestHeader(), 50, 300, 1, 0, true, 0, ct)).SubscriptionId;
await AddComposedItem(client, fast, anchor, 2, 50, ct);
var fastStream = await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
fastStream.ShouldNotBeSameAs(slowStream);
var initialFast = await PublishComposedNotification(client, 2, ct);
initialFast.Value.StatusCode.ShouldBe(StatusCodes.Good);
await slowStream.DisposeAsync();
await gated.RetryBlocked.Task.WaitAsync(TimeSpan.FromSeconds(10), ct);
var oldSlow = await client.RepublishAsync(new RequestHeader(), slow, initialSlow.Sequence, ct);
StatusCode.IsGood(ComposedChanges(oldSlow.NotificationMessage).Single(x => x.ClientHandle == 1).Value.StatusCode)
.ShouldBeTrue("continuously proven coverage preserves the original still-fresh source evidence");
var oldFast = await client.RepublishAsync(new RequestHeader(), fast, initialFast.Sequence, ct);
oldFast.NotificationMessage.ShouldNotBeNull();
StatusCode.IsGood(ComposedChanges(oldFast.NotificationMessage).Single(x => x.ClientHandle == 2).Value.StatusCode)
.ShouldBeTrue("the still-live cadence retains its own notification proof");
origin.Clock.Advance(TimeSpan.FromSeconds(3));
var expiredSlow = await client.RepublishAsync(new RequestHeader(), slow, initialSlow.Sequence, ct);
StatusCode.IsGood(ComposedChanges(expiredSlow.NotificationMessage).Single(x => x.ClientHandle == 1).Value.StatusCode)
.ShouldBeFalse("peer coverage cannot renew the original notification's two-second age bound");
}, staleTimeoutMilliseconds: 2000);
}
finally { gated.Release.TrySetResult(); }
}
private sealed class GateLaterSubscriptions(IDataSubscriptionService inner) : IDataSubscriptionService
{
private int _calls;
internal readonly TaskCompletionSource RetryBlocked = new(TaskCreationOptions.RunContinuationsAsynchronously);
internal readonly TaskCompletionSource Release = new(TaskCreationOptions.RunContinuationsAsynchronously);
public async Task<IDataSubscription> SubscribeAsync(CancellationToken ct)
{
if (Interlocked.Increment(ref _calls) > 2)
{
RetryBlocked.TrySetResult();
await Release.Task.WaitAsync(ct);
}
return await inner.SubscribeAsync(ct);
}
}
}