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 Full_write_quota_rejects_large_array_without_encoding_and_restores_capacity_after_completion() { using var fixture = new AccessFixture(writable: true, array: true) { BlockWrite = true }; MakeArrayLengthVariable(fixture); using var access = fixture.Create(maxBytes: 4 * 1024 * 1024); access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Write, out var first).ShouldBeTrue(); access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Write, out var second).ShouldBeTrue(); var budget = new DataBudget(TimeSpan.FromSeconds(5), 1, 4 * 1024 * 1024); // Fits the conservative post-fix encoding bound; rejection must come from the full quota. var large = new int[20_000]; try { var pending = access.WriteAsync(first!, new[] { 1, 2, 3 }, budget, fixture.Clock.GetTimestamp(), TestContext.Current.CancellationToken); await fixture.WriteEntered.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken); var before = GC.GetAllocatedBytesForCurrentThread(); var rejected = access.WriteAsync(second!, large, budget, fixture.Clock.GetTimestamp(), TestContext.Current.CancellationToken); rejected.IsCompleted.ShouldBeTrue("full quota must be rejected synchronously"); var allocated = GC.GetAllocatedBytesForCurrentThread() - before; (await rejected).State.ShouldBe(DataWriteState.RejectedBeforeDispatch); allocated.ShouldBeLessThan(128 * 1024L, "a quota-rejected array must not be boxed or copied"); fixture.WriteCalls.ShouldBe(1); fixture.ReleaseWrite.TrySetResult(); (await pending.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken)).State.ShouldBe(DataWriteState.OutcomeUnknown); var deadline = DateTime.UtcNow.AddSeconds(5); DataWriteOutcome result; do { result = await access.WriteAsync(second!, new[] { 4, 5, 6 }, budget, fixture.Clock.GetTimestamp(), TestContext.Current.CancellationToken); if (result.State != DataWriteState.RejectedBeforeDispatch) break; await Task.Delay(10, TestContext.Current.CancellationToken); } while (DateTime.UtcNow < deadline); result.State.ShouldBe(DataWriteState.OutcomeUnknown); fixture.WriteCalls.ShouldBe(2); } finally { fixture.ReleaseWrite.TrySetResult(); first?.Dispose(); second?.Dispose(); } } [Fact(Timeout = 30000), Trait("Phase", "P11")] public async Task Oversized_array_is_rejected_without_encoding_or_dispatch() { using var fixture = new AccessFixture(writable: true, array: true); MakeArrayLengthVariable(fixture); using var access = fixture.Create(maxBytes: 1024); access.TryAcquire(fixture.NodeId, fixture.Caller, DataOperations.Write, out var demand).ShouldBeTrue(); var large = new int[60_000]; try { var budget = new DataBudget(TimeSpan.FromSeconds(5), 1, 4 * 1024 * 1024); var before = GC.GetAllocatedBytesForCurrentThread(); var rejected = access.WriteAsync(demand!, large, budget, fixture.Clock.GetTimestamp(), TestContext.Current.CancellationToken); rejected.IsCompleted.ShouldBeTrue(); var allocated = GC.GetAllocatedBytesForCurrentThread() - before; (await rejected).State.ShouldBe(DataWriteState.RejectedBeforeDispatch); allocated.ShouldBeLessThan(128 * 1024L, "an oversized array must be rejected before boxing or copying"); fixture.WriteCalls.ShouldBe(0); } finally { demand?.Dispose(); } } private static void MakeArrayLengthVariable(AccessFixture fixture) { var metadata = fixture.Proof.Metadata with { Dimensions = [0] }; fixture.Proof = fixture.Proof with { Metadata = metadata, OriginMetadata = fixture.Proof.OriginMetadata with { Dimensions = [0] } }; fixture.Definition = fixture.Definition with { Metadata = metadata }; } }