Slot tests, red: written-count marks, flatten through an unowned base, consent capped, IsWritable is the write's answer
Vectors: extra-slot-store.cc gains the unit-arm record (tests.slot_unit); extra-slot-consent.cc
(records under tests.union_surface.wide) and slot-outcomes.msgpack are new. The consent reader
(SurfaceDeclaring, listing the wide id compatible) and UnitDoc move into UnionParityVectorTests;
the in-test consent store construction, Relabel and AppendRootSlot go. Red against 115bc0a2:
9 tests (spec cultlib-gaps:cut_spec:cut-slot-tolerance.r5).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e692bd90d1
commit
11520293af
5 changed files with 415 additions and 112 deletions
|
|
@ -6,6 +6,7 @@ using System.IO;
|
|||
using System.Linq;
|
||||
using GameCult.Caching.MessagePack;
|
||||
using MessagePack;
|
||||
using MessagePack.Formatters;
|
||||
using NUnit.Framework;
|
||||
using Surface = GameCult.Caching.Tests.UnionParityVectorTests.UnionSurface;
|
||||
|
||||
|
|
@ -18,30 +19,6 @@ namespace GameCult.Caching.Tests
|
|||
{
|
||||
private const string Extra = "extra-slot";
|
||||
|
||||
// Same members as UnionSurface under a new version, listing OldId compatible: the declaration that must not consent to a nested mark.
|
||||
private const string OldId = "tests.slot_readonly.old";
|
||||
|
||||
[CultDocument("tests.union_surface", "tests.union_surface.v2", CompatibleSchemaIds = new[] { OldId })]
|
||||
[MessagePackObject]
|
||||
public sealed class SurfaceDeclaring
|
||||
{
|
||||
[Key(0)] [CultName] public string Id = string.Empty;
|
||||
[Key(1)] public int Version;
|
||||
[Key(2)] public UnionParityVectorTests.Node? Root;
|
||||
[Key(3)] public List<UnionParityVectorTests.Outcome> Outcomes = new();
|
||||
[Key(4)] public UnionParityVectorTests.Item? Part;
|
||||
[Key(5)] public UnionParityVectorTests.CraftedItem? Crafted;
|
||||
[Key(6)] public UnionParityVectorTests.Node? Missing;
|
||||
}
|
||||
|
||||
[CultDocument("tests.slot_unit", "tests.slot_unit.v1")]
|
||||
[MessagePackObject]
|
||||
public sealed class UnitDoc
|
||||
{
|
||||
[Key(0)] [CultName] public string Name = string.Empty;
|
||||
[Key(1)] public UnionParityVectorTests.UnitOutcome? Outcome;
|
||||
}
|
||||
|
||||
// Slot 1 and 2 are retired gaps.
|
||||
[CultDocument("tests.slot_gap", "tests.slot_gap.v1")]
|
||||
[MessagePackObject]
|
||||
|
|
@ -52,8 +29,6 @@ namespace GameCult.Caching.Tests
|
|||
}
|
||||
|
||||
private static readonly CultDocumentRegistry SurfaceRegistry = CultDocumentRegistry.ForTypes(new[] { typeof(Surface) });
|
||||
private static readonly CultDocumentRegistry DeclaringRegistry = CultDocumentRegistry.ForTypes(new[] { typeof(SurfaceDeclaring) });
|
||||
private static readonly CultDocumentRegistry UnitRegistry = CultDocumentRegistry.ForTypes(new[] { typeof(UnitDoc) });
|
||||
private static readonly CultDocumentRegistry GapRegistry = CultDocumentRegistry.ForTypes(new[] { typeof(GapDoc) });
|
||||
private static readonly CultRecordKey Marked = new("marked");
|
||||
private static readonly CultRecordKey Clean = new("clean");
|
||||
|
|
@ -135,22 +110,6 @@ namespace GameCult.Caching.Tests
|
|||
Assert.That(cache.IsWritable(Unheld), Is.True, "a key no store holds is writable");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AUnitArmWithAnExtraSlotHoldsTheRecordReadOnly()
|
||||
{
|
||||
var arm = VectorFiles.Committed("union-parity", "extra-slot-unit-arm.msgpack");
|
||||
var payload = Emit((ref MessagePackWriter w) => { w.WriteArrayHeader(2); w.Write("u"); w.WriteRaw(arm); });
|
||||
var path = Seed(UnitRegistry, typeof(UnitDoc), () => new UnitDoc { Name = "u" }, payload);
|
||||
using var cache = Open(path, UnitRegistry);
|
||||
var unread = Stored(cache, Marked).UnreadSlots;
|
||||
Assert.That(unread, Is.Not.Null);
|
||||
Assert.That(unread!.TypeName, Is.EqualTo(nameof(UnionParityVectorTests.Denied2)));
|
||||
Assert.That(unread.SlotCount, Is.EqualTo(1));
|
||||
Assert.That(unread.DeclaredCount, Is.EqualTo(0));
|
||||
Assert.That(cache.IsWritable(Marked), Is.False);
|
||||
Assert.That(cache.IsWritable(Clean), Is.True);
|
||||
}
|
||||
|
||||
// ---- every staged write and removal of a marked key is refused, nothing else is ----
|
||||
|
||||
private static void AssertSlotRefusal(TestDelegate act, string what, string type, int slots, int declared)
|
||||
|
|
@ -194,69 +153,73 @@ namespace GameCult.Caching.Tests
|
|||
AssertSlotRefusal(() => cache.Commit(batch => batch.Remove(Marked)), "removal", "UnionSurface", 8, 7);
|
||||
}
|
||||
|
||||
// Every row of slot-outcomes.msgpack, from the committed files alone: the mark, Explained and IsWritable of each record of
|
||||
// extra-slot-store.cc and extra-slot-consent.cc, and a removal of the key refused exactly where IsWritable is false.
|
||||
[Test]
|
||||
public void AListedCompatibleIdDoesNotConsentToANestedMark()
|
||||
public void EverySlotOutcomeRowHoldsAndARemovalIsRefusedExactlyWhereTheRecordIsNotWritable()
|
||||
{
|
||||
// The record's stored id is one the reader lists compatible; the extra slot is nested, so the declaration does not reach it.
|
||||
var path = SeedSurface("extra-slot-nested-record.msgpack");
|
||||
Relabel(path, OldId);
|
||||
using var cache = Open(path, DeclaringRegistry);
|
||||
Assert.That(Stored(cache, Marked).StoredSchemaId, Is.EqualTo(OldId));
|
||||
Assert.That(cache.IsWritable(Marked), Is.False);
|
||||
AssertSlotRefusal(() => cache.Commit(batch => batch.Upsert(new SurfaceDeclaring { Id = "new" }, new CultRecordHandle<SurfaceDeclaring>(Marked))), "nested under a listed id", nameof(UnionParityVectorTests.Composition), 3, 2);
|
||||
Assert.That(cache.IsWritable(Clean), Is.True, "the clean record under the listed id is consented to (declared)");
|
||||
Assert.That(cache.Commit(batch => batch.Upsert(new SurfaceDeclaring { Id = "new" }, new CultRecordHandle<SurfaceDeclaring>(Clean))), Is.True);
|
||||
}
|
||||
|
||||
// The record stored under a listed compatible id whose catalog entry has one more member than the reader (slot 7), the payload
|
||||
// carrying that slot at depth 0: the loss a declaration consents to. With a nested mark beside it, nothing is consented to.
|
||||
[TestCase("union-surface.msgpack", true, TestName = "ADepthZeroSlotTheCatalogReportsUnderAListedIdIsConsentedTo")]
|
||||
[TestCase("extra-slot-nested-record.msgpack", false, TestName = "ANestedMarkBesideACatalogReportedSlotIsNotConsentedTo")]
|
||||
public void ACompatibleIdConsentsOnlyToTheDepthZeroSlotTheCatalogReports(string vector, bool consented)
|
||||
{
|
||||
var original = VectorFiles.Committed("union-parity", vector);
|
||||
var path = Seed(SurfaceRegistry, typeof(Surface), () => new Surface { Id = "x" }, AppendRootSlot(original, "wider writer"));
|
||||
var snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(path));
|
||||
var entry = snapshot.SchemaCatalog.Single();
|
||||
var last = entry.Members.OrderBy(member => member.Slot).Last();
|
||||
entry.Members = entry.Members.Append(new CultSchemaMemberCatalogEntry { Slot = 7, MemberName = "Wider", TypeName = last.TypeName }).ToArray();
|
||||
entry.SchemaId = OldId;
|
||||
foreach (var record in snapshot.Records) record.SchemaId = OldId;
|
||||
File.WriteAllBytes(path, CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot));
|
||||
|
||||
using var cache = Open(path, DeclaringRegistry);
|
||||
var unread = Stored(cache, Marked).UnreadSlots;
|
||||
Assert.That(unread, Is.Not.Null);
|
||||
Assert.That(unread!.Explained, Is.EqualTo(consented));
|
||||
Assert.That(cache.IsWritable(Marked), Is.EqualTo(consented));
|
||||
if (consented)
|
||||
Assert.That(cache.Commit(batch => batch.Upsert(new SurfaceDeclaring { Id = "new" }, new CultRecordHandle<SurfaceDeclaring>(Marked))), Is.True);
|
||||
else
|
||||
Assert.Throws<CultSchemaConflictException>(() => cache.Commit(batch => batch.Remove(Marked)));
|
||||
}
|
||||
|
||||
// The payload's root array with one string slot appended.
|
||||
private static byte[] AppendRootSlot(byte[] payload, string value)
|
||||
{
|
||||
var reader = new MessagePackReader(payload);
|
||||
var count = reader.ReadArrayHeader();
|
||||
var start = (int)reader.Consumed;
|
||||
for (var i = 0; i < count; i++) reader.Skip();
|
||||
var end = (int)reader.Consumed;
|
||||
return Emit((ref MessagePackWriter w) =>
|
||||
var reader = new MessagePackReader(VectorFiles.Committed("union-parity", "slot-outcomes.msgpack"));
|
||||
var rows = reader.ReadArrayHeader();
|
||||
Assert.That(rows, Is.EqualTo(7));
|
||||
for (var i = 0; i < rows; i++)
|
||||
{
|
||||
w.WriteArrayHeader(count + 1);
|
||||
w.WriteRaw(payload.AsSpan(start, end - start));
|
||||
w.Write(value);
|
||||
});
|
||||
Assert.That(reader.ReadArrayHeader(), Is.EqualTo(8));
|
||||
var store = reader.ReadString()!;
|
||||
var key = new CultRecordKey(reader.ReadString()!);
|
||||
var writable = reader.ReadBoolean();
|
||||
bool? explained = reader.TryReadNil() ? null : reader.ReadBoolean();
|
||||
var type = reader.TryReadNil() ? null : reader.ReadString();
|
||||
var counts = new int?[3];
|
||||
for (var c = 0; c < 3; c++) counts[c] = reader.TryReadNil() ? null : reader.ReadInt32();
|
||||
var (slots, declared, occurrences) = (counts[0], counts[1], counts[2]);
|
||||
var row = $"{store} {key.Value}";
|
||||
|
||||
var path = Path.Combine(_directory, $"{i}-{store}");
|
||||
File.WriteAllBytes(path, VectorFiles.Committed("union-parity", store));
|
||||
var before = File.ReadAllBytes(path);
|
||||
using var cache = Open(path, UnionParityVectorTests.SlotReader(store));
|
||||
var unread = Stored(cache, key).UnreadSlots;
|
||||
if (explained == null)
|
||||
{
|
||||
Assert.That(unread, Is.Null, row);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(unread, Is.Not.Null, row);
|
||||
Assert.That((unread!.Explained, unread.TypeName, unread.SlotCount, unread.DeclaredCount, unread.Occurrences),
|
||||
Is.EqualTo((explained.Value, type, slots, declared, occurrences)), row);
|
||||
}
|
||||
|
||||
Assert.That(cache.IsWritable(key), Is.EqualTo(writable), row);
|
||||
Assert.That(cache.IsWritable(Unheld), Is.True, "a key no store holds is writable");
|
||||
if (writable)
|
||||
{
|
||||
Assert.That(cache.Commit(batch => batch.Remove(key)), Is.True, row);
|
||||
}
|
||||
else
|
||||
{
|
||||
var refusal = Assert.Throws<CultSchemaConflictException>(() => cache.Commit(batch => batch.Remove(key)), row)!;
|
||||
Assert.That(refusal.UnreadSlots, Is.Not.Null, row);
|
||||
Assert.That(File.ReadAllBytes(path), Is.EqualTo(before), $"{row}: a refused removal leaves the store as it was");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void Relabel(string path, string id)
|
||||
// Consent reaches only the slots the catalog comparison reports: the wide id's catalog reports slot 7, so a payload carrying
|
||||
// slots 7 and 8 at depth 0 is not consented to, and a payload carrying slot 7 alone is.
|
||||
[Test]
|
||||
public void ConsentStopsAtTheSlotsTheCatalogReports()
|
||||
{
|
||||
var snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(path));
|
||||
snapshot.SchemaCatalog.Single().SchemaId = id;
|
||||
foreach (var record in snapshot.Records) record.SchemaId = id;
|
||||
File.WriteAllBytes(path, CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot));
|
||||
var path = Path.Combine(_directory, "extra-slot-consent.cc");
|
||||
File.WriteAllBytes(path, VectorFiles.Committed("union-parity", "extra-slot-consent.cc"));
|
||||
var before = File.ReadAllBytes(path);
|
||||
using var cache = Open(path, UnionParityVectorTests.SlotReader("extra-slot-consent.cc"));
|
||||
var over = new CultRecordHandle<UnionParityVectorTests.SurfaceDeclaring>(new CultRecordKey("over-consent"));
|
||||
AssertWriteAgrees(cache, over.Key, () => cache.Commit(batch => batch.Upsert(new UnionParityVectorTests.SurfaceDeclaring { Id = "over" }, over)));
|
||||
Assert.That(File.ReadAllBytes(path), Is.EqualTo(before), "the refused write leaves the store as it was");
|
||||
var consented = new CultRecordHandle<UnionParityVectorTests.SurfaceDeclaring>(new CultRecordKey("consented"));
|
||||
AssertWriteAgrees(cache, consented.Key, () => cache.Commit(batch => batch.Upsert(new UnionParityVectorTests.SurfaceDeclaring { Id = "consented" }, consented)));
|
||||
Assert.That(cache.IsWritable(consented.Key), Is.True);
|
||||
}
|
||||
|
||||
// ---- carried verbatim ----
|
||||
|
|
@ -349,5 +312,241 @@ namespace GameCult.Caching.Tests
|
|||
Assert.That(reader.IsWritable(Marked), Is.False, "the wide id is undeclared: unowned, though the mark is explained");
|
||||
Assert.Throws<CultSchemaConflictException>(() => reader.Commit(batch => batch.Remove(Marked)));
|
||||
}
|
||||
|
||||
// ---- declared slots are the slots the type's own formatter writes ----
|
||||
|
||||
// MessagePack writes a non-public [Key] member of an AllowPrivate type: 2 slots, where a public-member count says 1.
|
||||
[MessagePackObject(AllowPrivate = true)]
|
||||
public sealed class PrivInner
|
||||
{
|
||||
[Key(0)] public string A = string.Empty;
|
||||
[Key(1)] internal string B = string.Empty;
|
||||
}
|
||||
|
||||
// A [Key] getter that throws on an uninitialized instance; the parameterless constructor's instance is written instead.
|
||||
[MessagePackObject]
|
||||
public sealed class Counted
|
||||
{
|
||||
private List<string> _items = new();
|
||||
[Key(0)] public int Count { get => _items.Count; set => _items = Enumerable.Repeat(string.Empty, value).ToList(); }
|
||||
}
|
||||
|
||||
// The same getter and no parameterless constructor: no instance can be written, so the guard leaves the type alone.
|
||||
[MessagePackObject]
|
||||
public sealed class Uncounted
|
||||
{
|
||||
private readonly List<string> _items;
|
||||
[SerializationConstructor] public Uncounted(int count) => _items = Enumerable.Repeat(string.Empty, count).ToList();
|
||||
[Key(0)] public int Count => _items.Count;
|
||||
}
|
||||
|
||||
// A type naming its own formatter owns its layout: one [Key] slot, but the formatter writes the item count and each item.
|
||||
[MessagePackObject]
|
||||
[MessagePackFormatter(typeof(BagFormatter))]
|
||||
public sealed class Bag
|
||||
{
|
||||
[Key(0)] public List<string> Items = new();
|
||||
}
|
||||
|
||||
public sealed class BagFormatter : IMessagePackFormatter<Bag?>
|
||||
{
|
||||
public void Serialize(ref MessagePackWriter writer, Bag? value, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (value == null) { writer.WriteNil(); return; }
|
||||
writer.WriteArrayHeader(1 + value.Items.Count);
|
||||
writer.Write(value.Items.Count);
|
||||
foreach (var item in value.Items) writer.Write(item);
|
||||
}
|
||||
|
||||
public Bag? Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (reader.TryReadNil()) return null;
|
||||
var count = reader.ReadArrayHeader();
|
||||
reader.ReadInt32();
|
||||
var bag = new Bag();
|
||||
for (var i = 1; i < count; i++) bag.Items.Add(reader.ReadString()!);
|
||||
return bag;
|
||||
}
|
||||
}
|
||||
|
||||
[CultDocument("tests.slot_measured", "tests.slot_measured.v1")]
|
||||
[MessagePackObject]
|
||||
public sealed class MeasuredDoc
|
||||
{
|
||||
[Key(0)] [CultName] public string Name = string.Empty;
|
||||
[Key(1)] public PrivInner? Private;
|
||||
[Key(2)] public Counted? Counted;
|
||||
[Key(3)] public Uncounted? Uncounted;
|
||||
[Key(4)] public Bag? Bag;
|
||||
}
|
||||
|
||||
private static readonly CultDocumentRegistry MeasuredRegistry = CultDocumentRegistry.ForTypes(new[] { typeof(MeasuredDoc) });
|
||||
|
||||
private static MeasuredDoc Measured(string member) => member switch
|
||||
{
|
||||
"private" => new MeasuredDoc { Name = member, Private = new PrivInner { A = "a", B = "bee" } },
|
||||
"constructed" => new MeasuredDoc { Name = member, Counted = new Counted { Count = 2 } },
|
||||
"unmeasurable" => new MeasuredDoc { Name = member, Uncounted = new Uncounted(2) },
|
||||
"own-formatter" => new MeasuredDoc { Name = member, Bag = new Bag { Items = { "x", "y", "z" } } },
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(member), member, null)
|
||||
};
|
||||
|
||||
[TestCase("private", TestName = "APrivateKeyedMemberDoesNotMark")]
|
||||
[TestCase("constructed", TestName = "ATypeMeasuredThroughItsConstructorDoesNotMark")]
|
||||
[TestCase("unmeasurable", TestName = "AnUnmeasurableTypeIsNotGuarded")]
|
||||
[TestCase("own-formatter", TestName = "ATypeWithItsOwnFormatterIsNotMeasuredByKeys")]
|
||||
public void ARecordWrittenByItsOwnTypeDoesNotMark(string member)
|
||||
{
|
||||
var path = Path.Combine(_directory, "measured.cc");
|
||||
using (var cache = Open(path, MeasuredRegistry))
|
||||
{
|
||||
Assert.That(cache.Commit(batch => batch.Upsert(Measured(member), new CultRecordHandle<MeasuredDoc>(Marked))), Is.True);
|
||||
}
|
||||
|
||||
using (var cache = Open(path, MeasuredRegistry))
|
||||
{
|
||||
Assert.That(Stored(cache, Marked).UnreadSlots, Is.Null, member);
|
||||
Assert.That(cache.IsWritable(Marked), Is.True, member);
|
||||
Assert.That(cache.Commit(batch => batch.Upsert(Measured(member), new CultRecordHandle<MeasuredDoc>(Marked))), Is.True, member);
|
||||
Assert.That(cache.Commit(batch => batch.Remove(Marked)), Is.True, member);
|
||||
}
|
||||
}
|
||||
|
||||
// One slot past what the formatter writes still marks, with the written count as declared.
|
||||
[Test]
|
||||
public void AnExtraSlotPastTheWrittenCountStillMarks()
|
||||
{
|
||||
var privatePayload = Emit((ref MessagePackWriter w) =>
|
||||
{
|
||||
w.WriteArrayHeader(5); w.Write("p");
|
||||
w.WriteArrayHeader(3); w.Write("a"); w.Write("bee"); w.Write(Extra);
|
||||
w.WriteNil(); w.WriteNil(); w.WriteNil();
|
||||
});
|
||||
var path = Seed(MeasuredRegistry, typeof(MeasuredDoc), () => new MeasuredDoc { Name = "c" }, privatePayload, "private.cc");
|
||||
using (var cache = Open(path, MeasuredRegistry))
|
||||
{
|
||||
AssertSlotRefusal(() => cache.Commit(batch => batch.Remove(Marked)), "AllowPrivate type", nameof(PrivInner), 3, 2);
|
||||
Assert.That(cache.IsWritable(Clean), Is.True);
|
||||
}
|
||||
|
||||
var countedPayload = Emit((ref MessagePackWriter w) =>
|
||||
{
|
||||
w.WriteArrayHeader(5); w.Write("c"); w.WriteNil();
|
||||
w.WriteArrayHeader(2); w.Write(0); w.Write(Extra);
|
||||
w.WriteNil(); w.WriteNil();
|
||||
});
|
||||
path = Seed(MeasuredRegistry, typeof(MeasuredDoc), () => new MeasuredDoc { Name = "c" }, countedPayload, "counted.cc");
|
||||
using (var cache = Open(path, MeasuredRegistry))
|
||||
AssertSlotRefusal(() => cache.Commit(batch => batch.Remove(Marked)), "measured through its constructor", nameof(Counted), 2, 1);
|
||||
}
|
||||
|
||||
// ---- a flatten writes its base chain's decoded view: refused when a record on the chain is not owned ----
|
||||
|
||||
private static readonly CultRecordKey Base = new("base");
|
||||
private static readonly CultRecordKey Var = new("var");
|
||||
private static readonly CultRecordKey Fresh = new("fresh");
|
||||
|
||||
// A store holding plain record base and its variant var, base then given the payload.
|
||||
private string SeedVariant(byte[]? basePayload, string name)
|
||||
{
|
||||
var path = Path.Combine(_directory, name);
|
||||
using (var cache = Open(path, SurfaceRegistry))
|
||||
{
|
||||
Assert.That(cache.Commit(batch => batch.Upsert(new Surface { Id = "base" }, new CultRecordHandle<Surface>(Base))), Is.True);
|
||||
Assert.That(cache.Commit(batch => batch.UpsertVariant(Var, Base, Named(cache, Var))), Is.True);
|
||||
}
|
||||
|
||||
if (basePayload == null) return path;
|
||||
var snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(path));
|
||||
snapshot.Records.Single(record => record.Key == Base.Value).Payload = basePayload;
|
||||
File.WriteAllBytes(path, CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot));
|
||||
return path;
|
||||
}
|
||||
|
||||
// A variant overrides its name.
|
||||
private static CultVariantOverride[] Named(CultCache cache, CultRecordKey key) => new[] { cache.Override<Surface>(nameof(Surface.Id), key.Value) };
|
||||
|
||||
private static void AssertBaseRefusal(TestDelegate act, string what, bool unreadSlots)
|
||||
{
|
||||
var refusal = Assert.Throws<CultSchemaConflictException>(act, what)!;
|
||||
Assert.That(refusal.RecordKey, Is.EqualTo(Base.Value), $"{what}: the refusal is the base's own");
|
||||
Assert.That(refusal.UnreadSlots, unreadSlots ? Is.Not.Null : Is.Null, what);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AFlattenThroughABaseHeldForUnreadSlotsIsRefusedAndTheVariantStillWritesItsDelta()
|
||||
{
|
||||
var nested = VectorFiles.Committed("union-parity", "extra-slot-nested-record.msgpack");
|
||||
var path = SeedVariant(nested, "flatten.cc");
|
||||
var before = File.ReadAllBytes(path);
|
||||
using (var cache = Open(path, SurfaceRegistry))
|
||||
{
|
||||
Assert.That(cache.IsWritable(Base), Is.False);
|
||||
Assert.That(cache.IsWritable(Var), Is.True, "the variant persists only its delta");
|
||||
AssertBaseRefusal(() => cache.Commit(batch => batch.Flatten(Var)), "Commit(Flatten)", unreadSlots: true);
|
||||
AssertBaseRefusal(() => cache.FlattenAsync(Var).GetAwaiter().GetResult(), "FlattenAsync", unreadSlots: true);
|
||||
AssertBaseRefusal(() => cache.Commit(batch => { batch.UpsertVariant(Fresh, Base, Named(cache, Fresh)); batch.Flatten(Fresh); }), "a new variant flattened as it lands", unreadSlots: true);
|
||||
Assert.That(File.ReadAllBytes(path), Is.EqualTo(before), "refused flattens leave the store as it was");
|
||||
Assert.That(cache.Commit(batch => batch.UpsertVariant(Var, Base, Named(cache, Var))), Is.True, "a variant write persists only its delta");
|
||||
Assert.That(cache.Commit(batch => batch.Remove(Var)), Is.True);
|
||||
}
|
||||
|
||||
var after = CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(path));
|
||||
Assert.That(after.Records.Single(record => record.Key == Base.Value).Payload, Is.EqualTo(nested), "the base's bytes stay");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AFlattenThroughABaseUnderAnUndeclaredDroppingIdIsRefused()
|
||||
{
|
||||
var path = Path.Combine(_directory, "drift.cc");
|
||||
using (var writer = Open(path, CultDocumentRegistry.ForTypes(new[] { typeof(WideDoc) })))
|
||||
Assert.That(writer.Commit(batch => batch.Upsert(new WideDoc { Name = "base", Extra = "kept?" }, new CultRecordHandle<WideDoc>(Base))), Is.True);
|
||||
|
||||
using var reader = Open(path, CultDocumentRegistry.ForTypes(new[] { typeof(NarrowDoc) }));
|
||||
Assert.That(reader.IsWritable(Base), Is.False);
|
||||
Assert.That(reader.Commit(batch => batch.UpsertVariant(Var, Base, new[] { reader.Override<NarrowDoc>(nameof(NarrowDoc.Name), Var.Value) })), Is.True, "a variant over the unowned base persists only its delta");
|
||||
var before = File.ReadAllBytes(path);
|
||||
AssertBaseRefusal(() => reader.Commit(batch => batch.Flatten(Var)), "flatten over undeclared drift", unreadSlots: false);
|
||||
Assert.That(File.ReadAllBytes(path), Is.EqualTo(before));
|
||||
Assert.That(reader.Commit(batch => batch.Remove(Var)), Is.True);
|
||||
}
|
||||
|
||||
// ---- IsWritable is the write's own answer ----
|
||||
|
||||
// False exactly where the write throws.
|
||||
private static void AssertWriteAgrees(CultCache cache, CultRecordKey key, Action write)
|
||||
{
|
||||
var writable = cache.IsWritable(key);
|
||||
Exception? thrown = null;
|
||||
try { write(); }
|
||||
catch (InvalidOperationException exception) { thrown = exception; }
|
||||
Assert.That(thrown == null, Is.EqualTo(writable), $"IsWritable({key.Value}) is {writable}; the write {(thrown == null ? "landed" : "threw: " + thrown.Message)}");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsWritableIsFalseInAReadOnlyStoreAndAFlattenThereIsNotRefusedForItsCleanBase()
|
||||
{
|
||||
var path = SeedVariant(null, "readonly.cc");
|
||||
using var cache = CultCacheMessagePack.Create(path, new CultCacheOpenOptions { Registry = SurfaceRegistry, ReadOnly = true });
|
||||
Assert.That(cache.IsWritable(Base), Is.False);
|
||||
AssertWriteAgrees(cache, Base, () => cache.Commit(batch => batch.Upsert(new Surface { Id = "new" }, new CultRecordHandle<Surface>(Base))));
|
||||
AssertWriteAgrees(cache, Var, () => cache.Commit(batch => batch.Remove(Var)));
|
||||
var refusal = Assert.Throws<InvalidOperationException>(() => cache.Commit(batch => batch.Flatten(Var)))!;
|
||||
Assert.That(refusal, Is.Not.InstanceOf<CultSchemaConflictException>(), "nothing on the chain is lost: the refusal is the store's read-only one");
|
||||
Assert.That(refusal.Message, Does.Contain("read-only backing store"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsWritableIsFalseForARecordNoAttachedStoreIsHomeTo()
|
||||
{
|
||||
using var cache = new CultCache(CultDocumentRegistry.ForTypes(new[] { typeof(Surface), typeof(GapDoc) }));
|
||||
Assert.That(cache.Commit(batch => batch.Upsert(new Surface { Id = "memory" }, new CultRecordHandle<Surface>(Clean))), Is.True);
|
||||
Assert.That(cache.IsWritable(Clean), Is.True, "a store-less cache writes its own records");
|
||||
cache.AddBackingStore(new SingleFileMessagePackBackingStore(Path.Combine(_directory, "gaps.cc")), typeof(GapDoc));
|
||||
Assert.That(cache.IsWritable(Clean), Is.False);
|
||||
AssertWriteAgrees(cache, Clean, () => cache.Commit(batch => batch.Upsert(new Surface { Id = "x" }, new CultRecordHandle<Surface>(Clean))));
|
||||
AssertWriteAgrees(cache, Clean, () => cache.Commit(batch => batch.Remove(Clean)));
|
||||
Assert.That(cache.IsWritable(Unheld), Is.True, "an unheld key no store lists foreign is writable");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -252,7 +252,7 @@ namespace GameCult.Caching.Tests
|
|||
private static Emit ImageArm(string id, string uri, string alt) => ArmOf(5, Str(id), Str(uri), Nil, Str(alt));
|
||||
|
||||
// The constructed value's payload, with a part of the structure replaceable to build a refusal or an extra-slot case.
|
||||
private static byte[] Surface(Emit? root = null, Emit? outcomes = null, Emit? crafted = null, Extra? extra = null, Emit? trailing = null)
|
||||
private static byte[] Surface(Emit? root = null, Emit? outcomes = null, Emit? crafted = null, Extra? extra = null, params Emit[] trailing)
|
||||
{
|
||||
extra ??= new Extra();
|
||||
root ??= BoxArm("0000000000c0", Slots(
|
||||
|
|
@ -264,7 +264,7 @@ namespace GameCult.Caching.Tests
|
|||
outcomes ??= Slots(ArmOf(0, Str("wait"), Str("0000000000a1")), ArmOf(1, Num(7), Str("0000000000a2")), ArmOf(2, Str("0000000000a3")));
|
||||
crafted ??= ArmOf(1, Str("gear"), Num(4));
|
||||
var slots = new[] { Str("surface"), Num(3), root, outcomes, ArmOf(1, Str("hammer"), Num(12)), crafted, Nil };
|
||||
return Raw(Slots(trailing != null ? slots.Append(trailing).ToArray() : extra.Record ? slots.Append(ExtraSlot).ToArray() : slots));
|
||||
return Raw(Slots(trailing.Length > 0 ? slots.Concat(trailing).ToArray() : extra.Record ? slots.Append(ExtraSlot).ToArray() : slots));
|
||||
}
|
||||
|
||||
private static byte[] SerializeValue(UnionSurface value) => CultDocumentMessagePackSerialization.SerializeUntyped(value, typeof(UnionSurface), Registry);
|
||||
|
|
@ -342,6 +342,70 @@ namespace GameCult.Caching.Tests
|
|||
[MessagePackObject] public sealed class Floor2 : UnitOutcome { }
|
||||
[MessagePackObject] public sealed class Ceiling2 : UnitOutcome { }
|
||||
|
||||
// The document a unit arm is placed in for a store: [Name, Outcome].
|
||||
[CultDocument("tests.slot_unit", "tests.slot_unit.v1")]
|
||||
[MessagePackObject]
|
||||
public sealed class UnitDoc
|
||||
{
|
||||
[Key(0)] [CultName] public string Name = string.Empty;
|
||||
[Key(1)] public UnitOutcome? Outcome;
|
||||
}
|
||||
|
||||
// ---- the slot stores: extra-slot-store.cc, extra-slot-consent.cc and their outcomes as data ----
|
||||
|
||||
// The id extra-slot-consent.cc's records are stored under: UnionSurface's catalog entry with one more member, slot 7.
|
||||
internal const string WideId = "tests.union_surface.wide";
|
||||
|
||||
// UnionSurface's members under a new version, listing the wide id compatible: the consent vector's reader.
|
||||
[CultDocument("tests.union_surface", "tests.union_surface.v2", CompatibleSchemaIds = new[] { WideId })]
|
||||
[MessagePackObject]
|
||||
public sealed class SurfaceDeclaring
|
||||
{
|
||||
[Key(0)] [CultName] public string Id = string.Empty;
|
||||
[Key(1)] public int Version;
|
||||
[Key(2)] public Node? Root;
|
||||
[Key(3)] public List<Outcome> Outcomes = new();
|
||||
[Key(4)] public Item? Part;
|
||||
[Key(5)] public CraftedItem? Crafted;
|
||||
[Key(6)] public Node? Missing;
|
||||
}
|
||||
|
||||
// The registry each slot store is read with.
|
||||
internal static CultDocumentRegistry SlotReader(string store) => store switch
|
||||
{
|
||||
"extra-slot-store.cc" => CultDocumentRegistry.ForTypes(new[] { typeof(UnionSurface), typeof(UnitDoc) }),
|
||||
"extra-slot-consent.cc" => CultDocumentRegistry.ForTypes(new[] { typeof(SurfaceDeclaring) }),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(store), store, null)
|
||||
};
|
||||
|
||||
// One row of slot-outcomes.msgpack: what the store's reader holds at the key. The mark fields are null for an unmarked record.
|
||||
private sealed record SlotOutcome(string Store, string Key, bool Writable, bool? Explained, string? Type, int? Slots, int? Declared, int? Occurrences);
|
||||
|
||||
private static readonly SlotOutcome[] SlotOutcomes =
|
||||
{
|
||||
new("extra-slot-store.cc", "marked", false, false, nameof(Composition), 3, 2, 1),
|
||||
new("extra-slot-store.cc", "clean", true, null, null, null, null, null),
|
||||
new("extra-slot-store.cc", "unit-arm", false, false, nameof(Denied2), 1, 0, 1),
|
||||
new("extra-slot-consent.cc", "consented", true, true, nameof(SurfaceDeclaring), 8, 7, 1),
|
||||
new("extra-slot-consent.cc", "over-consent", false, false, nameof(SurfaceDeclaring), 9, 7, 1),
|
||||
new("extra-slot-consent.cc", "nested", false, false, nameof(SurfaceDeclaring), 8, 7, 2),
|
||||
new("extra-slot-consent.cc", "clean", true, null, null, null, null, null),
|
||||
};
|
||||
|
||||
private static byte[] SlotOutcomesFile() => Raw((ref MessagePackWriter w) =>
|
||||
{
|
||||
w.WriteArrayHeader(SlotOutcomes.Length);
|
||||
foreach (var row in SlotOutcomes)
|
||||
{
|
||||
w.WriteArrayHeader(8);
|
||||
w.Write(row.Store); w.Write(row.Key); w.Write(row.Writable);
|
||||
if (row.Explained is { } explained) w.Write(explained); else w.WriteNil();
|
||||
if (row.Type is { } type) w.Write(type); else w.WriteNil();
|
||||
foreach (var count in new[] { row.Slots, row.Declared, row.Occurrences })
|
||||
if (count is { } value) w.Write(value); else w.WriteNil();
|
||||
}
|
||||
});
|
||||
|
||||
private static MessagePackSerializerOptions UnitOptions => CultDocumentMessagePackSerialization.OptionsFor(typeof(UnionSurface).Assembly);
|
||||
|
||||
// The reference writes a member-less arm as [key, []]; the other runtimes read these files and write the same bytes.
|
||||
|
|
@ -417,27 +481,28 @@ namespace GameCult.Caching.Tests
|
|||
}
|
||||
}
|
||||
|
||||
// The store the slot-readonly tests open and the other runtimes read: the extra-slot-nested-record payload as the record "marked"
|
||||
// beside the ordinary value as "clean". The store's clock is pinned as union-surface.cc's.
|
||||
[Test]
|
||||
public void ExtraSlotStoreVectorHoldsAMarkedAndACleanRecord()
|
||||
// A single-file store of the records, each written as its type's value with the store's clock pinned as union-surface.cc's, then
|
||||
// its payload replaced by bytes no current writer produces; edit changes the snapshot further before it is serialized.
|
||||
private static byte[] SlotStore(CultDocumentRegistry registry, (string Key, object Value, byte[] Payload)[] records, Action<CultPersistedStoreSnapshot>? edit = null)
|
||||
{
|
||||
var work = Path.Combine(Path.GetTempPath(), $"cultlib-unionparity-{Guid.NewGuid():N}");
|
||||
Directory.CreateDirectory(work);
|
||||
try
|
||||
{
|
||||
var scratch = Path.Combine(work, "extra-slot-store.cc");
|
||||
using (var cache = CultCacheMessagePack.Create(scratch, new CultCacheOpenOptions { Registry = Registry, StoreFlushOnDispose = true }))
|
||||
var scratch = Path.Combine(work, "slots.cc");
|
||||
using (var cache = CultCacheMessagePack.Create(scratch, new CultCacheOpenOptions { Registry = registry, StoreFlushOnDispose = true }))
|
||||
{
|
||||
cache.Clock = () => StoredAt;
|
||||
cache.UpsertAsync(typeof(UnionSurface), Constructed(), new CultRecordKey("marked")).GetAwaiter().GetResult();
|
||||
cache.UpsertAsync(typeof(UnionSurface), Constructed(), new CultRecordKey("clean")).GetAwaiter().GetResult();
|
||||
foreach (var (key, value, _) in records)
|
||||
cache.UpsertAsync(value.GetType(), value, new CultRecordKey(key)).GetAwaiter().GetResult();
|
||||
cache.FlushAsync().GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
var snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(scratch));
|
||||
snapshot.Records.Single(record => record.Key == "marked").Payload = Committed("extra-slot-nested-record.msgpack");
|
||||
Check("extra-slot-store.cc", CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot));
|
||||
foreach (var (key, _, payload) in records)
|
||||
snapshot.Records.Single(record => record.Key == key).Payload = payload;
|
||||
edit?.Invoke(snapshot);
|
||||
return CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
@ -445,6 +510,44 @@ namespace GameCult.Caching.Tests
|
|||
}
|
||||
}
|
||||
|
||||
// The store the slot-readonly tests open and the other runtimes read: "marked" holds the extra-slot-nested-record payload,
|
||||
// "clean" the ordinary value, and "unit-arm" a tests.slot_unit document ["unit-arm", [2, ["extra-slot"]]].
|
||||
[Test]
|
||||
public void ExtraSlotStoreVectorHoldsAMarkedACleanAndAUnitArmRecord()
|
||||
{
|
||||
Check("extra-slot-store.cc", SlotStore(SlotReader("extra-slot-store.cc"), new (string, object, byte[])[]
|
||||
{
|
||||
("marked", Constructed(), Committed("extra-slot-nested-record.msgpack")),
|
||||
("clean", Constructed(), Surface()),
|
||||
("unit-arm", new UnitDoc { Name = "unit-arm", Outcome = new Denied2() }, Raw(Slots(Str("unit-arm"), ArmOf(2, ExtraSlot)))),
|
||||
}));
|
||||
}
|
||||
|
||||
// The consent store: every record under the wide id, whose catalog entry is UnionSurface's 7 members plus slot 7. Its reader
|
||||
// lists the wide id compatible, so the comparison reports slot 7 ignored; only a depth-0 mark within that slot is consented to.
|
||||
[Test]
|
||||
public void ExtraSlotConsentVectorHoldsTheConsentCases()
|
||||
{
|
||||
Check("extra-slot-consent.cc", SlotStore(Registry, new (string, object, byte[])[]
|
||||
{
|
||||
("consented", Constructed(), Surface(trailing: ExtraSlot)),
|
||||
("over-consent", Constructed(), Surface(trailing: new[] { ExtraSlot, ExtraSlot })),
|
||||
("nested", Constructed(), Surface(extra: new Extra(Composition: true), trailing: ExtraSlot)),
|
||||
("clean", Constructed(), Surface()),
|
||||
}, snapshot =>
|
||||
{
|
||||
var entry = snapshot.SchemaCatalog.Single();
|
||||
var last = entry.Members.OrderBy(member => member.Slot).Last();
|
||||
entry.Members = entry.Members.Append(new CultSchemaMemberCatalogEntry { Slot = 7, MemberName = "Wider", TypeName = last.TypeName }).ToArray();
|
||||
entry.SchemaId = WideId;
|
||||
foreach (var record in snapshot.Records) record.SchemaId = WideId;
|
||||
}));
|
||||
}
|
||||
|
||||
// The outcomes of both slot stores as data; SlotReadOnlyTests asserts every row from the committed files.
|
||||
[Test]
|
||||
public void SlotOutcomesVectorMatches() => Check("slot-outcomes.msgpack", SlotOutcomesFile());
|
||||
|
||||
// The value names every arm shape the vectors exist to hold other runtimes to.
|
||||
[Test]
|
||||
public void ConstructedValueExercisesEveryArmShape()
|
||||
|
|
@ -816,7 +919,8 @@ namespace GameCult.Caching.Tests
|
|||
Assert.That(File.ReadAllBytes(path), Is.EqualTo(new byte[] { 0x91 }), $"switch {off ?? "unset"} rewrites nothing");
|
||||
File.Delete(path);
|
||||
|
||||
foreach (var body in new Action[] { UnionSurfaceVectorMatches, UnionRefusalVectorsRefused, BoundsOutcomesFromCommittedFiles })
|
||||
foreach (var body in new Action[] { UnionSurfaceVectorMatches, UnionRefusalVectorsRefused, BoundsOutcomesFromCommittedFiles,
|
||||
ExtraSlotStoreVectorHoldsAMarkedACleanAndAUnitArmRecord, ExtraSlotConsentVectorHoldsTheConsentCases, SlotOutcomesVectorMatches })
|
||||
body();
|
||||
}
|
||||
|
||||
|
|
|
|||
BIN
tests/vectors/union-parity/extra-slot-consent.cc
Normal file
BIN
tests/vectors/union-parity/extra-slot-consent.cc
Normal file
Binary file not shown.
Binary file not shown.
BIN
tests/vectors/union-parity/slot-outcomes.msgpack
Normal file
BIN
tests/vectors/union-parity/slot-outcomes.msgpack
Normal file
Binary file not shown.
Loading…
Reference in a new issue