diff --git a/tests/GameCult.Caching.Tests/UpdateTests.cs b/tests/GameCult.Caching.Tests/UpdateTests.cs index 63462ba6..d8ec67e5 100644 --- a/tests/GameCult.Caching.Tests/UpdateTests.cs +++ b/tests/GameCult.Caching.Tests/UpdateTests.cs @@ -80,16 +80,27 @@ namespace GameCult.Caching.Tests var applied = 0; // Each round starts both writers together: an attempt reads fresh from the store, so only overlapping read-commit windows lose. var round = new Barrier(2); + // Starting together does not make the read-commit windows overlap: on a loaded or few-core machine one writer can finish its + // whole attempt before the other wakes, and then no attempt is ever lost. So each writer's first apply waits for the other's: + // both have read the seed by then, only one commit can win it, and the loser must rerun. That makes the race certain once. + var firstApplies = new Barrier(2); var racers = new[] { a, b }.Select(cache => Task.Run(() => { var results = new List(); + var firstApply = true; for (var i = 0; i < n; i++) { round.SignalAndWait(); results.Add(cache.Update(Key, counter => { Interlocked.Increment(ref applied); + if (firstApply) + { + firstApply = false; + Assert.That(firstApplies.SignalAndWait(TimeSpan.FromSeconds(30)), Is.True, "the other writer never began its first update"); + } + return Plus(counter, 1); }, maxAttempts: 200).Outcome); }