75e6ff9af8
#960: a projected read reserves 16 KiB for a fixed-size scalar (16 KiB + 256 B/element for a fixed-length fixed-size array) instead of the whole MaxPendingBytes, so the default 1 MiB admits 64 concurrent scalar reads; String/Bytes/variable arrays keep the whole cap, and a waiting whole-cap read holds back smaller ones for 100 ms (anti-starvation). Writes reserve 16 KiB + identifiers + 3 x the value bound (what the origin's DataWriteExecutor admits the batch against). Monitored-item renewals reserve 256 KiB. Measured: 4 x 250 ms maxAge=0 reads 1016 ms / 1 in flight -> 285 ms / 4 in flight. #961: TryAcquireProjectedItem reports BadResourceUnavailable for a stream-capacity refusal; the node manager's CreateMonitoredItem override turns the item manager's ServiceResultException into that item's status (was a whole-request fault), and an authority race is re-classified outside Lock like the per-item probe.
79 lines
4.5 KiB
C#
79 lines
4.5 KiB
C#
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
|
|
{
|
|
// #960: a write reserves what its value needs, so the held write is itself large enough (~2 MiB of the 4 MiB) that the
|
|
// second (~2.5 MiB) no longer fits beside it.
|
|
var pending = access.WriteAsync(first!, new int[16_000], 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 };
|
|
}
|
|
}
|