189 lines
10 KiB
C#
189 lines
10 KiB
C#
using Opc.Ua;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Authorization;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.OpcUaServer;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
|
|
|
|
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "P08")]
|
|
public sealed class GrpcDataProjectionApplierTests
|
|
{
|
|
[Fact(Timeout = 15000)]
|
|
public async Task Cursor_is_committed_only_by_successful_atomic_application()
|
|
{
|
|
var catalog = new Catalog();
|
|
var sink = new Sink();
|
|
using var applier = new ProjectedNamespaceApplier(catalog, sink, 2);
|
|
var request = Request();
|
|
sink.Accept = false;
|
|
(await applier.ApplyPageAsync(request, new NodeId("parent", 2), TestContext.Current.CancellationToken)).ShouldBeFalse();
|
|
catalog.LastPage.ShouldNotBeNull();
|
|
catalog.LastPage.Commits.ShouldBe(0);
|
|
catalog.LastPage.Disposed.ShouldBeTrue();
|
|
sink.Accept = true;
|
|
(await applier.ApplyPageAsync(request, new NodeId("parent", 2), TestContext.Current.CancellationToken)).ShouldBeTrue();
|
|
catalog.LastPage.ShouldNotBeNull();
|
|
catalog.LastPage.Commits.ShouldBe(1);
|
|
catalog.LastPage.Disposed.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact(Timeout = 15000)]
|
|
public async Task Authority_expiring_before_application_never_commits_the_cursor()
|
|
{
|
|
var catalog = new Catalog();
|
|
var sink = new Sink { BeforeCommit = () => catalog.Lease.Valid = false };
|
|
using var applier = new ProjectedNamespaceApplier(catalog, sink, 2);
|
|
(await applier.ApplyPageAsync(Request(), new NodeId("parent", 2), TestContext.Current.CancellationToken)).ShouldBeFalse();
|
|
catalog.LastPage.ShouldNotBeNull();
|
|
catalog.LastPage.Commits.ShouldBe(0);
|
|
catalog.LastPage.Disposed.ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Invalid_continuation_is_preserved_from_preparation_without_cursor_commit()
|
|
{
|
|
var catalog = new InvalidCatalog();
|
|
using var applier = new ProjectedNamespaceApplier(catalog, new Sink(), 2);
|
|
var status = await applier.ApplyPageResultAsync(Request(), new NodeId("parent", 2), TestContext.Current.CancellationToken);
|
|
status.ShouldBe(ProjectionPageStatus.InvalidContinuation);
|
|
}
|
|
|
|
[Fact(Timeout = 15000)]
|
|
[Trait("Phase", "P08Unit")]
|
|
public async Task Materialized_authority_checks_only_its_own_item_and_remains_valid_after_prepare_disposal()
|
|
{
|
|
var catalog = new GuardCatalog();
|
|
var sink = new GuardSink();
|
|
using var applier = new ProjectedNamespaceApplier(catalog, sink, 1);
|
|
(await applier.ApplyPageAsync(Request(), new NodeId("parent", 2), TestContext.Current.CancellationToken)).ShouldBeTrue();
|
|
catalog.Page.Disposed.ShouldBeTrue();
|
|
sink.Nodes.Count.ShouldBe(12);
|
|
foreach (var lease in catalog.Leases) lease.Checks = 0;
|
|
for (var i = 0; i < 50; i++) sink.Nodes[0].AuthorityIsCurrent().ShouldBeTrue();
|
|
catalog.Leases.Skip(1).Sum(x => x.Checks).ShouldBe(0, "checking one retained definition must not rescan its page");
|
|
var retained = sink.Nodes[0].AuthorityIsCurrent.Target.ShouldBeAssignableTo<IProjectedRetainedAuthority>();
|
|
retained!.RetainedBytes.ShouldBeGreaterThan(0);
|
|
catalog.Leases[0].Valid = false;
|
|
sink.Nodes[0].AuthorityIsCurrent().ShouldBeFalse();
|
|
sink.Nodes[1].AuthorityIsCurrent().ShouldBeTrue();
|
|
}
|
|
|
|
private sealed class CountingLease : IDataAuthorizationLease
|
|
{
|
|
public int Checks;
|
|
public bool Valid = true;
|
|
public bool IsValid { get { Checks++; return Valid; } }
|
|
public ServingIdentity Serving { get; } = new("node", "boot", "Primary", 1, 1, 1);
|
|
}
|
|
private sealed class GuardCatalog : IFederatedProjectionCatalog
|
|
{
|
|
public readonly CountingLease[] Leases = Enumerable.Range(0, 12).Select(_ => new CountingLease()).ToArray();
|
|
public readonly GuardPage Page;
|
|
public GuardCatalog()
|
|
{
|
|
var authority = new Authority();
|
|
ProjectedNodeProof Proof(int i)
|
|
{
|
|
var view = new DataView(new("origin", "urn:raw", "string", "child" + i), DataRealm.Raw, "mapping", 1, "Plant/Mount/child" + i);
|
|
var subject = new MappedNamespaceSubject(view,
|
|
new NodeScope { ClusterId = "cluster", Kind = NodeHierarchyKind.Raw, Segments = ["Plant", "Mount", "child" + i] },
|
|
["folder"], Leases[i].Serving, DataOperations.Browse);
|
|
var resolver = new MappedSubjectResolver([subject], 1, authority);
|
|
var metadata = new DataMetadata(view, "child" + i, "Tag", DataValueKind.Int32, -1, [], false,
|
|
DataOperations.Browse, 1, null, null, false, Serving: Leases[i].Serving);
|
|
var origin = metadata with { View = view with { MappingId = null, MappingRevision = 0, Subject = "origin/child" + i } };
|
|
return new ProjectedNodeProof(ProjectedDataIds.Identifier("mapping", view.Address), metadata, subject, resolver, Leases[i], 1, origin);
|
|
}
|
|
var items = Enumerable.Range(0, 12).Select(Proof).ToArray();
|
|
Page = new GuardPage(new(ProjectionPageStatus.Ready, items, true, null, 1, Leases[0], authority));
|
|
}
|
|
public TimeProvider Clock => TimeProvider.System;
|
|
public event Action<string, long>? Invalidated { add { } remove { } }
|
|
public ProjectedParentSnapshot TryGetParent(ProjectedBrowseRequest request) => Page.Snapshot;
|
|
public IProjectedPageGuard? CaptureParentGuard(ProjectedBrowseRequest request, ProjectedParentSnapshot expected) => new PageGuard();
|
|
public Task<IProjectedPreparedPage> PreparePageAsync(ProjectedBrowseRequest request, CancellationToken cancellationToken) => Task.FromResult<IProjectedPreparedPage>(Page);
|
|
public bool TryResolveProjected(DataRealm realm, string localId, out ProjectedNodeProof? proof) { proof = null; return false; }
|
|
public bool TryPinProjected(DataRealm realm, string localId, out IDisposable? pin) { pin = null; return false; }
|
|
private sealed class PageGuard : IProjectedPageGuard { public bool IsCurrent => true; public long RetainedBytes => 64; }
|
|
}
|
|
private sealed class GuardPage(ProjectedParentSnapshot snapshot) : IProjectedPreparedPage
|
|
{
|
|
public bool Disposed;
|
|
public ProjectedParentSnapshot Snapshot => snapshot;
|
|
public bool TryCommit() => true;
|
|
public void Dispose() => Disposed = true;
|
|
}
|
|
private sealed class GuardSink : IProjectedNamespacePageSink
|
|
{
|
|
public IReadOnlyList<ProjectedVariableDefinition> Nodes = [];
|
|
public bool TryApply(NodeId parent, IReadOnlyList<ProjectedVariableDefinition> nodes, Func<bool> commit)
|
|
{ Nodes = nodes; return commit(); }
|
|
public bool TryRemove(NodeId nodeId, ProjectedVariableDefinition expected) => false;
|
|
}
|
|
|
|
private sealed class InvalidCatalog : IFederatedProjectionCatalog
|
|
{
|
|
public TimeProvider Clock => TimeProvider.System;
|
|
public event Action<string, long>? Invalidated { add { } remove { } }
|
|
public ProjectedParentSnapshot TryGetParent(ProjectedBrowseRequest request) => new(ProjectionPageStatus.Pending, [], false, null, 0);
|
|
public Task<IProjectedPreparedPage> PreparePageAsync(ProjectedBrowseRequest request, CancellationToken cancellationToken) => Task.FromResult<IProjectedPreparedPage>(new InvalidPage());
|
|
public bool TryResolveProjected(DataRealm realm, string localId, out ProjectedNodeProof? proof) { proof = null; return false; }
|
|
public bool TryPinProjected(DataRealm realm, string localId, out IDisposable? pin) { pin = null; return false; }
|
|
private sealed class InvalidPage : IProjectedPreparedPage
|
|
{
|
|
public ProjectedParentSnapshot Snapshot => new(ProjectionPageStatus.InvalidContinuation, [], false, null, 0);
|
|
public bool TryCommit() => throw new InvalidOperationException("Invalid continuation must not commit.");
|
|
public void Dispose() { }
|
|
}
|
|
}
|
|
|
|
private static ProjectedBrowseRequest Request() => new("mapping",
|
|
new(new("origin", "urn:raw", "string", "root"), DataRealm.Raw, "mapping", 1, "Plant/Mount"),
|
|
null, "opaque-cursor", new("reader", "key", "version", null), new(TimeSpan.FromSeconds(5), 16, 4096), TimeProvider.System.GetTimestamp());
|
|
|
|
private sealed class Lease : IDataAuthorizationLease
|
|
{
|
|
public bool Valid = true;
|
|
public bool IsValid => Valid;
|
|
public ServingIdentity Serving { get; } = new("node", "boot", "Primary", 1, 1, 1);
|
|
}
|
|
private sealed class Authority : IDataMappedCatalogAuthority
|
|
{
|
|
public long CatalogEpoch => 1;
|
|
public long AuthorityEpoch => 1;
|
|
public bool IsActive => true;
|
|
}
|
|
private sealed class Page(Lease lease) : IProjectedPreparedPage
|
|
{
|
|
public int Commits;
|
|
public bool Disposed;
|
|
public ProjectedParentSnapshot Snapshot { get; } = new(ProjectionPageStatus.Ready, [], true, null, 1, lease, new Authority());
|
|
public bool TryCommit() { Commits++; return true; }
|
|
public void Dispose() => Disposed = true;
|
|
}
|
|
private sealed class Sink : IProjectedNamespacePageSink
|
|
{
|
|
public bool Accept = true;
|
|
public Action? BeforeCommit;
|
|
public bool TryApply(NodeId parent, IReadOnlyList<ProjectedVariableDefinition> nodes, Func<bool> commit)
|
|
{ if (!Accept) return false; BeforeCommit?.Invoke(); return commit(); }
|
|
public bool TryRemove(NodeId nodeId, ProjectedVariableDefinition expected) => false;
|
|
}
|
|
private sealed class Catalog : IFederatedProjectionCatalog
|
|
{
|
|
public Lease Lease { get; } = new();
|
|
public Page? LastPage;
|
|
public TimeProvider Clock => TimeProvider.System;
|
|
public event Action<string, long>? Invalidated { add { } remove { } }
|
|
public ProjectedParentSnapshot TryGetParent(ProjectedBrowseRequest request) => new(ProjectionPageStatus.Pending, [], false, null, 0);
|
|
public Task<IProjectedPreparedPage> PreparePageAsync(ProjectedBrowseRequest request, CancellationToken cancellationToken)
|
|
{ LastPage = new Page(Lease); return Task.FromResult<IProjectedPreparedPage>(LastPage); }
|
|
public bool TryResolveProjected(DataRealm realm, string localId, out ProjectedNodeProof? proof) { proof = null; return false; }
|
|
public bool TryPinProjected(DataRealm realm, string localId, out IDisposable? pin) { pin = null; return false; }
|
|
}
|
|
}
|