C# write set tests: a readable vector holding a variant refuses a write the cache never read, and Expect on a variant is retried after the reload
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
45d6866d31
commit
2649dd1f6b
3 changed files with 22 additions and 1 deletions
|
|
@ -61,6 +61,9 @@ namespace GameCult.Caching.Tests
|
|||
.Select(line => line.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries))
|
||||
.Select(cells => new TestCaseData(cells[0], cells[CSharp] == "reads").SetName($"{cells[0]} {cells[CSharp]}"));
|
||||
|
||||
private static bool HoldsAVariant(byte[] bytes) =>
|
||||
CultDocumentMessagePackSerialization.DeserializeSnapshot(bytes).Records.Any(record => record.Variant != null);
|
||||
|
||||
private static string Header(string path) => CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(path)).FormatVersion;
|
||||
|
||||
private string Seed(string name)
|
||||
|
|
@ -94,6 +97,14 @@ namespace GameCult.Caching.Tests
|
|||
File.WriteAllBytes(path, bytes);
|
||||
var store = cache.BackingStores[0];
|
||||
|
||||
if (reads && HoldsAVariant(bytes))
|
||||
{
|
||||
// The cache has read nothing of this file, and a variant resolves against records it has not read.
|
||||
Assert.That(() => store.PushAll(), Throws.TypeOf<CultWriteConflictException>());
|
||||
Assert.That(File.ReadAllBytes(path), Is.EqualTo(bytes), "a write into a store holding a variant this cache never read changed it");
|
||||
return;
|
||||
}
|
||||
|
||||
if (reads)
|
||||
{
|
||||
store.PushAll();
|
||||
|
|
@ -115,6 +126,13 @@ namespace GameCult.Caching.Tests
|
|||
var bytes = File.ReadAllBytes(Path.Combine(VectorRoot(), vector));
|
||||
File.WriteAllBytes(path, bytes);
|
||||
|
||||
if (reads && HoldsAVariant(bytes))
|
||||
{
|
||||
Assert.That(() => cache.Commit(batch => batch.Upsert(typeof(IdDeck), new IdDeck { Name = "e" }, new CultRecordKey("e"))), Throws.TypeOf<CultWriteConflictException>());
|
||||
Assert.That(File.ReadAllBytes(path), Is.EqualTo(bytes), "a write into a store holding a variant this cache never read changed it");
|
||||
return;
|
||||
}
|
||||
|
||||
if (reads)
|
||||
{
|
||||
cache.Commit(batch => batch.Upsert(typeof(IdDeck), new IdDeck { Name = "e" }, new CultRecordKey("e")));
|
||||
|
|
|
|||
|
|
@ -181,6 +181,9 @@ namespace GameCult.Caching.Tests
|
|||
var seen = a.Get<VariantGear>(BigKey)!;
|
||||
|
||||
EditBase(b, gear => gear.Tags.Add("pierce"));
|
||||
// The store holds a variant and moved, so the first write is refused and reloaded; the variant's own record did not move.
|
||||
Assert.Throws<CultWriteConflictException>(() => a.Commit(batch => batch.Upsert(typeof(VariantOther), new VariantOther { Name = "marker" }, new CultRecordKey("marker"))));
|
||||
seen = a.Get<VariantGear>(BigKey)!;
|
||||
Assert.That(a.Commit(batch =>
|
||||
{
|
||||
batch.Expect(BigKey, seen);
|
||||
|
|
|
|||
|
|
@ -443,7 +443,7 @@ namespace GameCult.Caching.Tests
|
|||
var path = PathOf("undecodable-variant.cc");
|
||||
Seed(path, false, "a");
|
||||
using (var seed = Open(path, false))
|
||||
seed.Commit(batch => batch.UpsertVariant(new CultRecordKey("v"), A, new[] { seed.Override<WsItem>(nameof(WsItem.Note), "variant") }));
|
||||
seed.Commit(batch => batch.UpsertVariant(new CultRecordKey("v"), A, new[] { seed.Override<WsItem>(nameof(WsItem.Name), "name-v"), seed.Override<WsItem>(nameof(WsItem.Note), "variant") }));
|
||||
var a = Open(path, false);
|
||||
AddUndecodableRecord(path, "r");
|
||||
var before = File.ReadAllBytes(path);
|
||||
|
|
|
|||
Loading…
Reference in a new issue