在下面的示例中,调用.Execute()ReactiveCommand 会挂起或创建死锁。为什么会发生这种情况,避免这种情况的最佳方法是什么?
该错误仅在调用 Dispatcher.CurrentDispatcher 时发生。不幸的是,不称其为显而易见的答案在更大的项目中不是一个选择。
我在项目中有nuget包reactiveui-core和reactiveui-winforms,都是v7.4.0。我正在使用 Resharper 从 Visual Studio 运行 nunit 测试。
该代码是一个 NUnit 测试夹具,注意 TimeoutAfterAsync 是一个帮助方法,用于在一定超时后取消测试,在没有这个包装器的情况下观察到行为
[TestFixture]
public class ReactiveCommandTests
{
private static async Task<bool> ExecuteCommand()
{
await Task.Delay(1000);
return true;
}
public static ReactiveCommand<Unit, bool> Command = ReactiveCommand.CreateFromTask(ExecuteCommand);
public static ReactiveCommand<Unit, bool> CommandOnTaskpoolScheduler = ReactiveCommand.CreateFromTask(ExecuteCommand, outputScheduler: RxApp.TaskpoolScheduler);
public static ReactiveCommand<Unit, bool> CommandAfterDispatcherInvoked = ReactiveCommand.CreateFromTask(ExecuteCommand);
[Test, Order(1)]
public async Task Test()
{
//THIS WORKS
try
{
await TimeoutAfterAsync(
Command.Execute(),
TimeSpan.FromSeconds(5),
"control");
}
catch (TimeoutException)
{
Assert.Fail("Control case timed out (not expected)");
}
}
[Test, Order(2)]
public async Task Test_CreateCommandAfterDispatcherCall()
{
//This line causes unwanted behaviour
var x = Dispatcher.CurrentDispatcher;
//THIS FAILS
try
{
await TimeoutAfterAsync(
CommandAfterDispatcherInvoked.Execute(),
TimeSpan.FromSeconds(5),
"after dispatcher creation");
}
catch (TimeoutException)
{
Assert.Fail("Executing commandAfterDispatcherInvoked timed out (expected, but not understood");
}
}
慕田峪4524236
相关分类