当在它之前创建 Form 时,其中包含 Application 循环的 NUnit 测试挂起

Ale*_*hov 2 .net c# nunit winforms async-await

我对用 MessageLoopWorker 包装的 WebBrowser 控件进行了一些测试,如下所述:WebBrowser Control in a new thread

但是当另一个测试创建用户控件或表单时,测试会冻结并且永远不会完成:

    [Test]
    public async Task WorksFine()
    {
        await MessageLoopWorker.Run(async () => new {});
    }

    [Test]
    public async Task NeverCompletes()
    {
        using (new Form()) ;
        await MessageLoopWorker.Run(async () => new {});
    }

    // a helper class to start the message loop and execute an asynchronous task
    public static class MessageLoopWorker
    {
        public static async Task<object> Run(Func<object[], Task<object>> worker, params object[] args)
        {
            var tcs = new TaskCompletionSource<object>();

            var thread = new Thread(() =>
            {
                EventHandler idleHandler = null;

                idleHandler = async (s, e) =>
                {
                    // handle Application.Idle just once
                    Application.Idle -= idleHandler;

                    // return to the message loop
                    await Task.Yield();

                    // and continue asynchronously
                    // propogate the result or exception
                    try
                    {
                        var result = await worker(args);
                        tcs.SetResult(result);
                    }
                    catch (Exception ex)
                    {
                        tcs.SetException(ex);
                    }

                    // signal to exit the message loop
                    // Application.Run will exit at this point
                    Application.ExitThread();
                };

                // handle Application.Idle just once
                // to make sure we're inside the message loop
                // and SynchronizationContext has been correctly installed
                Application.Idle += idleHandler;
                Application.Run();
            });

            // set STA model for the new thread
            thread.SetApartmentState(ApartmentState.STA);

            // start the thread and await for the task
            thread.Start();
            try
            {
                return await tcs.Task;
            }
            finally
            {
                thread.Join();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

代码很好地介入,除了return await tcs.Task;永远不会返回。

包装new Form到 MessageLoopWorker.Run(...) 似乎使它更好,但不幸的是,它不适用于更复杂的代码。我对表单和用户控件进行了很多其他测试,我希望避免将它们包装到 messageloopworker 中。

也许可以修复 MessageLoopWorker 以避免干扰其他测试?

更新:按照@Noseratio 的惊人回答,我在 MessageLoopWorker.Run 调用之前重置了同步上下文,现在运行良好。

更有意义的代码:

[Test]
public async Task BasicControlTests()
{
  var form = new CustomForm();
  form.Method1();
  Assert....
}

[Test]
public async Task BasicControlTests()
{
    var form = new CustomForm();
    form.Method1();
    Assert....
}

[Test]
public async Task WebBrowserExtensionTest()
{
    SynchronizationContext.SetSynchronizationContext(null);

    await MessageLoopWorker.Run(async () => {
        var browser = new WebBrowser();
        // subscribe on browser's events
        // do something with browser
        // assert the event order
    });
}
Run Code Online (Sandbox Code Playgroud)

当运行测试而不将同步上下文设为 null 时,WebBrowserExtensionTest 会在遵循 BasicControlTests 时阻塞。归零它通过得很好。

保持这样可以吗?

nos*_*tio 5

我在 MSTest 下对此进行了复制,但我相信以下所有内容同样适用于 NUnit。

首先,我知道这段代码可能已经脱离了上下文,但照原样,它似乎不是很有用。为什么要在 中创建一个表单NeverCompletes,该表单在随机 MSTest/NUnit 线程上运行,与由 产生的线程不同MessageLoopWorker

无论如何,您遇到了死锁,因为在原始单元测试线程上using (new Form())安装了一个实例WindowsFormsSynchronizationContextSynchronizationContext.Currentusing声明之后检查。然后,您将面临 Stephen Cleary 在他的“Don't Block on Async Code”中很好地解释的经典僵局。

是的,您不会阻塞自己,但是 MSTest/NUnit 会阻塞,因为它足够聪明,可以识别方法的async Task签名,NeverCompletes然后Task.Wait在它Task返回的方法上执行类似的操作。因为原始单元测试线程没有消息循环并且不泵送消息(与 预期不同WindowsFormsSynchronizationContext),await内部的延续NeverCompletes永远没有机会执行,Task.Wait只是挂起等待。

也就是说,MessageLoopWorker仅设计用于您传递给方法的范围内创建和运行WinForms对象,然后完成。例如,以下不会阻塞:asyncMessageLoopWorker.Run

[TestMethod]
public async Task NeverCompletes()
{
    await MessageLoopWorker.Run(async (args) =>
    {
        using (new Form()) ;
        return Type.Missing;
    });
}
Run Code Online (Sandbox Code Playgroud)

不是WinForms跨多个MessageLoopWorker.Run调用处理对象设计的。如果这就是您所需要的,您可能想MessageLoopApartment这里查看我的内容,例如:

[TestMethod]
public async Task NeverCompletes()
{
    using (var apartment = new MessageLoopApartment())
    {
        // create a form inside MessageLoopApartment
        var form = apartment.Invoke(() => new Form {
            Width = 400, Height = 300, Left = 10, Top = 10, Visible = true });

        try
        {
            // await outside MessageLoopApartment's thread
            await Task.Delay(2000);

            await apartment.Run(async () =>
            {
                // this runs on MessageLoopApartment's STA thread 
                // which stays the same for the life time of 
                // this MessageLoopApartment instance

                form.Show();
                await Task.Delay(1000);
                form.BackColor = System.Drawing.Color.Green;
                await Task.Delay(2000);
                form.BackColor = System.Drawing.Color.Red;
                await Task.Delay(3000);

            }, CancellationToken.None);
        }
        finally
        {
            // dispose of WebBrowser inside MessageLoopApartment
            apartment.Invoke(() => form.Dispose());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,如果您不担心潜在的测试耦合,您甚至可以在多个单元测试方法中使用它,例如 (MSTest):

[TestClass]
public class MyTestClass
{
    static MessageLoopApartment s_apartment;

    [ClassInitialize]
    public static void TestClassSetup()
    {
        s_apartment = new MessageLoopApartment();
    }

    [ClassCleanup]
    public void TestClassCleanup()
    {
        s_apartment.Dispose();
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)

最后,既MessageLoopWorker没有MessageLoopApartment设计也没有设计用于WinForms不同线程上创建的对象(无论如何这几乎不是一个好主意)。您可以拥有任意数量的MessageLoopWorker/MessageLoopApartment实例,但是一旦WinForm在特定MessageLoopWorker/MessageLoopApartment实例的线程上创建了一个对象,就应该进一步访问它并仅在同一线程上正确销毁它。