Skip to content
Merged
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
9 changes: 6 additions & 3 deletions Source/Testably.Abstractions.Testing/TimeSystem/TimerMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ internal void RegisterOnDispose(Action? onDispose)
}

#pragma warning disable S3776 // Cognitive Complexity of methods should not be too high
private async Task RunTimer(CancellationToken cancellationToken = default)
private async Task RunTimer(long startTicks, CancellationToken cancellationToken = default)
{
long nextPlannedExecution = _mockTimeSystem.TimeProvider.ElapsedTicks + _dueTime.Ticks;
long nextPlannedExecution = startTicks + _dueTime.Ticks;

if (_dueTime.TotalMilliseconds < 0)
{
Expand Down Expand Up @@ -330,6 +330,9 @@ private void Start()
}

CancellationToken token = runningCancellationTokenSource.Token;
// Read the clock on the calling thread, as the due time is relative to the call; reading it
// on the background thread would miss a time change before that thread is scheduled.
long startTicks = _mockTimeSystem.TimeProvider.ElapsedTicks;
using ManualResetEventSlim startCreateTimerThreads = new();
_ = Task.Run(
async () =>
Expand All @@ -344,7 +347,7 @@ private void Start()
// Ignore any ObjectDisposedException
}

await RunTimer(token).ConfigureAwait(false);
await RunTimer(startTicks, token).ConfigureAwait(false);
},
cancellationToken: token)
.ContinueWith(_ =>
Expand Down
Loading