Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataProjectedReadReservationTests.cs
T
Joseph Doherty 75e6ff9af8 fix(grpc-data): size projected unary reservations from the node's shape (#960); per-item status for projected items refused at create (#961)
#960: a projected read reserves 16 KiB for a fixed-size scalar (16 KiB + 256 B/element for a
fixed-length fixed-size array) instead of the whole MaxPendingBytes, so the default 1 MiB admits
64 concurrent scalar reads; String/Bytes/variable arrays keep the whole cap, and a waiting
whole-cap read holds back smaller ones for 100 ms (anti-starvation). Writes reserve
16 KiB + identifiers + 3 x the value bound (what the origin's DataWriteExecutor admits the batch
against). Monitored-item renewals reserve 256 KiB. Measured: 4 x 250 ms maxAge=0 reads 1016 ms /
1 in flight -> 285 ms / 4 in flight.

#961: TryAcquireProjectedItem reports BadResourceUnavailable for a stream-capacity refusal; the
node manager's CreateMonitoredItem override turns the item manager's ServiceResultException into
that item's status (was a whole-request fault), and an authority race is re-classified outside
Lock like the per-item probe.
2026-12-01 20:34:35 -05:00

89 lines
5.5 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Commons.DataConnections;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
using DataValue = ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.DataValue;
using ReadResult = ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.ReadResult;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
/// <summary>
/// #960: a projected read reserves only what its node's declared shape can answer, and that reservation is the read's
/// decode limit. These pin the constants against the REAL wire codec: a response carrying the widest value of each bounded
/// shape, with long serving identifiers, must decode within its reservation.
/// </summary>
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Issue", "960")]
public sealed class GrpcDataProjectedReadReservationTests
{
private static readonly ServingIdentity Serving = new(new string('n', 128), Guid.NewGuid().ToString("D") + new string('i', 92),
"Primary", long.MaxValue, long.MaxValue, long.MaxValue);
private static DataMetadata Metadata(DataValueKind kind, int length = 0, DataValueKind? element = null) =>
new(new DataView(new DataAddress("cluster", "https://zb.com/otopcua/raw", "s", "Plant/Speed"), DataRealm.Raw, null, 0, "Plant/Speed"),
"Speed", "Variable", kind, kind == DataValueKind.Array ? 1 : -1, kind == DataValueKind.Array ? [length] : [], false,
DataOperations.Read, 1, null, null, false, element, Serving);
public static TheoryData<string> BoundedShapes => new() { "Int64", "Double", "Timestamp", "Guid", "Int32[1000]", "Double[64]", "Guid[64]", "Timestamp[64]" };
private static (DataMetadata Metadata, DataValue Value) Widest(string shape)
{
var timestamp = DateTimeOffset.MaxValue.AddTicks(-1).ToUniversalTime();
return shape switch
{
"Int64" => (Metadata(DataValueKind.Int64), DataValue.FromScalar(DataValueKind.Int64, long.MinValue)),
"Double" => (Metadata(DataValueKind.Double), DataValue.FromScalar(DataValueKind.Double, double.MinValue)),
"Timestamp" => (Metadata(DataValueKind.Timestamp), DataValue.FromScalar(DataValueKind.Timestamp, timestamp)),
"Guid" => (Metadata(DataValueKind.Guid), DataValue.FromScalar(DataValueKind.Guid, Guid.NewGuid())),
"Int32[1000]" => (Metadata(DataValueKind.Array, 1000, DataValueKind.Int32),
DataValue.FromArray(DataValueKind.Int32, Enumerable.Repeat((object)int.MinValue, 1000).ToArray(), [1000])),
"Double[64]" => (Metadata(DataValueKind.Array, 64, DataValueKind.Double),
DataValue.FromArray(DataValueKind.Double, Enumerable.Repeat((object)double.MinValue, 64).ToArray(), [64])),
"Guid[64]" => (Metadata(DataValueKind.Array, 64, DataValueKind.Guid),
DataValue.FromArray(DataValueKind.Guid, Enumerable.Range(0, 64).Select(_ => (object)Guid.NewGuid()).ToArray(), [64])),
"Timestamp[64]" => (Metadata(DataValueKind.Array, 64, DataValueKind.Timestamp),
DataValue.FromArray(DataValueKind.Timestamp, Enumerable.Repeat((object)timestamp, 64).ToArray(), [64])),
_ => throw new ArgumentOutOfRangeException(nameof(shape)),
};
}
[Theory]
[MemberData(nameof(BoundedShapes))]
public void The_widest_value_of_a_bounded_shape_decodes_within_its_read_reservation(string shape)
{
var (metadata, value) = Widest(shape);
var reservation = ProjectedDataAccess.ReadReservationBytes(metadata, 4 * 1024 * 1024);
reservation.ShouldBeLessThan(4 * 1024 * 1024L, "a bounded shape is never charged the whole cap");
var sample = new DataSample(value, 0x80000000u, DateTimeOffset.UtcNow, DateTimeOffset.UtcNow, Serving, long.MaxValue);
var wire = DataProtoMapper.EncodeReadResponse([new ReadResult("0", DataItemStatus.Good, DataReadiness.Ready, TimeSpan.MaxValue, sample,
new string('d', 256), AgeBounds: new(TimeSpan.Zero, TimeSpan.FromDays(1)))]);
wire.Serving = DataProtoMapper.EncodeServing(Serving);
var decoded = DataProtoMapper.DecodeReadResponse(wire, new DataDecodeLimits(1, (int)reservation));
try { decoded.ShouldHaveSingleItem().Sample.ShouldNotBeNull(); }
finally { foreach (var item in decoded) item.Dispose(); }
// The origin's own admission of the same answer (LocalDataServices.ReadAsync: retained estimate + 256).
(value.EstimatedRetainedBytes + 256).ShouldBeLessThanOrEqualTo(reservation);
}
[Theory]
[InlineData(DataValueKind.String)]
[InlineData(DataValueKind.Bytes)]
[InlineData(DataValueKind.Unspecified)]
public void An_unbounded_shape_keeps_the_whole_per_read_cap(DataValueKind kind) =>
ProjectedDataAccess.ReadReservationBytes(Metadata(kind), 1024 * 1024).ShouldBe(1024 * 1024);
[Fact]
public void A_variable_length_or_string_array_keeps_the_whole_per_read_cap()
{
ProjectedDataAccess.ReadReservationBytes(Metadata(DataValueKind.Array, 0, DataValueKind.Int32), 1024 * 1024).ShouldBe(1024 * 1024);
ProjectedDataAccess.ReadReservationBytes(Metadata(DataValueKind.Array, 8, DataValueKind.String), 1024 * 1024).ShouldBe(1024 * 1024);
}
[Fact]
public void A_reservation_never_exceeds_the_configured_cap()
{
ProjectedDataAccess.ReadReservationBytes(Metadata(DataValueKind.Int32), 1100).ShouldBe(1100);
ProjectedDataAccess.ReadReservationBytes(Metadata(DataValueKind.Array, 100_000, DataValueKind.Double), 1024 * 1024).ShouldBe(1024 * 1024);
}
}