diff --git a/packages/cultcache-py/tests/test_soft_push_all.py b/packages/cultcache-py/tests/test_soft_push_all.py index a1874c7c..4bf8d4c1 100644 --- a/packages/cultcache-py/tests/test_soft_push_all.py +++ b/packages/cultcache-py/tests/test_soft_push_all.py @@ -6,7 +6,7 @@ import unittest from pathlib import Path from cultcache_py import JsonLinesBackingStore, SingleFileMessagePackBackingStore, StoreUnreadableError -from cultcache_py.backing_store import CultCacheEnvelope +from cultcache_py.backing_store import BackingStore, CultCacheEnvelope def envelope(key: str) -> CultCacheEnvelope: @@ -48,6 +48,32 @@ class SoftPushAllTests(unittest.TestCase): SingleFileMessagePackBackingStore(self.path).push_all([envelope("a")], soft=True) self.assertEqual(junk, self.path.read_bytes()) + def test_over_an_unreadable_jsonl_file_it_refuses_and_leaves_it(self) -> None: + junk = b"this is not json\n" + self.path.write_bytes(junk) + with self.assertRaises(ValueError): + JsonLinesBackingStore(self.path).push_all([envelope("a")], soft=True) + self.assertEqual(junk, self.path.read_bytes()) + + def test_the_protocol_default_push_all_has_the_same_three_branches(self) -> None: + class Memory(BackingStore): + def __init__(self) -> None: + self.items: list[CultCacheEnvelope] = [] + + def pull_all(self) -> list[CultCacheEnvelope]: + return list(self.items) + + def _replace_all(self, envelopes: list[CultCacheEnvelope]) -> None: + self.items = list(envelopes) + + store = Memory() + store.push_all([envelope("a")], soft=True) # nothing there: writes + self.assertEqual(["a"], [item.key for item in store.items]) + store.push_all([envelope("b")], soft=True) # something there: skips + self.assertEqual(["a"], [item.key for item in store.items]) + store.push_all([envelope("b")]) # plain: lands + self.assertEqual(["a", "b"], sorted(item.key for item in store.items)) + if __name__ == "__main__": unittest.main() diff --git a/packages/cultnet-py/src/cultnet_py/benchmark.py b/packages/cultnet-py/src/cultnet_py/benchmark.py index 0793bfc7..83834635 100644 --- a/packages/cultnet-py/src/cultnet_py/benchmark.py +++ b/packages/cultnet-py/src/cultnet_py/benchmark.py @@ -137,9 +137,7 @@ class InMemoryBackingStore: def push(self, envelope: CultCacheEnvelope) -> None: self._records[(envelope.type, envelope.key)] = envelope - def push_all(self, envelopes: list[CultCacheEnvelope], soft: bool = False) -> None: - if soft and self._records: - return + def push_all(self, envelopes: list[CultCacheEnvelope]) -> None: for envelope in envelopes: self.push(envelope) diff --git a/tests/GameCult.Caching.Tests/BackingStoreTests.cs b/tests/GameCult.Caching.Tests/BackingStoreTests.cs index ffc869e6..e6cd5dc8 100644 --- a/tests/GameCult.Caching.Tests/BackingStoreTests.cs +++ b/tests/GameCult.Caching.Tests/BackingStoreTests.cs @@ -87,6 +87,31 @@ namespace GameCult.Caching.Tests } } + [Test] + public async Task PlainPushAll_Over_Readable_Store_Writes_The_Staged_Records() + { + var filePath = Path.Combine(Path.GetTempPath(), $"cultlib-tests-{Guid.NewGuid():N}.msgpack"); + try + { + var store = new SingleFileMessagePackBackingStore(filePath); + var cache = new CultCache(); + cache.AddBackingStore(store); + await cache.AddAsync(new NamedTestEntry { Name = "first", Value = "one" }); + store.PushAll(); + var before = File.ReadAllBytes(filePath); + + await cache.AddAsync(new NamedTestEntry { Name = "second", Value = "two" }); + store.PushAll(); + + Assert.That(File.ReadAllBytes(filePath), Is.Not.EqualTo(before), "a plain PushAll skipped a store it reads"); + Assert.That(store.IsDirty, Is.False); + } + finally + { + if (File.Exists(filePath)) File.Delete(filePath); + } + } + [Test] public async Task SoftFlush_Over_Absent_Path_Writes() { diff --git a/tests/GameCult.Caching.Tests/DirectoryStoreDurabilityTests.cs b/tests/GameCult.Caching.Tests/DirectoryStoreDurabilityTests.cs index 840110bc..41af7c1b 100644 --- a/tests/GameCult.Caching.Tests/DirectoryStoreDurabilityTests.cs +++ b/tests/GameCult.Caching.Tests/DirectoryStoreDurabilityTests.cs @@ -208,6 +208,54 @@ namespace GameCult.Caching.Tests Assert.That(reader.AllEntries, Is.Empty); } + [Test] + public async Task SoftFlushOverAbsentManifestWrites() + { + using var cache = Open(out var store); + var key = await cache.UpsertAsync(typeof(Page), new Page { Name = "a", Text = "one" }); + + cache.FlushAllBackingStores(soft: true); + + Assert.That(File.Exists(_manifest), Is.True); + Assert.That(store.IsDirty, Is.False); + using var reader = Open(out _); + Assert.That(reader.Get(key)?.Text, Is.EqualTo("one")); + } + + [Test] + public async Task SoftFlushOverReadableManifestLeavesItAndKeepsStagedWritesDirty() + { + using var cache = Open(out var store); + await cache.UpsertAsync(typeof(Page), new Page { Name = "a", Text = "one" }); + cache.FlushAllBackingStores(); + var before = File.ReadAllBytes(_manifest); + var pages = Directory.GetFiles(_records, "*.msgpack").OrderBy(path => path).ToArray(); + var second = await cache.UpsertAsync(typeof(Page), new Page { Name = "b", Text = "two" }); + + cache.FlushAllBackingStores(soft: true); + + Assert.That(File.ReadAllBytes(_manifest), Is.EqualTo(before), "a soft flush rewrote a manifest it reads"); + Assert.That(Directory.GetFiles(_records, "*.msgpack").OrderBy(path => path).ToArray(), Is.EqualTo(pages)); + Assert.That(store.IsDirty, Is.True, "a skipped soft flush discarded a staged write"); + + cache.FlushAllBackingStores(); + using var reader = Open(out _); + Assert.That(reader.Get(second)?.Text, Is.EqualTo("two")); + } + + [Test] + public async Task SoftFlushOverUnreadableManifestRefusesAndLeavesIt() + { + using var cache = Open(out var store); + await cache.UpsertAsync(typeof(Page), new Page { Name = "a", Text = "one" }); + var junk = new byte[] { 0xC1, 0x01, 0x02, 0x03 }; + File.WriteAllBytes(_manifest, junk); + + Assert.That(() => store.PushAll(soft: true), Throws.Exception); + Assert.That(File.ReadAllBytes(_manifest), Is.EqualTo(junk)); + Assert.That(store.IsDirty, Is.True); + } + private CultCache Open(out DirectoryMessagePackBackingStore store) { var cache = new CultCache(Registry); diff --git a/tests/GameCult.Networking.Tests/NetworkingTests.cs b/tests/GameCult.Networking.Tests/NetworkingTests.cs index 606d5625..09764518 100644 --- a/tests/GameCult.Networking.Tests/NetworkingTests.cs +++ b/tests/GameCult.Networking.Tests/NetworkingTests.cs @@ -7377,6 +7377,59 @@ namespace GameCult.Networking.Tests Assert.That(developmentServer.LogSensitivePayloads, Is.True); } + [Test] + public async Task CultNetLocal_Host_SoftFlush_Passes_The_Flag_To_The_Store() + { + var filePath = Path.Combine(Path.GetTempPath(), $"cultnet-host-soft-{Guid.NewGuid():N}.msgpack"); + CultNetHost? host = null; + + try + { + host = await CultNetLocal.CreateHostAsync(filePath, new CultNetHostOptions { StartServer = false }); + await host.Cache.UpsertAsync(new PlayerData { PlayerId = Guid.NewGuid(), Email = "a@example.test", PasswordHash = "hash", Username = "First" }); + await host.FlushAsync(); + var before = File.ReadAllBytes(filePath); + + await host.Cache.UpsertAsync(new PlayerData { PlayerId = Guid.NewGuid(), Email = "b@example.test", PasswordHash = "hash", Username = "Second" }); + await host.FlushAsync(soft: true); + Assert.That(File.ReadAllBytes(filePath), Is.EqualTo(before), "a soft host flush rewrote a store it reads"); + + await host.FlushAsync(); + Assert.That(File.ReadAllBytes(filePath), Is.Not.EqualTo(before), "a plain host flush after a skipped soft flush did not land the staged write"); + } + finally + { + host?.Dispose(); + if (File.Exists(filePath)) File.Delete(filePath); + } + } + + [Test] + public async Task CultMeshNode_SoftFlush_Passes_The_Flag_To_The_Store() + { + var rootPath = Path.Combine(TestContext.CurrentContext.WorkDirectory, "cultmesh-soft-flush", Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(rootPath); + var cachePath = Path.Combine(rootPath, "world.ccmp"); + var documents = new CultNetDocumentRegistry() + .Register(CultNetDocumentBinding.ForDocument()); + + using var node = await CultMesh.CreateNodeAsync(cachePath, new CultMeshNodeOptions + { + StartServer = false, + DatabaseOptions = new CultNetDatabaseOptions { RuntimeId = "soft-flush-local", DocumentRegistry = documents } + }); + await node.Database.PutAsync(new CultRecordKey("note:one"), new MeshQuickstartNote { NoteId = "note:one", Body = "one" }); + await node.FlushAsync(); + var before = File.ReadAllBytes(cachePath); + + await node.Database.PutAsync(new CultRecordKey("note:two"), new MeshQuickstartNote { NoteId = "note:two", Body = "two" }); + await node.FlushAsync(soft: true); + Assert.That(File.ReadAllBytes(cachePath), Is.EqualTo(before), "a soft node flush rewrote a store it reads"); + + await node.FlushAsync(); + Assert.That(File.ReadAllBytes(cachePath), Is.Not.EqualTo(before), "a plain node flush after a skipped soft flush did not land the staged write"); + } + [Test] public async Task CultNetLocal_CreateHostAsync_Wires_Durable_Cache_Without_Starting_Server() {