9d194a7e97
OriginHistoryPort (public, implements IOriginHistoryPort) keeps SDK types inside OpcUaServer instead of an InternalsVisibleTo: runs the synchronous ReadHistory off the caller's thread, one PeerHistoryContinuationScope per request, absent bounds -> port MinValue, miswired scope surfaced as WiringFault. Exposed through the SDK sink so DeferredOriginHistoryPort resolves it after server start; aggregate wire table pinned to AggregateFunctionMap.
141 lines
7.8 KiB
C#
141 lines
7.8 KiB
C#
using Opc.Ua;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests;
|
|
|
|
/// <summary>
|
|
/// H2: the SDK-free <see cref="OriginHistoryPort"/> facade a remote-history transport uses over the H1 port — served off
|
|
/// the caller's thread, one continuation scope per request, absent bounds mapped to the port's "unset", a miswired scope
|
|
/// surfaced distinctly, and the aggregate wire-name table pinned to the SDK's AggregateFunction ids.
|
|
/// </summary>
|
|
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "H2")]
|
|
public sealed class OriginHistoryPortTests(HistoryReadPortServerFixture fx) : IClassFixture<HistoryReadPortServerFixture>
|
|
{
|
|
private static readonly DateTime T0 = new(2026, 3, 1, 10, 0, 0, DateTimeKind.Utc);
|
|
|
|
[Fact]
|
|
public void The_aggregate_wire_table_matches_the_servers_aggregate_function_map_exactly()
|
|
{
|
|
DataHistoryAggregates.IdByName.Count.ShouldBe(AggregateFunctionMap.ByNodeId.Count);
|
|
foreach (var (nodeId, aggregate) in AggregateFunctionMap.ByNodeId)
|
|
{
|
|
nodeId.NamespaceIndex.ShouldBe((ushort)0);
|
|
nodeId.TryGetValue(out uint id).ShouldBeTrue();
|
|
DataHistoryAggregates.IdByName[aggregate.ToString()].ShouldBe(id, aggregate.ToString());
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Serving_is_null_without_a_historian_and_names_the_backends_aggregates_with_one()
|
|
{
|
|
await Run(async () =>
|
|
{
|
|
var port = new OriginHistoryPort(fx.Nm);
|
|
port.Serving.ShouldBeNull("the Null source serves nothing remotely");
|
|
fx.Nm.HistorianDataSource = new HistoryReadPortParityTests.ParitySource();
|
|
var serving = port.Serving.ShouldNotBeNull();
|
|
serving.Aggregates.ShouldContain("Average");
|
|
serving.TimestampResolutionTicks.ShouldBe(1);
|
|
port.IsHistorized(AddressSpaceRealm.Uns, "port/dbl").ShouldBeTrue();
|
|
port.IsHistorized(AddressSpaceRealm.Uns, "port/plain").ShouldBeFalse();
|
|
port.IsHistorized(AddressSpaceRealm.Raw, "port/dbl").ShouldBeFalse("realm-qualified");
|
|
await Task.CompletedTask;
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Raw_pages_resume_once_through_the_cursor_store_and_a_resume_under_other_details_is_invalid()
|
|
{
|
|
await Run(async () =>
|
|
{
|
|
var source = new HistoryReadPortParityTests.ParitySource();
|
|
source.Series["P.dbl"] = Enumerable.Range(0, 5).Select(i => new DataValueSnapshot((double)i, 0, T0.AddSeconds(i), T0.AddSeconds(i))).ToList();
|
|
fx.Nm.HistorianDataSource = source;
|
|
var port = new OriginHistoryPort(fx.Nm);
|
|
var cursors = new OriginHistoryCursors("peer", "chan-1", 8);
|
|
var details = new DataHistoryRawDetails(new DateTimeOffset(T0), new DateTimeOffset(T0.AddSeconds(10)), 2, false);
|
|
var item = new OriginHistoryItem(AddressSpaceRealm.Uns, "port/dbl", "peer:uns", null, null);
|
|
|
|
var first = (await port.ReadAsync(cursors, "peer", () => "inc-A", details, [item], CancellationToken.None)).Single();
|
|
first.StatusCode.ShouldBe(StatusCodes.Good.Code);
|
|
first.Samples.Select(s => s.Value).ShouldBe(new object?[] { 0.0, 1.0 });
|
|
first.Samples[0].SourceTimestampUtc.ShouldBe(T0);
|
|
first.Continuation.ShouldNotBeNull().Length.ShouldBe(16);
|
|
cursors.Count.ShouldBe(1);
|
|
|
|
var second = (await port.ReadAsync(cursors, "peer", () => "inc-A", details, [item with { Continuation = first.Continuation }],
|
|
CancellationToken.None)).Single();
|
|
second.Samples.Select(s => s.Value).ShouldBe(new object?[] { 2.0, 3.0 });
|
|
|
|
// Single-use: the consumed token replays as invalid (a lost page is never silently skipped).
|
|
var replay = (await port.ReadAsync(cursors, "peer", () => "inc-A", details, [item with { Continuation = first.Continuation }],
|
|
CancellationToken.None)).Single();
|
|
replay.StatusCode.ShouldBe(StatusCodes.BadContinuationPointInvalid.Code);
|
|
|
|
// Another incarnation (failover) or other details ⇒ invalid.
|
|
var moved = (await port.ReadAsync(cursors, "peer", () => "inc-B", details, [item with { Continuation = second.Continuation }],
|
|
CancellationToken.None)).Single();
|
|
moved.StatusCode.ShouldBe(StatusCodes.BadContinuationPointInvalid.Code);
|
|
var third = (await port.ReadAsync(cursors, "peer", () => "inc-A", details, [item], CancellationToken.None)).Single();
|
|
var other = (await port.ReadAsync(cursors, "peer", () => "inc-A", details with { NumValuesPerNode = 3 },
|
|
[item with { Continuation = third.Continuation }], CancellationToken.None)).Single();
|
|
other.StatusCode.ShouldBe(StatusCodes.BadContinuationPointInvalid.Code);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Absent_bounds_are_the_ports_unset_so_an_end_only_read_returns_the_latest_values_newest_first()
|
|
{
|
|
await Run(async () =>
|
|
{
|
|
var source = new HistoryReadPortParityTests.ParitySource();
|
|
source.Series["P.dbl"] = Enumerable.Range(0, 5).Select(i => new DataValueSnapshot((double)i, 0, T0.AddSeconds(i), T0.AddSeconds(i))).ToList();
|
|
fx.Nm.HistorianDataSource = source;
|
|
var port = new OriginHistoryPort(fx.Nm);
|
|
var cursors = new OriginHistoryCursors("peer", "chan-1", 8);
|
|
var endOnly = new DataHistoryRawDetails(null, new DateTimeOffset(T0.AddSeconds(10)), 2, false);
|
|
var outcome = (await port.ReadAsync(cursors, "peer", () => "inc-A", endOnly,
|
|
[new OriginHistoryItem(AddressSpaceRealm.Uns, "port/dbl", "peer:uns", null, null)], CancellationToken.None)).Single();
|
|
outcome.Samples.Select(s => s.Value).ShouldBe(new object?[] { 4.0, 3.0 });
|
|
OriginHistoryPort.ToVariant(endOnly).ShouldBeOfType<HistoryReadRawVariant>().StartUtc.ShouldBe(DateTime.MinValue);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_miswired_scope_is_a_distinct_wiring_fault_never_unsupported_or_denied()
|
|
{
|
|
await Run(async () =>
|
|
{
|
|
var source = new HistoryReadPortParityTests.ParitySource();
|
|
source.Series["P.dbl"] = Enumerable.Range(0, 5).Select(i => new DataValueSnapshot((double)i, 0, T0.AddSeconds(i), T0.AddSeconds(i))).ToList();
|
|
fx.Nm.HistorianDataSource = source;
|
|
var port = new OriginHistoryPort(fx.Nm);
|
|
var cursors = new OriginHistoryCursors("peer", "chan-1", 8);
|
|
var details = new DataHistoryRawDetails(new DateTimeOffset(T0), new DateTimeOffset(T0.AddSeconds(10)), 2, false);
|
|
var item = new OriginHistoryItem(AddressSpaceRealm.Uns, "port/dbl", "peer:uns", null, null);
|
|
var blank = (await port.ReadAsync(cursors, "peer", () => "", details, [item], CancellationToken.None)).Single();
|
|
blank.WiringFault.ShouldBeTrue();
|
|
blank.StatusCode.ShouldBe(StatusCodes.BadInternalError.Code);
|
|
var stranger = (await port.ReadAsync(cursors, "someone-else", () => "inc-A", details, [item], CancellationToken.None)).Single();
|
|
stranger.WiringFault.ShouldBeTrue("a scope for a principal the store does not belong to");
|
|
var fine = (await port.ReadAsync(cursors, "peer", () => "inc-A", details, [item], CancellationToken.None)).Single();
|
|
fine.WiringFault.ShouldBeFalse();
|
|
});
|
|
}
|
|
|
|
private async Task Run(Func<Task> body)
|
|
{
|
|
await fx.Gate.WaitAsync(TestContext.Current.CancellationToken);
|
|
try { await body(); }
|
|
finally
|
|
{
|
|
fx.Nm.HistorianDataSource = NullHistorianDataSource.Instance;
|
|
fx.Gate.Release();
|
|
}
|
|
}
|
|
}
|