Repository navigation
Description
From a test ergonomics point of view, having the backward-compatible TimeProvider extensions methods run continuations asynchronously is bad for business.
I have a ManualTimeProvider implementation I am currently using included in https://github.com/egil/TimeScheduler, where I ported the TimeProvider type to .NET 6 as well as the TimeProviderTaskExtensions type. However, in my implementation, I changed the following:
Line 18 in 277a28d
| public DelayState() : base(TaskCreationOptions.RunContinuationsAsynchronously) {} |
to
public DelayState() : base() { }This allows me to write the following test:
[Fact]
public void Callbacks_runs_synchronously()
{
// arrange
var sut = new ManualTimeProvider();
var callbackCount = 0;
_ = Continuation(sut, () => callbackCount++);
// act
sut.ForwardTime(TimeSpan.FromSeconds(10));
// assert
callbackCount.Should().Be(1);
static async Task Continuation(TimeProvider timeProvider, Action callback)
{
await timeProvider.Delay(TimeSpan.FromSeconds(10));
callback();
}
}With the original version that uses TaskCreationOptions.RunContinuationsAsynchronously, I have to add a Task.Yield() or similar into the test after forwarding time, otherwise the test will fail.
[Fact]
public async void Callbacks_runs_asynchronously()
{
// arrange
var sut = new ManualTimeProvider();
var callbackCount = 0;
_ = Continuation(sut, () => callbackCount++);
// act
sut.ForwardTime(TimeSpan.FromSeconds(10));
+ await Task.Yield();
// assert
callbackCount.Should().Be(1);
static async Task Continuation(TimeProvider timeProvider, Action callback)
{
await timeProvider.Delay(TimeSpan.FromSeconds(10));
callback();
}
}It is a small change, but it does make the test less readable, adds noise, and more importantly, makes the test non-deterministic.
cc. @tarekgh.
Activity
tarekgh commentedon Apr 25, 2023
CC @stephentoub
egil commentedon May 19, 2023
I have continued to experiment with this, and I do think there is a difference here that needs to be addressed after trying out the preview 4 release of .NET 8 and the back-compatible package
Microsoft.Bcl.TimeProvider.These two tests fail on
.NET6andnet standard 2.0which uses the down-level packageMicrosoft.Bcl.TimeProvider, but passes on .NET 8 which does not.The tests pass in both .NET 8 and .NET 6 if I add a
await Task.Delay(1)right aftersut.ForwardTime(TimeSpan.FromSeconds(10));.I think they should behave the same, but I recognize that there may be things related to how I have implemented my
ManualTimerthat could influence things. Here is the implementation of that and myManualTimeProvider.egil commentedon May 19, 2023
Perhaps related: A similar issue is visible with
PeriodicTimer, although that does only apply to PeriodicTimer that is supported in .NET 8.The following test code will fail if
periodicTimer.WaitForNextTickAsync(CancellationToken.None)is not followed by.ConfigureAwait(false)in the helper methodWaitForNextTickInLoopbelow the test.tarekgh commentedon May 19, 2023
Thanks @egil for the info, this is very helpful. We'll look at this issue at some point.
pharring commentedon May 21, 2023
Suggestion: Make ManualTimeProvider.ForwardTime async (Task-returning) and have it yield.
egil commentedon May 22, 2023
@pharring I tried adding a
Thread.Sleep(1);inForwardTimeand that does makeDelay_callbacks_runs_synchronouslyabove pass in .NET 6 pass as well.However, if I also change
Delay_callbacks_runs_synchronouslyto emulate a long-running operation after theDelaycall, it only passes in .NET 8 and not in .NET 6.[Fact] public async Task Delay_callbacks_runs_synchronously() { // arrange var sut = new ManualTimeProvider(); var callbackCount = 0; var continuationTask = Continuation(sut, () => callbackCount++); // act sut.ForwardTime(TimeSpan.FromSeconds(10)); // assert callbackCount.Should().Be(1); await continuationTask; static async Task Continuation(TimeProvider timeProvider, Action callback) { await timeProvider.Delay(TimeSpan.FromSeconds(10)); + // emulate stuff taking a long time + Thread.Sleep(100); callback(); } }So adding a sleep or yield in
ForwardTimemoves the goal post, but it doesn't solve the problem that there is a difference in behavior between .NET 8 and older versions.