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 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-27 21:57:44 +01:00
commit 72028ede94

View file

@ -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<CultUpdateOutcome>();
var firstApply = true;
for (var i = 0; i < n; i++)
{
round.SignalAndWait();
results.Add(cache.Update<UpdateCounter>(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);
}