From 72028ede94ea6a1ecb8b652a0d48efca0f1ce8de Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 27 Nov 2026 21:57:44 +0100 Subject: [PATCH] UpdateTests: make the two-writer race certain instead of probable TwoWritersUpdatingLoseNoIncrement asserted that some attempt was lost, but only started the writers together; on a loaded or few-core machine one can finish its attempt before the other wakes and the assertion fails with no code change (200 applies, expected more than 200). Each writer's first apply now waits for the other's, so both read the seed and one must rerun. Co-Authored-By: Claude Sonnet 5.5 --- tests/GameCult.Caching.Tests/UpdateTests.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) 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); }