test: validate grpc data connections with local docker and opcua cli
This commit is contained in:
@@ -2,6 +2,7 @@ using Google.Protobuf;
|
||||
using Grpc.Core;
|
||||
using Microsoft.Extensions.Options;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.DataConnections;
|
||||
using ZB.MOM.WW.OtOpcUa.Commons.Protos;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
||||
using ZB.MOM.WW.OtOpcUa.Host.Configuration;
|
||||
using W = ZB.MOM.WW.OtOpcUa.Commons.Protos.DataConnection.V1;
|
||||
@@ -140,13 +141,18 @@ public sealed class DataConnectionGrpcService(IDataConnectionFactory factory, IO
|
||||
initial.CancelAfter(_options.UnaryTimeout);
|
||||
caps = await Ready(connection, DataOperations.Subscribe, initial.Token).ConfigureAwait(false);
|
||||
}
|
||||
var codecHeaders = context.RequestHeaders.Where(entry => string.Equals(entry.Key, DataConnectionProtoContract.CompactCodecHeader, StringComparison.OrdinalIgnoreCase)).ToArray();
|
||||
if (codecHeaders.Length > 1 || codecHeaders.Length == 1 &&
|
||||
(codecHeaders[0].IsBinary || codecHeaders[0].Value != "1"))
|
||||
throw new RpcException(new Status(StatusCode.InvalidArgument, "Invalid compact codec negotiation."));
|
||||
var compact = codecHeaders.Length == 1 && DataProtoMapper.NegotiateCompact(caps);
|
||||
subscription = await connection.SubscribeAsync(context.CancellationToken).ConfigureAwait(false);
|
||||
await using (subscription)
|
||||
{
|
||||
if (subscription is not IDataSubscriptionTransport transport)
|
||||
throw new RpcException(new Status(StatusCode.Unavailable, "Subscription transport is unavailable."));
|
||||
using var stop = CancellationTokenSource.CreateLinkedTokenSource(context.CancellationToken);
|
||||
var sink = new GrpcDeliverySink(responses, context, caps, _options.MaxMessageBytes,
|
||||
var sink = new GrpcDeliverySink(responses, context, caps, _options.MaxMessageBytes, compact,
|
||||
() => CanPublish(connection, caller, caps.Serving));
|
||||
var delivery = transport.RunDeliveryAsync(sink, stop.Token);
|
||||
var commands = ReceiveCommands();
|
||||
@@ -174,6 +180,8 @@ public sealed class DataConnectionGrpcService(IDataConnectionFactory factory, IO
|
||||
AddSubscriptionItems add => add with { AcknowledgementTimeout = ack },
|
||||
ModifySubscriptionItems modify => modify with { AcknowledgementTimeout = ack },
|
||||
RemoveSubscriptionItems remove => remove with { AcknowledgementTimeout = ack },
|
||||
ChallengeSubscriptionFreshness challenge when caps.SubscriptionFreshnessChallenges && caps.SubscriptionAgeReadiness =>
|
||||
challenge with { AcknowledgementTimeout = ack },
|
||||
_ => throw new ArgumentException("Unsupported stream command.")
|
||||
};
|
||||
using var commandDeadline = CancellationTokenSource.CreateLinkedTokenSource(stop.Token);
|
||||
@@ -191,7 +199,7 @@ public sealed class DataConnectionGrpcService(IDataConnectionFactory factory, IO
|
||||
}
|
||||
|
||||
private sealed class GrpcDeliverySink(IServerStreamWriter<W.SubscriptionFrame> writer, ServerCallContext context,
|
||||
DataCapabilities caps, int maximumBytes, Func<bool> hostGuard) : IDataSubscriptionDeliverySink
|
||||
DataCapabilities caps, int maximumBytes, bool compact, Func<bool> hostGuard) : IDataSubscriptionDeliverySink
|
||||
{
|
||||
public ValueTask<IDataPreparedSubscriptionDelivery> PrepareAsync(DataSubscriptionFrame frame, long reservedBytes,
|
||||
CancellationToken cancellationToken)
|
||||
@@ -210,7 +218,7 @@ public sealed class DataConnectionGrpcService(IDataConnectionFactory factory, IO
|
||||
};
|
||||
if (charge > reservedBytes) throw new RpcException(new Status(StatusCode.ResourceExhausted, "Prepared frame exceeds its reservation."));
|
||||
var wire = frame is DataSubscriptionValues valuesFrame
|
||||
? DataProtoMapper.EncodeValues(valuesFrame, compactNegotiated: false, subscriptionAgeReadiness: true)
|
||||
? DataProtoMapper.EncodeValues(valuesFrame, compactNegotiated: compact && valuesFrame.Items.Count > 0, subscriptionAgeReadiness: true)
|
||||
: DataProtoMapper.EncodeControlOrAck(frame);
|
||||
wire.Serving = DataProtoMapper.EncodeServing(caps.Serving);
|
||||
if (wire.CalculateSize() > maximumBytes) throw new RpcException(new Status(StatusCode.ResourceExhausted, "Stream message exceeds transport limits."));
|
||||
|
||||
Reference in New Issue
Block a user