From fb41b7899c511c473e7fdd601aa042ac4d731beb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Fri, 30 Oct 2026 01:10:32 +0100 Subject: [PATCH] fix: do not miss a clock advance while a PeriodicTimer starts waiting With auto-advance disabled, an AdvanceBy between reading the clock and registering the TimeChanged callback was lost and WaitForNextTickAsync hung forever, which is exactly the window where tests react to WaitingForNextTick; the clock is now re-checked after registering, as TaskMock already does. --- .../TimeSystem/PeriodicTimerMock.cs | 10 +- .../TimeSystem/PeriodicTimerMockTests.cs | 106 ++++++++++++++++++ 2 files changed, 113 insertions(+), 3 deletions(-) diff --git a/Source/Testably.Abstractions.Testing/TimeSystem/PeriodicTimerMock.cs b/Source/Testably.Abstractions.Testing/TimeSystem/PeriodicTimerMock.cs index cb761e51a..1bf4639aa 100644 --- a/Source/Testably.Abstractions.Testing/TimeSystem/PeriodicTimerMock.cs +++ b/Source/Testably.Abstractions.Testing/TimeSystem/PeriodicTimerMock.cs @@ -82,9 +82,13 @@ public void Dispose() { using IAwaitableCallback onTimeChanged = _timeSystem.On .TimeChanged(predicate: _ => _timeSystem.TimeProvider.ElapsedTicks >= nextTime); - await onTimeChanged.WaitAsync( - timeout: Timeout.InfiniteTimeSpan, - cancellationToken: cancellationToken).ConfigureAwait(false); + if (_timeSystem.TimeProvider.ElapsedTicks < nextTime) + { + await onTimeChanged.WaitAsync( + timeout: Timeout.InfiniteTimeSpan, + cancellationToken: cancellationToken).ConfigureAwait(false); + } + _lastTime = _timeSystem.TimeProvider.ElapsedTicks; } } diff --git a/Tests/Testably.Abstractions.Testing.Tests/TimeSystem/PeriodicTimerMockTests.cs b/Tests/Testably.Abstractions.Testing.Tests/TimeSystem/PeriodicTimerMockTests.cs index ce1a37173..69265a6ef 100644 --- a/Tests/Testably.Abstractions.Testing.Tests/TimeSystem/PeriodicTimerMockTests.cs +++ b/Tests/Testably.Abstractions.Testing.Tests/TimeSystem/PeriodicTimerMockTests.cs @@ -1,5 +1,6 @@ #if FEATURE_PERIODIC_TIMER using aweXpect.Chronology; +using System.Collections.ObjectModel; using System.Threading; using Testably.Abstractions.Testing.TimeSystem; using Testably.Abstractions.TimeSystem; @@ -74,6 +75,34 @@ public async Task ShouldNotBeAffectedByTimeChange() await timerTask; } + [Test] + public async Task + WaitForNextTickAsync_DisableAutoAdvance_WhenClockAdvancesWhileStartingToWait_ShouldTick() + { + InterleavingTimeProviderFactory factory = new( + new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc)); + MockTimeSystem timeSystem = new(factory, o => o.DisableAutoAdvance()); + using CancellationTokenSource cts = CancellationTokenSource + .CreateLinkedTokenSource(TestContext.Current!.Execution.CancellationToken); + cts.CancelAfter(5.Seconds()); + CancellationToken token = cts.Token; + using IPeriodicTimer timer = timeSystem.PeriodicTimer.New(30.Seconds()); + using IAwaitableCallback onWaiting = timeSystem.On.PeriodicTimer + .WaitingForNextTick(_ => factory.Provider!.AdvanceOnNextRead = 30.Seconds()); + long start = timeSystem.TimeProvider.ElapsedTicks; + + bool result = false; + async Task Act() + => result = await timer.WaitForNextTickAsync(token); + + await That(Act).DoesNotThrow() + .Because("the clock was advanced to the next tick while the timer started to wait"); + await That(result).IsTrue(); + await That(timeSystem.TimeProvider.ElapsedTicks - start) + .IsEqualTo(TimeSpan.FromSeconds(30).Ticks) + .Because("the clock was advanced by exactly one period"); + } + [Test] public async Task WaitForNextTickCount_AfterDispose_ShouldStopIncreasing() { @@ -246,5 +275,82 @@ public async Task WaitingForNextTick_WhenTimerIsDisposed_ShouldNotFireCallback() await That(receivedTimer).IsNull(); } + + private sealed class InterleavingTimeProviderFactory(DateTime start) : ITimeProviderFactory + { + public InterleavingTimeProvider? Provider { get; private set; } + + public ITimeProvider Create(Action onTimeChanged) + => Provider = new InterleavingTimeProvider( + TimeProviderFactory.Use(start).Create(onTimeChanged)); + } + + /// + /// Simulates another thread advancing the clock right after the next read of + /// . + /// + private sealed class InterleavingTimeProvider(ITimeProvider inner) : ITimeProvider + { + public TimeSpan? AdvanceOnNextRead { get; set; } + + public long ElapsedTicks + { + get + { + long value = inner.ElapsedTicks; + if (AdvanceOnNextRead is { } interval) + { + AdvanceOnNextRead = null; + inner.AdvanceBy(interval); + } + + return value; + } + } + + public TimeZoneInfo LocalTimeZone + { + get => inner.LocalTimeZone; + set => inner.LocalTimeZone = value; + } + + public DateTime MaxValue + { + get => inner.MaxValue; + set => inner.MaxValue = value; + } + + public DateTime MinValue + { + get => inner.MinValue; + set => inner.MinValue = value; + } + + public DateTime StartTime => inner.StartTime; + + public DateTime UnixEpoch + { + get => inner.UnixEpoch; + set => inner.UnixEpoch = value; + } + + public void AdvanceBy(TimeSpan interval) + => inner.AdvanceBy(interval); + + public TimeZoneInfo FindSystemTimeZoneById(string id) + => inner.FindSystemTimeZoneById(id); + + public ReadOnlyCollection GetSystemTimeZones() + => inner.GetSystemTimeZones(); + + public DateTime Read() + => inner.Read(); + + public void RegisterTimeZone(TimeZoneInfo timeZone) + => inner.RegisterTimeZone(timeZone); + + public void SetTo(DateTime value) + => inner.SetTo(value); + } } #endif