e66f072121
DataProtoMapper.DecodeMetadata decodes an undefined ValueKind / ArrayElementKind number to an unknown-kind marker (DataMetadata.HasUnknownKind, ValidateDecoded) instead of throwing; Validate/Encode still refuse it. The resolver's #941 IsMaterialisable skips the marker whatever its node kind (a Folder included); a remote root carrying it is refused. An undefined per-item metadata status decodes to Unavailable (no value, no lease). Unspecified (0), bad array shape, unknown realm and unknown branch status still fail closed.
116 lines
7.9 KiB
C#
116 lines
7.9 KiB
C#
using Opc.Ua;
|
|
using Opc.Ua.Client;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
|
using DataBrowseRequest = ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.BrowseRequest;
|
|
using DataReadRequest = ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.ReadRequest;
|
|
using DataReadResult = ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections.ReadResult;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
|
|
|
|
/// <summary>
|
|
/// #941: the consumer's browse path (resolver → applier → node manager → real SDK Browse/BrowseNext) tolerates what an
|
|
/// origin may legitimately send — a child of a kind this build cannot materialise is skipped instead of failing the
|
|
/// whole folder, and an empty <c>Complete = false</c> page (the origin's name filter can produce one) is paged through.
|
|
/// </summary>
|
|
public sealed partial class GrpcDataConfiguredDiscoveryWireTests
|
|
{
|
|
[Theory(Timeout = 90000)]
|
|
[InlineData("UnsupportedVariable", DataValueKind.Null)] // a pre-#917 origin's alarm-bearing / unsupported value node
|
|
[InlineData("Method", DataValueKind.Null)] // a node kind a newer origin might add
|
|
[InlineData("Variable", DataValueKind.Guid)] // a value kind the consumer has no OPC UA type for
|
|
[InlineData("Variable", DataValueKind.Bytes)]
|
|
[InlineData("Variable", (DataValueKind)99)] // #956: a value-kind number a newer origin added (decoded as a marker)
|
|
[InlineData("Folder", (DataValueKind)99)] // #956: even a Folder — a supported node kind — is skipped, not Validate()d
|
|
public async Task A_child_of_an_unmaterialisable_kind_is_skipped_not_the_whole_folder_941(string nodeKind, DataValueKind valueKind)
|
|
{
|
|
using var origin = new LocalDataFixture(additionalRawNodes: 2, servingGeneration: 1);
|
|
var connections = new TamperingConnections(origin) { InjectKind = (nodeKind, valueKind) };
|
|
await WithComposedServer(origin, origin.Root, async (session, _, _, _, _, _, anchor) =>
|
|
{
|
|
// Page size 2: each origin page is one real child plus the injected one, so every page mixes the two.
|
|
var children = await BrowseAll((SessionClient)session, anchor, 2);
|
|
children.Select(x => x.BrowseName.Name).Order(StringComparer.Ordinal).ShouldBe(["Speed", "Speed0", "Speed1"]);
|
|
children.ShouldAllBe(x => x.NodeClass == NodeClass.Variable);
|
|
connections.Injected.ShouldBeGreaterThan(0, "the origin page carried the unmaterialisable item");
|
|
}, connectionSource: connections);
|
|
}
|
|
|
|
[Fact(Timeout = 90000)]
|
|
public async Task An_empty_incomplete_origin_page_is_paged_through_by_sdk_BrowseNext_941()
|
|
{
|
|
using var origin = new LocalDataFixture(additionalRawNodes: 2, servingGeneration: 1);
|
|
var connections = new TamperingConnections(origin) { EmptyFirstPage = true };
|
|
await WithComposedServer(origin, origin.Root, async (session, _, _, _, _, _, anchor) =>
|
|
{
|
|
var children = await BrowseAll((SessionClient)session, anchor, 1);
|
|
children.Select(x => x.BrowseName.Name).Order(StringComparer.Ordinal).ShouldBe(["Speed", "Speed0", "Speed1"]);
|
|
connections.EmptyPagesServed.ShouldBeGreaterThan(0, "the origin served an empty Complete=false page");
|
|
}, connectionSource: connections);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Wraps the origin's direct connection and rewrites Browse answers: <see cref="InjectKind"/> adds one item of that
|
|
/// kind to every Good page of the root's children; <see cref="EmptyFirstPage"/> answers the first page of every
|
|
/// browse with no items, <c>Complete = false</c> and a token that resumes at the origin's real first page.
|
|
/// </summary>
|
|
private sealed class TamperingConnections(LocalDataFixture peer) : IGrpcDataAdapterConnectionSource
|
|
{
|
|
private const string ResumeToken = "resume-at-first-page";
|
|
private int _injected, _empty;
|
|
internal (string NodeKind, DataValueKind ValueKind)? InjectKind { get; init; }
|
|
internal bool EmptyFirstPage { get; init; }
|
|
internal int Injected => Volatile.Read(ref _injected);
|
|
internal int EmptyPagesServed => Volatile.Read(ref _empty);
|
|
|
|
public ValueTask<IGrpcDataAdapterConnectionLease?> AcquireAsync(string connectionId, CancellationToken cancellationToken) =>
|
|
ValueTask.FromResult<IGrpcDataAdapterConnectionLease?>(connectionId == "link" ? new Lease(new Connection(peer.Connection(), this)) : null);
|
|
|
|
private sealed class Lease(IDataConnection connection) : IGrpcDataAdapterConnectionLease
|
|
{
|
|
public IDataConnection Connection => connection;
|
|
public CancellationToken CancellationToken => CancellationToken.None;
|
|
public ValueTask DisposeAsync() => connection.DisposeAsync();
|
|
}
|
|
|
|
private sealed class Connection(IDataConnection inner, TamperingConnections owner) : IDataConnection
|
|
{
|
|
public DataTarget Target => inner.Target;
|
|
public TimeProvider Clock => inner.Clock;
|
|
public Task<DataCapabilities> GetCapabilitiesAsync(DataBudget budget, CancellationToken cancellationToken) => inner.GetCapabilitiesAsync(budget, cancellationToken);
|
|
public async Task<DataBrowseBranch> BrowseAsync(DataBrowseRequest request, CancellationToken cancellationToken)
|
|
{
|
|
if (owner.EmptyFirstPage && request.Continuation is null)
|
|
{
|
|
var real = await inner.BrowseAsync(request, cancellationToken);
|
|
if (real.Status != DataItemStatus.Good) return real;
|
|
Interlocked.Increment(ref owner._empty);
|
|
return real with { Items = [], Complete = false, Continuation = ResumeToken };
|
|
}
|
|
if (request.Continuation == ResumeToken) request = request with { Continuation = null };
|
|
if (owner.InjectKind is not { } kind || request.Budget.MaxItems < 2) return await inner.BrowseAsync(request, cancellationToken);
|
|
// Leave room for the injected item so the page stays within the consumer's item budget.
|
|
var branch = await inner.BrowseAsync(request with { Budget = request.Budget with { MaxItems = request.Budget.MaxItems - 1 } }, cancellationToken);
|
|
if (branch.Status != DataItemStatus.Good || branch.Items.Count == 0) return branch;
|
|
var template = branch.Items[0];
|
|
var identifier = request.Parent.Address.Identifier + "/Ghost" + branch.Items.Count;
|
|
var ghost = template with
|
|
{
|
|
View = template.View with { Address = template.View.Address with { Identifier = identifier }, Subject = identifier },
|
|
BrowseName = "Ghost" + branch.Items.Count, NodeKind = kind.NodeKind, ValueKind = kind.ValueKind,
|
|
ValueRank = -1, Dimensions = [], ArrayElementKind = null, Writable = false, History = null,
|
|
};
|
|
Interlocked.Increment(ref owner._injected);
|
|
return branch with { Items = [ghost, .. branch.Items] };
|
|
}
|
|
public Task<IReadOnlyList<DataItemResult<DataMetadata>>> GetMetadataAsync(MetadataRequest request, CancellationToken cancellationToken) => inner.GetMetadataAsync(request, cancellationToken);
|
|
public Task<IReadOnlyList<DataReadResult>> ReadAsync(DataReadRequest request, CancellationToken cancellationToken) => inner.ReadAsync(request, cancellationToken);
|
|
public Task<IReadOnlyList<WriteItemResult>> WriteAsync(WriteBatch request, CancellationToken cancellationToken) => inner.WriteAsync(request, cancellationToken);
|
|
public Task<IDataSubscription> SubscribeAsync(CancellationToken cancellationToken) => inner.SubscribeAsync(cancellationToken);
|
|
public ValueTask DisposeAsync() => inner.DisposeAsync();
|
|
}
|
|
}
|
|
}
|