112 lines
6.8 KiB
C#
112 lines
6.8 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
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.Runtime.Tests.Harness;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
|
|
|
|
[Trait("Feature", "GrpcData"), Trait("Phase", "P11Renewal")]
|
|
public sealed partial class GrpcDataMetadataRenewalCacheTests
|
|
{
|
|
[Fact]
|
|
public void Overlap_is_one_charged_predecessor_and_never_extends_its_original_lease()
|
|
{
|
|
using var fixture = new RenewalCacheFixture();
|
|
var initial = fixture.Install();
|
|
fixture.Cache.TryPinProjected(DataRealm.Raw, initial.LocalIdentifier, out var oldPin).ShouldBeTrue();
|
|
fixture.Clock.Advance(TimeSpan.FromSeconds(5));
|
|
fixture.Cache.TryRenewOverlap(initial, fixture.Proof(), 1, out var successor).ShouldBeTrue();
|
|
successor.ShouldNotBeNull();
|
|
fixture.Cache.IsUsableProof(initial).ShouldBeTrue();
|
|
fixture.Cache.IsUsableProof(successor).ShouldBeTrue();
|
|
fixture.Cache.TryPinProjected(DataRealm.Raw, initial.LocalIdentifier, out var currentPin).ShouldBeTrue();
|
|
fixture.Cache.TryRenewOverlap(successor, fixture.Proof(), 1, out _).ShouldBeFalse("a second predecessor cannot be retained");
|
|
fixture.Clock.Advance(TimeSpan.FromSeconds(10));
|
|
fixture.Cache.IsUsableProof(initial).ShouldBeFalse();
|
|
fixture.Cache.IsUsableProof(successor).ShouldBeTrue();
|
|
oldPin!.Dispose();
|
|
fixture.Clock.Advance(TimeSpan.FromSeconds(1));
|
|
fixture.Cache.TryRenewOverlap(successor, fixture.Proof(), 1, out var later).ShouldBeTrue();
|
|
fixture.Cache.IsUsableProof(successor).ShouldBeTrue();
|
|
fixture.Cache.IsUsableProof(initial).ShouldBeFalse("later authority must never revive an expired predecessor");
|
|
currentPin!.Dispose();
|
|
fixture.Cache.IsUsableProof(successor).ShouldBeFalse("last predecessor pin release detaches the overlap");
|
|
fixture.Cache.IsUsableProof(later!).ShouldBeTrue();
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("acl")]
|
|
[InlineData("route")]
|
|
[InlineData("binding")]
|
|
[InlineData("target")]
|
|
[InlineData("role")]
|
|
public void Changed_authority_or_provenance_cannot_bridge_existing_evidence(string change)
|
|
{
|
|
using var fixture = new RenewalCacheFixture();
|
|
var original = fixture.Install();
|
|
fixture.Cache.TryPinProjected(DataRealm.Raw, original.LocalIdentifier, out var pin).ShouldBeTrue();
|
|
try
|
|
{
|
|
fixture.Cache.TryRenewOverlap(original, fixture.Proof(change), 1, out _).ShouldBeFalse();
|
|
fixture.Cache.IsUsableProof(original).ShouldBeTrue();
|
|
}
|
|
finally { pin!.Dispose(); }
|
|
}
|
|
|
|
private sealed class RenewalCacheFixture : IDisposable
|
|
{
|
|
private readonly LocalDataFixture _local = new(servingGeneration: 1);
|
|
private readonly IDataMappedCatalogAuthority _authority;
|
|
private readonly object _route = new(), _binding = new();
|
|
private readonly Dictionary<IDataAuthorizationLease, MetadataAuthorityLease> _remoteLeases = new();
|
|
internal bool RouteCurrent = true, BindingCurrent = true;
|
|
private readonly DataTarget _target;
|
|
private readonly ProjectedBrowseRequest _request;
|
|
internal ProjectedCatalogCache Cache { get; }
|
|
internal ManualTimeProvider Clock => _local.Clock;
|
|
internal RenewalCacheFixture(bool clusterTarget = false)
|
|
{
|
|
_target = clusterTarget ? new(DataTargetMode.Cluster, "origin", null) : new(DataTargetMode.Node, "cluster", "node-a");
|
|
var policy = new DataOperationPolicy(_local.Authority, new TriePermissionEvaluator(_local.Authority.Tries, Clock), Clock);
|
|
Cache = new(2, 3, Clock, policy);
|
|
_authority = _local.Activation.CaptureAuthority();
|
|
var view = new DataView(new DataAddress("origin", "urn:raw", "string", "plant"), DataRealm.Raw, "mapping", 1, "local/anchor");
|
|
var caller = new DataCallerIdentity(_local.Context.PrincipalId, _local.Context.ApiKeyId, _local.Context.CredentialVersion, _local.Context.CredentialExpiresUtc);
|
|
_request = new("mapping", view, null, null, caller, new(TimeSpan.FromSeconds(3), 20, 10000), Clock.GetTimestamp());
|
|
Cache.CaptureGeneration("mapping");
|
|
}
|
|
internal ProjectedNodeProof Proof(string? change = null, ServingIdentity? alternateServing = null)
|
|
{
|
|
var serving = alternateServing ?? (change == "role" ? _local.Serving with { Role = "Secondary" } : _local.Serving);
|
|
var remote = new MetadataAuthorityLease(serving, new(1, 1, TimeSpan.FromSeconds(15)), Clock.GetTimestamp(), TimeSpan.FromSeconds(15), TimeSpan.Zero, Clock);
|
|
var lease = new BoundProjectionLease(remote, CancellationToken.None, _authority, Cache, "mapping", 1, () => BindingCurrent,
|
|
() => RouteCurrent, change == "route" ? new object() : _route,
|
|
change == "target" ? _target with { ExpectedNodeId = "node-b" } : _target, change == "binding" ? new object() : _binding);
|
|
_remoteLeases.Add(lease, remote);
|
|
var operations = change == "acl" ? DataOperations.Browse | DataOperations.Read : DataOperations.Browse;
|
|
var view = _request.Parent;
|
|
var subject = new MappedNamespaceSubject(view, new NodeScope { ClusterId = "cluster", Kind = NodeHierarchyKind.Raw, Segments = ["Plant"] }, ["folder"], serving, operations);
|
|
var resolver = new MappedSubjectResolver([subject], serving.CatalogEpoch, _authority);
|
|
var metadata = new DataMetadata(view, "branch", "Folder", DataValueKind.Null, -1, [], false, operations, 1, null, null, false, Serving: serving);
|
|
var origin = metadata with { View = view with { MappingId = null, MappingRevision = 0, Subject = "origin/plant" } };
|
|
return new(ProjectedDataIds.Identifier("mapping", view.Address), metadata, subject, resolver, lease, 1, origin, TimeSpan.FromSeconds(30));
|
|
}
|
|
internal void Invalidate(ProjectedNodeProof proof) => _remoteLeases[proof.Lease].Invalidate();
|
|
internal bool TryPublishPage(ProjectedNodeProof proof, Func<(bool Success, string? Cursor)>? commit,
|
|
out ProjectedParentSnapshot? committed, int maximumMappingNodes = int.MaxValue) =>
|
|
Cache.TryPublish(_request with { RequestStartedTimestamp = Clock.GetTimestamp() },
|
|
new(ProjectionPageStatus.Ready, [proof], true, null, proof.Generation, proof.Lease, _authority, proof),
|
|
commit, out committed, maximumMappingNodes);
|
|
internal ProjectedNodeProof Install()
|
|
{
|
|
var proof = Proof();
|
|
TryPublishPage(proof, null, out _).ShouldBeTrue();
|
|
Cache.TryResolveProjected(DataRealm.Raw, proof.LocalIdentifier, out var installed).ShouldBeTrue();
|
|
return installed!;
|
|
}
|
|
public void Dispose() => _local.Dispose();
|
|
}
|
|
}
|