fix: preserve SDK 2.0 client value and lifetime contracts
This commit is contained in:
+4
-5
@@ -26,8 +26,7 @@ public class AggregateTypeMapperTests
|
||||
public void ToNodeId_ReturnsNonNullForAllValues(AggregateType aggregate)
|
||||
{
|
||||
var nodeId = AggregateTypeMapper.ToNodeId(aggregate);
|
||||
nodeId.ShouldNotBeNull();
|
||||
nodeId.IsNullNodeId.ShouldBeFalse();
|
||||
nodeId.IsNull.ShouldBeFalse();
|
||||
}
|
||||
|
||||
/// <summary>Verifies that Average aggregate type maps to the correct OPC UA node ID.</summary>
|
||||
@@ -109,9 +108,9 @@ public class AggregateTypeMapperTests
|
||||
[MemberData(nameof(AllAggregates))]
|
||||
public void ToNodeId_maps_to_the_same_named_AggregateFunction(AggregateType aggregate)
|
||||
{
|
||||
var field = typeof(ObjectIds).GetField("AggregateFunction_" + aggregate);
|
||||
field.ShouldNotBeNull();
|
||||
AggregateTypeMapper.ToNodeId(aggregate).ShouldBe((NodeId)field.GetValue(null)!);
|
||||
var property = typeof(ObjectIds).GetProperty("AggregateFunction_" + aggregate);
|
||||
property.ShouldNotBeNull();
|
||||
AggregateTypeMapper.ToNodeId(aggregate).ShouldBe((NodeId)property.GetValue(null)!);
|
||||
}
|
||||
|
||||
/// <summary>Every client aggregate.</summary>
|
||||
|
||||
+26
-10
@@ -29,19 +29,18 @@ public class HistoryReadPagingTests
|
||||
Calls.Add(new Call(cp, release));
|
||||
if (release) return Task.FromResult<HistoryReadResult?>(new HistoryReadResult { StatusCode = StatusCodes.Good });
|
||||
var (count, next) = _pages.Dequeue();
|
||||
var data = new HistoryData();
|
||||
for (var i = 0; i < count; i++) data.DataValues.Add(new DataValue(new Variant(i)));
|
||||
var data = new HistoryData { DataValues = Enumerable.Range(0, count).Select(i => new DataValue(Variant.From(i))).ToArrayOf() };
|
||||
return Task.FromResult<HistoryReadResult?>(new HistoryReadResult
|
||||
{
|
||||
StatusCode = StatusCodes.Good,
|
||||
ContinuationPoint = next,
|
||||
ContinuationPoint = ByteString.From(next),
|
||||
HistoryData = new ExtensionObject(data)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<DataValue> Values(HistoryReadResult r) =>
|
||||
r.HistoryData?.Body is HistoryData d ? d.DataValues : [];
|
||||
r.HistoryData.TryGetValue(out HistoryData? d) ? d.DataValues.ToArray() ?? [] : [];
|
||||
|
||||
/// <summary>
|
||||
/// A server page limit below the caller's cap hands back a continuation point: the follow-up must READ the next
|
||||
@@ -136,8 +135,7 @@ public class HistoryReadPagingTests
|
||||
calls.Add((cp, release, ct.IsCancellationRequested));
|
||||
if (release) return Task.FromResult<HistoryReadResult?>(new HistoryReadResult { StatusCode = StatusCodes.Good });
|
||||
if (cp != null) { cts.Cancel(); ct.ThrowIfCancellationRequested(); }
|
||||
var data = new HistoryData();
|
||||
data.DataValues.Add(new DataValue(new Variant(1)));
|
||||
var data = new HistoryData { DataValues = [new DataValue(Variant.From(1))] };
|
||||
return Task.FromResult<HistoryReadResult?>(new HistoryReadResult
|
||||
{
|
||||
StatusCode = StatusCodes.Good, ContinuationPoint = [5], HistoryData = new ExtensionObject(data)
|
||||
@@ -165,8 +163,7 @@ public class HistoryReadPagingTests
|
||||
releaseSawCancelled = ct.IsCancellationRequested;
|
||||
return Task.FromResult<HistoryReadResult?>(new HistoryReadResult { StatusCode = StatusCodes.Good });
|
||||
}
|
||||
var data = new HistoryData();
|
||||
data.DataValues.Add(new DataValue(new Variant(1)));
|
||||
var data = new HistoryData { DataValues = [new DataValue(Variant.From(1))] };
|
||||
cts.Cancel(); // the caller gives up right as the capped page arrives
|
||||
return Task.FromResult<HistoryReadResult?>(new HistoryReadResult
|
||||
{
|
||||
@@ -189,8 +186,7 @@ public class HistoryReadPagingTests
|
||||
{
|
||||
calls++;
|
||||
if (release) throw new ServiceResultException(StatusCodes.BadSessionClosed);
|
||||
var data = new HistoryData();
|
||||
data.DataValues.Add(new DataValue(new Variant(1)));
|
||||
var data = new HistoryData { DataValues = [new DataValue(Variant.From(1))] };
|
||||
return Task.FromResult<HistoryReadResult?>(new HistoryReadResult
|
||||
{
|
||||
StatusCode = StatusCodes.Good, ContinuationPoint = [9], HistoryData = new ExtensionObject(data)
|
||||
@@ -216,4 +212,24 @@ public class HistoryReadPagingTests
|
||||
await Should.ThrowAsync<ServiceResultException>(() =>
|
||||
HistoryReadPaging.ReadAllAsync(Read, Values, maxValues: 10, Node, CancellationToken.None));
|
||||
}
|
||||
[Fact]
|
||||
public async Task RefusedContinuationPage_ReleasesCursorAndPreservesStatus()
|
||||
{
|
||||
var releases = new List<byte[]>();
|
||||
Task<HistoryReadResult?> Read(byte[]? cp, bool release, CancellationToken ct)
|
||||
{
|
||||
if (release)
|
||||
{
|
||||
releases.Add(cp!);
|
||||
throw new ServiceResultException(StatusCodes.BadSessionClosed);
|
||||
}
|
||||
return Task.FromResult<HistoryReadResult?>(cp == null
|
||||
? new HistoryReadResult { StatusCode = StatusCodes.Good, ContinuationPoint = [7] }
|
||||
: new HistoryReadResult { StatusCode = StatusCodes.BadUserAccessDenied });
|
||||
}
|
||||
var exception = await Should.ThrowAsync<ServiceResultException>(() =>
|
||||
HistoryReadPaging.ReadAllAsync(Read, Values, 10, Node, TestContext.Current.CancellationToken));
|
||||
exception.StatusCode.ShouldBe(StatusCodes.BadUserAccessDenied);
|
||||
releases.ShouldHaveSingleItem().ShouldBe(new byte[] { 7 });
|
||||
}
|
||||
}
|
||||
|
||||
+11
-14
@@ -7,32 +7,29 @@ namespace ZB.MOM.WW.OtOpcUa.Client.Shared.Tests.Helpers;
|
||||
|
||||
public class HistoryReadStatusTests
|
||||
{
|
||||
public static IEnumerable<object[]> Refusals() => new[] { StatusCodes.BadHistoryOperationUnsupported,
|
||||
StatusCodes.BadUserAccessDenied, StatusCodes.BadNodeIdUnknown, StatusCodes.BadTimeout }.Select(s => new object[] { s });
|
||||
public static IEnumerable<object[]> NonRefusals() => new[] { StatusCodes.Good, StatusCodes.GoodNoData,
|
||||
StatusCodes.GoodMoreData, StatusCodes.UncertainNoCommunicationLastUsableValue, StatusCodes.BadNoData }.Select(s => new object[] { s });
|
||||
/// <summary>A refused read must surface, carrying the status code, rather than read as an empty result.</summary>
|
||||
/// <param name="code">The Bad per-node status code.</param>
|
||||
[Theory]
|
||||
[InlineData(StatusCodes.BadHistoryOperationUnsupported)]
|
||||
[InlineData(StatusCodes.BadUserAccessDenied)]
|
||||
[InlineData(StatusCodes.BadNodeIdUnknown)]
|
||||
[InlineData(StatusCodes.BadTimeout)]
|
||||
public void Bad_status_throws_ServiceResultException_with_the_code(uint code)
|
||||
[MemberData(nameof(Refusals))]
|
||||
public void Bad_status_throws_ServiceResultException_with_the_code(StatusCode code)
|
||||
{
|
||||
var ex = Should.Throw<ServiceResultException>(
|
||||
() => HistoryReadStatus.ThrowIfRefused(new StatusCode(code), new NodeId("n", 2)));
|
||||
() => HistoryReadStatus.ThrowIfRefused(code, new NodeId("n", 2)));
|
||||
|
||||
ex.StatusCode.ShouldBe(code);
|
||||
ex.StatusCode.ShouldBe(code.Code);
|
||||
ex.Message.ShouldContain("ns=2;s=n");
|
||||
}
|
||||
|
||||
/// <summary>Good, GoodNoData, Uncertain and BadNoData are all non-refusals (the window may simply be empty).</summary>
|
||||
/// <param name="code">The non-refusal status code.</param>
|
||||
[Theory]
|
||||
[InlineData(StatusCodes.Good)]
|
||||
[InlineData(StatusCodes.GoodNoData)]
|
||||
[InlineData(StatusCodes.GoodMoreData)]
|
||||
[InlineData(StatusCodes.UncertainNoCommunicationLastUsableValue)]
|
||||
[InlineData(StatusCodes.BadNoData)]
|
||||
public void Non_refusal_status_does_not_throw(uint code)
|
||||
[MemberData(nameof(NonRefusals))]
|
||||
public void Non_refusal_status_does_not_throw(StatusCode code)
|
||||
{
|
||||
Should.NotThrow(() => HistoryReadStatus.ThrowIfRefused(new StatusCode(code), new NodeId("n", 2)));
|
||||
Should.NotThrow(() => HistoryReadStatus.ThrowIfRefused(code, new NodeId("n", 2)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
using Opc.Ua;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Client.Shared.Tests.Helpers;
|
||||
|
||||
public class VariantBoundaryTests
|
||||
{
|
||||
public static IEnumerable<object?[]> Payloads()
|
||||
{
|
||||
foreach (var value in new object?[] { null, true, (sbyte)-1, (byte)1, (short)-2, (ushort)2,
|
||||
-3, 3u, -4L, 4UL, 1.25f, 2.5, "", "text", Guid.Parse("de305d54-75b4-431b-adb2-eb6b9e546014"),
|
||||
new DateTime(2026, 11, 10, 0, 0, 0, DateTimeKind.Utc), Array.Empty<byte>(), new byte[] { 1, 2 },
|
||||
Array.Empty<int>(), new[] { 1 }, new[] { 1, 2 }, Array.Empty<string>(), new[] { "", "text" } })
|
||||
yield return [value];
|
||||
foreach (var value in new object[] { true, (sbyte)-1, (byte)1, (short)-2, (ushort)2,
|
||||
-3, 3u, -4L, 4UL, 1.25f, 2.5, "text", Guid.Empty,
|
||||
new DateTime(2026, 11, 10, 0, 0, 0, DateTimeKind.Utc) })
|
||||
{
|
||||
foreach (var count in new[] { 0, 1, 3 })
|
||||
{
|
||||
var array = Array.CreateInstance(value.GetType(), count);
|
||||
for (var i = 0; i < count; i++) array.SetValue(value, i);
|
||||
yield return [array];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Payloads))]
|
||||
public void RoundTrip_PreservesClrTypeAndValue(object? expected)
|
||||
{
|
||||
var actual = ValueConverter.ToClrObject(ValueConverter.ToVariant(expected));
|
||||
if (expected == null) actual.ShouldBeNull();
|
||||
else
|
||||
{
|
||||
actual.ShouldNotBeNull();
|
||||
actual.GetType().ShouldBe(expected.GetType());
|
||||
actual.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RetainedArrays_AreDetachedFromCaller()
|
||||
{
|
||||
var bytes = new byte[] { 1, 2 };
|
||||
var numbers = new[] { 3, 4 };
|
||||
var byteValue = ValueConverter.ToVariant(bytes);
|
||||
var numericValue = ValueConverter.ToVariant(numbers);
|
||||
bytes[0] = 99;
|
||||
numbers[0] = 99;
|
||||
ValueConverter.ToClrObject(byteValue).ShouldBe(new byte[] { 1, 2 });
|
||||
ValueConverter.ToClrObject(numericValue).ShouldBe(new[] { 3, 4 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ByteString_NullAndEmptyRemainDistinct()
|
||||
{
|
||||
ValueConverter.ToClrObject(Variant.From(default(ByteString))).ShouldBeNull();
|
||||
ValueConverter.ToClrObject(Variant.From(ByteString.Empty)).ShouldBe(Array.Empty<byte>());
|
||||
ValueConverter.ToClrObject(Variant.Null).ShouldBeNull();
|
||||
ValueConverter.ToClrObject(Variant.From(0)).ShouldBe(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NullWrapperInputs_RemainNullAfterRetention()
|
||||
{
|
||||
ValueConverter.ToClrObject(ValueConverter.ToVariant(default(ByteString))).ShouldBeNull();
|
||||
ValueConverter.ToClrObject(ValueConverter.ToVariant(default(ArrayOf<int>))).ShouldBeNull();
|
||||
ValueConverter.ToVariant(default(ArrayOf<byte>)).TypeInfo.ValueRank.ShouldBe(ValueRanks.OneDimension);
|
||||
ValueConverter.ToClrObject(ValueConverter.ToVariant(Variant.From(default(ByteString)))).ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ReadOnlyWrappers_AreDetachedFromMutableBackingBuffers()
|
||||
{
|
||||
var bytes = new byte[] { 1, 2 };
|
||||
var numbers = new[] { 3, 4 };
|
||||
var retainedBytes = ValueConverter.ToVariant(new ByteString(bytes));
|
||||
var retainedNumbers = ValueConverter.ToVariant(new ArrayOf<int>(numbers));
|
||||
bytes[0] = 99;
|
||||
numbers[0] = 99;
|
||||
ValueConverter.ToClrObject(retainedBytes).ShouldBe(new byte[] { 1, 2 });
|
||||
ValueConverter.ToClrObject(retainedNumbers).ShouldBe(new[] { 3, 4 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RetainedByteStringArray_CopiesElementBuffersAndPreservesNullEmpty()
|
||||
{
|
||||
var bytes = new byte[] { 1, 2 };
|
||||
var value = ValueConverter.ToVariant(new ByteString[] { default, ByteString.Empty, new(bytes) });
|
||||
bytes[0] = 99;
|
||||
var actual = ValueConverter.ToClrObject(value).ShouldBeOfType<byte[][]>();
|
||||
actual[0].ShouldBeNull();
|
||||
actual[1].ShouldBeEmpty();
|
||||
actual[2].ShouldBe(new byte[] { 1, 2 });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ByteArrayAndByteString_KeepWireRank()
|
||||
{
|
||||
ValueConverter.ToVariant(new byte[] { 1 }).TypeInfo.BuiltInType.ShouldBe(BuiltInType.ByteString);
|
||||
ValueConverter.ToVariant(new ArrayOf<byte>(new byte[] { 1 })).TypeInfo.BuiltInType.ShouldBe(BuiltInType.Byte);
|
||||
ValueConverter.ToVariant(new ArrayOf<byte>(new byte[] { 1 })).TypeInfo.ValueRank.ShouldBe(ValueRanks.OneDimension);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NodeIds_KeepOpaqueBytesAndLiteralStringIdentifiers()
|
||||
{
|
||||
var opaque = NodeId.Parse("ns=2;b=AQID");
|
||||
ValueConverter.ToClrObject(ValueConverter.ToVariant(opaque)).ShouldBe(opaque);
|
||||
var literal = new NodeId("ns=2;s=literal", 4);
|
||||
var result = ValueConverter.ToClrObject(ValueConverter.ToVariant(literal)).ShouldBeOfType<NodeId>();
|
||||
result.NamespaceIndex.ShouldBe((ushort)4);
|
||||
result.TryGetValue(out string? identifier).ShouldBeTrue();
|
||||
identifier.ShouldBe("ns=2;s=literal");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnsupportedShape_IsRejected()
|
||||
{
|
||||
Should.Throw<ServiceResultException>(() => ValueConverter.ToVariant(new int[2, 2]));
|
||||
Should.Throw<ServiceResultException>(() => ValueConverter.ToVariant(new object()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user