I am trying to write a helper method that allows me to pass an arbitrary task and a timeout. If the task completes before the timeout expires, the delegate is called successfully, otherwise the delegate is called with an error. The method is as follows:
public static async Task AwaitWithTimeout(Task task, int timeout, Action success, Action error)
{
if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
{
if (success != null)
{
success();
}
}
else
{
if (error != null)
{
error();
}
}
}
Now this seems to work most of the time, but I also wanted to write some tests to make sure. This test, to my surprise, fails and calls the delegate errors instead of success:
var taskToAwait = Task.Delay(1);
var successCalled = false;
await TaskHelper.AwaitWithTimeout(taskToAwait, 10, () => successCalled = true, null);
Assert.IsTrue(successCalled);
This test, however, is green:
var taskToAwait = Task.Run(async () =>
{
await Task.Delay(1);
});
var successCalled = false;
await TaskHelper.AwaitWithTimeout(taskToAwait, 10, () => successCalled = true, null);
Assert.IsTrue(successCalled);
How to make both tests green? Is my use of Task.WhenAny wrong?
source
share