C# write set tests: fix the five tests the first run named, and the Mesh test's catalog comparison

The unconditional-commit last-writer-wins test now keeps another writer's record; the load-minted ids
stay for MintElementIds after an unconditional write; a detached store marks the variant it writes, not
the one it copies; a variant write supplies the indexed override; the conditional/unconditional
comparison uses one batch (a conditional commit refuses staged writes by design).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-03 03:20:17 +02:00
commit 88934bcb76
5 changed files with 56 additions and 19 deletions

View file

@ -604,7 +604,7 @@ namespace GameCult.Caching.Tests
}
[Test]
public void UnconditionalCommitIsLastWriterWinsLikeFlush()
public void UnconditionalCommitIsLastWriterWinsPerKeyLikeFlush()
{
var committed = LastWriter(SingleFile(PathOf("commit.cc")), (cache, key) =>
Assert.That(cache.Commit(batch => batch.Upsert(new Counter { Name = key.Value }, new CultRecordHandle<Counter>(key))), Is.True));
@ -614,10 +614,10 @@ namespace GameCult.Caching.Tests
cache.FlushAllBackingStores();
});
Assert.That(committed, Is.EqualTo(new[] { "a", "x" }), "an unconditional commit kept a record its cache never saw");
Assert.That(committed, Is.EqualTo(new[] { "a", "c", "x" }), "an unconditional commit dropped a record another writer added");
Assert.That(committed, Is.EqualTo(flushed));
// A directory store's flush lands staged keys onto the current manifest, and its commit does exactly the same.
// A directory store's flush lands staged keys onto the current manifest, and its commit does exactly the same: so does a single file's.
var directoryCommitted = LastWriter(DirectoryStore(PathOf("commit-dir.cc")), (cache, key) =>
Assert.That(cache.Commit(batch => batch.Upsert(new Counter { Name = key.Value }, new CultRecordHandle<Counter>(key))), Is.True));
var directoryFlushed = LastWriter(DirectoryStore(PathOf("flush-dir.cc")), (cache, key) =>

View file

@ -515,14 +515,15 @@ namespace GameCult.Caching.Tests
}
[Test]
public void AnUnconditionalWritePersistsLoadMintedIdsAndTakesThemOffTheList()
public void AnUnconditionalWriteLeavesTheLoadMintedIdsOfTheRecordsItDidNotStageOnTheList()
{
var path = PathOf("unconditional.cc");
WritePreIdStore(path, null, ("a", "a", 1), ("b", "b", 1));
using var cache = Open(path);
cache.Commit(batch => batch.Upsert(typeof(IdDeck), Deck("other"), new CultRecordKey("other")));
Assert.That(AllIds(MessagePackSerializer.Deserialize<IdDeck>(DiskRecord(path, "b").Payload)), Has.All.Not.Empty, "the write persisted b's ids");
Assert.That(cache.MintElementIds(), Is.EqualTo(0), "nothing is left that exists only in memory");
Assert.That(AllIds(MessagePackSerializer.Deserialize<IdDeck>(DiskRecord(path, "b").Payload)), Has.All.Empty, "the write did not touch b");
Assert.That(cache.MintElementIds(), Is.EqualTo(2), "a and b exist with ids only in memory until MintElementIds writes them");
Assert.That(AllIds(MessagePackSerializer.Deserialize<IdDeck>(DiskRecord(path, "b").Payload)), Has.All.Not.Empty);
}
[Test]
@ -772,10 +773,11 @@ namespace GameCult.Caching.Tests
Assert.That(HeaderOf(path), Is.EqualTo("cultcache.store.v1"), "the last id taken away after staging unmarks it");
}
// A store with no cache has no codec to read a variant's override values with, so it cannot see whether they hold an id: the
// variant counts as holding one. The same record read through a cache is marked by what its values hold.
// A store with no cache has no codec to read a variant's override values with, so it cannot see whether they hold an id: a
// variant it writes counts as holding one. The same record read through a cache is marked by what its values hold. A
// flush that stages nothing copies the variant as stored.
[Test]
public void AVariantAStoreCannotReadCountsAsHoldingIds()
public void AVariantAStoreCannotReadCountsAsHoldingIdsWhenItWritesIt()
{
var path = PathOf("detached-variant.cc");
using (var cache = Open(path))
@ -795,7 +797,17 @@ namespace GameCult.Caching.Tests
blind.AttachRegistry(Registry);
blind.PullAll();
blind.PushAll();
Assert.That(HeaderOf(path), Is.EqualTo("cultcache.store.v3"), "read without one, it cannot be seen, so it is marked");
Assert.That(HeaderOf(path), Is.EqualTo("cultcache.store.v2"), "a flush that staged nothing copies the variant and its header");
blind.Push(new CultStoredDocument(
new CultRecordKey("v"),
DateTimeOffset.UtcNow.ToString("O"),
Registry.GetRequired(typeof(IdDeck)),
new CultVariantDelta("base", Array.Empty<CultVariantOverride>()),
null,
null));
blind.PushAll();
Assert.That(HeaderOf(path), Is.EqualTo("cultcache.store.v3"), "read without a codec, the variant it writes cannot be seen, so it is marked");
}
[Test]

View file

@ -783,11 +783,14 @@ namespace GameCult.Caching.Tests
{
Assert.That(reopened.BackingStores[0].LastSchemaMigrationReports.SelectMany(report => report.IgnoredExtraSlots), Is.EqualTo(new[] { 99 }));
Assert.That(reopened.Get<VariantGear>(new CultRecordKey("hand-variant"))!.Name, Is.EqualTo("hand variant"));
reopened.Commit(batch => batch.UpsertVariant(new CultRecordKey("hand-variant"), new CultRecordKey("hand-base"),
new[] { reopened.Override<VariantGear>(nameof(VariantGear.Name), "hand variant") }));
reopened.Commit(batch => batch.UpsertVariant(new CultRecordKey("hand-variant"), new CultRecordKey("hand-base"), new[]
{
reopened.Override<VariantGear>(nameof(VariantGear.Name), "hand variant"),
reopened.Override<VariantGear>(nameof(VariantGear.Code), "hand-code")
}));
}
Assert.That(OverriddenSlotsOn(path, "hand-variant"), Is.EqualTo(new[] { 0 }), "a write of the variant stores only what it was given");
Assert.That(OverriddenSlotsOn(path, "hand-variant").OrderBy(slot => slot), Is.EqualTo(new[] { 0, 2 }), "a write of the variant stores only what it was given");
}
private static int[] OverriddenSlotsOn(string path, string key) =>

View file

@ -225,9 +225,9 @@ namespace GameCult.Caching.Tests
return -1;
}
// The condition is the only difference between a conditional and an unconditional commit: one staged set, one apply.
// The condition is the only difference between a conditional and an unconditional commit: one batch, one apply.
[Test]
public void AConditionalAndAnUnconditionalCommitOfOneStagedSetOntoOneStoreWriteTheSameBytes()
public void AConditionalAndAnUnconditionalCommitOfOneBatchOntoOneStoreWriteTheSameBytes()
{
var first = PathOf("unconditional.cc");
var second = PathOf("conditional.cc");
@ -237,7 +237,6 @@ namespace GameCult.Caching.Tests
foreach (var (path, conditional) in new[] { (first, false), (second, true) })
{
using var cache = Open(path, false);
cache.UpsertAsync(typeof(WsItem), new WsItem { Name = "name-s", Note = "staged" }, new CultRecordKey("s")).GetAwaiter().GetResult();
var current = cache.Get<WsItem>(A)!;
Assert.That(cache.Commit(batch =>
{
@ -251,15 +250,32 @@ namespace GameCult.Caching.Tests
byte[] Normalized(string path)
{
var snapshot = Read(path);
foreach (var record in snapshot.Records.Where(record => record.Key is "s" or "z"))
foreach (var record in snapshot.Records.Where(record => record.Key == "z"))
record.StoredAt = string.Empty;
return CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot);
}
Assert.That(Keys(first), Is.EqualTo(new[] { "a", "s", "z" }));
Assert.That(Keys(first), Is.EqualTo(new[] { "a", "z" }));
Assert.That(Normalized(second), Is.EqualTo(Normalized(first)));
}
// An unconditional commit also lands what was staged before it, on the store as it is now.
[Test]
public void AnUnconditionalCommitLandsEarlierStagedWritesWithItsBatchOntoWhatAnotherWriterWrote([Values] bool directory)
{
var path = PathOf("staged.cc");
Seed(path, directory, "a");
using var cache = Open(path, directory);
using (var other = Open(path, directory))
other.Commit(batch => batch.Upsert(typeof(WsItem), new WsItem { Name = "name-x" }, X));
cache.UpsertAsync(typeof(WsItem), new WsItem { Name = "name-s" }, new CultRecordKey("s")).GetAwaiter().GetResult();
Assert.That(cache.Commit(batch => batch.Upsert(typeof(WsItem), new WsItem { Name = "name-y" }, new CultRecordKey("y"))), Is.True);
Assert.That(Keys(path), Is.EqualTo(new[] { "a", "s", "x", "y" }));
Assert.That(cache.IsDirty, Is.False);
}
private static void MarkAs(string path, string header)
{
var snapshot = Read(path);

View file

@ -110,6 +110,12 @@ public sealed class CultMeshSingleFileStoreTests
}
}
private static byte[] EntryBytes(CultPersistedStoreSnapshot snapshot, string schemaId) =>
CultDocumentMessagePackSerialization.SerializeSnapshot(new CultPersistedStoreSnapshot
{
SchemaCatalog = snapshot.SchemaCatalog.Where(entry => entry.SchemaId == schemaId).ToArray()
});
private static CultPersistedStoreSnapshot Snapshot(string path) =>
CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(path));
@ -141,7 +147,7 @@ public sealed class CultMeshSingleFileStoreTests
kept.Payload.Should().Equal(was.Payload);
}
after.SchemaCatalog.Single(entry => entry.SchemaId == SchemaId()).RawBytes.Should().Equal(before.SchemaCatalog.Single(entry => entry.SchemaId == SchemaId()).RawBytes);
EntryBytes(after, SchemaId()).Should().Equal(EntryBytes(before, SchemaId()));
after.SchemaCatalog.Should().Contain(entry => entry.SchemaId == "raw:schema");
CultMesh.WriteSingleFileDocumentPayload(path, Key, Raw, null, new byte[] { 0x91, 0x01 });