Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.Communication.Tests/Grpc/AuditWireBudgetTests.cs
T
2026-09-09 16:13:58 -04:00

40 lines
1.9 KiB
C#

using Grpc.Core;
using ZB.MOM.WW.ScadaBridge.Communication.Grpc;
namespace ZB.MOM.WW.ScadaBridge.Communication.Tests.Grpc;
public class AuditWireBudgetTests
{
[Fact]
public void OversizeSoftBudgetRow_TravelsAloneWithoutTruncation()
{
var batch = new AuditEventBatch();
var bytes = AuditWireBudget.InitialBytes;
var row = new AuditEventDto { EventId = "large", ErrorDetail = new string('x', GrpcMessageLimits.AuditBatchBytes + 1) };
Assert.True(AuditWireBudget.TryAppend(batch.Events, row, ref bytes, row.EventId));
Assert.False(AuditWireBudget.TryAppend(batch.Events, new AuditEventDto { EventId = "next" }, ref bytes, "next"));
Assert.Same(row, Assert.Single(batch.Events));
Assert.Equal(row.ErrorDetail, batch.Events[0].ErrorDetail);
Assert.True(bytes < GrpcMessageLimits.MaxMessageBytes);
}
[Fact]
public void HardOversizeRow_DoesNotBlockGoodPrefix_AndFailsExplicitlyWhenFirst()
{
var good = new AuditEventDto { EventId = "good", ErrorMessage = "small" };
var huge = new AuditEventDto { EventId = "retained-id", ErrorDetail = new string('x', GrpcMessageLimits.MaxMessageBytes) };
var batch = new AuditEventBatch();
var bytes = AuditWireBudget.InitialBytes;
Assert.True(AuditWireBudget.TryAppend(batch.Events, good, ref bytes, good.EventId));
Assert.False(AuditWireBudget.TryAppend(batch.Events, huge, ref bytes, huge.EventId));
Assert.Equal("good", Assert.Single(batch.Events).EventId);
var next = new AuditEventBatch();
bytes = AuditWireBudget.InitialBytes;
var ex = Assert.Throws<RpcException>(() => AuditWireBudget.TryAppend(next.Events, huge, ref bytes, huge.EventId));
Assert.Equal(StatusCode.ResourceExhausted, ex.StatusCode);
Assert.Contains("retained-id", ex.Status.Detail);
Assert.Contains("retained locally", ex.Status.Detail);
Assert.Empty(next.Events);
}
}