CultCache: own the observer list and isolate every observer

The cache no longer fans changes out through an R3 Subject. It keeps a
copy-on-write observer array and Publish catches per observer: every observer
receives every change, an exception that escapes (a fail-fast R3 handler) joins
the OnUpdate failures rethrown to the writer after delivery, and the observer
stays subscribed. Dispose completes observers, and a disposed cache completes
new ones at once. Deletes the false "OnNext does not throw" comment.

The composition contract now states the observer policy and that a throwing
name or index getter refuses a change set, loads included.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-29 16:51:33 +02:00
commit ecee41e8ff
4 changed files with 252 additions and 12 deletions

View file

@ -59,5 +59,40 @@ namespace GameCult.Networking.Tests
Assert.That(database.LastWriteSequence(SchemaId, after), Is.Not.Null, "a later commit was never logged");
});
}
// One observer's escaped exception (a fail-fast R3 handler) does not stop the database observer after it, and the writer gets it.
[Test]
public async Task AnEscapedObserverExceptionDoesNotStopTheDatabaseOrLaterChanges()
{
var previous = ObservableSystem.GetUnhandledExceptionHandler();
ObservableSystem.RegisterUnhandledExceptionHandler(exception => throw exception);
try
{
var cache = new CultCache();
using var thrower = cache.Watch<NetworkSchemaNote>().Subscribe(change =>
{
if (change.Document?.Text == "boom") throw new InvalidOperationException("observer boom");
});
var database = new CultNetDatabase(cache);
var seen = new ConcurrentQueue<string?>();
using var watch = database.Watch<NetworkSchemaNote>().Subscribe(change => seen.Enqueue(change.Document?.Text));
var boom = new CultRecordKey("pub:boom");
var escaped = Assert.ThrowsAsync<InvalidOperationException>(() => cache.UpsertAsync(Note("boom"), new CultRecordHandle<NetworkSchemaNote>(boom)));
ObservableSystem.RegisterUnhandledExceptionHandler(previous);
var after = new CultRecordKey("pub:after");
await database.PutAsync(after, Note("after"));
Assert.Multiple(() =>
{
Assert.That(escaped!.Message, Is.EqualTo("observer boom"));
Assert.That(seen, Is.EqualTo(new[] { "boom", "after" }), "the database observer after the thrower saw both changes");
Assert.That(database.LastWriteSequence(SchemaId, after), Is.Not.Null, "a later commit was never logged");
});
}
finally
{
ObservableSystem.RegisterUnhandledExceptionHandler(previous);
}
}
}
}