435 lines
24 KiB
C#
435 lines
24 KiB
C#
using System.Collections.Concurrent;
|
|
using Akka.Actor;
|
|
using Akka.Configuration;
|
|
using Akka.TestKit.Xunit;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.Types;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Deploy;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.DeploymentCache;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.OpcUa;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.VirtualTags;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Host.Tests.DataConnections;
|
|
|
|
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "P08")]
|
|
public sealed class GrpcDataProjectionRoutingTests : TestKit
|
|
{
|
|
public GrpcDataProjectionRoutingTests() : base(ConfigurationFactory.ParseString("""
|
|
akka.actor.provider = cluster
|
|
akka.remote.dot-netty.tcp.hostname = "127.0.0.1"
|
|
akka.remote.dot-netty.tcp.port = 0
|
|
akka.loglevel = WARNING
|
|
""")) { }
|
|
|
|
private static ProjectedValueRoute Route(string mapping = "mapping", string originIdentifier = "Plant/Value")
|
|
{
|
|
var id = ProjectedDataIds.Identifier(mapping, new("source", "urn:source/raw", "string", originIdentifier));
|
|
return new("grpc-data:connection", ProjectedDataIds.DependencyReference(DataRealm.Raw, id), DataRealm.Raw,
|
|
id, 1, new("origin", "boot", "Primary", 1, 1, 1), Guid.NewGuid());
|
|
}
|
|
|
|
private IActorRef Start(RecordingSink sink, IActorRef? mux = null, ActorProjectedValuePublisher? projectedPublisher = null)
|
|
{
|
|
var publisher = Sys.ActorOf(OpcUaPublishActor.PropsForTests(sink: sink));
|
|
var acknowledgments = CreateTestProbe();
|
|
var fetcher = new EmptyArtifact();
|
|
var host = Sys.ActorOf(DriverHostActor.Props(dbFactory: null, localNode: NodeId.Parse("node-a"),
|
|
localRoles: new HashSet<string> { "driver" }, dependencyMux: mux, opcUaPublishActor: publisher,
|
|
ackRouter: acknowledgments.Ref, fetchAndCacheMode: true, artifactFetcher: fetcher,
|
|
projectedPublisher: projectedPublisher));
|
|
var revision = RevisionHash.Parse(Convert.ToHexStringLower(System.Security.Cryptography.SHA256.HashData(fetcher.Bytes)));
|
|
host.Tell(new DispatchDeployment(DeploymentId.NewId(), revision, CorrelationId.NewId()));
|
|
acknowledgments.ExpectMsg<ApplyAck>(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken)
|
|
.Outcome.ShouldBe(ApplyAckOutcome.Applied);
|
|
return host;
|
|
}
|
|
|
|
private static ProjectedValueUpdate Update(ProjectedValueRoute route, Gate proof, object value) =>
|
|
new(route, new(value, 0, null, null), new(TimeSpan.FromSeconds(3), null), proof.Clock.GetTimestamp(), proof);
|
|
|
|
[Fact(Timeout = 20000)]
|
|
public async Task Real_host_routes_immutable_typed_samples_to_sdk_and_exact_dependency_key()
|
|
{
|
|
var sink = new RecordingSink();
|
|
var mux = Sys.ActorOf(DependencyMuxActor.Props());
|
|
var subscriber = CreateTestProbe();
|
|
var route = Route();
|
|
mux.Tell(new DependencyMuxActor.RegisterInterest([route.DependencyReference,
|
|
ProjectedDataIds.DependencyReference(DataRealm.Uns, route.LocalIdentifier), "Plant/Value"], subscriber.Ref));
|
|
await mux.Ask<ActorIdentity>(new Identify("registered"), TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
IActorRef? host = null;
|
|
IActorRef StartOnce() => host ??= Start(sink, mux);
|
|
using var broker = new ActorProjectedValuePublisher(() => StartOnce(), _ => true, 8, 1_000_000, TimeProvider.System);
|
|
var proof = new Gate();
|
|
var bytes = new byte[] { 1, 2 };
|
|
broker.TryBind(route).ShouldBeTrue();
|
|
broker.TryPublish([Update(route, proof, bytes) with { IsCompleteSnapshot = true, SourceVersion = 17 }]).ShouldBeTrue();
|
|
bytes[0] = 99;
|
|
await sink.Completed.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
var stored = sink.Writes.ShouldHaveSingleItem();
|
|
stored.Snapshot.Value.ShouldBeOfType<byte[]>().ShouldBe(new byte[] { 1, 2 });
|
|
stored.OriginAgeBounds.Lower.ShouldBe(TimeSpan.FromSeconds(3));
|
|
stored.IsCompleteSnapshot.ShouldBeTrue();
|
|
stored.SourceVersion.ShouldBe(17);
|
|
stored.Proof.IsValid.ShouldBeTrue();
|
|
VirtualTagActor.DependencyValueChanged? dependency = null;
|
|
subscriber.ExpectMsg<DependencyMuxActor.ProjectedDependencyWake>(TimeSpan.FromSeconds(5),
|
|
cancellationToken: TestContext.Current.CancellationToken).Consume(items => dependency = items.ShouldHaveSingleItem());
|
|
dependency.ShouldNotBeNull().TagId.ShouldBe(route.DependencyReference);
|
|
proof.Invalidate();
|
|
stored.Proof.IsValid.ShouldBeFalse();
|
|
AwaitAssert(() => broker.PendingBytes.ShouldBe(0), TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
|
|
}
|
|
|
|
[Theory(Timeout = 20000), Trait("Phase", "P11")]
|
|
[InlineData(0L)]
|
|
[InlineData(-1L)]
|
|
public void Nonpositive_supplied_source_version_is_rejected_and_invalidated(long sourceVersion)
|
|
{
|
|
var host = CreateTestProbe();
|
|
using var broker = new ActorProjectedValuePublisher(() => host.Ref, _ => true, 8, 1_000_000, TimeProvider.System);
|
|
var route = Route(); var proof = new Gate();
|
|
broker.TryBind(route).ShouldBeTrue();
|
|
broker.TryPublish([Update(route, proof, 42) with { SourceVersion = sourceVersion }]).ShouldBeFalse();
|
|
proof.IsValid.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact(Timeout = 20000)]
|
|
public async Task Replacing_a_live_source_fences_old_witnesses_without_retiring_the_new_source()
|
|
{
|
|
var sink = new RecordingSink(); var host = Start(sink);
|
|
using var broker = new ActorProjectedValuePublisher(() => host, _ => true, 8, 1_000_000, TimeProvider.System);
|
|
var route = Route(); var original = new Gate(); var replacement = new Gate();
|
|
broker.TryBind(route).ShouldBeTrue();
|
|
broker.TryPublish([Update(route, original, 1)]).ShouldBeTrue();
|
|
AwaitAssert(() => sink.Writes.Count.ShouldBe(1), TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
|
|
var old = sink.Writes.Single().Proof;
|
|
broker.TryPublish([Update(route, replacement, 2) with { IsCompleteSnapshot = true }]).ShouldBeTrue();
|
|
AwaitAssert(() => sink.Writes.Count.ShouldBe(2), TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
|
|
var current = sink.Writes.Last().Proof;
|
|
original.IsValid.ShouldBeTrue("replacement does not rely on invalidating an external source");
|
|
old.IsSourceValid.ShouldBeFalse();
|
|
old.IsValid.ShouldBeFalse();
|
|
current.IsValid.ShouldBeTrue();
|
|
old.Invalidate();
|
|
current.IsValid.ShouldBeTrue("retiring the old witness cannot retire its replacement");
|
|
}
|
|
|
|
[Theory(Timeout = 20000)]
|
|
[InlineData(false)]
|
|
[InlineData(true)]
|
|
public void Completed_slot_reuse_cannot_deliver_newer_samples_before_older_queued_samples(bool sdkStage)
|
|
{
|
|
var host = CreateTestProbe(); var sdk = CreateTestProbe();
|
|
using var broker = new ActorProjectedValuePublisher(() => host.Ref, _ => true, 8, 1_000_000, TimeProvider.System);
|
|
var route = Route(); var proof = new Gate(); broker.TryBind(route).ShouldBeTrue();
|
|
broker.TryPublish([Update(route, proof, 1)]).ShouldBeTrue();
|
|
broker.TryPublish([Update(route, proof, 2)]).ShouldBeTrue();
|
|
object oldest;
|
|
if (sdkStage)
|
|
{
|
|
var queued = ((Array)Invoke(broker, "TakeQueuedValues", host.Ref)!).Cast<object>().ToArray();
|
|
oldest = queued.Single(batch => Value(batch) == 1);
|
|
foreach (var batch in queued) Invoke(broker, "EnqueueForSdk", batch, sdk.Ref, null);
|
|
}
|
|
else
|
|
{
|
|
var pending = (System.Collections.IEnumerable)typeof(ActorProjectedValuePublisher)
|
|
.GetField("_pending", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!.GetValue(broker)!;
|
|
oldest = pending.Cast<object>().Single(batch => Value(batch) == 1);
|
|
}
|
|
Invoke(oldest, "TryBegin").ShouldBe(true);
|
|
Invoke(oldest, "Complete", true);
|
|
broker.TryPublish([Update(route, proof, 3)]).ShouldBeTrue();
|
|
var remaining = ((Array)Invoke(broker, "TakeQueuedValues", host.Ref)!).Cast<object>().ToArray();
|
|
if (sdkStage)
|
|
{
|
|
foreach (var batch in remaining) Invoke(broker, "EnqueueForSdk", batch, sdk.Ref, null);
|
|
remaining = ((Array)Invoke(broker, "TakeSdkValues", sdk.Ref)!).Cast<object>().ToArray();
|
|
}
|
|
remaining.Select(Value).ShouldBe(new[] { 2, 3 });
|
|
|
|
static object? Invoke(object target, string name, params object?[] arguments) => target.GetType()
|
|
.GetMethod(name, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!.Invoke(target, arguments);
|
|
static int Value(object batch) => (int)((IReadOnlyList<ProjectedValueWrite>)batch.GetType()
|
|
.GetProperty("Writes", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)!.GetValue(batch)!)[0].Snapshot.Value!;
|
|
}
|
|
|
|
[Fact(Timeout = 20000)]
|
|
public void Paused_host_has_bounded_payload_free_wakeups_after_repeated_cancellations()
|
|
{
|
|
var host = CreateTestProbe();
|
|
using var broker = new ActorProjectedValuePublisher(() => host.Ref, _ => true, 8, 1_000_000, TimeProvider.System);
|
|
var route = Route();
|
|
var sweep = typeof(ActorProjectedValuePublisher).GetMethod("SweepQueued", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).ShouldNotBeNull();
|
|
for (var i = 0; i < 40; i++)
|
|
{
|
|
var proof = new Gate();
|
|
broker.TryBind(route).ShouldBeTrue();
|
|
broker.TryPublish([Update(route, proof, new byte[16_384])]).ShouldBeTrue();
|
|
proof.Invalidate();
|
|
sweep.Invoke(broker, null);
|
|
broker.PendingBytes.ShouldBe(0);
|
|
}
|
|
// One value wake plus one independently coalesced retirement wake at most.
|
|
host.ExpectMsg<object>(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
|
|
host.ExpectMsg<object>(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
|
|
host.ExpectNoMsg(TimeSpan.FromMilliseconds(200), TestContext.Current.CancellationToken);
|
|
}
|
|
|
|
[Fact(Timeout = 20000)]
|
|
public async Task Paused_mux_receives_bounded_wakes_while_sdk_accepts_many_projected_values()
|
|
{
|
|
var sink = new RecordingSink(); var mux = CreateTestProbe(); var host = Start(sink, mux.Ref);
|
|
using var broker = new ActorProjectedValuePublisher(() => host, _ => true, 8, 1_000_000, TimeProvider.System);
|
|
var route = Route(); var proof = new Gate(); broker.TryBind(route).ShouldBeTrue();
|
|
for (var i = 0; i < 20; i++)
|
|
{
|
|
broker.TryPublish([Update(route, proof, new byte[1024])]).ShouldBeTrue();
|
|
var expected = i + 1;
|
|
AwaitAssert(() => sink.Writes.Count.ShouldBe(expected), TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
|
|
}
|
|
await sink.Completed.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
mux.ExpectMsg<object>(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
|
|
mux.ExpectNoMsg(TimeSpan.FromMilliseconds(200), TestContext.Current.CancellationToken);
|
|
}
|
|
|
|
[Fact(Timeout = 20000)]
|
|
public async Task Paused_subscriber_receives_bounded_wakes_after_projected_mux_fanout()
|
|
{
|
|
var sink = new RecordingSink(); var mux = Sys.ActorOf(DependencyMuxActor.Props()); var subscriber = CreateTestProbe();
|
|
var route = Route();
|
|
mux.Tell(new DependencyMuxActor.RegisterInterest([route.DependencyReference], subscriber.Ref));
|
|
await mux.Ask<ActorIdentity>(new Identify("registered"), TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
var host = Start(sink, mux);
|
|
using var broker = new ActorProjectedValuePublisher(() => host, _ => true, 8, 1_000_000, TimeProvider.System);
|
|
var proof = new Gate(); broker.TryBind(route).ShouldBeTrue();
|
|
for (var i = 0; i < 20; i++)
|
|
{
|
|
broker.TryPublish([Update(route, proof, new byte[1024])]).ShouldBeTrue();
|
|
var expected = i + 1;
|
|
AwaitAssert(() => sink.Writes.Count.ShouldBe(expected), TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
|
|
}
|
|
await mux.Ask<ActorIdentity>(new Identify("drained"), TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
subscriber.ExpectMsg<object>(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
|
|
subscriber.ExpectNoMsg(TimeSpan.FromMilliseconds(200), TestContext.Current.CancellationToken);
|
|
}
|
|
|
|
[Fact(Timeout = 20000)]
|
|
public async Task Retiring_an_old_stream_owner_cannot_remove_its_replacement()
|
|
{
|
|
var sink = new RecordingSink();
|
|
var host = Start(sink);
|
|
using var broker = new ActorProjectedValuePublisher(() => host, _ => true, 8, 1_000_000, TimeProvider.System);
|
|
var old = Route(); var current = old with { PublicationId = Guid.NewGuid() };
|
|
broker.TryBind(old).ShouldBeTrue(); broker.TryBind(current).ShouldBeTrue();
|
|
broker.Retire(old);
|
|
broker.TryPublish([Update(old, new Gate(), 1)]).ShouldBeFalse();
|
|
broker.TryPublish([Update(current, new Gate(), 2)]).ShouldBeTrue();
|
|
await sink.Completed.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
sink.Writes.ShouldHaveSingleItem().Snapshot.Value.ShouldBe(2);
|
|
}
|
|
|
|
[Fact(Timeout = 20000)]
|
|
public async Task Disposal_keeps_physical_sink_work_charged_until_it_returns()
|
|
{
|
|
using var release = new ManualResetEventSlim();
|
|
var sink = new RecordingSink { BeforePublish = () => release.Wait(TimeSpan.FromSeconds(10)) };
|
|
var host = Start(sink);
|
|
using var broker = new ActorProjectedValuePublisher(() => host, _ => true, 8, 1_000_000, TimeProvider.System);
|
|
var route = Route();
|
|
var proof = new Gate();
|
|
try
|
|
{
|
|
broker.TryBind(route).ShouldBeTrue();
|
|
broker.TryPublish([Update(route, proof, 42)]).ShouldBeTrue();
|
|
await sink.Entered.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
var held = broker.PendingBytes;
|
|
held.ShouldBeGreaterThan(0);
|
|
broker.Dispose();
|
|
broker.PendingBytes.ShouldBe(held);
|
|
release.Set();
|
|
AwaitAssert(() => broker.PendingBytes.ShouldBe(0), TimeSpan.FromSeconds(5),
|
|
cancellationToken: TestContext.Current.CancellationToken);
|
|
sink.Writes.ShouldBeEmpty();
|
|
}
|
|
finally { release.Set(); }
|
|
}
|
|
|
|
[Fact(Timeout = 15000)]
|
|
public void Retiring_repeated_owners_of_one_view_coalesces_the_host_wakeup()
|
|
{
|
|
var host = CreateTestProbe();
|
|
using var broker = new ActorProjectedValuePublisher(() => host.Ref, _ => true, 2, 1_000_000, TimeProvider.System);
|
|
var first = Route();
|
|
for (var i = 0; i < 100; i++)
|
|
{
|
|
var route = first with { PublicationId = Guid.NewGuid() };
|
|
broker.TryBind(route).ShouldBeTrue();
|
|
broker.Retire(route);
|
|
}
|
|
host.ExpectMsg<object>(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
|
|
host.ExpectNoMsg(TimeSpan.FromMilliseconds(100), TestContext.Current.CancellationToken);
|
|
}
|
|
|
|
[Fact(Timeout = 20000)]
|
|
public void New_host_activation_drains_retained_retirement_and_restores_admission()
|
|
{
|
|
var oldHost = CreateTestProbe();
|
|
IActorRef host = oldHost.Ref;
|
|
using var broker = new ActorProjectedValuePublisher(() => host, _ => true, 1, 1_000_000, TimeProvider.System);
|
|
var old = Route();
|
|
broker.TryBind(old).ShouldBeTrue();
|
|
broker.Retire(old);
|
|
oldHost.ExpectMsg<object>(TimeSpan.FromSeconds(5), cancellationToken: TestContext.Current.CancellationToken);
|
|
var next = Route("next-mapping");
|
|
broker.TryBind(next).ShouldBeFalse();
|
|
host = Start(new RecordingSink(), projectedPublisher: broker);
|
|
AwaitAssert(() => broker.TryBind(next).ShouldBeTrue(), TimeSpan.FromSeconds(5),
|
|
cancellationToken: TestContext.Current.CancellationToken);
|
|
}
|
|
|
|
[Fact]
|
|
public void Route_and_retirement_residency_respects_a_byte_limit_independently_of_route_count()
|
|
{
|
|
var host = CreateTestProbe();
|
|
using var broker = new ActorProjectedValuePublisher(() => host.Ref, _ => true, 8, 1_000_000,
|
|
TimeProvider.System, maximumControlBytes: 8192);
|
|
var large = Route(originIdentifier: new string('x', 8192));
|
|
large.DependencyReference.Length.ShouldBeGreaterThan(8192);
|
|
broker.TryBind(large).ShouldBeFalse();
|
|
broker.TryBind(Route()).ShouldBeTrue();
|
|
}
|
|
|
|
[Fact(Timeout = 20000)]
|
|
public async Task Ordinary_deltas_keep_the_same_live_source_witness_at_the_sdk_boundary()
|
|
{
|
|
var sink = new RecordingSink();
|
|
var host = Start(sink);
|
|
using var broker = new ActorProjectedValuePublisher(() => host, _ => true, 8, 1_000_000, TimeProvider.System);
|
|
var route = Route();
|
|
var proof = new Gate();
|
|
broker.TryBind(route).ShouldBeTrue();
|
|
broker.TryPublish([Update(route, proof, 1)]).ShouldBeTrue();
|
|
await sink.Completed.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
broker.TryPublish([Update(route, proof, 2)]).ShouldBeTrue();
|
|
AwaitAssert(() => sink.Writes.Count.ShouldBe(2), TimeSpan.FromSeconds(5),
|
|
cancellationToken: TestContext.Current.CancellationToken);
|
|
var values = sink.Writes.ToArray();
|
|
values[0].Proof.SourceIdentity.ShouldBeSameAs(values[1].Proof.SourceIdentity);
|
|
proof.Invalidate();
|
|
values.ShouldAllBe(value => !value.Proof.IsValid);
|
|
}
|
|
|
|
[Fact(Timeout = 20000)]
|
|
public async Task A_new_sample_cannot_freshen_an_older_queued_sample_from_the_same_source()
|
|
{
|
|
var clock = new TestClock();
|
|
var sink = new RecordingSink();
|
|
var host = Start(sink);
|
|
using var broker = new ActorProjectedValuePublisher(() => host, _ => true, 8, 1_000_000, clock);
|
|
var route = Route();
|
|
var proof = new Gate(clock);
|
|
ProjectedValueUpdate Sample(int value) => new(route, new(value, 0, null, null),
|
|
new(TimeSpan.Zero, TimeSpan.Zero), clock.GetTimestamp(), proof, MaximumSampleAge: TimeSpan.FromSeconds(5));
|
|
broker.TryBind(route).ShouldBeTrue();
|
|
broker.TryPublish([Sample(1)]).ShouldBeTrue();
|
|
await sink.Completed.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
var old = sink.Writes.ShouldHaveSingleItem();
|
|
clock.Advance(TimeSpan.FromSeconds(6));
|
|
old.Proof.IsSourceValid.ShouldBeTrue();
|
|
old.Proof.IsValid.ShouldBeFalse();
|
|
broker.TryPublish([Sample(2)]).ShouldBeTrue();
|
|
AwaitAssert(() => sink.Writes.Count.ShouldBe(2), TimeSpan.FromSeconds(5),
|
|
cancellationToken: TestContext.Current.CancellationToken);
|
|
var current = sink.Writes.Last();
|
|
current.Proof.SourceIdentity.ShouldBeSameAs(old.Proof.SourceIdentity);
|
|
current.Proof.IsValid.ShouldBeTrue();
|
|
old.Proof.IsValid.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void An_unknown_upper_age_bound_cannot_prove_finite_freshness()
|
|
{
|
|
var host = CreateTestProbe();
|
|
using var broker = new ActorProjectedValuePublisher(() => host.Ref, _ => true, 8, 1_000_000, TimeProvider.System);
|
|
var route = Route();
|
|
broker.TryBind(route).ShouldBeTrue();
|
|
broker.TryPublish([Update(route, new Gate(), 1) with
|
|
{
|
|
OriginAgeBounds = new(TimeSpan.Zero, null), MaximumSampleAge = TimeSpan.FromSeconds(5)
|
|
}]).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void Larger_replacement_route_metadata_cannot_bypass_its_control_byte_limit()
|
|
{
|
|
var host = CreateTestProbe();
|
|
using var broker = new ActorProjectedValuePublisher(() => host.Ref, _ => true, 8, 1_000_000,
|
|
TimeProvider.System, maximumControlBytes: 8192);
|
|
var route = Route();
|
|
broker.TryBind(route).ShouldBeTrue();
|
|
var held = broker.RetainedControlBytes;
|
|
var replacement = route with { PublicationId = Guid.NewGuid(),
|
|
Serving = route.Serving with { Incarnation = new string('x', 100_000) } };
|
|
broker.TryBind(replacement).ShouldBeFalse();
|
|
broker.RetainedControlBytes.ShouldBe(held);
|
|
}
|
|
|
|
private sealed class Gate(TimeProvider? clock = null) : IProjectedSamplePublicationProof
|
|
{
|
|
private int _valid = 1;
|
|
public bool IsValid => Volatile.Read(ref _valid) != 0;
|
|
public TimeProvider Clock => clock ?? TimeProvider.System;
|
|
public void Invalidate() => Interlocked.Exchange(ref _valid, 0);
|
|
}
|
|
|
|
private sealed class TestClock : TimeProvider
|
|
{
|
|
private long _ticks;
|
|
public override long TimestampFrequency => TimeSpan.TicksPerSecond;
|
|
public override long GetTimestamp() => Volatile.Read(ref _ticks);
|
|
public override DateTimeOffset GetUtcNow() => DateTimeOffset.UnixEpoch.AddTicks(GetTimestamp());
|
|
internal void Advance(TimeSpan elapsed) => Interlocked.Add(ref _ticks, elapsed.Ticks);
|
|
}
|
|
|
|
private sealed class EmptyArtifact : IDeploymentArtifactFetcher
|
|
{
|
|
internal readonly byte[] Bytes = System.Text.Encoding.UTF8.GetBytes("{\"DriverInstances\":[]}");
|
|
public Task<byte[]?> FetchAsync(string deploymentId, string expectedRevisionHash, CancellationToken cancellationToken) =>
|
|
Task.FromResult<byte[]?>(Bytes);
|
|
}
|
|
|
|
private sealed class RecordingSink : IOpcUaAddressSpaceSink, IProjectedValueSink
|
|
{
|
|
internal readonly ConcurrentQueue<ProjectedValueWrite> Writes = new();
|
|
internal readonly TaskCompletionSource Completed = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
internal readonly TaskCompletionSource Entered = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
internal Action? BeforePublish { get; init; }
|
|
public bool TryPublish(IReadOnlyList<ProjectedValueWrite> values)
|
|
{
|
|
Entered.TrySetResult();
|
|
BeforePublish?.Invoke();
|
|
if (values.Any(value => !value.Proof.IsValid)) return false;
|
|
foreach (var value in values) Writes.Enqueue(value);
|
|
Completed.TrySetResult(); return true;
|
|
}
|
|
public void WriteValue(string nodeId, object? value, uint statusCode, DateTime sourceTimestampUtc, AddressSpaceRealm realm) { }
|
|
public void WriteAlarmCondition(string alarmNodeId, AlarmConditionSnapshot state, DateTime sourceTimestampUtc, AddressSpaceRealm realm) { }
|
|
public void WriteAlarmQuality(string alarmNodeId, uint statusCode, DateTime sourceTimestampUtc, AddressSpaceRealm realm) { }
|
|
public void MaterialiseAlarmCondition(string alarmNodeId, string equipmentNodeId, string displayName, string alarmType, int severity, AddressSpaceRealm realm, bool isNative = false) { }
|
|
public void WireAlarmNotifiers(string alarmNodeId, AddressSpaceRealm alarmRealm, IReadOnlyList<string> notifierFolderNodeIds, AddressSpaceRealm notifierFolderRealm) { }
|
|
public void EnsureFolder(string folderNodeId, string? parentNodeId, string displayName, AddressSpaceRealm realm) { }
|
|
public void EnsureVariable(string variableNodeId, string? parentFolderNodeId, string displayName, string dataType, bool writable, AddressSpaceRealm realm, string? historianTagname = null, bool isArray = false, uint? arrayLength = null, bool refuseArrayOnScalar = false) { }
|
|
public void RebuildAddressSpace() { }
|
|
public void RaiseNodesAddedModelChange(string affectedNodeId, AddressSpaceRealm realm) { }
|
|
public void AddReference(string sourceNodeId, AddressSpaceRealm sourceRealm, string targetNodeId, AddressSpaceRealm targetRealm, string referenceType = "Organizes") { }
|
|
}
|
|
}
|