From 0a543c9fb29a4773398d8f32686fb0ba2f27f533 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 29 Sep 2026 17:46:36 +0200 Subject: [PATCH] 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 --- src/GameCult.Caching/CultCache.cs | 16 +++---- .../CultCacheObserverTests.cs | 35 ++++++++++++++++ .../CultCacheProjectionTests.cs | 42 +++++++++++++++++++ 3 files changed, 85 insertions(+), 8 deletions(-) diff --git a/src/GameCult.Caching/CultCache.cs b/src/GameCult.Caching/CultCache.cs index d2115535..f9497cb3 100644 --- a/src/GameCult.Caching/CultCache.cs +++ b/src/GameCult.Caching/CultCache.cs @@ -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 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 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)) { diff --git a/tests/GameCult.Caching.Tests/CultCacheObserverTests.cs b/tests/GameCult.Caching.Tests/CultCacheObserverTests.cs index 8cb3992d..7fde01e7 100644 --- a/tests/GameCult.Caching.Tests/CultCacheObserverTests.cs +++ b/tests/GameCult.Caching.Tests/CultCacheObserverTests.cs @@ -135,5 +135,40 @@ namespace GameCult.Caching.Tests using var after = cache.Watch().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(); + using var one = cache.Watch().Subscribe(_ => { }, _ => + { + completed.Add("one"); + throw new InvalidOperationException("one"); + }); + using var two = cache.Watch().Subscribe(_ => { }, _ => + { + completed.Add("two"); + throw new InvalidOperationException("two"); + }); + + var escaped = Assert.Throws(() => 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().Subscribe(_ => { }, _ => throw new InvalidOperationException("only")); + + var escaped = Assert.Throws(() => cache.Dispose())!; + + Assert.That(escaped.Message, Is.EqualTo("only")); + } } } diff --git a/tests/GameCult.Caching.Tests/CultCacheProjectionTests.cs b/tests/GameCult.Caching.Tests/CultCacheProjectionTests.cs index 88f251c6..57135929 100644 --- a/tests/GameCult.Caching.Tests/CultCacheProjectionTests.cs +++ b/tests/GameCult.Caching.Tests/CultCacheProjectionTests.cs @@ -212,5 +212,47 @@ namespace GameCult.Caching.Tests Assert.That(cache.GetByIndex("code", "base-code-2"), Is.Not.Null); Assert.That(cache.GetByName("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(nameof(ProjectedRecord.Name), "variant"), + cache.Override(nameof(ProjectedRecord.Code), "shared") + }); + }); + + Assert.That(landed, Is.True); + Assert.That(cache.GetByIndex("code", "shared"), Is.Not.Null); + Assert.That(cache.Get(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(nameof(ProjectedRecord.Name), ""), + cache.Override(nameof(ProjectedRecord.Code), "variant-code") + })); + + Assert.That(cache.Get(new CultRecordKey("projected:variant")), Is.Not.Null); + } } }