From ed22d1c8b5ff461ce308dbe5e2cfffb2cf658d92 Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Thu, 3 Dec 2026 21:19:16 +0100 Subject: [PATCH] Test dello scheduler: giudica la lettura che ha visto (#185) WaitUntil read the condition twice, and the scheduler's first round leaves the clock waited on only for an instant: the test failed once on the CI (run 586). It now judges the reading it saw; AStateSeenOnceIsEnoughForTheWait keeps it so. Reproduced 20 times in 3,000 parallel runs before the fix, never in 18,000 after. Co-Authored-By: Claude Opus 5.5 Co-authored-by: Alberto Balbo --- 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"); } }