Preserve dirty CultCache state during pulls

This commit is contained in:
Meta Vens 2026-07-18 23:19:45 +01:00
commit 49c43baa4c
15 changed files with 117 additions and 5 deletions

View file

@ -107,6 +107,53 @@ namespace GameCult.Caching.Tests
}
}
[Test]
public async Task SingleFileMessagePackBackingStore_PullAll_DoesNotEraseUnflushedLocalMutations()
{
var filePath = Path.Combine(Path.GetTempPath(), $"cultlib-tests-{Guid.NewGuid():N}.msgpack");
try
{
using (var seed = await CultCacheMessagePack.OpenAsync(filePath))
{
await seed.UpsertAsync(new NamedTestEntry
{
Name = "persisted",
Value = "before-local-write"
});
await seed.FlushAsync();
}
CultRecordHandle<NamedTestEntry> localHandle;
using (var cache = await CultCacheMessagePack.OpenAsync(filePath))
{
localHandle = await cache.UpsertAsync(new NamedTestEntry
{
Name = "local",
Value = "must-survive-pull"
});
Assert.That(cache.IsDirty, Is.True);
await cache.PullAllBackingStoresAsync();
Assert.That(cache.Get<NamedTestEntry>(localHandle.Key)?.Value,
Is.EqualTo("must-survive-pull"));
Assert.That(cache.IsDirty, Is.True,
"pulling a clean disk snapshot must not pardon staged local mutations");
await cache.FlushAsync();
}
using var reopened = await CultCacheMessagePack.OpenAsync(filePath);
Assert.That(reopened.Get<NamedTestEntry>(localHandle.Key)?.Value,
Is.EqualTo("must-survive-pull"));
}
finally
{
if (File.Exists(filePath))
File.Delete(filePath);
}
}
[Test]
public async Task CultCache_DirtyState_Tracks_Mutations_And_ExplicitFlush()
{
@ -425,6 +472,60 @@ namespace GameCult.Caching.Tests
}
}
[Test]
public async Task DirectoryMessagePackBackingStore_PullAll_PreservesUnflushedLocalKeys()
{
var filePath = Path.Combine(Path.GetTempPath(), $"cultlib-tests-{Guid.NewGuid():N}.cc");
var recordsPath = DirectoryMessagePackBackingStore.DefaultRecordDirectoryPath(filePath);
try
{
using (var seed = await CultCacheMessagePack.OpenAsync(
filePath,
new CultCacheOpenOptions { UseDirectoryStore = true }))
{
await seed.UpsertAsync(new NamedTestEntry
{
Name = "persisted",
Value = "before-local-write"
});
await seed.FlushAsync();
}
CultRecordHandle<NamedTestEntry> localHandle;
using (var cache = await CultCacheMessagePack.OpenAsync(
filePath,
new CultCacheOpenOptions { UseDirectoryStore = true }))
{
localHandle = await cache.UpsertAsync(new NamedTestEntry
{
Name = "local",
Value = "must-survive-pull"
});
await cache.PullAllBackingStoresAsync();
Assert.That(cache.Get<NamedTestEntry>(localHandle.Key)?.Value,
Is.EqualTo("must-survive-pull"));
Assert.That(cache.IsDirty, Is.True);
await cache.FlushAsync();
}
using var reopened = await CultCacheMessagePack.OpenAsync(
filePath,
new CultCacheOpenOptions { UseDirectoryStore = true });
Assert.That(reopened.Get<NamedTestEntry>(localHandle.Key)?.Value,
Is.EqualTo("must-survive-pull"));
}
finally
{
if (File.Exists(filePath))
File.Delete(filePath);
if (Directory.Exists(recordsPath))
Directory.Delete(recordsPath, recursive: true);
}
}
[Test]
public async Task DirectoryMessagePackBackingStore_Remains_Dirty_When_Deletion_Fails()
{