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; /// /// #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. /// [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 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); } }