85 lines
4.4 KiB
C#
85 lines
4.4 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Redundancy;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.Redundancy;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
|
|
|
|
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "P07")]
|
|
public sealed class GrpcDataConsumerScopeTests
|
|
{
|
|
private static DataConnectionScopeLease WaitForLease(DataConnectionManager manager)
|
|
{
|
|
DataConnectionScopeLease? lease = null;
|
|
SpinWait.SpinUntil(() => { try { lease = manager.Acquire("link"); return true; } catch (UnauthorizedAccessException) { return false; } }, TimeSpan.FromSeconds(5)).ShouldBeTrue();
|
|
return lease!;
|
|
}
|
|
|
|
[Fact]
|
|
public void Warm_standby_cannot_acquire_and_hot_secondary_cannot_write()
|
|
{
|
|
DataConnectionRoleAdapter.MayAcquire(RedundancyMode.Warm, WarmRole.Standby, RedundancyRole.Secondary).ShouldBeFalse();
|
|
DataConnectionRoleAdapter.MayAcquire(RedundancyMode.Warm, null, null).ShouldBeFalse();
|
|
DataConnectionRoleAdapter.MayAcquire(RedundancyMode.Warm, WarmRole.Active, RedundancyRole.Primary).ShouldBeTrue();
|
|
DataConnectionRoleAdapter.MayAcquire(RedundancyMode.Hot, null, RedundancyRole.Secondary).ShouldBeTrue();
|
|
DataConnectionRoleAdapter.MayAcquire(RedundancyMode.Hot, null, null).ShouldBeFalse();
|
|
DataConnectionRoleAdapter.MayWrite(RedundancyRole.Secondary).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Blocked_scope_cleanup_stays_charged_and_coalesces_revisions()
|
|
{
|
|
using var manager = new DataConnectionManager("node-a", 1);
|
|
var members = new HashSet<string> { "node-a" };
|
|
manager.ApplyScope(new Dictionary<string, DataConnectionScope> { ["link"] = new(1, members) });
|
|
using var old = manager.Acquire("link");
|
|
using var release = new ManualResetEventSlim();
|
|
var entered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
using var registration = old.CancellationToken.Register(() => { entered.TrySetResult(); release.Wait(); });
|
|
try
|
|
{
|
|
manager.ApplyScope(new Dictionary<string, DataConnectionScope> { ["link"] = new(2, members) });
|
|
await entered.Task.WaitAsync(TestContext.Current.CancellationToken);
|
|
manager.ApplyScope(new Dictionary<string, DataConnectionScope> { ["link"] = new(3, members) });
|
|
Should.Throw<UnauthorizedAccessException>(() => manager.Acquire("link"));
|
|
}
|
|
finally { release.Set(); }
|
|
old.Dispose();
|
|
using var replacement = WaitForLease(manager);
|
|
replacement.Epoch.ShouldBeGreaterThan(old.Epoch);
|
|
}
|
|
|
|
[Fact]
|
|
public void A_new_connection_revision_retires_old_lifetimes_even_when_consumers_are_unchanged()
|
|
{
|
|
using var manager = new DataConnectionManager("node-a", 4);
|
|
var members = new HashSet<string> { "node-a" };
|
|
manager.ApplyScope(new Dictionary<string, DataConnectionScope> { ["link"] = new(1, members) });
|
|
using var old = manager.Acquire("link");
|
|
manager.ApplyScope(new Dictionary<string, DataConnectionScope> { ["link"] = new(2, members) });
|
|
old.CancellationToken.IsCancellationRequested.ShouldBeTrue();
|
|
old.Dispose();
|
|
using var replacement = WaitForLease(manager);
|
|
replacement.Epoch.ShouldBeGreaterThan(old.Epoch);
|
|
}
|
|
|
|
[Fact]
|
|
public void Scope_removal_cancels_existing_lifetime_and_readdition_uses_new_epoch()
|
|
{
|
|
using var manager = new DataConnectionManager("node-a", 4);
|
|
manager.ApplyScope(new Dictionary<string, IReadOnlySet<string>> { ["link"] = new HashSet<string> { "node-a" } });
|
|
using var first = manager.Acquire("link");
|
|
first.CancellationToken.IsCancellationRequested.ShouldBeFalse();
|
|
manager.ApplyScope(new Dictionary<string, IReadOnlySet<string>> { ["link"] = new HashSet<string> { "node-b" } });
|
|
first.CancellationToken.IsCancellationRequested.ShouldBeTrue();
|
|
Should.Throw<UnauthorizedAccessException>(() => manager.Acquire("link"));
|
|
manager.ApplyScope(new Dictionary<string, IReadOnlySet<string>> { ["link"] = new HashSet<string> { "node-a" } });
|
|
first.Dispose();
|
|
using var next = WaitForLease(manager);
|
|
next.Epoch.ShouldBeGreaterThan(first.Epoch);
|
|
next.CancellationToken.IsCancellationRequested.ShouldBeFalse();
|
|
}
|
|
}
|