Tests: a write over a durable record under an undeclared id is refused where it lands, in both store kinds and through CultMesh
A record another writer moved to an undeclared id after the load, a staged removal of one, and one that appeared at a written key are refused at the apply and reloaded read-only. A direct store Push, Delete and CommitBatch of a read-only record are refused and never relabel it. Removing a foreign record is refused typed. A CultMesh write over a record its entry does not declare is refused, and one whose entry lists the id lands under its own id. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
28c9a03388
commit
51fbe6f533
2 changed files with 207 additions and 0 deletions
|
|
@ -24,6 +24,12 @@ namespace GameCult.Caching.Tests
|
|||
private static readonly CultDocumentRegistry DeckOnly = CultDocumentRegistry.ForTypes(new[] { Deck });
|
||||
private static readonly string WidgetId = Full.GetRequired(Widget).SchemaId;
|
||||
|
||||
// Another writer's deck (an older or newer build, another runtime) under an id the deck does not declare. It declares the
|
||||
// deck's own id compatible, so it may rewrite the deck's records; the deck still reads them through that listing, read-only.
|
||||
private static readonly Type MovingDeck = Emit("ForeignSuiteMovingDeck", "tests.foreign_deck", "tests.foreign_deck.v3", new[] { new Field("Name", typeof(string), 0, IsName: true) }, new[] { DeckOnly.GetRequired(Deck).SchemaId });
|
||||
private static readonly CultDocumentRegistry Moving = CultDocumentRegistry.ForTypes(new[] { MovingDeck });
|
||||
private static readonly string MovingId = Moving.GetRequired(MovingDeck).SchemaId;
|
||||
|
||||
private static readonly CultRecordKey D = new("d");
|
||||
private static readonly CultRecordKey E = new("e");
|
||||
private static readonly CultRecordKey W = new("w");
|
||||
|
|
@ -854,5 +860,166 @@ namespace GameCult.Caching.Tests
|
|||
Assert.That(refusal.Message, Does.Contain(v.Value).And.Contain(D.Value));
|
||||
Assert.That(Fingerprint(path), Is.EqualTo(before));
|
||||
}
|
||||
|
||||
// The other writer rewrites a record under its own id, which this build reads but does not declare.
|
||||
private static void Move(string path, CultRecordKey key, string name, bool directory)
|
||||
{
|
||||
using var other = Open(path, Moving, directory);
|
||||
Assert.That(other.Commit(batch => batch.Upsert(MovingDeck, New(MovingDeck, ("Name", name)), key)), Is.True);
|
||||
}
|
||||
|
||||
private static void RefusedOnDisk(CultSchemaConflictException refusal, CultRecordKey key)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That((refusal.RecordKey, refusal.SchemaId), Is.EqualTo((key.Value, MovingId)));
|
||||
Assert.That(refusal.SchemaNames, Is.EqualTo(new[] { "tests.foreign_deck" }));
|
||||
Assert.That(refusal.Message, Does.Not.Contain(Canary), "a refusal never echoes the document it refused");
|
||||
});
|
||||
}
|
||||
|
||||
// The staging half cannot see a record another writer moved to an undeclared id after this cache loaded it: the write is
|
||||
// refused where it lands, under the store's lock, in both store kinds. Nothing is written, and the cache reloads the record as
|
||||
// the store holds it, read-only, so the next write of it is refused before staging.
|
||||
[Test]
|
||||
public void AWriteOverARecordAnotherWriterMovedToAnUndeclaredIdIsRefusedAndReloadedReadOnly(
|
||||
[Values] bool directory,
|
||||
[Values(Write.Flush, Write.Commit)] Write write)
|
||||
{
|
||||
var path = PathOf("moved.cc");
|
||||
using var cache = Open(path, DeckOnly, directory);
|
||||
cache.Commit(batch => batch.Upsert(Deck, DeckOf("d"), D));
|
||||
if (write == Write.Flush)
|
||||
cache.UpsertAsync(Deck, DeckOf(Canary), D).GetAwaiter().GetResult();
|
||||
Move(path, D, "moved", directory);
|
||||
var before = Fingerprint(path);
|
||||
|
||||
var refusal = Assert.Throws<CultSchemaConflictException>(() =>
|
||||
{
|
||||
if (write == Write.Flush)
|
||||
cache.FlushAsync().GetAwaiter().GetResult();
|
||||
else
|
||||
cache.Commit(batch => batch.Upsert(Deck, DeckOf(Canary), D));
|
||||
})!;
|
||||
|
||||
RefusedOnDisk(refusal, D);
|
||||
Assert.That(Fingerprint(path), Is.EqualTo(before));
|
||||
Assert.That(EmittedDocumentTypes.Read(cache.Get(D)!, "Name"), Is.EqualTo("moved"), "the cache serves what the store holds");
|
||||
Assert.That(cache.IsDirty, Is.False);
|
||||
RefusedOnDisk(Assert.Throws<CultSchemaConflictException>(() => cache.UpsertAsync(Deck, DeckOf(Canary), D).GetAwaiter().GetResult())!, D);
|
||||
Assert.That(cache.IsDirty, Is.False, "the second write was refused at staging");
|
||||
cache.FlushAsync().GetAwaiter().GetResult();
|
||||
Assert.That(Fingerprint(path), Is.EqualTo(before));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AStagedRemovalOfARecordMovedToAnUndeclaredIdIsRefused([Values] bool directory)
|
||||
{
|
||||
var path = PathOf("moved-removal.cc");
|
||||
using var cache = Open(path, DeckOnly, directory);
|
||||
cache.Commit(batch => batch.Upsert(Deck, DeckOf("d"), D));
|
||||
Assert.That(cache.Remove(D), Is.True);
|
||||
Move(path, D, "moved", directory);
|
||||
var moved = Stored(path, D);
|
||||
|
||||
RefusedOnDisk(Assert.Throws<CultSchemaConflictException>(() => cache.FlushAsync().GetAwaiter().GetResult())!, D);
|
||||
|
||||
Assert.That(Stored(path, D), Is.EqualTo(moved));
|
||||
Assert.That(EmittedDocumentTypes.Read(cache.Get(D)!, "Name"), Is.EqualTo("moved"));
|
||||
Assert.That(cache.IsDirty, Is.False);
|
||||
}
|
||||
|
||||
// A key this cache never held, which another writer created under an undeclared id after the load: the write is refused, and
|
||||
// the cache then holds the record read-only.
|
||||
[Test]
|
||||
public void ARecordThatAppearedUnderAnUndeclaredIdAtAWrittenKeyIsRefusedAndHeld([Values] bool directory)
|
||||
{
|
||||
var path = PathOf("appeared.cc");
|
||||
using var cache = Open(path, DeckOnly, directory);
|
||||
cache.Commit(batch => batch.Upsert(Deck, DeckOf("e"), E));
|
||||
Move(path, D, "appeared", directory);
|
||||
var before = Fingerprint(path);
|
||||
|
||||
cache.UpsertAsync(Deck, DeckOf(Canary), D).GetAwaiter().GetResult();
|
||||
RefusedOnDisk(Assert.Throws<CultSchemaConflictException>(() => cache.FlushAsync().GetAwaiter().GetResult())!, D);
|
||||
|
||||
Assert.That(Fingerprint(path), Is.EqualTo(before));
|
||||
Assert.That(EmittedDocumentTypes.Read(cache.Get(D)!, "Name"), Is.EqualTo("appeared"));
|
||||
RefusedOnDisk(Assert.Throws<CultSchemaConflictException>(() => cache.Commit(batch => batch.Upsert(Deck, DeckOf(Canary), D)))!, D);
|
||||
Assert.That(cache.IsDirty, Is.False);
|
||||
}
|
||||
|
||||
public enum DirectWrite
|
||||
{
|
||||
Push,
|
||||
Delete,
|
||||
CommitUpsert,
|
||||
CommitDelete
|
||||
}
|
||||
|
||||
// The store's own write API reaches no staging refusal: a read-only record written through it directly is refused where the
|
||||
// write lands, in both store kinds, and the store keeps the other writer's record under its id.
|
||||
[Test]
|
||||
public void ADirectStoreWriteOfAReadOnlyRecordIsRefusedAndNeverRelabelsIt([Values] bool directory, [Values] DirectWrite write)
|
||||
{
|
||||
var path = PathOf("direct.cc");
|
||||
Move(path, D, "theirs", directory);
|
||||
var before = Fingerprint(path);
|
||||
using var cache = Open(path, DeckOnly, directory);
|
||||
var store = StoreOf(cache);
|
||||
var mine = new CultStoredDocument(D, DateTimeOffset.UtcNow.ToString("O"), DeckOnly.GetRequired(Deck), DeckOf(Canary));
|
||||
var held = cache.GetStored(D)!;
|
||||
|
||||
var refusal = Assert.Throws<CultSchemaConflictException>(() =>
|
||||
{
|
||||
switch (write)
|
||||
{
|
||||
case DirectWrite.Push:
|
||||
store.Push(mine);
|
||||
store.PushAll();
|
||||
break;
|
||||
case DirectWrite.Delete:
|
||||
store.Delete(held);
|
||||
store.PushAll();
|
||||
break;
|
||||
case DirectWrite.CommitUpsert:
|
||||
store.CommitBatch(new CultCommitRequest(new[] { mine }, Array.Empty<CultStoredDocument>(), Array.Empty<(CultRecordKey, string?)>(), false), wait: true);
|
||||
break;
|
||||
case DirectWrite.CommitDelete:
|
||||
store.CommitBatch(new CultCommitRequest(Array.Empty<CultStoredDocument>(), new[] { held }, Array.Empty<(CultRecordKey, string?)>(), false), wait: true);
|
||||
break;
|
||||
}
|
||||
})!;
|
||||
|
||||
RefusedOnDisk(refusal, D);
|
||||
Assert.That(Fingerprint(path), Is.EqualTo(before));
|
||||
Assert.That(Read(path).Records.Single().SchemaId, Is.EqualTo(MovingId), "never relabelled");
|
||||
Assert.That(store.IsDirty, Is.False);
|
||||
Assert.That(EmittedDocumentTypes.Read(cache.Get(D)!, "Name"), Is.EqualTo("theirs"));
|
||||
}
|
||||
|
||||
// A removal of a foreign record, which the cache does not hold, is refused typed: the caller is never told it removed what the
|
||||
// store still carries.
|
||||
[Test]
|
||||
public void RemovingAForeignRecordIsRefusedNotReportedAsDone([Values] bool directory, [Values] bool batch)
|
||||
{
|
||||
var path = Seeded(directory ? "remove-foreign-dir.cc" : "remove-foreign-file.cc", directory);
|
||||
var before = Fingerprint(path);
|
||||
using (var cache = Open(path, DeckOnly, directory))
|
||||
{
|
||||
var refusal = Assert.Throws<CultSchemaConflictException>(() =>
|
||||
{
|
||||
if (batch)
|
||||
cache.Commit(stage => stage.Remove(W));
|
||||
else
|
||||
cache.Remove(W);
|
||||
})!;
|
||||
Assert.That((refusal.RecordKey, refusal.SchemaId), Is.EqualTo((W.Value, WidgetId)));
|
||||
Assert.That(cache.IsDirty, Is.False);
|
||||
Assert.That(cache.Remove(new CultRecordKey("absent")), Is.False, "a key nothing holds is still no removal");
|
||||
}
|
||||
|
||||
Assert.That(Fingerprint(path), Is.EqualTo(before));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -200,6 +200,46 @@ public sealed class CultMeshSingleFileStoreTests
|
|||
File.ReadAllBytes(path).Should().Equal(bytes);
|
||||
}
|
||||
|
||||
// A Mesh write declares the ids of the catalog entry it writes: its own and those it lists compatible. Over a record under any
|
||||
// other id, typed or raw, it is refused with the typed schema conflict naming the record, and the file is left byte for byte.
|
||||
[Test]
|
||||
public void AMeshWriteOverARecordUnderAnIdItsEntryDoesNotDeclareIsRefused([Values] bool typed)
|
||||
{
|
||||
var path = Path.Combine(_root, $"undeclared-{typed}.cc");
|
||||
if (typed)
|
||||
CultMesh.WriteSingleFileDocumentPayload(path, Key, Raw, null, EmptyArray);
|
||||
else
|
||||
CultMesh.WriteSingleFileDocument(path, Key, Publication());
|
||||
var bytes = File.ReadAllBytes(path);
|
||||
|
||||
var refusal = Assert.Throws<CultSchemaConflictException>(() =>
|
||||
{
|
||||
if (typed)
|
||||
CultMesh.WriteSingleFileDocument(path, Key, Publication("other"));
|
||||
else
|
||||
CultMesh.WriteSingleFileDocumentPayload(path, Key, Raw, null, new byte[] { 0x91, 0x01 });
|
||||
})!;
|
||||
|
||||
(refusal.RecordKey, refusal.SchemaId).Should().Be((Key.Value, typed ? Raw.SchemaId : SchemaId()));
|
||||
refusal.Message.Should().Contain("nothing was reloaded");
|
||||
File.ReadAllBytes(path).Should().Equal(bytes);
|
||||
}
|
||||
|
||||
// A write whose entry lists the held id compatible owns the record and replaces it under its own id.
|
||||
[Test]
|
||||
public void AMeshWriteWhoseEntryListsTheHeldIdLandsUnderItsOwnId()
|
||||
{
|
||||
var path = Path.Combine(_root, "listed.cc");
|
||||
CultMesh.WriteSingleFileDocumentPayload(path, Key, Raw, null, EmptyArray);
|
||||
var next = new CultMeshSingleFileDocumentSchema("raw:schema.v2", "RawSchema", "2") { CompatibleSchemaIds = new[] { Raw.SchemaId } };
|
||||
|
||||
CultMesh.WriteSingleFileDocumentPayload(path, Key, next, null, new byte[] { 0x91, 0x01 });
|
||||
|
||||
var record = Snapshot(path).Records.Single();
|
||||
(record.Key, record.SchemaId).Should().Be((Key.Value, "raw:schema.v2"));
|
||||
record.Payload.Should().Equal(new byte[] { 0x91, 0x01 });
|
||||
}
|
||||
|
||||
// The writer cannot see whether the raw payload or the records it copies hold element ids, so a marked file stays marked.
|
||||
[Test]
|
||||
public void AMeshRawWriteIntoAMarkedStoreKeepsItMarked()
|
||||
|
|
|
|||
Loading…
Reference in a new issue