Tests: pin the Stryker survivors in the lay-back and the directory refusal

The file's record moves when only its storedAt or only its schema id differs from the loaded one; a
staged removal is refused and dropped with the rest; a refusal leaves staged what it did not refuse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-02 17:58:50 +02:00
commit 0a98004bc4
2 changed files with 63 additions and 0 deletions

View file

@ -293,6 +293,27 @@ namespace GameCult.Caching.Tests
Assert.That(CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(path)).Records.Select(record => record.Key), Is.EqualTo(new[] { "other" }));
}
// The file's record counts as moved when either half of its name (schema id, storedAt) is no longer the loaded one.
[TestCase(true, false)]
[TestCase(false, true)]
public void AWholeViewWriteCarriesTheFilesRecordWhenOnlyItsStoredAtOrOnlyItsIdMoved(bool newerStoredAt, bool ownersId)
{
var path = UnownedStore("moved.cc");
using var cache = Open(path);
var snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(path));
var moved = snapshot.Records.Single();
if (newerStoredAt)
moved.StoredAt = DateTimeOffset.UtcNow.AddMinutes(5).ToString("O");
if (ownersId)
moved.SchemaId = Registry.GetRequired(typeof(IdDeck)).SchemaId;
File.WriteAllBytes(path, CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot));
cache.Commit(batch => batch.Upsert(typeof(IdDeck), Deck("other"), new CultRecordKey("other")));
var after = DiskRecord(path, "old");
Assert.That((after.SchemaId, after.StoredAt), Is.EqualTo((moved.SchemaId, moved.StoredAt)));
}
// A record that carried no ids when it was laid back does not mark the store, though the ids it minted at load are in memory.
[Test]
public void ALaidBackRecordWithoutIdsDoesNotMarkTheStore()

View file

@ -240,6 +240,48 @@ namespace GameCult.Caching.Tests
Assert.That(Stored(path, D), Is.EqualTo(widget));
}
// The directory store stages a removal as its own pending key: a refusal takes it too.
[Test]
public void AStagedRemovalOfARecordAnotherWriterMadeForeignIsRefusedOnceAndThenGone()
{
var path = Seeded("staged-removal.cc", directory: true);
using var cache = Open(path, DeckOnly, directory: true);
using (var other = Open(path, Full, directory: true))
other.Commit(batch => batch.Upsert(Widget, WidgetOf("now a widget"), D));
var widget = Stored(path, D);
cache.Remove(D);
var refusal = Assert.Throws<CultSchemaConflictException>(() => cache.FlushAsync().GetAwaiter().GetResult())!;
Land(cache, Write.Commit, E, DeckOf("e"));
Assert.That(refusal.RecordKey, Is.EqualTo(D.Value));
Assert.That(Stored(path, D), Is.EqualTo(widget));
}
// What a refusal leaves staged is exactly what it did not refuse: the other record's write still lands on the next flush,
// and a store with nothing left staged is clean.
[Test]
public void ARefusalLeavesStagedWhatItDidNotRefuse()
{
var path = Seeded("pending.cc", directory: true);
using var cache = Open(path, DeckOnly, directory: true);
using (var other = Open(path, Full, directory: true))
other.Commit(batch => batch.Upsert(Widget, WidgetOf("now a widget"), D));
cache.UpsertAsync(Deck, DeckOf(Canary), D).GetAwaiter().GetResult();
Assert.Throws<CultSchemaConflictException>(() => cache.FlushAsync().GetAwaiter().GetResult());
Assert.That(cache.IsDirty, Is.False, "nothing is left staged");
cache.UpsertAsync(Deck, DeckOf(Canary), D).GetAwaiter().GetResult();
cache.UpsertAsync(Deck, DeckOf("e"), E).GetAwaiter().GetResult();
Assert.Throws<CultSchemaConflictException>(() => cache.FlushAsync().GetAwaiter().GetResult());
Assert.That(cache.IsDirty, Is.True, "e is still staged");
cache.FlushAsync().GetAwaiter().GetResult();
Assert.That(Read(path).Records.Select(record => record.Key), Does.Contain("e"));
Assert.That(cache.IsDirty, Is.False);
}
[TestCase(false)]
[TestCase(true)]
public void RemovingARecordAnotherWriterMadeForeignIsRefused(bool directory)