Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataWriteRegistryTests.cs
T

114 lines
5.7 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
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", "P05")]
[Trait("Category", "Unit")]
public sealed class GrpcDataWriteRegistryTests
{
private static readonly byte[] Hash = new byte[32];
private static readonly DataWriteOutcome Success = new(DataWriteState.DriverConfirmedSuccess, 0, null);
private static WriteOperationRegistry Registry(int count = 4, long bytes = 4096, ManualTimeProvider? clock = null) =>
new("inc-1", count, bytes, TimeSpan.FromSeconds(10), clock);
[Fact]
public async Task Concurrent_duplicates_share_one_claim_and_waiter_cancellation_does_not_cancel_work()
{
var registry = Registry();
var results = await Task.WhenAll(Enumerable.Range(0, 32).Select(_ => Task.Run(() =>
registry.Claim("principal", "op", "inc-1", Hash))));
results.Count(r => r.Kind == WriteClaimKind.Owner).ShouldBe(1);
var owner = results.Single(r => r.Kind == WriteClaimKind.Owner).Claim!;
results.ShouldAllBe(r => ReferenceEquals(r.Claim, owner));
owner.TryMarkDispatched().ShouldBeTrue();
using var cancellation = new CancellationTokenSource();
var detached = owner.Completion.WaitAsync(cancellation.Token);
cancellation.Cancel();
await Should.ThrowAsync<OperationCanceledException>(async () => await detached);
owner.Complete(Success).ShouldBeTrue();
(await owner.Completion).ShouldBe(Success);
registry.Claim("principal", "op", "inc-1", Hash).Claim!.CurrentOutcome.ShouldBe(Success);
}
[Fact]
public void Hash_is_copied_and_conflicting_payload_or_stale_incarnation_cannot_dispatch()
{
var registry = Registry();
var semantic = new byte[32];
var owner = registry.Claim("principal", "op", "inc-1", semantic);
semantic[0] = 1;
registry.Claim("principal", "op", "inc-1", semantic).Kind.ShouldBe(WriteClaimKind.Conflict);
registry.Claim("principal", "op", "old-inc", Hash).Kind.ShouldBe(WriteClaimKind.Stale);
registry.Claim("principal", "op", "inc-1", Hash).Claim.ShouldBeSameAs(owner.Claim);
registry.Claim("other-principal", "op", "inc-1", Hash).Kind.ShouldBe(WriteClaimKind.Owner);
Should.Throw<ArgumentException>(() => registry.Claim("principal", "bad", "inc-1", new byte[31]));
}
[Fact]
public void Principal_identity_is_case_insensitive_but_operation_id_is_ordinal()
{
var registry = Registry();
var first = registry.Claim("Service-Principal", "Operation", "inc-1", Hash);
registry.Claim("service-principal", "Operation", "inc-1", Hash).Claim.ShouldBeSameAs(first.Claim);
registry.Claim("service-principal", "operation", "inc-1", Hash).Kind.ShouldBe(WriteClaimKind.Owner);
}
[Fact]
public void Inflight_entries_are_never_evicted_and_completed_retention_uses_monotonic_time()
{
var clock = new ManualTimeProvider();
var registry = Registry(count: 1, clock: clock);
var claim = registry.Claim("p", "one", "inc-1", Hash).Claim!;
clock.Advance(TimeSpan.FromMinutes(1));
registry.Claim("p", "two", "inc-1", Hash).Kind.ShouldBe(WriteClaimKind.Capacity);
registry.Claim("p", "one", "inc-1", Hash).Claim.ShouldBeSameAs(claim);
claim.Complete(new(DataWriteState.RejectedBeforeDispatch, 0, null)).ShouldBeTrue();
clock.WallClockOffset = TimeSpan.FromDays(30);
registry.Claim("p", "two", "inc-1", Hash).Kind.ShouldBe(WriteClaimKind.Capacity);
clock.Advance(TimeSpan.FromSeconds(11));
registry.Claim("p", "two", "inc-1", Hash).Kind.ShouldBe(WriteClaimKind.Owner);
}
[Fact]
public void Retained_byte_capacity_rejects_without_evicting_existing_claim()
{
var registry = Registry(bytes: 1);
registry.Claim("principal", "op", "inc-1", Hash).Kind.ShouldBe(WriteClaimKind.Capacity);
}
[Fact]
public async Task Late_refinement_updates_snapshot_without_changing_already_completed_unknown_task()
{
var claim = Registry().Claim("p", "one", "inc-1", Hash).Claim!;
claim.TryMarkDispatched().ShouldBeTrue();
var unknown = new DataWriteOutcome(DataWriteState.OutcomeUnknown, 0, null);
claim.Complete(unknown).ShouldBeTrue();
claim.LateRefine(Success).ShouldBeTrue();
(await claim.Completion).ShouldBe(unknown);
claim.CurrentOutcome.ShouldBe(Success);
claim.LateRefine(new(DataWriteState.RejectedBeforeDispatch, 0, null)).ShouldBeFalse();
}
[Fact]
public void A2b_the_per_principal_cap_refuses_one_principal_and_admits_another_and_release_frees_the_slot()
{
var registry = new WriteOperationRegistry("inc-1", 10, 4096, TimeSpan.FromSeconds(10), maxEntriesPerPrincipal: 2);
registry.Claim("A", "1", "inc-1", Hash).Kind.ShouldBe(WriteClaimKind.Owner);
var second = registry.Claim("a", "2", "inc-1", Hash).Claim!; // principal keys are case-insensitive
registry.Claim("A", "3", "inc-1", Hash).Kind.ShouldBe(WriteClaimKind.Capacity);
registry.Claim("B", "1", "inc-1", Hash).Kind.ShouldBe(WriteClaimKind.Owner); // B is not A's operation
// Only a claim that completed UNDISPATCHED can be released; a pending/dispatched one cannot.
registry.ReleaseUndispatched("a", "2", second).ShouldBeFalse();
second.Complete(new(DataWriteState.RejectedBeforeDispatch, 0, null)).ShouldBeTrue();
registry.ReleaseUndispatched("a", "2", second).ShouldBeTrue();
registry.CountForTests.ShouldBe(2);
registry.Claim("A", "3", "inc-1", Hash).Kind.ShouldBe(WriteClaimKind.Owner);
}
}