Test: Subscribe snapshot ahead of a pending commit is forced by a cache observer ahead of the journal

This commit is contained in:
Meta Vens 2026-11-27 23:39:56 +01:00
commit 7285794f15

View file

@ -745,8 +745,8 @@ namespace GameCult.Networking.Tests
Assert.That(run.Peer.Changes.Select(change => change.CommitCount), Is.All.EqualTo(2));
}
// Two commits the cache admitted and Watch has not seen (the subscription server's gate is held, so neither publisher has
// run; the test waits for the source to hold both, so the interleaving is forced, not hoped for). C1 sets a=s2, C2 sets a=s3
// Two commits the cache admitted and Watch has not seen (a gate holds the publishers; the test waits for the source to
// hold both, so the interleaving is forced, not hoped for). C1 sets a=s2, C2 sets a=s3
// and b=s2, valid only after C1. A snapshot taken now reflects both; Watch must not replay C1 over it.
private static (Task First, Task Second) AdmitTwoCommitsBehindTheGate(Replication run)
{
@ -779,17 +779,27 @@ namespace GameCult.Networking.Tests
Assert.That(run.Client.Fault, Is.Null);
}
// Subscribe registers Watch under the gate and then reads its snapshot; a commit the cache admitted before that read and
// that has not reached the journal yet is delivered to Watch afterwards. A cache observer registered ahead of the database's
// holds both writers between admission and publication, so the subscription is made inside that window.
[Test]
public async Task ASubscribeSnapshotAheadOfAPendingCommitIsNotReplayed()
{
using var run = new Replication(source => Stage(source, ("a", "s1"), ("b", "s9")), subscribe: false);
using var release = new ManualResetEventSlim();
var hold = true;
using var run = new Replication(
source =>
{
Stage(source, ("a", "s1"), ("b", "s9"));
source.Watch<ReplicaItem>().Subscribe(_ => { if (Volatile.Read(ref hold)) release.Wait(10_000); });
},
subscribe: false);
(Task First, Task Second) writes;
lock (run.Gate)
{
writes = AdmitTwoCommitsBehindTheGate(run);
run.Subscribe("late").GetAwaiter().GetResult();
}
writes = AdmitTwoCommitsBehindTheGate(run);
run.Subscribe("late").GetAwaiter().GetResult();
Volatile.Write(ref hold, false);
release.Set();
await Task.WhenAll(writes.First, writes.Second);
await Until(() => run.ReplicaItem("a")?.Sku == "s3" && run.ReplicaItem("b")?.Sku == "s2");
await Task.Delay(100);