Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataOriginHistorySessionTests.cs
T

110 lines
6.4 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]
[Trait("Phase", "H4")]
public void Open_continuations_count_a_chunk_holding_its_page_cursor_once()
{
// H4 review item 4: Cursors.Count + ChunkCount counted a chunk AND the page cursor behind it.
var sessions = new OriginHistorySessions(TimeProvider.System);
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");
session.SaveChunk(new OriginHistorySession.Chunk(view, "k", "i", [], 0, Page(), null, 0, 10), 1000).ShouldNotBeNull();
session.SaveChunk(new OriginHistorySession.Chunk(view, "k", "i", [], 0, null, null, 0, 10), 1000).ShouldNotBeNull();
Page(); // a plain page cursor, no chunk
(session.Cursors.Count, session.ChunkCount).ShouldBe((2, 2));
session.OpenContinuations.ShouldBe(3, "chunk+its cursor, a chunk without one, a plain cursor");
sessions.OpenContinuations.ShouldBe(3);
}
[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");
}
}