Feature Request: task.ShouldComplete()
rettigcd opened this issue · comments
Many times my tests fail because an asynchronous task hangs because it is blocked by something it shouldn't be. It would be super-helpful to catch these issues (and not have all the tests hang) if we had an extension method as follows:
const int defaultWaitMs = 3000;
public static async Task ShouldComplete( this Task task, string taskDescription = "[Task]", int ms = defaultWaitMs ) {
TimeSpan waitTime = TimeSpan.FromMilliseconds( ms );
await task.WaitAsync( waitTime );
if(!task.IsCompletedSuccessfully)
throw new Exception( $"{taskDescription} did not complete in {waitTime}" );
}
An alternate method signatures might be:
ShouldCompleteIn( this Task task, int ms, string taskDescription="[Task]"
I started to create the PR but .WaitAsync(...) is not available. Maybe I should upgrade the project to .NET 7?
It looks to me like this feature already exists via .TimeoutAfter(...)