测试此任务的正确方法是什么?
What is the correct way to test this Task?
我有一个使用任务重复工作的方法 async/await。
public static Task ToRepeatingWork(this Action work, int delayInMilliseconds)
{
Action action = async () =>
{
while (true)
{
try
{
work();
}
catch (MyException ex)
{
// Do Nothing
}
await TaskEx.Delay(new TimeSpan(0, 0, 0, 0, delayInMilliseconds));
}
};
return new Task(action, SomeCt, TaskCreationOptions.LongRunning);
}
我写了相应的测试:
[TestMethod, TestCategory("Unit")]
public async Task Should_do_repeating_work_and_rethrow_exceptions()
{
Action work = () =>
{
throw new Exception("Some other exception.");
};
var task = work.ToRepeatingWork(1);
task.Start();
await task;
}
我预计这个测试会失败,但它通过了(并使测试运行器崩溃)。
但是,如果在 ToRepeatingWork 方法中,我将操作从异步更改为正常操作并使用 Wait 而不是 await,测试将按预期运行。
TaskEx.Delay(new TimeSpan(0, 0, 0, 0, delayInMilliseconds)).Wait();
这里有什么问题?
您永远不应该使用任务构造函数。如果您有工作要放在线程池上,则使用 Task.Run
。这是一个问题,但不是导致崩溃的原因。
您还应避免使用 async void
,因此请使用 Func<Task>
而不是 Action
。这就是导致崩溃的原因。
public static Task ToRepeatingWork(this Action work, int delayInMilliseconds)
{
Func<Task> action = async () =>
{
while (true)
{
try
{
work();
}
catch (MyException ex)
{
// Do Nothing
}
await TaskEx.Delay(new TimeSpan(0, 0, 0, 0, delayInMilliseconds));
}
};
return Task.Run(() => action());
}
[TestMethod, TestCategory("Unit")]
public async Task Should_do_repeating_work_and_rethrow_exceptions()
{
Action work = () =>
{
throw new Exception("Some other exception.");
};
var task = work.ToRepeatingWork(1);
await task;
}
我有一个使用任务重复工作的方法 async/await。
public static Task ToRepeatingWork(this Action work, int delayInMilliseconds)
{
Action action = async () =>
{
while (true)
{
try
{
work();
}
catch (MyException ex)
{
// Do Nothing
}
await TaskEx.Delay(new TimeSpan(0, 0, 0, 0, delayInMilliseconds));
}
};
return new Task(action, SomeCt, TaskCreationOptions.LongRunning);
}
我写了相应的测试:
[TestMethod, TestCategory("Unit")]
public async Task Should_do_repeating_work_and_rethrow_exceptions()
{
Action work = () =>
{
throw new Exception("Some other exception.");
};
var task = work.ToRepeatingWork(1);
task.Start();
await task;
}
我预计这个测试会失败,但它通过了(并使测试运行器崩溃)。
但是,如果在 ToRepeatingWork 方法中,我将操作从异步更改为正常操作并使用 Wait 而不是 await,测试将按预期运行。
TaskEx.Delay(new TimeSpan(0, 0, 0, 0, delayInMilliseconds)).Wait();
这里有什么问题?
您永远不应该使用任务构造函数。如果您有工作要放在线程池上,则使用 Task.Run
。这是一个问题,但不是导致崩溃的原因。
您还应避免使用 async void
,因此请使用 Func<Task>
而不是 Action
。这就是导致崩溃的原因。
public static Task ToRepeatingWork(this Action work, int delayInMilliseconds)
{
Func<Task> action = async () =>
{
while (true)
{
try
{
work();
}
catch (MyException ex)
{
// Do Nothing
}
await TaskEx.Delay(new TimeSpan(0, 0, 0, 0, delayInMilliseconds));
}
};
return Task.Run(() => action());
}
[TestMethod, TestCategory("Unit")]
public async Task Should_do_repeating_work_and_rethrow_exceptions()
{
Action work = () =>
{
throw new Exception("Some other exception.");
};
var task = work.ToRepeatingWork(1);
await task;
}