using Shouldly; using Xunit; using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections; public sealed partial class GrpcDataProjectedAccessTests { [Fact(Timeout = 30000), Trait("Phase", "P11")] public async Task Concurrent_cohort_retries_use_independently_sampled_delays() { var ct = TestContext.Current.CancellationToken; using var fixture = new AccessFixture(streams: true, subscribeOnly: true) { ScalarSnapshots = true }; using var access = fixture.Create(); var calls = 0; var sampledBoth = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); access.RetryJitterForTests = _ => { var call = Interlocked.Increment(ref calls); if (call == 2) sampledBoth.TrySetResult(); return call == 1 ? TimeSpan.FromMilliseconds(20) : TimeSpan.FromSeconds(2); }; access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Subscribe, out var first).ShouldBeTrue(); access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Subscribe, out var second).ShouldBeTrue(); try { access.StartSubscription(first!, TimeSpan.FromSeconds(1)).ShouldBeTrue(); access.StartSubscription(second!, TimeSpan.FromSeconds(2)).ShouldBeTrue(); await fixture.SecondStreamOpened.Task.WaitAsync(TimeSpan.FromSeconds(5), ct); SpinWait.SpinUntil(() => fixture.StreamHandles.Count == 2, TimeSpan.FromSeconds(5)).ShouldBeTrue(); fixture.EndAllStreams(); await sampledBoth.Task.WaitAsync(TimeSpan.FromSeconds(5), ct); SpinWait.SpinUntil(() => Volatile.Read(ref fixture.StreamOpens) >= 3, TimeSpan.FromSeconds(5)).ShouldBeTrue(); await Task.Delay(200, ct); Volatile.Read(ref fixture.StreamOpens).ShouldBe(3, "the later sampled retry must remain independently delayed"); Volatile.Read(ref calls).ShouldBe(2); } finally { first?.Dispose(); second?.Dispose(); } } }