40 lines
1.7 KiB
C#
40 lines
1.7 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests.DataConnections;
|
|
|
|
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "P08")]
|
|
public sealed class GrpcDataProjectedNodeLifetimeTests
|
|
{
|
|
[Fact]
|
|
public void Active_pin_blocks_idle_retirement_and_forced_retirement_invalidates_it()
|
|
{
|
|
var lifetime = new ProjectedNodeLifetime();
|
|
lifetime.TryPin(out var pin).ShouldBeTrue();
|
|
pin.ShouldNotBeNull();
|
|
lifetime.TryRetire().ShouldBeFalse();
|
|
pin.IsValid.ShouldBeTrue();
|
|
lifetime.ForceRetire();
|
|
pin.IsValid.ShouldBeFalse();
|
|
lifetime.TryPin(out _).ShouldBeFalse();
|
|
pin.Dispose();
|
|
}
|
|
|
|
[Fact(Timeout = 15000)]
|
|
public async Task Concurrent_acquisition_and_idle_prune_have_only_one_winner()
|
|
{
|
|
for (var i = 0; i < 64; i++)
|
|
{
|
|
var lifetime = new ProjectedNodeLifetime();
|
|
using var start = new Barrier(2);
|
|
var acquire = Task.Run(() => { start.SignalAndWait(); return lifetime.TryPin(out var pin) ? pin : null; }, TestContext.Current.CancellationToken);
|
|
var retire = Task.Run(() => { start.SignalAndWait(); return lifetime.TryRetire(); }, TestContext.Current.CancellationToken);
|
|
var pin = await acquire.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
var retired = await retire.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
(pin is not null).ShouldBe(!retired);
|
|
if (pin is not null) { pin.IsValid.ShouldBeTrue(); pin.Dispose(); lifetime.TryRetire().ShouldBeTrue(); }
|
|
lifetime.TryPin(out _).ShouldBeFalse();
|
|
}
|
|
}
|
|
}
|