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

102 lines
6.1 KiB
C#

using System.Reflection;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
using ZB.MOM.WW.OtOpcUa.Core.Authorization;
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
[Trait("Feature", "GrpcData"), Trait("Phase", "P11")]
public sealed class GrpcDataFreshnessChallengeTests
{
[Fact(Timeout = 30000)]
public async Task Revoked_authority_cannot_obtain_a_challenge_snapshot()
{
var ct = TestContext.Current.CancellationToken;
var demand = new DeferredSubscriptionDemand(); demand.Accept();
var capacity = new SubscriptionTransportCapacity(4, 2, 1_000_000);
LocalDataFixture? fixture = null;
using (fixture = new LocalDataFixture(servingGeneration: 1, targetSubscriptions: (context, target) =>
new LocalSubscriptionService(fixture!.Services, context, target, demand,
new(16, 8, 100_000, TimeSpan.FromMinutes(1)), capacity)))
{
fixture.Publish(42);
await using var connection = fixture.Connection();
await using var stream = await connection.SubscribeAsync(ct);
var settings = new DataSubscriptionSettings(TimeSpan.Zero, TimeSpan.Zero, DataChangeTrigger.StatusValue, null);
await stream.SendAsync(new OpenSubscription("s", 1, "open", TimeSpan.FromSeconds(10),
[new("item", fixture.Raw, settings)], false, TimeSpan.FromSeconds(30), LocalDataFixture.Budget), ct);
await using var reader = stream.ReadAllAsync(ct).GetAsyncEnumerator(ct);
(await reader.MoveNextAsync()).ShouldBeTrue();
var handle = reader.Current.ShouldBeOfType<DataSubscriptionAck>().Items.Single().Handle.ShouldNotBeNull();
while (await reader.MoveNextAsync())
{
if (reader.Current is DataSubscriptionControl { Kind: DataStreamControlKind.SnapshotEnd }) break;
(reader.Current as IDisposable)?.Dispose();
}
fixture.Authority.Apply("cluster", "revoked", [], [],
new Dictionary<AclScopeRef, IReadOnlyList<NodeAclPath>>());
await stream.SendAsync(new ChallengeSubscriptionFreshness("s", 1, "freshness-1", TimeSpan.FromSeconds(10), [handle]), ct);
(await reader.MoveNextAsync().AsTask().WaitAsync(TimeSpan.FromSeconds(5), ct)).ShouldBeFalse();
}
}
[Fact(Timeout = 30000)]
public async Task Challenge_does_not_relabel_a_value_whose_age_was_already_captured()
{
var ct = TestContext.Current.CancellationToken;
var demand = new DeferredSubscriptionDemand(); demand.Accept();
var capacity = new SubscriptionTransportCapacity(4, 2, 1_000_000);
LocalDataFixture? fixture = null;
using (fixture = new LocalDataFixture(servingGeneration: 1, targetSubscriptions: (context, target) =>
new LocalSubscriptionService(fixture!.Services, context, target, demand,
new(16, 8, 100_000, TimeSpan.FromMinutes(1)), capacity)))
{
fixture.Publish(42);
await using var connection = fixture.Connection();
await using var subscription = await connection.SubscribeAsync(ct);
var session = subscription.ShouldBeOfType<DataSubscriptionSession>();
using var captured = new ManualResetEventSlim();
using var release = new ManualResetEventSlim();
var once = 0;
var settings = new DataSubscriptionSettings(TimeSpan.Zero, TimeSpan.Zero, DataChangeTrigger.StatusValue, null);
await subscription.SendAsync(new OpenSubscription("s", 1, "open", TimeSpan.FromSeconds(10),
[new("item", fixture.Raw, settings)], false, TimeSpan.FromSeconds(30), LocalDataFixture.Budget), ct);
await using var reader = subscription.ReadAllAsync(ct).GetAsyncEnumerator(ct);
(await reader.MoveNextAsync()).ShouldBeTrue();
var handle = reader.Current.ShouldBeOfType<DataSubscriptionAck>().Items.Single().Handle.ShouldNotBeNull();
(await reader.MoveNextAsync()).ShouldBeTrue();
reader.Current.ShouldBeOfType<DataSubscriptionControl>().Kind.ShouldBe(DataStreamControlKind.SnapshotBegin);
(await reader.MoveNextAsync()).ShouldBeTrue();
reader.Current.ShouldBeOfType<DataSubscriptionValues>().Dispose();
(await reader.MoveNextAsync()).ShouldBeTrue();
reader.Current.ShouldBeOfType<DataSubscriptionControl>().Kind.ShouldBe(DataStreamControlKind.SnapshotEnd);
session.BeforeDirectCommitForTests = () =>
{
if (Interlocked.Exchange(ref once, 1) == 0) { captured.Set(); release.Wait(ct); }
};
HubTest.Publish(fixture.Hub, fixture.Ticket, 2, 43); fixture.Hub.ProcessPending();
fixture.Clock.Advance(TimeSpan.FromMilliseconds(200));
var oldFrame = Task.Run(async () => { (await reader.MoveNextAsync()).ShouldBeTrue(); return reader.Current; }, ct);
try
{
captured.Wait(TimeSpan.FromSeconds(5), ct).ShouldBeTrue();
await subscription.SendAsync(new ChallengeSubscriptionFreshness("s", 1, "freshness-1", TimeSpan.FromSeconds(10), [handle]), ct);
var field = typeof(DataSubscriptionSession).GetField("_freshnessChallengeId", BindingFlags.Instance | BindingFlags.NonPublic)!;
SpinWait.SpinUntil(() => (string?)field.GetValue(session) == "freshness-1", TimeSpan.FromSeconds(5)).ShouldBeTrue();
}
finally { release.Set(); }
using (var value = (await oldFrame).ShouldBeOfType<DataSubscriptionValues>())
value.Items.Single().FreshnessChallengeId.ShouldBeNull();
while (await reader.MoveNextAsync())
{
if (reader.Current is not DataSubscriptionValues value) continue;
using (value)
if (value.Items.Single().FreshnessChallengeId == "freshness-1") return;
}
throw new Xunit.Sdk.XunitException("The authorized challenge snapshot was not delivered.");
}
}
}