测试驱动非同步任务 [英] test driven asynch tasks

查看:185
本文介绍了测试驱动非同步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要找的总体思路和/或一般的主题链接,虽然我此刻的具体动机正在使用BackgroundWorker的和/或TPL进展报告相关的UI任务。我与一般的异步编程经验水平是新手。测试工具,我知道最好的是NUnit的和犀牛。

有些头脑风暴的想法从我的头顶:


  1. 请不要打扰 - 它太复杂,你只是风测试BGW或TPL。

  2. 请一些假的或模拟的。

  3. 使用EventWaitHandles


解决方案

单元测试异步code不是世界上最简单的事情,因为我为我的 Nito.Async 库。 :)

首先,你要确定你真正想要测试的内容。你只是想测试是否执行异步操作,或者你想确保你的BGW /任务是否正常同步其UI进度报告?

测试动作是pretty简单:只需等待,直到操作完成,然后再检查后置条件。 (但是,要知道,BGW RunWorkerCompleted 将在线程池螺纹上调,除非你给它像一个同步上下文下面的例子)。

测试适当的同步(例如,每件code是在正确的线程中运行)是比较复杂的。

对于每一个测试,你需要建立一个同步上下文。这将是嘲讽UI同步上下文。我Nito.Async库可能与帮助;它有一个 ActionThread 这是一个独立的线程,包含适用于拥有EAP组件(例如,BGW)和调度任务(同步环境如的TaskScheduler .FromCurrentSynchronizationContext )。

它可以像这样使用(使用MSTest的例子):

  [TestMethod的]
公共无效测试()
{
  使用(ActionThread线程=新ActionThread())
  {
    thread.Start();    //传递做任何事情在ActionThread被执行。
    thread.Do(()=>
    {
      //创建BGW或致电TaskScheduler.FromCurrentSynchronizationContext。
    });    //线程是在使用块结束优雅退出。
  }
}

我觉得 Thread.CurrentThread.ManagedThreadId Thread.CurrentThread.IsThreadPoolThread 要检查的最简单方法正确的同步。如果您的测试code是从内部<​​code> ActionThread.Do 运行,那么就应该同步进度更新(并完成通知)到 ActionThread

很多Nito.Async单元测试使用 ActionThread 以这种方式,所以你可以看看那里的各种例子。

I am looking for general thoughts and/or links on the topic in general, although my specific motivation at the moment are UI tasks related to progress reporting using either BackgroundWorker and / or TPL. My experience level with async programming in general is novice. The testing tools I know best are NUnit and Rhino.

Some brain storming ideas off the top of my head:

  1. Don't bother - it's too complicated and you just wind up testing the BGW or TPL.
  2. Make some sort of fake or mock.
  3. Use EventWaitHandles

解决方案

Unit testing asynchronous code is not the simplest thing in the world, as I learned when writing unit tests for my Nito.Async library. :)

First, you want to define what you actually want to test. Do you just want to test whether the asynchronous action is performed, or do you want to ensure that your BGW/tasks are properly synchronizing their UI progress reporting?

Testing the action is pretty straightforward: just wait until the action is complete and then check for postconditions. (However, be aware that the BGW RunWorkerCompleted will be raised on a ThreadPool thread unless you give it a synchronization context like the example below).

Testing proper synchronization (e.g., that each piece of code is running on the correct thread) is more complex.

For each test you'll need to establish a synchronization context. This will be mocking the UI synchronization context. My Nito.Async library may help with that; it has an ActionThread which is a separate thread that contains a synchronization context suitable for owning EAP components (e.g., BGW) and scheduling tasks (e.g., TaskScheduler.FromCurrentSynchronizationContext).

It can be used like this (using an MSTest example):

[TestMethod]
public void Test()
{
  using (ActionThread thread = new ActionThread())
  {
    thread.Start();

    // Anything passed to Do is executed in that ActionThread.
    thread.Do(() =>
    {
      // Create BGW or call TaskScheduler.FromCurrentSynchronizationContext.
    });

    // The thread is gracefully exited at the end of the using block.
  }
}

I find Thread.CurrentThread.ManagedThreadId and Thread.CurrentThread.IsThreadPoolThread to be the easiest ways to check for proper synchronization. If your test code is run from within ActionThread.Do, then it should synchronize its progress updates (and completion notification) to that ActionThread.

A lot of the Nito.Async unit tests use ActionThread in this manner, so you could look there for various examples.

这篇关于测试驱动非同步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆