orleans icon indicating copy to clipboard operation
orleans copied to clipboard

Tests relied on `FakeTimeProvider` failed after upgrading to orleans 8.2.0

Open zeinali-ali opened this issue 1 year ago • 4 comments

In some of our grains we relied on TimeProvider and have written some time-based tests using Microsoft.Orleans.TestingHost but after upgrading to Orleans 8.2.0 it seems there are some unpredictable problems for those test which relied on FakeTimeProvider we are using this code siloBuilder.Services.AddSingleton<TimeProvider>(FakeTime); to setup a fake time-provider. and now we are facing some unpredictable problems like:

  1. Implicit subscription not work if we use siloBuilder.Services.AddSingleton<TimeProvider>(FakeTime) .
  2. In some cases FakeTime.SetUtcNow(someTime) took 10min to execute

zeinali-ali avatar Sep 01 '24 15:09 zeinali-ali

Orleans doesn't yet fully support testing with a fake TimeProvider. There are some places in the runtime where we have added support for TimeProvider, but it's not everywhere yet.

ReubenBond avatar Sep 01 '24 15:09 ReubenBond

Yes and that's exactly the problem, as you changed some part of runtime using TimeProvider when we replace TimeProvider with a fake one to test our Grains Behaviors the Orleans-Runtime also take affect and work in an unpredictable behavior. Isn't it a better practice to use Keyed-Service for TimeProvider in Orleans-Runtime to avoid conflict?

zeinali-ali avatar Sep 01 '24 15:09 zeinali-ali

@zeinali-ali perhaps Keyed DI would be useful in the interim before all timers are replaced. For now, are you able to use keyed DI to inject your fake time provider into your grains? Apologies for the hassle

ReubenBond avatar Sep 01 '24 15:09 ReubenBond

Sure, we can switch to Keyed-DI to avoid conflict. However we currently rolled-back to 8.1.0. I'm just suggesting that a Keyed-DI TimeProvider used in libraries and frameworks can be a better choice to avoid such a conflicts with other libraries and user codes

zeinali-ali avatar Sep 01 '24 15:09 zeinali-ali

I am now in the process of upgrading our application to .NET 9 which binds me to this partial implementation in Orleans. Is there a timeline for when Orleans will implement (keyed) TimeProvider instances consistently throughout the framework?

Thanks for your work on this!

KSlingerland avatar Jul 11 '25 12:07 KSlingerland

@KSlingerland are you able to use Keyed DI for TimeProvider at the application level?

ReubenBond avatar Jul 11 '25 15:07 ReubenBond

I was able to substitute with Keyed DI, it did complicate things a bunch with testing but it's a workaround for now. Thanks for the quick response!

KSlingerland avatar Jul 11 '25 16:07 KSlingerland