97 lines
4.7 KiB
C#
97 lines
4.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();
|
|
}
|
|
}
|