2026-10-11 02:58:54 +02:00
|
|
|
#nullable enable
|
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using GameCult.Caching;
|
|
|
|
|
using GameCult.Caching.MessagePack;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
|
|
namespace GameCult.Caching.Tests
|
|
|
|
|
{
|
|
|
|
|
// The clock behind every storedAt: the wall clock by default, and a pinned one when a golden store has to reproduce.
|
|
|
|
|
public class CultCacheClockTests
|
|
|
|
|
{
|
|
|
|
|
private static readonly CultDocumentRegistry Registry = CultDocumentRegistry.ForTypes(new[] { typeof(UnionParityVectorTests.UnionSurface) });
|
|
|
|
|
private static readonly CultRecordKey Key = new("surface");
|
|
|
|
|
|
|
|
|
|
private string _directory = string.Empty;
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void CreateDirectory()
|
|
|
|
|
{
|
|
|
|
|
_directory = Path.Combine(Path.GetTempPath(), $"cultlib-clock-{Guid.NewGuid():N}");
|
|
|
|
|
Directory.CreateDirectory(_directory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
|
public void DeleteDirectory()
|
|
|
|
|
{
|
|
|
|
|
if (Directory.Exists(_directory)) Directory.Delete(_directory, recursive: true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CultCache Open() => CultCacheMessagePack.Create(Path.Combine(_directory, "clock.cc"), new CultCacheOpenOptions { Registry = Registry });
|
|
|
|
|
|
|
|
|
|
private static string Upsert(CultCache cache)
|
|
|
|
|
{
|
|
|
|
|
cache.UpsertAsync(typeof(UnionParityVectorTests.UnionSurface), new UnionParityVectorTests.UnionSurface(), Key).GetAwaiter().GetResult();
|
|
|
|
|
return cache.GetStored(Key)!.StoredAt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ADefaultCacheStampsTheWallClock()
|
|
|
|
|
{
|
|
|
|
|
using var cache = Open();
|
|
|
|
|
var before = DateTimeOffset.UtcNow;
|
|
|
|
|
var stamp = DateTimeOffset.Parse(Upsert(cache), CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
|
|
|
|
|
var after = DateTimeOffset.UtcNow;
|
|
|
|
|
Assert.That(stamp, Is.InRange(before, after));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void APinnedClockIsTheStoredAt()
|
|
|
|
|
{
|
|
|
|
|
var pinned = new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero);
|
|
|
|
|
using var cache = Open();
|
|
|
|
|
cache.Clock = () => pinned;
|
|
|
|
|
Assert.That(Upsert(cache), Is.EqualTo("2026-01-01T00:00:00.0000000+00:00"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void APinnedClockStillIncreasesStoredAtPerKey()
|
|
|
|
|
{
|
|
|
|
|
var pinned = new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero);
|
|
|
|
|
using var cache = Open();
|
|
|
|
|
cache.Clock = () => pinned;
|
|
|
|
|
Upsert(cache);
|
|
|
|
|
Assert.That(Upsert(cache), Is.EqualTo("2026-01-01T00:00:00.0000001+00:00"), "the same instant again is one tick later");
|
|
|
|
|
}
|
2026-11-28 02:48:44 +00:00
|
|
|
|
|
|
|
|
private static readonly CultDocumentRegistry DeckRegistry = CultDocumentRegistry.ForTypes(new[] { typeof(ElementIdTests.IdDeck) });
|
|
|
|
|
private static readonly DateTimeOffset Pinned = new(2026, 1, 1, 0, 0, 0, TimeSpan.Zero);
|
|
|
|
|
|
|
|
|
|
private static ElementIdTests.IdDeck Deck(string? reelId) => new()
|
|
|
|
|
{
|
|
|
|
|
Name = "deck",
|
|
|
|
|
Reels = { new ElementIdTests.IdReel { Label = "reel", Id = reelId ?? string.Empty, Marks = { new ElementIdTests.IdMark { Text = "m", Id = "00000000c10a" } } } }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Replays the same keyed writes (a write, then a replacement) into a fresh store file and returns the file's bytes.
|
|
|
|
|
private byte[] Replay(string name, string? reelId)
|
|
|
|
|
{
|
|
|
|
|
var path = Path.Combine(_directory, name);
|
|
|
|
|
using (var cache = CultCacheMessagePack.Create(path, new CultCacheOpenOptions { Registry = DeckRegistry }))
|
|
|
|
|
{
|
|
|
|
|
cache.Clock = () => Pinned;
|
|
|
|
|
cache.Commit(batch => batch.Upsert(typeof(ElementIdTests.IdDeck), Deck(reelId), new CultRecordKey("deck")));
|
|
|
|
|
cache.FlushAsync().GetAwaiter().GetResult();
|
|
|
|
|
cache.Commit(batch => batch.Upsert(typeof(ElementIdTests.IdDeck), Deck(reelId), new CultRecordKey("deck")));
|
|
|
|
|
cache.FlushAsync().GetAwaiter().GetResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return File.ReadAllBytes(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TwoReplaysWithAPinnedClockWriteIdenticalBytes()
|
|
|
|
|
{
|
|
|
|
|
Assert.That(Replay("a.cc", "00000000c10b"), Is.EqualTo(Replay("b.cc", "00000000c10b")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void AnUnsetElementIdBreaksByteIdentity()
|
|
|
|
|
{
|
|
|
|
|
Assert.That(Replay("a.cc", null), Is.Not.EqualTo(Replay("b.cc", null)), "a write mints an unset element id at random");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void SettingANullClockThrows()
|
|
|
|
|
{
|
|
|
|
|
using var cache = Open();
|
|
|
|
|
Assert.Throws<ArgumentNullException>(() => cache.Clock = null!);
|
|
|
|
|
Assert.That(Upsert(cache), Is.Not.Null, "the refused value left the clock as it was");
|
|
|
|
|
}
|
2026-10-11 02:58:54 +02:00
|
|
|
}
|
|
|
|
|
}
|