From 1f0c4b322fb8d6d9a632a23f03b0b687e8218ef2 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Thu, 3 Dec 2026 20:50:14 +0100 Subject: [PATCH] Judge the scheduler test on the reading it saw LoopWaitsOnTheInjectedClock failed once on the CI (run 586) in 38 ms, although its wait lasts up to five seconds. WaitUntil read the condition in its loop and then read it again for the assertion. The scheduler's first round registers a wait on the clock and drops it at once, because the job added before RunAsync has already signalled a change, so the loop could see the clock waited on and the assertion could see it no longer waited on. The test run 3,000 times in parallel failed 20 times before the fix and never in 18,000 runs after it. WaitUntil now judges the reading it saw, and AStateSeenOnceIsEnoughForTheWait, which fails with the old helper, keeps it that way. The scheduler itself is unchanged. Co-Authored-By: Claude Opus 5.5 --- tests/Anubis.Tests/CoreTests.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/Anubis.Tests/CoreTests.cs b/tests/Anubis.Tests/CoreTests.cs index 1f5ff24..1c37e4b 100644 --- a/tests/Anubis.Tests/CoreTests.cs +++ b/tests/Anubis.Tests/CoreTests.cs @@ -288,13 +288,28 @@ public sealed class SchedulerTests await Assert.ThrowsAsync(() => loop).ConfigureAwait(false); } + [TestMethod] + [TestCategory(Category.Unit)] + public async Task AStateSeenOnceIsEnoughForTheWait() + { + // A state that holds for an instant (CI run 586): the wait judges the reading it saw, it does not ask again. + var readings = 0; + await WaitUntil(() => Interlocked.Increment(ref readings) == 2).ConfigureAwait(false); + Assert.AreEqual(2, readings); + } + internal static async Task WaitUntil(Func condition) { - for (var i = 0; i < 500 && !condition(); i++) + // One reading per round, and the reading seen is the one judged. Asking again for the assertion failed the test + // when the state held only for an instant: the scheduler's first round registers a wait on the clock and drops it + // at once, because the job added before RunAsync has already signalled a change. + var met = condition(); + for (var i = 0; i < 500 && !met; i++) { await Task.Delay(10).ConfigureAwait(false); + met = condition(); } - Assert.IsTrue(condition(), "condizione non raggiunta in tempo"); + Assert.IsTrue(met, "condizione non raggiunta in tempo"); } } -- 2.54.1