53e3970f27
Nodes are read in sub-batches sized to the response budget and no sub-batch starts once it is spent: the rest are DEFERRED chunk tokens (transport; read on resume from their original continuation) or BadTooManyOperations (direct consumer). Processed intervals and AtTime times above MaxHistoryValuesPerPage are refused unsent. Remainder bytes are charged to a per-principal budget across sessions (concurrent reads x max(response budget, one page)); sizes come from the encoded CalculateSize; a single value above the response budget is a per-item BadTooManyOperations. Sessions are capped per principal (own LRU evicted first) and globally (the heaviest principal pays); a release never creates a session; channel-close registrations are unregistered on clear.
72 lines
3.9 KiB
C#
72 lines
3.9 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 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");
|
|
}
|
|
}
|