203 lines
13 KiB
C#
203 lines
13 KiB
C#
using Akka.Actor;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.Drivers;
|
|
|
|
[Trait("Feature", "GrpcData"), Trait("Category", "Unit"), Trait("Phase", "P06")]
|
|
public sealed class GrpcDataAcquisitionCapacityTests : RuntimeActorTestBase
|
|
{
|
|
[Fact]
|
|
public async Task Abandoned_establishment_pins_shared_driver_capacity_until_the_late_handle_is_released()
|
|
{
|
|
var capacity = new SubscriptionAcquisitionCapacity(2);
|
|
var parent = CreateTestProbe();
|
|
var firstDriver = new GateDriver("same", blocked: true, blockCleanup: true);
|
|
var first = parent.ChildActorOf(DriverInstanceActor.Props(firstDriver, commsLossTimeout: TimeSpan.Zero,
|
|
clusterId: "cluster", sourceIncarnation: 7, acquisitionCapacity: capacity), TestContext.Current.CancellationToken);
|
|
first.Tell(new DriverInstanceActor.InitializeRequested("{}"));
|
|
parent.ExpectMsg<DriverInstanceActor.ConnectivityChanged>(m => m.Connected, PresenceBudget,
|
|
cancellationToken: TestContext.Current.CancellationToken);
|
|
first.Tell(new DriverInstanceActor.SetDesiredSubscriptions(["tag"], TimeSpan.FromSeconds(1), [], 1, 7, true));
|
|
await firstDriver.Entered.Task.WaitAsync(PresenceBudget, TestContext.Current.CancellationToken);
|
|
var watcher = CreateTestProbe();
|
|
watcher.Watch(first);
|
|
Sys.Stop(first);
|
|
watcher.ExpectTerminated(first, PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
|
parent.ExpectMsg<DriverInstanceActor.ConnectivityChanged>(m => !m.Connected && m.ProducerIncarnation == 7,
|
|
PresenceBudget, cancellationToken: TestContext.Current.CancellationToken);
|
|
|
|
var replacementDriver = new GateDriver("same", blocked: false, blockCleanup: false);
|
|
var replacement = parent.ChildActorOf(DriverInstanceActor.Props(replacementDriver, commsLossTimeout: TimeSpan.Zero,
|
|
clusterId: "cluster", sourceIncarnation: 8, acquisitionCapacity: capacity), TestContext.Current.CancellationToken);
|
|
replacement.Tell(new DriverInstanceActor.InitializeRequested("{}"));
|
|
parent.ExpectMsg<DriverInstanceActor.ConnectivityChanged>(m => m.Connected, PresenceBudget,
|
|
cancellationToken: TestContext.Current.CancellationToken);
|
|
var denied = await replacement.Ask<DriverInstanceActor.AcquisitionDemandApplied>(
|
|
new DriverInstanceActor.SetDesiredSubscriptions(["tag"], TimeSpan.FromSeconds(1), [], 2, 8, true), PresenceBudget,
|
|
TestContext.Current.CancellationToken);
|
|
denied.Succeeded.ShouldBeFalse();
|
|
replacementDriver.Entered.Task.IsCompleted.ShouldBeFalse();
|
|
|
|
var otherDriver = new GateDriver("other", blocked: false, blockCleanup: false);
|
|
var other = parent.ChildActorOf(DriverInstanceActor.Props(otherDriver, commsLossTimeout: TimeSpan.Zero,
|
|
clusterId: "cluster", sourceIncarnation: 9, acquisitionCapacity: capacity), TestContext.Current.CancellationToken);
|
|
other.Tell(new DriverInstanceActor.InitializeRequested("{}"));
|
|
parent.ExpectMsg<DriverInstanceActor.ConnectivityChanged>(m => m.Connected, PresenceBudget,
|
|
cancellationToken: TestContext.Current.CancellationToken);
|
|
var otherAck = await other.Ask<DriverInstanceActor.AcquisitionDemandApplied>(
|
|
new DriverInstanceActor.SetDesiredSubscriptions(["tag"], TimeSpan.FromSeconds(1), [], 3, 9, true), PresenceBudget,
|
|
TestContext.Current.CancellationToken);
|
|
otherAck.Succeeded.ShouldBeTrue();
|
|
|
|
firstDriver.Complete();
|
|
await firstDriver.CleanupEntered.Task.WaitAsync(PresenceBudget, TestContext.Current.CancellationToken);
|
|
denied = await replacement.Ask<DriverInstanceActor.AcquisitionDemandApplied>(
|
|
new DriverInstanceActor.SetDesiredSubscriptions(["tag"], TimeSpan.FromSeconds(1), [], 4, 8, true), PresenceBudget,
|
|
TestContext.Current.CancellationToken);
|
|
denied.Succeeded.ShouldBeFalse("a late handle's unsubscribe is still in flight");
|
|
firstDriver.CompleteCleanup();
|
|
AwaitAssert(() => capacity.ReservedDriverCount.ShouldBe(1), PresenceBudget,
|
|
cancellationToken: TestContext.Current.CancellationToken);
|
|
var accepted = await replacement.Ask<DriverInstanceActor.AcquisitionDemandApplied>(
|
|
new DriverInstanceActor.SetDesiredSubscriptions(["tag"], TimeSpan.FromSeconds(1), [], 5, 8, true), PresenceBudget,
|
|
TestContext.Current.CancellationToken);
|
|
accepted.Succeeded.ShouldBeTrue();
|
|
firstDriver.CleanupCount.ShouldBe(1);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Replacement_keeps_capacity_until_previous_handle_actually_finishes_cleanup()
|
|
{
|
|
var capacity = new SubscriptionAcquisitionCapacity(2);
|
|
var parent = CreateTestProbe();
|
|
var driver = new GateDriver("same", blocked: false, blockCleanup: true);
|
|
var actor = parent.ChildActorOf(DriverInstanceActor.Props(driver, commsLossTimeout: TimeSpan.Zero,
|
|
clusterId: "cluster", sourceIncarnation: 7, acquisitionCapacity: capacity), TestContext.Current.CancellationToken);
|
|
actor.Tell(new DriverInstanceActor.InitializeRequested("{}"));
|
|
parent.ExpectMsg<DriverInstanceActor.ConnectivityChanged>(m => m.Connected, PresenceBudget,
|
|
cancellationToken: TestContext.Current.CancellationToken);
|
|
var first = await actor.Ask<DriverInstanceActor.AcquisitionDemandApplied>(
|
|
new DriverInstanceActor.SetDesiredSubscriptions(["tag"], TimeSpan.FromSeconds(1), [], 1, 7, true), PresenceBudget,
|
|
TestContext.Current.CancellationToken);
|
|
first.Succeeded.ShouldBeTrue();
|
|
var replacement = actor.Ask<DriverInstanceActor.AcquisitionDemandApplied>(
|
|
new DriverInstanceActor.SetDesiredSubscriptions(["tag", "next"], TimeSpan.FromSeconds(1), [], 2, 7, true), PresenceBudget,
|
|
TestContext.Current.CancellationToken);
|
|
await driver.CleanupEntered.Task.WaitAsync(PresenceBudget, TestContext.Current.CancellationToken);
|
|
(await replacement.WaitAsync(PresenceBudget, TestContext.Current.CancellationToken)).Succeeded.ShouldBeTrue();
|
|
var third = await actor.Ask<DriverInstanceActor.AcquisitionDemandApplied>(
|
|
new DriverInstanceActor.SetDesiredSubscriptions(["tag", "third"], TimeSpan.FromSeconds(1), [], 3, 7, true), PresenceBudget,
|
|
TestContext.Current.CancellationToken);
|
|
third.Succeeded.ShouldBeFalse();
|
|
capacity.ReservedDriverCount.ShouldBe(1);
|
|
driver.CompleteCleanup();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Timed_out_replacement_blocks_further_dispatch_until_late_handle_cleanup_finishes()
|
|
{
|
|
var clock = new ManualTimeProvider();
|
|
var capacity = new SubscriptionAcquisitionCapacity(2);
|
|
var parent = CreateTestProbe();
|
|
var driver = new SlowReplacementDriver();
|
|
var actor = parent.ChildActorOf(DriverInstanceActor.Props(driver, commsLossTimeout: TimeSpan.Zero,
|
|
clusterId: "cluster", sourceIncarnation: 7, acquisitionCapacity: capacity,
|
|
acquisitionTimeout: TimeSpan.FromSeconds(5), acquisitionClock: clock), TestContext.Current.CancellationToken);
|
|
actor.Tell(new DriverInstanceActor.InitializeRequested("{}"));
|
|
parent.ExpectMsg<DriverInstanceActor.ConnectivityChanged>(m => m.Connected, PresenceBudget,
|
|
cancellationToken: TestContext.Current.CancellationToken);
|
|
var first = await actor.Ask<DriverInstanceActor.AcquisitionDemandApplied>(
|
|
new DriverInstanceActor.SetDesiredSubscriptions(["tag"], TimeSpan.FromSeconds(1), [], 1, 7, true), PresenceBudget,
|
|
TestContext.Current.CancellationToken);
|
|
first.Succeeded.ShouldBeTrue();
|
|
var deadline = clock.GetUtcNow().AddSeconds(5);
|
|
var second = actor.Ask<DriverInstanceActor.AcquisitionDemandApplied>(
|
|
new DriverInstanceActor.SetDesiredSubscriptions(["tag", "b"], TimeSpan.FromSeconds(1), [], 2, 7, true), PresenceBudget,
|
|
TestContext.Current.CancellationToken);
|
|
await driver.Entered.Task.WaitAsync(PresenceBudget, TestContext.Current.CancellationToken);
|
|
await clock.WaitForTimerDueAtAsync(deadline, PresenceBudget);
|
|
clock.Advance(TimeSpan.FromSeconds(5));
|
|
(await second.WaitAsync(PresenceBudget, TestContext.Current.CancellationToken)).Succeeded.ShouldBeFalse();
|
|
var denied = await actor.Ask<DriverInstanceActor.AcquisitionDemandApplied>(
|
|
new DriverInstanceActor.SetDesiredSubscriptions(["tag", "c"], TimeSpan.FromSeconds(1), [], 3, 7, true), PresenceBudget,
|
|
TestContext.Current.CancellationToken);
|
|
denied.Succeeded.ShouldBeFalse();
|
|
denied.Busy.ShouldBeTrue();
|
|
var ready = denied.RetryWhenReady.ShouldNotBeNull();
|
|
driver.CallCount.ShouldBe(2);
|
|
driver.CompleteLate();
|
|
await driver.CleanupEntered.Task.WaitAsync(PresenceBudget, TestContext.Current.CancellationToken);
|
|
ready.IsCompleted.ShouldBeFalse();
|
|
driver.CompleteCleanup();
|
|
(await ready.WaitAsync(PresenceBudget, TestContext.Current.CancellationToken)).ShouldBeTrue();
|
|
var fourth = await actor.Ask<DriverInstanceActor.AcquisitionDemandApplied>(
|
|
new DriverInstanceActor.SetDesiredSubscriptions(["tag", "d"], TimeSpan.FromSeconds(1), [], 4, 7, true), PresenceBudget,
|
|
TestContext.Current.CancellationToken);
|
|
fourth.Succeeded.ShouldBeTrue();
|
|
driver.CallCount.ShouldBe(3);
|
|
}
|
|
|
|
private sealed class SlowReplacementDriver : StubDriver, ISubscribable, IAcknowledgedAcquisitionSource
|
|
{
|
|
private readonly TaskCompletionSource _late = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
private readonly TaskCompletionSource _cleanup = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
internal readonly TaskCompletionSource Entered = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
internal readonly TaskCompletionSource CleanupEntered = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
internal int CallCount;
|
|
public event EventHandler<DataChangeEventArgs>? OnDataChange { add { } remove { } }
|
|
public Task<ISubscriptionHandle> SubscribeAsync(IReadOnlyList<string> refs, TimeSpan interval, CancellationToken ct) =>
|
|
Task.FromResult<ISubscriptionHandle>(new Handle());
|
|
public async Task UnsubscribeAsync(ISubscriptionHandle handle, CancellationToken ct)
|
|
{
|
|
CleanupEntered.TrySetResult();
|
|
await _cleanup.Task;
|
|
}
|
|
public async Task<AcquisitionSubscriptionResult> SubscribeAcquisitionAsync(AcquisitionSubscriptionRequest request,
|
|
Action<AcquisitionBatch> callback, CancellationToken ct)
|
|
{
|
|
if (Interlocked.Increment(ref CallCount) == 2)
|
|
{
|
|
Entered.TrySetResult();
|
|
await _late.Task; // deliberate cancellation-ignoring late operation
|
|
}
|
|
return new(new Handle(), request.RequestedSamplingInterval, request.DesiredRevision, request.ProducerIncarnation);
|
|
}
|
|
internal void CompleteLate() => _late.TrySetResult();
|
|
internal void CompleteCleanup() => _cleanup.TrySetResult();
|
|
private sealed class Handle : ISubscriptionHandle { public string DiagnosticId => "late-replacement"; }
|
|
}
|
|
|
|
private sealed class GateDriver(string id, bool blocked, bool blockCleanup) : StubDriver, IDriver, ISubscribable, IAcknowledgedAcquisitionSource
|
|
{
|
|
string IDriver.DriverInstanceId => id;
|
|
private readonly TaskCompletionSource _result = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
private readonly TaskCompletionSource _cleanup = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
internal readonly TaskCompletionSource Entered = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
internal readonly TaskCompletionSource CleanupEntered = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
internal int CleanupCount;
|
|
public event EventHandler<DataChangeEventArgs>? OnDataChange { add { } remove { } }
|
|
public Task<ISubscriptionHandle> SubscribeAsync(IReadOnlyList<string> refs, TimeSpan interval, CancellationToken ct) =>
|
|
Task.FromResult<ISubscriptionHandle>(new Handle());
|
|
public async Task UnsubscribeAsync(ISubscriptionHandle handle, CancellationToken ct)
|
|
{
|
|
Interlocked.Increment(ref CleanupCount);
|
|
CleanupEntered.TrySetResult();
|
|
if (blockCleanup) await _cleanup.Task;
|
|
}
|
|
public async Task<AcquisitionSubscriptionResult> SubscribeAcquisitionAsync(AcquisitionSubscriptionRequest request,
|
|
Action<AcquisitionBatch> callback, CancellationToken ct)
|
|
{
|
|
Entered.TrySetResult();
|
|
if (blocked) await _result.Task; // deliberately ignores actor cancellation
|
|
return new(new Handle(), request.RequestedSamplingInterval, request.DesiredRevision, request.ProducerIncarnation);
|
|
}
|
|
internal void Complete() => _result.TrySetResult();
|
|
internal void CompleteCleanup() => _cleanup.TrySetResult();
|
|
private sealed class Handle : ISubscriptionHandle { public string DiagnosticId => "capacity"; }
|
|
}
|
|
}
|