📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

How would you test time-dependent code in .NET, such as token expiry, retries with backoff or a scheduled job, without making tests slow or flaky?

Senior Theory Advanced distributed-systems dotnet testing time timeprovider

🎯 Key points

  • Inject TimeProvider (.NET 8+) as a singleton instead of DateTime.UtcNow, Task.Delay or Stopwatch
  • FakeTimeProvider (Microsoft.Extensions.TimeProvider.Testing): SetUtcNow and Advance
  • Pass the provider to Task.Delay, PeriodicTimer, CreateTimer and CancellationTokenSource
  • No Thread.Sleep or real waits in tests
  • Test boundaries: exactly at expiry, one tick before and after
  • Work in UTC; test local-time rules (daylight saving in Sydney or Melbourne) with SetLocalTimeZone

⏱ 30-second answer

Inject TimeProvider (registered as TimeProvider.System in DI) instead of calling DateTime.UtcNow or Task.Delay directly. In tests use FakeTimeProvider from Microsoft.Extensions.TimeProvider.Testing: set a start time, call Advance to move time forward, and timers, delays and PeriodicTimer created from it fire deterministically with no real waiting. Test the boundaries (exactly at expiry, one tick either side), keep business time in UTC, and cover daylight-saving rules with a fake local time zone.

🔍 What the interviewer is checking

Checks practical use of TimeProvider and FakeTimeProvider.

↩ Usually asked after

More in Distributed Systems Core Concepts

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me