116 lines
5.1 KiB
C#
116 lines
5.1 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
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", "P06")]
|
|
public sealed class GrpcDataDeliveryFairnessTests
|
|
{
|
|
[Fact(Timeout = 30000)]
|
|
public async Task A_quota_blocked_waiter_does_not_strand_another_principal_with_a_free_global_slot()
|
|
{
|
|
var capacity = new SubscriptionTransportCapacity(2, 1, 1_000);
|
|
capacity.TryReserve("a", 100, out var active).ShouldBeTrue();
|
|
using var cancellation = new CancellationTokenSource();
|
|
var blockedA = capacity.ReserveAsync("a", 100, cancellation.Token).AsTask();
|
|
blockedA.IsCompleted.ShouldBeFalse();
|
|
var waitingB = capacity.ReserveAsync("b", 100, TestContext.Current.CancellationToken).AsTask();
|
|
try
|
|
{
|
|
var grantedB = await waitingB.WaitAsync(TimeSpan.FromSeconds(2), TestContext.Current.CancellationToken);
|
|
grantedB.ShouldNotBeNull();
|
|
blockedA.IsCompleted.ShouldBeFalse();
|
|
grantedB.Dispose();
|
|
}
|
|
finally
|
|
{
|
|
cancellation.Cancel(); active!.Dispose();
|
|
try { await blockedA; } catch (OperationCanceledException) { }
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void Command_rate_limit_is_shared_by_case_insensitive_principal_and_uses_monotonic_time()
|
|
{
|
|
var clock = new ManualTimeProvider();
|
|
var capacity = new SubscriptionTransportCapacity(4, 2, 1_000_000, clock: clock, maxCommandsPerSecond: 2);
|
|
capacity.TryAdmitCommand("peer").ShouldBeTrue();
|
|
capacity.TryAdmitCommand("PEER").ShouldBeTrue();
|
|
capacity.TryAdmitCommand("peer").ShouldBeFalse();
|
|
capacity.TryAdmitCommand("other").ShouldBeTrue();
|
|
clock.Advance(TimeSpan.FromSeconds(1));
|
|
capacity.TryAdmitCommand("peer").ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_waiting_principal_gets_the_next_physical_writer_slot_before_a_hot_stream_reacquires_it()
|
|
{
|
|
var capacity = new SubscriptionTransportCapacity(1, 1, 1_000);
|
|
capacity.TryReserve("busy", 500, out var busy).ShouldBeTrue();
|
|
var quiet = capacity.ReserveAsync("quiet", 500, TestContext.Current.CancellationToken).AsTask();
|
|
quiet.IsCompleted.ShouldBeFalse();
|
|
busy!.Dispose();
|
|
capacity.TryReserve("busy", 500, out _).ShouldBeFalse();
|
|
var granted = await quiet.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
granted.ShouldNotBeNull();
|
|
granted.Dispose();
|
|
capacity.ActiveOperations.ShouldBe(0);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Canceling_a_waiter_removes_it_without_consuming_a_future_slot()
|
|
{
|
|
var capacity = new SubscriptionTransportCapacity(1, 1, 1_000);
|
|
capacity.TryReserve("busy", 500, out var busy).ShouldBeTrue();
|
|
using var cancellation = new CancellationTokenSource();
|
|
var waiting = capacity.ReserveAsync("quiet", 500, cancellation.Token).AsTask();
|
|
cancellation.Cancel();
|
|
await Should.ThrowAsync<OperationCanceledException>(async () => await waiting);
|
|
busy!.Dispose();
|
|
capacity.TryReserve("next", 500, out var next).ShouldBeTrue();
|
|
next!.Dispose();
|
|
}
|
|
|
|
[Fact]
|
|
public void Changing_hot_source_during_traversal_does_not_restart_or_starve_other_work()
|
|
{
|
|
var scheduler = new DeliveryScheduler(maxSources: 4, maxInterests: 16);
|
|
var hot = HubTest.Ticket("driver", "hot").Key;
|
|
var quiet = HubTest.Ticket("driver", "quiet").Key;
|
|
for (var i = 0; i < 8; i++) scheduler.TryRegister(hot, i + 1).ShouldBeTrue();
|
|
scheduler.TryRegister(quiet, 100).ShouldBeTrue();
|
|
scheduler.TryMarkDirty(hot, 1).ShouldBeTrue();
|
|
scheduler.TryMarkDirty(quiet, 1).ShouldBeTrue();
|
|
var first = scheduler.TakeSlice(2);
|
|
first.Count.ShouldBeLessThanOrEqualTo(2);
|
|
scheduler.TryMarkDirty(hot, 2).ShouldBeTrue();
|
|
var seen = first.ToList();
|
|
for (var i = 0; i < 16; i++)
|
|
{
|
|
var slice = scheduler.TakeSlice(2);
|
|
slice.Count.ShouldBeLessThanOrEqualTo(2);
|
|
seen.AddRange(slice);
|
|
}
|
|
seen.ShouldContain(x => x.Source == quiet && x.InterestId == 100);
|
|
foreach (var interest in Enumerable.Range(1, 8))
|
|
seen.ShouldContain(x => x.Source == hot && x.InterestId == interest);
|
|
seen.ShouldContain(x => x.Source == hot && x.Generation == 2);
|
|
}
|
|
|
|
[Fact]
|
|
public void Interest_and_dirty_capacity_are_enforced_before_allocating_more_work()
|
|
{
|
|
var scheduler = new DeliveryScheduler(maxSources: 1, maxInterests: 1);
|
|
var first = HubTest.Ticket("driver", "first").Key;
|
|
var second = HubTest.Ticket("driver", "second").Key;
|
|
scheduler.TryRegister(first, 1).ShouldBeTrue();
|
|
scheduler.TryRegister(first, 2).ShouldBeFalse();
|
|
scheduler.TryRegister(second, 3).ShouldBeFalse();
|
|
scheduler.TryMarkDirty(first, 1).ShouldBeTrue();
|
|
scheduler.TryMarkDirty(first, 2).ShouldBeTrue();
|
|
scheduler.TryMarkDirty(second, 1).ShouldBeFalse();
|
|
}
|
|
}
|