Kill the Stryker survivors in the projection and observer cuts

Delete the dead removed-ancestor branch and the duplicated name-read in the
variant name rule (resolution already refuses a missing or removed base and a
variant shares its base type). Add tests for the index rule counting a record
the same commit removes, blank names not colliding, and Dispose rethrowing
escaped completions after every observer completed.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-29 17:46:36 +02:00
commit 0a543c9fb2
3 changed files with 85 additions and 8 deletions

View file

@ -2551,7 +2551,7 @@ namespace GameCult.Caching
var descriptor = stored.Descriptor;
string? name = null;
if (descriptor.NameAccessor != null)
name = Read(stored, $"[CultName] member {descriptor.NameMember}", descriptor.NameAccessor) is { Length: > 0 } read ? read : null;
name = NameRead(stored) is { Length: > 0 } read ? read : null;
var indexes = new List<(string Alias, string Value)>();
foreach (var pair in descriptor.IndexAccessors)
{
@ -2565,6 +2565,9 @@ namespace GameCult.Caching
}
}
private static string? NameRead(CultStoredDocument stored) =>
Read(stored, $"[CultName] member {stored.Descriptor.NameMember}", stored.Descriptor.NameAccessor!);
private static string? Read(CultStoredDocument stored, string what, Func<object, string?> getter)
{
try
@ -2590,7 +2593,7 @@ namespace GameCult.Caching
// The variant rules are judged only when a variant is involved.
if (_variantKeys.Count != 0 || admitted.Any(stored => stored.Variant != null))
{
RefuseVariantNameSharing(plan, evicted);
RefuseVariantNameSharing(plan);
RefuseVariantIndexSharing(plan, evicted);
}
@ -2744,20 +2747,17 @@ namespace GameCult.Caching
// R6: the name index maps one name to one key, so a variant that kept its base's name would shadow it. A variant's
// name is its projection; an ancestor that lands is read from its projection, one that stays is read as held.
private void RefuseVariantNameSharing(VariantPlan plan, IReadOnlyList<CultStoredDocument> evicted)
private void RefuseVariantNameSharing(VariantPlan plan)
{
var gone = evicted.Select(stored => stored.Key.Value).ToHashSet(StringComparer.Ordinal);
var landing = plan.Admitted.Concat(plan.Dependents).ToDictionary(stored => stored.Key.Value, StringComparer.Ordinal);
// Resolution already refused a base that is missing or removed, so an ancestor is landing or held.
CultStoredDocument? Ancestor(string key) =>
landing.TryGetValue(key, out var landed) ? landed
: gone.Contains(key) ? null
: _entries.TryGetValue(key, out var held) ? held : null;
string? NameOf(CultStoredDocument stored) =>
plan.Projections.TryGetValue(stored.Key.Value, out var projection)
? projection.Name
: stored.Descriptor.NameAccessor == null ? null : Read(stored, $"[CultName] member {stored.Descriptor.NameMember}", stored.Descriptor.NameAccessor);
plan.Projections.TryGetValue(stored.Key.Value, out var projection) ? projection.Name : NameRead(stored);
foreach (var variant in landing.Values.Where(stored => stored.Variant != null))
{

View file

@ -135,5 +135,40 @@ namespace GameCult.Caching.Tests
using var after = cache.Watch<ObservedPing>().Subscribe(_ => { }, _ => completed++);
Assert.That(completed, Is.EqualTo(2), "subscribing to a disposed cache completes at once");
}
// Completing an observer is publication too: every observer is completed, and a fail-fast handler's exception
// reaches the disposer afterwards (an AggregateException for several).
[Test]
public void ACompletionThatEscapesReachesTheDisposerAfterEveryObserverCompleted()
{
var cache = new CultCache(Registry);
var completed = new List<string>();
using var one = cache.Watch<ObservedPing>().Subscribe(_ => { }, _ =>
{
completed.Add("one");
throw new InvalidOperationException("one");
});
using var two = cache.Watch<ObservedPing>().Subscribe(_ => { }, _ =>
{
completed.Add("two");
throw new InvalidOperationException("two");
});
var escaped = Assert.Throws<AggregateException>(() => cache.Dispose())!;
Assert.That(completed, Is.EqualTo(new[] { "one", "two" }));
Assert.That(escaped.InnerExceptions.Select(exception => exception.Message), Is.EqualTo(new[] { "one", "two" }));
}
[Test]
public void ASingleEscapedCompletionIsRethrownAsItself()
{
var cache = new CultCache(Registry);
using var only = cache.Watch<ObservedPing>().Subscribe(_ => { }, _ => throw new InvalidOperationException("only"));
var escaped = Assert.Throws<InvalidOperationException>(() => cache.Dispose())!;
Assert.That(escaped.Message, Is.EqualTo("only"));
}
}
}

View file

@ -212,5 +212,47 @@ namespace GameCult.Caching.Tests
Assert.That(cache.GetByIndex<ProjectedRecord>("code", "base-code-2"), Is.Not.Null);
Assert.That(cache.GetByName<ProjectedRecord>("variant"), Is.Not.Null);
}
// The index rule counts the holders a commit leaves behind: a record removed by the same commit no longer holds its value.
[Test]
public void AVariantMayTakeAnIndexedValueFromARecordTheSameCommitRemoves()
{
using var cache = new CultCache(Registry, CultCacheMessagePack.CreateCodec(Registry));
var baseKey = new CultRecordKey("projected:base");
var holderKey = new CultRecordKey("projected:holder");
Put(cache, new ProjectedRecord { Name = "base", Code = "base-code" }, baseKey);
Put(cache, new ProjectedRecord { Name = "holder", Code = "shared" }, holderKey);
var landed = cache.Commit(batch =>
{
batch.Remove(holderKey);
batch.UpsertVariant(new CultRecordKey("projected:variant"), baseKey, new[]
{
cache.Override<ProjectedRecord>(nameof(ProjectedRecord.Name), "variant"),
cache.Override<ProjectedRecord>(nameof(ProjectedRecord.Code), "shared")
});
});
Assert.That(landed, Is.True);
Assert.That(cache.GetByIndex<ProjectedRecord>("code", "shared"), Is.Not.Null);
Assert.That(cache.Get<ProjectedRecord>(holderKey), Is.Null);
}
// A blank name is no name: a variant and its base that are both blank do not shadow each other.
[Test]
public void ABlankNameIsNoNameSoBlankVariantAndBaseDoNotCollide()
{
using var cache = new CultCache(Registry, CultCacheMessagePack.CreateCodec(Registry));
var baseKey = new CultRecordKey("projected:base");
Put(cache, new ProjectedRecord { Name = "", Code = "base-code" }, baseKey);
cache.Commit(batch => batch.UpsertVariant(new CultRecordKey("projected:variant"), baseKey, new[]
{
cache.Override<ProjectedRecord>(nameof(ProjectedRecord.Name), ""),
cache.Override<ProjectedRecord>(nameof(ProjectedRecord.Code), "variant-code")
}));
Assert.That(cache.Get<ProjectedRecord>(new CultRecordKey("projected:variant")), Is.Not.Null);
}
}
}