Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Docs/pages/docs/04-time-system/02-auto-advance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ using CancellationTokenSource cts = new(TimeSpan.FromSeconds(1), timeProvider);
await Task.Delay(TimeSpan.FromSeconds(5), timeProvider, cts.Token);
```

A `Task.Delay` on the `TimeProvider` holds the clock until it registered its cancellation token, so that a timeout due before the delay can still cancel it. This takes up to 5 seconds of real time when the delay never registers, e.g. with the token of a disposed `CancellationTokenSource`.

A wait that its timeout cancels on the way stops the clock at the timeout. This applies to `Task.Delay` and `PeriodicTimer.WaitForNextTickAsync` of the `MockTimeSystem` and to `Task.Delay` on the `TimeProvider`.

:::warning[Waiting only for a timeout]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ internal sealed class TimerScheduler
/// </remarks>
private static readonly TimeSpan BlockedCallbackLimit = TimeSpan.FromSeconds(1);

/// <summary>
/// How long the timers that advance the clock wait for the creator of the earliest timer to set it up, e.g. for
/// <c>Task.Delay</c> on the <c>TimeProvider</c> to register its cancellation.
/// </summary>
/// <remarks>
/// A creator usually finishes at once, but on a loaded machine its thread may not run for a while, and a timer
/// that fires before then cannot be canceled by it. The limit only matters for a creator that never finishes,
/// e.g. on the token of a disposed source, which then delays the clock once for each such timer.
/// </remarks>
private static readonly TimeSpan SetUpLimit = TimeSpan.FromSeconds(5);

/// <summary>
/// How often the thread that fired a callback checks whether the callback is blocked.
/// </summary>
Expand Down Expand Up @@ -94,7 +105,7 @@ public TimerScheduler(ITimeProvider clock, TimeSpan? strictCallbackTimeout = nul
{
_clock = clock;
_strictCallbackTimeout = strictCallbackTimeout;
_setUpLimit = strictCallbackTimeout ?? BlockedCallbackLimit;
_setUpLimit = strictCallbackTimeout ?? SetUpLimit;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,48 @@ await That(timeSystem.Scheduler.IsAdvancing).IsFalse()
await That(timeSystem.DateTime.UtcNow).IsEqualTo(start);
}

[Test]
public async Task AutoAdvance_WhenTheCreatorNeverSetsUpATimer_ShouldFireItAfterTheLimit()
{
MockTimeSystem timeSystem = new();
using ManualResetEventSlim fired = new();
TimerScheduler.ScheduledTimer timer = new(
timeSystem.TimeProvider.ElapsedTicks + TimeSpan.FromSeconds(1).Ticks, true, () => fired.Set(),
() => true);

timeSystem.Scheduler.Schedule(timer, CancellationToken.None);
bool isFired = fired.Wait(15000, TestContext.Current!.Execution.CancellationToken);

await That(isFired).IsTrue()
.Because("a creator that never finishes, e.g. on the token of a disposed source, must not stop the clock");
}

[Test]
public async Task AutoAdvance_WhenTheCreatorSetsUpATimerLongerThanOneSecond_ShouldWaitForIt()
{
MockTimeSystem timeSystem = new();
int isSetUp = 0;
int wasSetUpWhenFired = -1;
using ManualResetEventSlim fired = new();
TimerScheduler.ScheduledTimer timer = new(
timeSystem.TimeProvider.ElapsedTicks + TimeSpan.FromSeconds(1).Ticks, true,
() =>
{
Volatile.Write(ref wasSetUpWhenFired, Volatile.Read(ref isSetUp));
fired.Set();
},
() => Volatile.Read(ref isSetUp) == 0);

timeSystem.Scheduler.Schedule(timer, CancellationToken.None);
Thread.Sleep(1200);
Volatile.Write(ref isSetUp, 1);
bool isFired = fired.Wait(10000, TestContext.Current!.Execution.CancellationToken);

await That(isFired).IsTrue();
await That(Volatile.Read(ref wasSetUpWhenFired)).IsEqualTo(1)
.Because("a creator whose thread does not run for a while, e.g. on a loaded machine, must finish first");
}

[Test]
public async Task AutoAdvance_WhileATimerIsCreated_ShouldNotMoveTheClock()
{
Expand Down
Loading