148 lines
7.1 KiB
C#
148 lines
7.1 KiB
C#
using Google.Protobuf;
|
|
using Grpc.Core;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Host.Grpc;
|
|
using W = ZB.MOM.WW.OtOpcUa.Commons.Protos.DataConnection.V1;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Host.Tests.DataConnections;
|
|
|
|
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "P07")]
|
|
public sealed class GrpcDataPublicationHandoffTests
|
|
{
|
|
[Fact]
|
|
public void Monotonic_unary_deadline_is_rechecked_after_validation_even_without_a_timer_callback()
|
|
{
|
|
var binder = new CaptureBinder();
|
|
DataConnectionGrpcBinding.BindService(binder, null!);
|
|
var clock = new PublicationClock();
|
|
var deadline = new DataConnectionUnaryLifetime(clock, TimeSpan.FromSeconds(1));
|
|
var response = new W.CapabilitiesResponse { ProtocolMajor = 1 };
|
|
using var registration = DataConnectionPublication.RegisterUnary(response, () =>
|
|
{ clock.Advance(TimeSpan.FromSeconds(2)); return true; }, 1024, deadline);
|
|
var context = new CaptureContext();
|
|
Should.Throw<RpcException>(() => binder.Unary["GetCapabilities"](response, context));
|
|
context.Handoffs.ShouldBe(0);
|
|
}
|
|
|
|
private sealed class PublicationClock : TimeProvider
|
|
{
|
|
private long _ticks;
|
|
public override long TimestampFrequency => TimeSpan.TicksPerSecond;
|
|
public override long GetTimestamp() => _ticks;
|
|
internal void Advance(TimeSpan duration) => _ticks += duration.Ticks;
|
|
}
|
|
[Fact]
|
|
public void Registered_binding_checks_revocation_after_method_return_before_any_body_handoff()
|
|
{
|
|
var binder = new CaptureBinder();
|
|
DataConnectionGrpcBinding.BindService(binder, null!);
|
|
var response = new W.CapabilitiesResponse { ProtocolMajor = 1 };
|
|
var allowed = true;
|
|
using var registration = DataConnectionPublication.Register(response, () => allowed, 1024);
|
|
allowed = false;
|
|
var context = new CaptureContext();
|
|
Should.Throw<RpcException>(() => binder.Unary["GetCapabilities"](response, context));
|
|
context.Handoffs.ShouldBe(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void Registered_binding_performs_exactly_one_successful_private_buffer_handoff()
|
|
{
|
|
var binder = new CaptureBinder();
|
|
DataConnectionGrpcBinding.BindService(binder, null!);
|
|
var response = new W.CapabilitiesResponse { ProtocolMajor = 1 };
|
|
using var registration = DataConnectionPublication.Register(response, () => true, 1024);
|
|
var context = new CaptureContext();
|
|
binder.Unary["GetCapabilities"](response, context);
|
|
context.Handoffs.ShouldBe(1);
|
|
W.CapabilitiesResponse.Parser.ParseFrom(context.Payload!).ProtocolMajor.ShouldBe(1);
|
|
Should.Throw<RpcException>(() => binder.Unary["GetCapabilities"](response, context));
|
|
context.Handoffs.ShouldBe(1);
|
|
}
|
|
|
|
[Fact]
|
|
public void Skipped_serializer_cleanup_and_unregistered_responses_fail_closed()
|
|
{
|
|
var binder = new CaptureBinder();
|
|
DataConnectionGrpcBinding.BindService(binder, null!);
|
|
var response = new W.CapabilitiesResponse();
|
|
var registration = DataConnectionPublication.Register(response, () => true, 1024);
|
|
registration.Dispose();
|
|
var context = new CaptureContext();
|
|
Should.Throw<RpcException>(() => binder.Unary["GetCapabilities"](response, context));
|
|
Should.Throw<RpcException>(() => binder.Unary["GetCapabilities"](new W.CapabilitiesResponse(), context));
|
|
context.Handoffs.ShouldBe(0);
|
|
}
|
|
|
|
[Fact(Timeout = 15000)]
|
|
public async Task Disposal_during_a_blocked_final_guard_prevents_handoff()
|
|
{
|
|
var binder = new CaptureBinder();
|
|
DataConnectionGrpcBinding.BindService(binder, null!);
|
|
var response = new W.CapabilitiesResponse { ProtocolMajor = 1 };
|
|
using var entered = new ManualResetEventSlim();
|
|
using var resume = new ManualResetEventSlim();
|
|
using var registration = DataConnectionPublication.Register(response, () =>
|
|
{
|
|
entered.Set();
|
|
if (!resume.Wait(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken)) throw new TimeoutException();
|
|
return true;
|
|
}, 1024);
|
|
var context = new CaptureContext();
|
|
var serialization = Task.Run(() =>
|
|
{
|
|
try { binder.Unary["GetCapabilities"](response, context); return (Exception?)null; }
|
|
catch (Exception error) { return error; }
|
|
}, TestContext.Current.CancellationToken);
|
|
try
|
|
{
|
|
entered.Wait(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken).ShouldBeTrue();
|
|
registration.Dispose();
|
|
}
|
|
finally { resume.Set(); }
|
|
(await serialization.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken)).ShouldBeOfType<RpcException>();
|
|
context.Handoffs.ShouldBe(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void Prepared_stream_delivery_requires_one_live_runtime_publication_guard()
|
|
{
|
|
var binder = new CaptureBinder();
|
|
DataConnectionGrpcBinding.BindService(binder, null!);
|
|
W.SubscriptionFrame Frame() => new() { SubscriptionId = "s", StreamEpoch = 1, Sequence = 1, Control = new W.Control() };
|
|
var unbound = Frame();
|
|
using (var prepared = new PreparedDataSubscription(unbound, unbound.ToByteArray(), 1024, 1024,
|
|
() => true, TestContext.Current.CancellationToken))
|
|
Should.Throw<RpcException>(() => binder.Duplex["Subscribe"](unbound, new CaptureContext()));
|
|
var valid = Frame();
|
|
using var delivery = new PreparedDataSubscription(valid, valid.ToByteArray(), 1024, 1024,
|
|
() => true, TestContext.Current.CancellationToken);
|
|
((IDataPreparedSubscriptionPublication)delivery).SetPublicationGuard(() => true);
|
|
Should.Throw<InvalidOperationException>(() => delivery.SetPublicationGuard(() => true));
|
|
var context = new CaptureContext();
|
|
binder.Duplex["Subscribe"](valid, context);
|
|
context.Handoffs.ShouldBe(1);
|
|
}
|
|
|
|
private sealed class CaptureBinder : ServiceBinderBase
|
|
{
|
|
internal readonly Dictionary<string, Action<IMessage, SerializationContext>> Unary = [];
|
|
internal readonly Dictionary<string, Action<IMessage, SerializationContext>> Duplex = [];
|
|
public override void AddMethod<TRequest, TResponse>(Method<TRequest, TResponse> method,
|
|
UnaryServerMethod<TRequest, TResponse>? handler) =>
|
|
Unary.Add(method.Name, (response, context) => method.ResponseMarshaller.ContextualSerializer((TResponse)(object)response, context));
|
|
public override void AddMethod<TRequest, TResponse>(Method<TRequest, TResponse> method,
|
|
DuplexStreamingServerMethod<TRequest, TResponse>? handler) =>
|
|
Duplex.Add(method.Name, (response, context) => method.ResponseMarshaller.ContextualSerializer((TResponse)(object)response, context));
|
|
}
|
|
|
|
private sealed class CaptureContext : SerializationContext
|
|
{
|
|
internal int Handoffs;
|
|
internal byte[]? Payload;
|
|
public override void Complete(byte[] payload) { Handoffs++; Payload = payload; }
|
|
}
|
|
}
|