116 lines
7.1 KiB
C#
116 lines
7.1 KiB
C#
using System.Reflection;
|
|
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("Category", "Unit")]
|
|
[Trait("Phase", "P11")]
|
|
public sealed class GrpcDataProjectionValidityDiagnosticsTests
|
|
{
|
|
[Theory]
|
|
[InlineData("route", "RouteAlive")]
|
|
[InlineData("local", "LocalAuthorityActive")]
|
|
[InlineData("remote", "RemoteLeaseValid")]
|
|
[InlineData("binding", "MappingBindingCurrent")]
|
|
[InlineData("generation", "CacheGenerationCurrent")]
|
|
public void Diagnostics_report_the_independent_failed_fence_without_granting_authority(string failure, string failedFlag)
|
|
{
|
|
var clock = new ManualTimeProvider();
|
|
var cache = new ProjectedCatalogCache(4, 4, clock);
|
|
var generation = cache.CaptureGeneration("mapping");
|
|
var serving = new ServingIdentity("node", "incarnation", "Primary", 1, 1);
|
|
var remote = new MetadataAuthorityLease(serving, new MetadataLease(1, 1, TimeSpan.FromSeconds(15)),
|
|
clock.GetTimestamp(), TimeSpan.FromSeconds(15), TimeSpan.Zero, clock);
|
|
var local = new TestAuthority();
|
|
using var route = new CancellationTokenSource();
|
|
var bindingCurrent = true;
|
|
var lease = new BoundProjectionLease(remote, route.Token, local, cache, "mapping", generation, () => bindingCurrent);
|
|
lease.IsValid.ShouldBeTrue();
|
|
switch (failure)
|
|
{
|
|
case "route": route.Cancel(); break;
|
|
case "local": local.Active = false; break;
|
|
case "remote": clock.Advance(TimeSpan.FromSeconds(15)); break;
|
|
case "binding": bindingCurrent = false; break;
|
|
case "generation":
|
|
cache.MarkUnavailable("mapping", new DataView(new DataAddress("origin", "urn:raw", "string", "p"), DataRealm.Raw, "mapping", 1, "p"));
|
|
break;
|
|
default: throw new InvalidOperationException();
|
|
}
|
|
lease.IsValid.ShouldBeFalse();
|
|
var capture = typeof(BoundProjectionLease).GetMethod("CaptureDiagnostics", BindingFlags.Instance | BindingFlags.NonPublic);
|
|
capture.ShouldNotBeNull("the bounded diagnostic must expose each validity fence independently");
|
|
var result = capture!.Invoke(lease, null).ShouldNotBeNull();
|
|
foreach (var flag in new[] { "RouteAlive", "LocalAuthorityActive", "RemoteLeaseValid", "MappingBindingCurrent", "CacheGenerationCurrent" })
|
|
((bool)result.GetType().GetProperty(flag)!.GetValue(result)!).ShouldBe(flag != failedFlag, flag);
|
|
lease.IsValid.ShouldBeFalse("observing a failed fence must never renew or grant authority");
|
|
}
|
|
|
|
private sealed class TestAuthority : IDataMappedCatalogAuthority
|
|
{
|
|
public long CatalogEpoch => 1;
|
|
public long AuthorityEpoch => 1;
|
|
public bool Active = true;
|
|
public bool IsActive => Active;
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(true)]
|
|
[InlineData(false)]
|
|
public void Retained_cache_observation_reports_only_fixed_flags_and_never_grants_an_expired_proof(bool bound)
|
|
{
|
|
using var fixture = new LocalDataFixture();
|
|
var policy = new DataOperationPolicy(fixture.Authority, new TriePermissionEvaluator(fixture.Authority.Tries, fixture.Clock), fixture.Clock);
|
|
var cache = new ProjectedCatalogCache(4, 4, fixture.Clock, policy);
|
|
var generation = cache.CaptureGeneration("mapping");
|
|
var view = new DataView(new DataAddress("origin", "urn:raw", "string", "plant"), DataRealm.Raw, "mapping", 1, "local/anchor");
|
|
var caller = new DataCallerIdentity(fixture.Context.PrincipalId, fixture.Context.ApiKeyId, fixture.Context.CredentialVersion, fixture.Context.CredentialExpiresUtc);
|
|
var request = new ProjectedBrowseRequest("mapping", view, null, null, caller,
|
|
new DataBudget(TimeSpan.FromSeconds(3), 20, 10000), fixture.Clock.GetTimestamp());
|
|
var local = fixture.Activation.CaptureAuthority();
|
|
var serving = fixture.Serving with { ServingGeneration = 1 };
|
|
var remote = new MetadataAuthorityLease(serving, new MetadataLease(1, 1, TimeSpan.FromSeconds(15)),
|
|
fixture.Clock.GetTimestamp(), TimeSpan.FromSeconds(15), TimeSpan.Zero, fixture.Clock);
|
|
IDataAuthorizationLease lease = bound ? new BoundProjectionLease(remote, CancellationToken.None, local, cache,
|
|
"mapping", generation, () => true) : remote;
|
|
var scope = new NodeScope { ClusterId = "cluster", Kind = NodeHierarchyKind.Raw, Segments = ["Plant"] };
|
|
var subject = new MappedNamespaceSubject(view, scope, ["folder"], serving, DataOperations.Browse);
|
|
var resolver = new MappedSubjectResolver([subject], serving.CatalogEpoch, local);
|
|
var metadata = new DataMetadata(view, "branch", "Folder", DataValueKind.Null, -1, [], false,
|
|
DataOperations.Browse, 1, null, null, false, Serving: serving);
|
|
var origin = metadata with { View = view with { MappingId = null, MappingRevision = 0, Subject = "origin/plant" } };
|
|
var proof = new ProjectedNodeProof(ProjectedDataIds.Identifier("mapping", view.Address), metadata, subject, resolver, lease, generation, origin);
|
|
cache.TryPublish(request, new(ProjectionPageStatus.Ready, [proof], true, null, generation, lease, local, proof)).ShouldBeTrue();
|
|
cache.TryPinProjected(DataRealm.Raw, proof.LocalIdentifier, out var pin).ShouldBeTrue();
|
|
try
|
|
{
|
|
var capture = typeof(ProjectedCatalogCache).GetMethod("CaptureRetainedDiagnostics", BindingFlags.Instance | BindingFlags.NonPublic).ShouldNotBeNull();
|
|
object Observe() => capture.Invoke(cache, [DataRealm.Raw, proof.LocalIdentifier]).ShouldNotBeNull();
|
|
object? Field(object result, string name) => result.GetType().GetProperty(name)!.GetValue(result);
|
|
var before = Observe();
|
|
Field(before, "Retained").ShouldBe(true);
|
|
Field(before, "Retired").ShouldBe(false);
|
|
Field(before, "RemoteLeaseValid").ShouldBe(bound ? (bool?)true : null);
|
|
before.GetType().GetProperties().Select(x => x.Name).Order().ShouldBe(new[]
|
|
{ "CacheGenerationCurrent", "Generation", "LocalAuthorityActive", "MappingBindingCurrent", "RemoteLeaseValid", "Retained", "Retired", "RouteAlive" });
|
|
fixture.Clock.Advance(TimeSpan.FromSeconds(15));
|
|
cache.TryResolveProjected(DataRealm.Raw, proof.LocalIdentifier, out _).ShouldBeFalse();
|
|
var expired = Observe();
|
|
Field(expired, "Retained").ShouldBe(true);
|
|
Field(expired, "RemoteLeaseValid").ShouldBe(bound ? (bool?)false : null);
|
|
cache.MarkUnavailable("mapping", view);
|
|
var retired = Observe();
|
|
Field(retired, "Retained").ShouldBe(true);
|
|
Field(retired, "Retired").ShouldBe(true);
|
|
Field(retired, "CacheGenerationCurrent").ShouldBe(bound ? (bool?)false : null);
|
|
cache.TryResolveProjected(DataRealm.Raw, proof.LocalIdentifier, out _).ShouldBeFalse();
|
|
}
|
|
finally { pin!.Dispose(); }
|
|
}
|
|
}
|