Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataOriginHistorySessionTests.cs
T
Joseph Doherty a3da73e1e7 fix(grpc-data): H2 review2-2..6 - byte-bounded pages, one deadline, no leaked cursors
Each node's Raw page is clamped from a per-value byte estimate of its catalog type (wide strings/arrays get small
pages plus a continuation) and sub-batches are sized by estimated bytes, so the in-flight read respects
MaxHistoryResponseBytes; AtTime/Processed whose values x estimate exceed the budget are refused per node with a
diagnostic. One deadline per request: nodes not started when it passes are deferred (transport) or BadTimeout
(direct). Every refusal releases the page cursor it was just given; releasing, invalidating or expiring a chunk
releases the page cursor behind it; results are disposed on the exception path. Chunk bindings use per-node details.
OpcUaServer IVT for Runtime.Tests/Host.Tests so tests can hold real page cursors.
2026-11-23 23:16:14 -05:00

92 lines
5.2 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;
/// <summary>H2 review: origin history sessions are bounded per principal and a principal cannot evict another's.</summary>
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "H2")]
public sealed class GrpcDataOriginHistorySessionTests
{
private static DataAuthenticatedContext Caller(string principal) => new(principal, "key-" + principal, "v1", null, 1);
private static DataHistoryChannel Channel(string id, CancellationTokenSource? closed = null) => new(id, closed?.Token ?? CancellationToken.None);
[Fact]
public void A_principal_past_its_cap_evicts_its_own_oldest_session_never_another_principals()
{
var sessions = new OriginHistorySessions(TimeProvider.System, maxSessions: 100, maxSessionsPerPrincipal: 3);
var victim = sessions.Get(Caller("b"), Channel("b-1"), 4);
var a = Enumerable.Range(0, 3).Select(i => sessions.Get(Caller("a"), Channel("a-" + i), 4)).ToArray();
sessions.Get(Caller("a"), Channel("a-3"), 4);
sessions.Count.ShouldBe(4, "a stays at its cap of 3, b keeps its one");
sessions.TryGet(Caller("a"), Channel("a-0")).ShouldBeNull("a's least-recently-used session went");
sessions.TryGet(Caller("b"), Channel("b-1")).ShouldBeSameAs(victim);
a[1].ShouldBeSameAs(sessions.TryGet(Caller("a"), Channel("a-1")));
}
[Fact]
public void At_the_global_cap_the_principal_holding_the_most_sessions_pays()
{
var sessions = new OriginHistorySessions(TimeProvider.System, maxSessions: 4, maxSessionsPerPrincipal: 10);
for (var i = 0; i < 3; i++) sessions.Get(Caller("heavy"), Channel("h-" + i), 4);
var light = sessions.Get(Caller("light"), Channel("l-0"), 4);
sessions.Get(Caller("light"), Channel("l-1"), 4);
sessions.Count.ShouldBe(4);
sessions.TryGet(Caller("light"), Channel("l-0")).ShouldBeSameAs(light);
sessions.TryGet(Caller("heavy"), Channel("h-0")).ShouldBeNull();
}
[Fact]
public void Lookup_for_a_release_never_creates_a_session_and_a_closed_channel_clears_and_unregisters()
{
var sessions = new OriginHistorySessions(TimeProvider.System);
sessions.TryGet(Caller("a"), Channel("x")).ShouldBeNull();
sessions.Count.ShouldBe(0);
using var closed = new CancellationTokenSource();
var session = sessions.Get(Caller("a"), Channel("c", closed), 4);
session.SaveChunk(new OriginHistorySession.Chunk(new(new("c", "u", "string", "x"), DataRealm.Raw, null, 0, "x"), "k", "i", [], 0, null, null, 0, 100),
1000).ShouldNotBeNull();
sessions.RetainedBytes("a").ShouldBe(100);
closed.Cancel();
sessions.Count.ShouldBe(0);
sessions.RetainedBytes("a").ShouldBe(0, "clearing a session returns its remainder bytes");
}
[Fact]
public void Releasing_or_expiring_a_chunk_also_releases_the_page_cursor_behind_it()
{
var clock = new ZB.MOM.WW.OtOpcUa.Runtime.Tests.Harness.ManualTimeProvider();
var sessions = new OriginHistorySessions(clock);
var session = sessions.Get(Caller("a"), Channel("1"), 8);
var view = new DataView(new("c", "u", "string", "x"), DataRealm.Raw, null, 0, "x");
byte[] Page() => session.Cursors.Store.Save(new ZB.MOM.WW.OtOpcUa.OpcUaServer.HistoryContinuationState("t", DateTime.UnixEpoch,
DateTime.UnixEpoch, 0, 1) { BoundIdentifier = "x" }, "a", "inc", "h");
var released = session.SaveChunk(new OriginHistorySession.Chunk(view, "k", "i", [], 0, Page(), null, 0, 10), 1000).ShouldNotBeNull();
session.SaveChunk(new OriginHistorySession.Chunk(view, "k", "i", [], 0, Page(), null, 0, 0, Deferred: true), 1000).ShouldNotBeNull();
session.Cursors.Count.ShouldBe(2);
session.Release(released);
session.Cursors.Count.ShouldBe(1, "release dropped the chunk AND its page cursor");
clock.Advance(TimeSpan.FromSeconds(61));
session.ChunkCount.ShouldBe(0);
session.Cursors.Count.ShouldBe(0, "idle expiry of a chunk dropped its page cursor too");
sessions.RetainedBytes("a").ShouldBe(0);
}
[Fact]
public void Remainder_bytes_are_budgeted_per_principal_across_sessions()
{
var sessions = new OriginHistorySessions(TimeProvider.System);
var view = new DataView(new("c", "u", "string", "x"), DataRealm.Raw, null, 0, "x");
OriginHistorySession.Chunk Chunk(long bytes) => new(view, "k", "i", [], 0, null, null, 0, bytes);
var one = sessions.Get(Caller("a"), Channel("1"), 8);
var two = sessions.Get(Caller("a"), Channel("2"), 8);
var other = sessions.Get(Caller("b"), Channel("3"), 8);
var token = one.SaveChunk(Chunk(600), 1000).ShouldNotBeNull();
two.SaveChunk(Chunk(600), 1000).ShouldBeNull("the principal's budget spans its sessions");
other.SaveChunk(Chunk(600), 1000).ShouldNotBeNull("another principal has its own budget");
one.TakeChunk(token).ShouldNotBeNull();
two.SaveChunk(Chunk(600), 1000).ShouldNotBeNull("taking a chunk returned its bytes");
}
}