135 lines
5.6 KiB
C#
135 lines
5.6 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("Category", "Unit"), Trait("Phase", "P11")]
|
|
public sealed class GrpcDataIngressMailboxRaceTests
|
|
{
|
|
[Theory(Timeout = 30000)]
|
|
[InlineData("status")]
|
|
[InlineData("remove")]
|
|
[InlineData("rebind")]
|
|
public async Task Queued_reserved_value_cannot_cross_a_later_source_fence(string fence)
|
|
{
|
|
using var hub = HubTest.Create(partitions: 1, bytes: 100_000);
|
|
var source = HubTest.Ticket("driver", "mailbox");
|
|
HubTest.BindAndPublish(hub, source, 1, 1);
|
|
hub.ProcessPending();
|
|
hub.TryReserveIngress(source, 288, out var reservation).ShouldBeTrue();
|
|
using (reservation!)
|
|
{
|
|
var sample = new DataSample(DataValue.FromScalar(DataValueKind.Int32, 2), 0, null, null, HubTest.Serving, 2);
|
|
var transferred = false;
|
|
try
|
|
{
|
|
await WhilePartitionHeld(hub, source, () =>
|
|
transferred = reservation!.TryCommit(sample, 2));
|
|
transferred.ShouldBeTrue("the reserved mailbox accepts without waiting for the partition worker");
|
|
}
|
|
finally { if (!transferred) sample.Dispose(); }
|
|
}
|
|
if (fence == "status") hub.TryPublishStatus(source, 3, 0x80310000u, null, null).ShouldBeTrue();
|
|
else if (fence == "remove") hub.TryRemove(source, 3).ShouldBeTrue();
|
|
else
|
|
{
|
|
var replacement = new SourceTicket(source.Key, new SourceGeneration(2, 1));
|
|
hub.TryBind(replacement).ShouldBeTrue();
|
|
HubTest.Publish(hub, replacement, 1, 3);
|
|
hub.ProcessPending();
|
|
using var current = hub.Read(replacement);
|
|
current.Readiness.ShouldBe(DataReadiness.Ready);
|
|
current.Sample!.Value.GetScalar().ShouldBe(3);
|
|
}
|
|
hub.ProcessPending();
|
|
using var old = hub.Read(source);
|
|
if (fence == "status" && old.Sample is not null)
|
|
old.Sample.StatusCode.ShouldBe(0x80310000u);
|
|
else old.Readiness.ShouldNotBe(DataReadiness.Ready);
|
|
}
|
|
|
|
[Fact(Timeout = 30000)]
|
|
public async Task Mailbox_keeps_original_receipt_age_and_releases_owned_bytes()
|
|
{
|
|
var clock = new ManualTimeProvider();
|
|
var hub = HubTest.Create(partitions: 1, bytes: 100_000, clock: clock);
|
|
var source = HubTest.Ticket("driver", "age");
|
|
try
|
|
{
|
|
hub.TryBind(source).ShouldBeTrue();
|
|
var received = clock.GetTimestamp();
|
|
hub.TryReserveIngress(source, 288, out var reservation).ShouldBeTrue();
|
|
using (reservation!)
|
|
{
|
|
var sample = new DataSample(DataValue.FromScalar(DataValueKind.Int32, 7), 0, null, null, HubTest.Serving, 1);
|
|
var transferred = false;
|
|
try
|
|
{
|
|
await WhilePartitionHeld(hub, source, () =>
|
|
{
|
|
transferred = reservation!.TryCommit(sample, 1);
|
|
clock.Advance(TimeSpan.FromSeconds(5));
|
|
});
|
|
transferred.ShouldBeTrue();
|
|
}
|
|
finally { if (!transferred) sample.Dispose(); }
|
|
}
|
|
hub.ProcessPending();
|
|
using (var read = hub.Read(source))
|
|
{
|
|
read.Readiness.ShouldBe(DataReadiness.Ready);
|
|
read.ReceivedTimestamp.ShouldBe(received);
|
|
clock.GetElapsedTime(read.ReceivedTimestamp).ShouldBe(TimeSpan.FromSeconds(5));
|
|
}
|
|
}
|
|
finally { hub.Dispose(); }
|
|
hub.Resources.RetainedBytes.ShouldBe(0);
|
|
hub.CaptureAccounting().TrackedSources.ShouldBe(0);
|
|
}
|
|
|
|
[Fact(Timeout = 30000)]
|
|
public void Late_commit_after_disposal_rejects_without_taking_payload_and_releases_charge_once()
|
|
{
|
|
var hub = HubTest.Create(partitions: 1, bytes: 100_000);
|
|
var source = HubTest.Ticket("driver", "dispose");
|
|
hub.TryBind(source).ShouldBeTrue();
|
|
hub.TryReserveIngress(source, 288, out var reservation).ShouldBeTrue();
|
|
hub.Dispose();
|
|
hub.Resources.RetainedBytes.ShouldBeGreaterThan(0, "the outstanding physical reservation remains charged");
|
|
using (var sample = new DataSample(DataValue.FromScalar(DataValueKind.Int32, 9), 0, null, null, HubTest.Serving, 1))
|
|
{
|
|
reservation!.TryCommit(sample, 1).ShouldBeFalse();
|
|
sample.Value.GetScalar().ShouldBe(9, "a rejected commit leaves the borrowed sample with its caller");
|
|
}
|
|
reservation!.Dispose();
|
|
reservation.Dispose();
|
|
hub.Resources.RetainedBytes.ShouldBe(0);
|
|
hub.CaptureAccounting().TrackedSources.ShouldBe(0);
|
|
}
|
|
|
|
private static async Task WhilePartitionHeld(PartitionedSampleHub hub, SourceTicket source, Action action)
|
|
{
|
|
var ct = TestContext.Current.CancellationToken;
|
|
using var entered = new ManualResetEventSlim();
|
|
using var release = new ManualResetEventSlim();
|
|
var holder = Task.Run(() => hub.WithPartitionLockForTest(source.Key, () =>
|
|
{
|
|
entered.Set();
|
|
release.Wait(ct);
|
|
}), ct);
|
|
try
|
|
{
|
|
entered.Wait(TimeSpan.FromSeconds(5), ct).ShouldBeTrue();
|
|
await Task.Run(action, ct).WaitAsync(TimeSpan.FromSeconds(5), ct);
|
|
}
|
|
finally
|
|
{
|
|
release.Set();
|
|
await holder.WaitAsync(TimeSpan.FromSeconds(5), ct);
|
|
}
|
|
}
|
|
}
|