78 lines
3.8 KiB
C#
78 lines
3.8 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
|
|
|
|
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "P08")]
|
|
public sealed class GrpcDataConfiguredAcquisitionTests
|
|
{
|
|
[Fact(Timeout = 30000)]
|
|
public async Task Configured_interest_owns_one_scoped_acquisition_until_last_interest_is_removed()
|
|
{
|
|
var reference = ProjectedDataIds.DependencyReference(DataRealm.Raw,
|
|
ProjectedDataIds.Identifier("mapping", new DataAddress("origin", "urn:raw", "string", "plant/tag")));
|
|
var discovery = new Discovery();
|
|
await using var owner = new ConfiguredProjectedInterestOwner(discovery, 2);
|
|
owner.SetInterest(reference, true);
|
|
var token = TestContext.Current.CancellationToken;
|
|
await discovery.Started.Task.WaitAsync(TimeSpan.FromSeconds(5), token);
|
|
owner.SetInterest(reference, true);
|
|
discovery.Calls.ShouldBe(1);
|
|
owner.SetInterest(reference, false);
|
|
await discovery.Lease.Closed.Task.WaitAsync(TimeSpan.FromSeconds(5), token);
|
|
discovery.Lease.Valid.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact(Timeout = 30000)]
|
|
public async Task Many_interests_share_a_fixed_acquisition_worker_limit()
|
|
{
|
|
var discovery = new BlockingDiscovery();
|
|
await using var owner = new ConfiguredProjectedInterestOwner(discovery, 16, maxConcurrentCalls: 2);
|
|
try
|
|
{
|
|
for (var i = 0; i < 12; i++)
|
|
owner.SetInterest(ProjectedDataIds.DependencyReference(DataRealm.Raw, ProjectedDataIds.Identifier("mapping", new DataAddress("origin", "urn:raw", "string", "plant/" + i))), true);
|
|
await discovery.TwoEntered.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
|
discovery.Maximum.ShouldBe(2);
|
|
discovery.Calls.ShouldBe(2);
|
|
}
|
|
finally { discovery.Release.TrySetResult(); }
|
|
}
|
|
|
|
private sealed class BlockingDiscovery : IConfiguredProjectionDiscovery
|
|
{
|
|
private int _running; public int Calls, Maximum;
|
|
public readonly TaskCompletionSource TwoEntered = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
public readonly TaskCompletionSource Release = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
public async Task<IConfiguredProjectionLease?> ResolveAsync(string reference, CancellationToken cancellationToken)
|
|
{
|
|
var count = Interlocked.Increment(ref _running); Interlocked.Increment(ref Calls);
|
|
if (count > Maximum) Maximum = count;
|
|
if (count == 2) TwoEntered.TrySetResult();
|
|
try { await Release.Task.WaitAsync(TimeSpan.FromSeconds(5), cancellationToken); return new ScopedLease(); }
|
|
finally { Interlocked.Decrement(ref _running); }
|
|
}
|
|
}
|
|
|
|
private sealed class Discovery : IConfiguredProjectionDiscovery
|
|
{
|
|
public int Calls;
|
|
public readonly TaskCompletionSource Started = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
public readonly ScopedLease Lease = new();
|
|
public Task<IConfiguredProjectionLease?> ResolveAsync(string reference, CancellationToken cancellationToken)
|
|
{
|
|
Interlocked.Increment(ref Calls); Started.TrySetResult();
|
|
return Task.FromResult<IConfiguredProjectionLease?>(Lease);
|
|
}
|
|
}
|
|
private sealed class ScopedLease : IConfiguredProjectionLease
|
|
{
|
|
public bool Valid = true;
|
|
public bool IsValid => Valid;
|
|
public readonly TaskCompletionSource Closed = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
public ValueTask DisposeAsync() { Valid = false; Closed.TrySetResult(); return ValueTask.CompletedTask; }
|
|
}
|
|
}
|