From 8f59c0c6bc4a996f8e7eb60f55baefb9926bf4b9 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Mon, 23 Nov 2026 19:36:21 -0500 Subject: [PATCH] test(grpc-data): H1 PeerHistoryContinuationStore binding, expiry, eviction and token tests --- .../PeerHistoryContinuationStoreTests.cs | 236 ++++++++++++++++++ 1 file changed, 236 insertions(+) create mode 100644 tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/PeerHistoryContinuationStoreTests.cs diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/PeerHistoryContinuationStoreTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/PeerHistoryContinuationStoreTests.cs new file mode 100644 index 000000000..3745237e8 --- /dev/null +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/PeerHistoryContinuationStoreTests.cs @@ -0,0 +1,236 @@ +using Shouldly; +using Xunit; +using ZB.MOM.WW.OtOpcUa.Commons.OpcUa; + +namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests; + +/// +/// H1 (plan ยง3.2): the origin's per-(principal, incarnation) store for remote HistoryRead continuations โ€” idle expiry, +/// capacity eviction (oldest-idle), every binding field (principal, incarnation, epoch, view, details hash), single-use +/// takes and the token's shape. +/// +[Trait("Feature", "GrpcData")] +[Trait("Phase", "H1")] +public sealed class PeerHistoryContinuationStoreTests +{ + private const string Principal = "svc-consumer"; + private const string Incarnation = "inc-1"; + private const string Hash = "hash-a"; + + private static HistoryContinuationState State(string identifier = "Plant/dev/t1", long epoch = 3, AddressSpaceRealm realm = AddressSpaceRealm.Raw) => + new("WW.T1", DateTime.UnixEpoch, DateTime.UnixEpoch.AddDays(1), 0, 10) + { + BoundRealm = realm, BoundIdentifier = identifier, AddressSpaceEpoch = epoch, + }; + + private static PeerHistoryContinuationKey Key( + string principal = Principal, string incarnation = Incarnation, long epoch = 3, + AddressSpaceRealm realm = AddressSpaceRealm.Raw, string identifier = "Plant/dev/t1", string hash = Hash) => + new(principal, incarnation, epoch, realm, identifier, hash); + + private static PeerHistoryContinuationStore Store(int capacity = 8, ManualClock? clock = null, TimeSpan? idle = null) => + new(Principal, Incarnation, capacity, idle, clock ?? new ManualClock()); + + [Fact] + public void A_matching_take_returns_the_state_once() + { + var store = Store(); + var state = State(); + var token = store.Save(state, Hash); + + store.Count.ShouldBe(1); + store.TryTake(token, Key()).ShouldBe(state); + store.TryTake(token, Key()).ShouldBeNull("takes are single-use"); + store.Count.ShouldBe(0); + } + + [Fact] + public void A_mismatched_take_consumes_the_record_too() + { + var store = Store(); + var token = store.Save(State(), Hash); + store.TryTake(token, Key(hash: "other")).ShouldBeNull(); + store.TryTake(token, Key()).ShouldBeNull("a failed take never leaves the token replayable"); + } + + [Fact] + public void Every_bound_field_must_match() + { + var cases = new (string Name, PeerHistoryContinuationKey Key)[] + { + ("principal", Key(principal: "someone-else")), + ("incarnation", Key(incarnation: "inc-2")), + ("epoch bump", Key(epoch: 4)), + ("realm", Key(realm: AddressSpaceRealm.Uns)), + ("identifier", Key(identifier: "Plant/dev/t2")), + ("details hash", Key(hash: "hash-b")), + }; + foreach (var (name, key) in cases) + { + var store = Store(); + var token = store.Save(State(), Hash); + store.TryTake(token, key).ShouldBeNull(name); + } + } + + [Fact] + public void An_idle_record_expires() + { + var clock = new ManualClock(); + var store = Store(clock: clock); + var fresh = store.Save(State(), Hash); + clock.Advance(TimeSpan.FromSeconds(59)); + store.TryTake(fresh, Key()).ShouldNotBeNull("59 s is inside the default 60 s idle expiry"); + + var stale = store.Save(State(), Hash); + clock.Advance(TimeSpan.FromSeconds(60)); + store.Count.ShouldBe(0); + store.TryTake(stale, Key()).ShouldBeNull(); + PeerHistoryContinuationStore.DefaultIdleExpiry.ShouldBe(TimeSpan.FromSeconds(60)); + } + + [Fact] + public void A_custom_idle_expiry_applies() + { + var clock = new ManualClock(); + var store = Store(clock: clock, idle: TimeSpan.FromSeconds(5)); + var token = store.Save(State(), Hash); + clock.Advance(TimeSpan.FromSeconds(5)); + store.TryTake(token, Key()).ShouldBeNull(); + } + + [Fact] + public void Capacity_evicts_the_oldest_idle_record() + { + var clock = new ManualClock(); + var store = Store(capacity: 3, clock: clock); + var tokens = new List(); + for (var i = 0; i < 3; i++) + { + tokens.Add(store.Save(State(), Hash)); + clock.Advance(TimeSpan.FromSeconds(1)); + } + + // Take the middle one: the oldest-idle survivor is still tokens[0]. + store.TryTake(tokens[1], Key()).ShouldNotBeNull(); + tokens.Add(store.Save(State(), Hash)); // 3 live: 0, 2, 3 โ€” at capacity, nothing evicted yet + store.Count.ShouldBe(3); + tokens.Add(store.Save(State(), Hash)); // over capacity โ‡’ evicts tokens[0] + store.Count.ShouldBe(3); + + store.TryTake(tokens[0], Key()).ShouldBeNull("the oldest-idle record was evicted"); + store.TryTake(tokens[2], Key()).ShouldNotBeNull(); + store.TryTake(tokens[3], Key()).ShouldNotBeNull(); + store.TryTake(tokens[4], Key()).ShouldNotBeNull(); + } + + [Fact] + public void Expired_records_are_swept_before_capacity_evicts_a_live_one() + { + var clock = new ManualClock(); + var store = Store(capacity: 2, clock: clock); + store.Save(State(), Hash); + clock.Advance(TimeSpan.FromSeconds(61)); + var live = store.Save(State(), Hash); + var newest = store.Save(State(), Hash); + store.TryTake(live, Key()).ShouldNotBeNull(); + store.TryTake(newest, Key()).ShouldNotBeNull(); + } + + [Fact] + public void Release_drops_a_record_and_is_idempotent() + { + var store = Store(); + var token = store.Save(State(), Hash); + store.Release(token).ShouldBeTrue(); + store.Release(token).ShouldBeFalse(); + store.TryTake(token, Key()).ShouldBeNull(); + store.Release(null).ShouldBeFalse(); + store.Release([1, 2, 3]).ShouldBeFalse(); + } + + [Fact] + public void Clear_drops_everything() + { + var store = Store(); + var token = store.Save(State(), Hash); + store.Save(State(), Hash); + store.Clear(); + store.Count.ShouldBe(0); + store.TryTake(token, Key()).ShouldBeNull(); + } + + [Fact] + public void Tokens_are_random_16_byte_ids() + { + var store = Store(capacity: 2000); + var seen = new HashSet(); + for (var i = 0; i < 1000; i++) + { + var token = store.Save(State(), Hash); + token.Length.ShouldBe(PeerHistoryContinuationStore.TokenLength); + PeerHistoryContinuationStore.TokenLength.ShouldBe(16); + seen.Add(Convert.ToHexString(token)).ShouldBeTrue("tokens never repeat"); + } + + // Random, not sequential: across 1000 tokens every byte position takes many values. + var tokens = seen.Select(Convert.FromHexString).ToList(); + for (var position = 0; position < 16; position++) + tokens.Select(t => t[position]).Distinct().Count().ShouldBeGreaterThan(100, $"byte {position}"); + } + + [Fact] + public void Malformed_tokens_are_misses() + { + var store = Store(); + store.Save(State(), Hash); + store.TryTake(null, Key()).ShouldBeNull(); + store.TryTake([], Key()).ShouldBeNull(); + store.TryTake(new byte[15], Key()).ShouldBeNull(); + store.TryTake(new byte[17], Key()).ShouldBeNull(); + store.TryTake(new byte[16], Key()).ShouldBeNull(); + store.Count.ShouldBe(1); + } + + [Fact] + public void A_record_must_be_bound_to_a_view_and_arguments_are_validated() + { + var store = Store(); + Should.Throw(() => store.Save(State() with { BoundIdentifier = null }, Hash)); + Should.Throw(() => new PeerHistoryContinuationStore(Principal, Incarnation, 0)); + Should.Throw(() => new PeerHistoryContinuationStore(" ", Incarnation, 1)); + Should.Throw(() => new PeerHistoryContinuationStore(Principal, "", 1)); + Should.Throw(() => new PeerHistoryContinuationStore(Principal, Incarnation, 1, TimeSpan.Zero)); + } + + [Fact] + public void The_scope_binds_the_request_details_hash() + { + var store = Store(); + var scope = new PeerHistoryContinuationScope(store); + var details = new HistoryReadRawVariant(DateTime.UnixEpoch, DateTime.UnixEpoch.AddHours(1), 10, ReturnBounds: false); + var binding = new HistoryContinuationBinding(AddressSpaceRealm.Raw, "Plant/dev/t1", 3, details); + + var token = scope.Save(State(), binding)!; + scope.TryTake(token, binding with { Details = details with { NumValuesPerNode = 11 } }).ShouldBeNull("other details"); + + token = scope.Save(State(), binding)!; + scope.TryTake(token, binding with { Details = details with { } }).ShouldNotBeNull("equal details hash equal"); + + token = scope.Save(State(), binding)!; + scope.TryTake(token, binding with { Epoch = 4 }).ShouldBeNull("epoch bump"); + store.Count.ShouldBe(0); + } + + /// A monotonic test clock. + private sealed class ManualClock : TimeProvider + { + private long _ticks = 1_000_000; + + public override long TimestampFrequency => TimeSpan.TicksPerSecond; + + public override long GetTimestamp() => Interlocked.Read(ref _ticks); + + public void Advance(TimeSpan by) => Interlocked.Add(ref _ticks, by.Ticks); + } +}