154 lines
9.6 KiB
C#
154 lines
9.6 KiB
C#
using System.Collections;
|
|
using System.Reflection;
|
|
using System.Threading.Channels;
|
|
using Opc.Ua;
|
|
using Opc.Ua.Client;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.OpcUaServer;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
|
|
|
|
public sealed partial class GrpcDataConfiguredDiscoveryWireTests
|
|
{
|
|
[Fact(Timeout = 90000), Trait("Phase", "P11Renewal")]
|
|
public async Task Key_only_generation_change_reauthorizes_the_whole_page_after_one_active_child_renews()
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
LocalDataFixture? peer = null;
|
|
var subscriptions = new FakeSubscriptions(() => peer!.Serving);
|
|
using var origin = new LocalDataFixture(additionalRawNodes: 1, servingGeneration: 1,
|
|
targetSubscriptions: (_, _) => subscriptions);
|
|
peer = origin; subscriptions.SampleClock = origin.Clock;
|
|
var published = Channel.CreateUnbounded<ProjectedValueUpdate>();
|
|
LocalCatalogCoordinator? coordinator = null;
|
|
await WithComposedServer(origin, origin.Root, async (session, sink, _, _, _, placements, anchor) =>
|
|
{
|
|
var client = (SessionClient)session;
|
|
var initial = await BrowseAll(client, anchor, 10);
|
|
initial.Count.ShouldBe(2);
|
|
var scalar = ExpandedNodeId.ToNodeId(initial.Single(item => item.BrowseName.Name == "Speed").NodeId, session.NamespaceUris);
|
|
var sibling = ExpandedNodeId.ToNodeId(initial.Single(item => item.BrowseName.Name == "Speed0").NodeId, session.NamespaceUris);
|
|
var lookup = (IProjectedNodeLookup)sink;
|
|
lookup.TryResolve(scalar, out var oldScalar).ShouldBeTrue();
|
|
lookup.TryResolve(sibling, out var oldSibling).ShouldBeTrue();
|
|
oldScalar.ShouldNotBeNull(); oldSibling.ShouldNotBeNull();
|
|
oldScalar.ProjectionGeneration.ShouldBe(oldSibling.ProjectionGeneration);
|
|
var subscription = (await client.CreateSubscriptionAsync(new RequestHeader(), 100, 300, 1, 0, true, 0, ct)).SubscriptionId;
|
|
await AddComposedItem(client, subscription, scalar, 1, 100, ct);
|
|
await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
|
|
var original = await NextPublished(published.Reader, x => x.IsCompleteSnapshot, ct);
|
|
(await PublishComposedValue(client, 1, ct)).StatusCode.ShouldBe(StatusCodes.Good);
|
|
|
|
// Identical mapping revision and placement, with a new local authority/cache
|
|
// generation as in a key-only deployment. Only Speed has active demand.
|
|
var replacement = Prepare(coordinator!);
|
|
var mapping = Mapping(origin.Root);
|
|
mapping.AllowedOperations |= DataOperations.Read | DataOperations.Metadata;
|
|
mapping.Revision.ShouldBe(oldScalar.Metadata.View.MappingRevision);
|
|
placements.Stage(replacement, [mapping], [Connection()]);
|
|
coordinator!.Complete(replacement, true).ShouldBeTrue();
|
|
subscriptions.SnapshotVersion = 2; subscriptions.SnapshotValue = 8;
|
|
origin.Clock.Advance(TimeSpan.FromSeconds(5));
|
|
var recovered = await NextPublished(published.Reader, x => x.IsCompleteSnapshot && x.SourceVersion == 2, ct);
|
|
recovered.SourceProof.IsValid.ShouldBeTrue();
|
|
original.SourceProof.IsValid.ShouldBeFalse();
|
|
lookup.TryResolve(scalar, out var newScalar).ShouldBeTrue();
|
|
newScalar.ShouldNotBeNull().ProjectionGeneration.ShouldNotBe(oldScalar.ProjectionGeneration);
|
|
var manager = sink.GetType().GetField("_nodeManager", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(sink)!;
|
|
var definitions = (IDictionary)manager.GetType().GetField("_projectedDefinitions", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(manager)!;
|
|
var staleSibling = (ProjectedVariableDefinition)definitions[sibling]!;
|
|
staleSibling.ProjectionGeneration.ShouldBe(oldSibling.ProjectionGeneration);
|
|
staleSibling.Lifetime.IsActive.ShouldBeTrue();
|
|
staleSibling.AuthorityIsCurrent().ShouldBeFalse();
|
|
|
|
// A fresh authenticated complete page must reauthorize every compatible
|
|
// sibling rather than reject the page because the inactive one is old.
|
|
var after = await BrowseAll(client, anchor, 10);
|
|
after.Select(item => item.BrowseName.Name).Order().ShouldBe(new[] { "Speed", "Speed0" });
|
|
lookup.TryResolve(sibling, out var newSibling).ShouldBeTrue();
|
|
newSibling.ShouldNotBeNull().ProjectionGeneration.ShouldBe(newScalar.ProjectionGeneration);
|
|
newSibling.Lifetime.ShouldBeSameAs(staleSibling.Lifetime);
|
|
original.SourceProof.IsValid.ShouldBeFalse("reauthorizing a page must not revive captured old samples");
|
|
}, onPublished: update => published.Writer.TryWrite(update), onCoordinator: value => coordinator = value);
|
|
}
|
|
|
|
[Fact(Timeout = 90000), Trait("Phase", "P11Renewal")]
|
|
public async Task Reauthorizing_a_page_rejects_incompatible_or_retired_children_and_failed_authority()
|
|
{
|
|
using var origin = new LocalDataFixture(additionalRawNodes: 1, servingGeneration: 1);
|
|
LocalCatalogCoordinator? coordinator = null;
|
|
await WithComposedServer(origin, origin.Root, async (session, sink, _, _, _, placements, anchor) =>
|
|
{
|
|
var client = (SessionClient)session;
|
|
var initial = await BrowseAll(client, anchor, 10);
|
|
initial.Count.ShouldBe(2);
|
|
var ids = initial.Select(item => ExpandedNodeId.ToNodeId(item.NodeId, session.NamespaceUris)).ToArray();
|
|
var manager = sink.GetType().GetField("_nodeManager", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(sink)!;
|
|
var definitions = (IDictionary)manager.GetType().GetField("_projectedDefinitions", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(manager)!;
|
|
var old = ids.Select(id => (ProjectedVariableDefinition)definitions[id]!).ToArray();
|
|
var page = (IProjectedNamespacePageSink)sink;
|
|
old.All(item => item.AuthorityIsCurrent() && item.Lifetime.IsActive).ShouldBeTrue();
|
|
var premature = old.Select(item => item with { ProjectionGeneration = item.ProjectionGeneration + 1,
|
|
AuthorityIsCurrent = () => true }).ToArray();
|
|
var prematurelyCommitted = false;
|
|
page.TryApply(anchor, premature, () => { prematurelyCommitted = true; return true; }).ShouldBeFalse();
|
|
prematurelyCommitted.ShouldBeFalse();
|
|
var replacement = Prepare(coordinator!);
|
|
var mapping = Mapping(origin.Root);
|
|
mapping.AllowedOperations |= DataOperations.Read | DataOperations.Metadata;
|
|
placements.Stage(replacement, [mapping], [Connection()]);
|
|
coordinator!.Complete(replacement, true).ShouldBeTrue();
|
|
old.All(item => !item.AuthorityIsCurrent() && item.Lifetime.IsActive).ShouldBeTrue();
|
|
|
|
var fresh = old.Select(item => item with
|
|
{
|
|
ProjectionGeneration = item.ProjectionGeneration + 1,
|
|
AuthorityIsCurrent = () => true,
|
|
Lifetime = new ProjectedNodeLifetime()
|
|
}).ToArray();
|
|
var commits = 0;
|
|
bool Commit() { commits++; return true; }
|
|
void Unchanged()
|
|
{
|
|
foreach (var (id, index) in ids.Select((id, index) => (id, index)))
|
|
definitions[id].ShouldBeSameAs(old[index]);
|
|
}
|
|
|
|
var badSchema = fresh.ToArray();
|
|
badSchema[1] = badSchema[1] with { Metadata = badSchema[1].Metadata with { ValueKind = DataValueKind.String } };
|
|
page.TryApply(anchor, badSchema, Commit).ShouldBeFalse();
|
|
commits.ShouldBe(0); Unchanged();
|
|
var badRevision = fresh.ToArray();
|
|
badRevision[1] = badRevision[1] with { Metadata = badRevision[1].Metadata with
|
|
{ View = badRevision[1].Metadata.View with { MappingRevision = badRevision[1].Metadata.View.MappingRevision + 1 } } };
|
|
page.TryApply(anchor, badRevision, Commit).ShouldBeFalse();
|
|
commits.ShouldBe(0); Unchanged();
|
|
var stale = fresh.ToArray();
|
|
stale[1] = stale[1] with { AuthorityIsCurrent = () => false };
|
|
page.TryApply(anchor, stale, Commit).ShouldBeFalse();
|
|
commits.ShouldBe(0); Unchanged();
|
|
var rollback = fresh.ToArray();
|
|
rollback[1] = rollback[1] with { ProjectionGeneration = old[1].ProjectionGeneration - 1 };
|
|
page.TryApply(anchor, rollback, Commit).ShouldBeFalse();
|
|
commits.ShouldBe(0); Unchanged();
|
|
page.TryApply(anchor, fresh, () => false).ShouldBeFalse();
|
|
Unchanged();
|
|
var authorityCurrent = true;
|
|
var expiring = fresh.ToArray();
|
|
expiring[1] = expiring[1] with { AuthorityIsCurrent = () => authorityCurrent };
|
|
page.TryApply(anchor, expiring, () => { authorityCurrent = false; return true; }).ShouldBeFalse();
|
|
authorityCurrent.ShouldBeFalse("the guard must be checked again after the commit runs");
|
|
Unchanged();
|
|
var retiredDuringCommit = false;
|
|
page.TryApply(anchor, fresh, () => { old[1].Lifetime.ForceRetire(); retiredDuringCommit = true; return true; }).ShouldBeFalse();
|
|
retiredDuringCommit.ShouldBeTrue();
|
|
Unchanged();
|
|
page.TryApply(anchor, fresh, Commit).ShouldBeFalse();
|
|
commits.ShouldBe(0); Unchanged();
|
|
}, onCoordinator: value => coordinator = value);
|
|
}
|
|
}
|