150 lines
8.8 KiB
C#
150 lines
8.8 KiB
C#
using Google.Protobuf;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.Protos.DataConnection.V1;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
using WireReadResult = ZB.MOM.WW.OtOpcUa.Commons.Protos.DataConnection.V1.ReadResult;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Commons.Tests.DataConnections;
|
|
|
|
[Trait("Feature", "GrpcData"), Trait("Phase", "P06"), Trait("Category", "Unit")]
|
|
public sealed class GrpcDataAgeReadinessCodecTests
|
|
{
|
|
private static readonly ServingIdentity Serving = new("n", "i", "primary", 1, 2);
|
|
private static readonly DataItemHandle Handle = new("s", 1, 1, 1);
|
|
private static readonly IReadOnlyDictionary<ulong, DataItemHandle> Handles = new Dictionary<ulong, DataItemHandle> { [1] = Handle };
|
|
|
|
private static DataSubscriptionValues Frame(DataAgeBounds age, DataReadiness readiness) => new("s", 1, 1, false,
|
|
new[] { new DataSubscriptionValue(Handle, new DataSample(DataValue.FromScalar(DataValueKind.Int32, 42), 123, null, null, Serving, 9), age, readiness) });
|
|
|
|
[Fact]
|
|
public void Additive_descriptor_fields_lock_numbers_types_and_presence()
|
|
{
|
|
Check(CapabilitiesResponse.Descriptor.FindFieldByName("subscription_age_readiness"), 11, 8, false);
|
|
Check(ValueUpdate.Descriptor.FindFieldByName("origin_age_ticks"), 3, 3, true);
|
|
Check(ValueUpdate.Descriptor.FindFieldByName("readiness"), 4, 5, true);
|
|
Check(CompactUpdate.Descriptor.FindFieldByName("origin_age_ticks"), 8, 3, true);
|
|
Check(CompactUpdate.Descriptor.FindFieldByName("readiness"), 9, 5, true);
|
|
for (var i = 1; i <= 7; i++) CompactUpdate.Descriptor.FindFieldByNumber(i).ShouldNotBeNull();
|
|
static void Check(Google.Protobuf.Reflection.FieldDescriptor field, int number, int type, bool optional)
|
|
{
|
|
field.ShouldNotBeNull();
|
|
field.FieldNumber.ShouldBe(number);
|
|
((int)field.ToProto().Type).ShouldBe(type);
|
|
field.ToProto().Proto3Optional.ShouldBe(optional);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public void Negotiated_frames_preserve_zero_nonzero_unknown_and_original_sample(bool compact)
|
|
{
|
|
foreach (var ticks in new long?[] { null, 0, 123 })
|
|
{
|
|
using var frame = Frame(new DataAgeBounds(ticks is null ? null : TimeSpan.FromTicks(ticks.Value), null), DataReadiness.Ready);
|
|
var wire = DataProtoMapper.EncodeValues(frame, compact, subscriptionAgeReadiness: true);
|
|
using var decoded = DataProtoMapper.DecodeValues(wire, Handles, 10, 10000, Serving, subscriptionAgeReadiness: true);
|
|
var item = decoded.Items.Single();
|
|
item.AgeBounds.Lower.ShouldBe(ticks is null ? null : TimeSpan.FromTicks(ticks.Value));
|
|
item.AgeBounds.Upper.ShouldBeNull();
|
|
item.Readiness.ShouldBe(DataReadiness.Ready);
|
|
item.Sample.StatusCode.ShouldBe(123u);
|
|
item.Sample.Value.Kind.ShouldBe(DataValueKind.Int32);
|
|
item.Sample.Value.GetScalar().ShouldBe(42);
|
|
item.Sample.SourceTimestampUtc.ShouldBeNull();
|
|
item.Sample.ServerTimestampUtc.ShouldBeNull();
|
|
item.Sample.Serving.ShouldBe(Serving);
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public void Old_peer_fallback_ignores_extension_and_does_not_infer_readiness(bool compact)
|
|
{
|
|
using var frame = Frame(new DataAgeBounds(TimeSpan.Zero, null), DataReadiness.Ready);
|
|
var wire = DataProtoMapper.EncodeValues(frame, compact, subscriptionAgeReadiness: true);
|
|
using var oldPeer = DataProtoMapper.DecodeValues(wire, Handles, 10, 10000, Serving, subscriptionAgeReadiness: false);
|
|
oldPeer.Items.Single().AgeBounds.ShouldBe(default(DataAgeBounds));
|
|
oldPeer.Items.Single().Readiness.ShouldBe(DataReadiness.Unspecified);
|
|
var legacyWire = DataProtoMapper.EncodeValues(frame, compact, subscriptionAgeReadiness: false);
|
|
Should.Throw<ArgumentException>(() => DataProtoMapper.DecodeValues(legacyWire, Handles, 10, 10000, Serving, subscriptionAgeReadiness: true));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public void Negative_age_missing_or_invalid_readiness_and_limits_are_rejected(bool compact)
|
|
{
|
|
using var frame = Frame(new DataAgeBounds(TimeSpan.Zero, null), DataReadiness.Ready);
|
|
var wire = DataProtoMapper.EncodeValues(frame, compact, subscriptionAgeReadiness: true);
|
|
void Set(long age, int readiness)
|
|
{
|
|
if (compact) { wire.CompactValues.Items[0].OriginAgeTicks = age; wire.CompactValues.Items[0].Readiness = readiness; }
|
|
else { wire.Values.Items[0].OriginAgeTicks = age; wire.Values.Items[0].Readiness = readiness; }
|
|
}
|
|
Set(-1, (int)DataReadiness.Ready);
|
|
Should.Throw<ArgumentException>(() => DataProtoMapper.DecodeValues(wire, Handles, 10, 10000, Serving, subscriptionAgeReadiness: true));
|
|
foreach (var readiness in new[] { 0, -1, 99 })
|
|
{
|
|
Set(0, readiness);
|
|
Should.Throw<ArgumentException>(() => DataProtoMapper.DecodeValues(wire, Handles, 10, 10000, Serving, subscriptionAgeReadiness: true));
|
|
}
|
|
Set(0, (int)DataReadiness.Ready);
|
|
Should.Throw<ArgumentException>(() => DataProtoMapper.DecodeValues(wire, Handles, 10, 1, Serving, subscriptionAgeReadiness: true));
|
|
Should.Throw<ArgumentException>(() => DataProtoMapper.DecodeValues(wire, Handles, 10, 10000, Serving with { Incarnation = "other" }, subscriptionAgeReadiness: true));
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public void Large_payload_still_obeys_owned_decode_budgets(bool compact)
|
|
{
|
|
using var frame = new DataSubscriptionValues("s", 1, 1, false, new[] {
|
|
new DataSubscriptionValue(Handle, new DataSample(DataValue.FromBytes(new byte[32_000]), 0, null, null, Serving, 1),
|
|
new DataAgeBounds(TimeSpan.Zero, null), DataReadiness.Ready) });
|
|
var wire = DataProtoMapper.EncodeValues(frame, compact, subscriptionAgeReadiness: true);
|
|
wire.CalculateSize().ShouldBeLessThan(100_000);
|
|
// Encoded bytes fit, but the conservative decoded ownership estimate does not.
|
|
Should.Throw<ArgumentException>(() => DataProtoMapper.DecodeValues(wire, Handles, 1, 100_000, Serving, subscriptionAgeReadiness: true));
|
|
using var decoded = DataProtoMapper.DecodeValues(wire, Handles, 1, 160_000, Serving, subscriptionAgeReadiness: true);
|
|
decoded.Items.Single().Sample.Value.GetBytes().Length.ShouldBe(32_000);
|
|
Should.Throw<ArgumentException>(() => DataProtoMapper.DecodeValues(wire, Handles, 1, 1_000, Serving, subscriptionAgeReadiness: true));
|
|
}
|
|
|
|
[Fact]
|
|
public void Capability_bit_is_explicit_not_inferred_from_minor_version()
|
|
{
|
|
var limits = new DataLimits(10, 10, 10000, 10, 1, 10000, TimeSpan.FromSeconds(10), TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
|
|
var capabilities = new DataCapabilities(1, 1, Serving, DataReadiness.Ready, DataOperations.Subscribe,
|
|
new HashSet<DataValueKind> { DataValueKind.Int32 }, limits, true, 1, "cluster");
|
|
var wire = DataProtoMapper.EncodeCapabilities(capabilities);
|
|
DataProtoMapper.DecodeCapabilities(wire).SubscriptionAgeReadiness.ShouldBeFalse();
|
|
wire.SubscriptionAgeReadiness = true;
|
|
DataProtoMapper.DecodeCapabilities(wire).SubscriptionAgeReadiness.ShouldBeTrue();
|
|
DataProtoMapper.EncodeCapabilities(capabilities with { SubscriptionAgeReadiness = true }).SubscriptionAgeReadiness.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public void Unary_wire_age_is_not_presented_as_exact_receiver_age()
|
|
{
|
|
var wire = new WireReadResult { CorrelationId = "x", Status = (int)DataItemStatus.Good, Readiness = (int)DataReadiness.Ready, AgeTicks = 5 };
|
|
using var bounded = DataProtoMapper.DecodeReadResult(wire, causalRequestElapsed: TimeSpan.FromTicks(3));
|
|
bounded.Age.ShouldBeNull();
|
|
bounded.AgeBounds.ShouldBe(new DataAgeBounds(TimeSpan.FromTicks(5), TimeSpan.FromTicks(8)));
|
|
using var unknownUpper = DataProtoMapper.DecodeReadResult(wire);
|
|
unknownUpper.AgeBounds.ShouldBe(new DataAgeBounds(TimeSpan.FromTicks(5), null));
|
|
wire.ClearAgeTicks();
|
|
using var absent = DataProtoMapper.DecodeReadResult(wire, causalRequestElapsed: TimeSpan.Zero);
|
|
absent.AgeBounds.ShouldBe(default(DataAgeBounds));
|
|
wire.AgeTicks = -1;
|
|
Should.Throw<ArgumentException>(() => DataProtoMapper.DecodeReadResult(wire));
|
|
wire.AgeTicks = long.MaxValue;
|
|
Should.Throw<OverflowException>(() => DataProtoMapper.DecodeReadResult(wire, causalRequestElapsed: TimeSpan.FromTicks(1)));
|
|
wire.AgeTicks = 0;
|
|
Should.Throw<ArgumentOutOfRangeException>(() => DataProtoMapper.DecodeReadResult(wire, causalRequestElapsed: TimeSpan.FromTicks(-1)));
|
|
}
|
|
}
|