using Microsoft.Extensions.Time.Testing;
namespace Cleanuparr.Infrastructure.Tests.TestHelpers;
///
/// Clock control for handlers that wait on a .
///
public static class FakeTimeProviderExtensions
{
private static readonly TimeSpan Step = TimeSpan.FromSeconds(15);
private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(10);
private const int MaxAdvances = 5;
///
/// Awaits a running handler, advancing the clock past the delays it waits on.
///
///
/// A handler registers its timer asynchronously, so a single advance can land before the timer
/// exists and leave it waiting for a due time the clock has already passed. Advancing a few
/// times covers that race, and the cap keeps the clock close enough for file age assertions.
///
/// The handler did not finish after the last advance.
public static async Task AdvanceUntilCompleted(this FakeTimeProvider timeProvider, Task execution)
{
for (int advance = 0; advance < MaxAdvances && !execution.IsCompleted; advance++)
{
timeProvider.Advance(Step);
await Task.Delay(20);
}
await execution.WaitAsync(Timeout);
}
}