单元测试多线程应用程序? [英] Unit testing a multithreaded application?

查看:293
本文介绍了单元测试多线程应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有什么建议以一致的方式进行单元测试多线程应用程序?我做了一个应用程序,其中我们的模拟工作线程曾与被公共成员变量指定一个时间Thread.sleep代码。我们会用这个,所以我们可以设置一个特定的线程需要多少时间来完成工作,那么,我们可以做我们的断言。更好的办法的任何想法做到这一点?任何好的模拟框架的.Net可以处理呢?

Does anyone have any advice for a consistent way to unit test a multithreaded application? I have done one application where our mock "worker threads" had a thread.sleep with a time that was specified by a public member variable. We would use this so we could set how long a particular thread would take to complete its work, then we could do our assertions. Any ideas of a better way to do this? Any good mock frameworks for .Net that can handle this?

推荐答案

我的建议是不要依赖单元测试,以检测并发问题有以下几个原因:

My advice would be not to rely on unit tests to detect concurrency issues for several reasons:

  • 在缺乏可重复性:该测试将在一段时间失败只有一次,也不会是很有益的,以发现问题
  • 在不稳定的失败构建会惹恼每个人都在球队 - 因为最后一次提交会一直被错误地怀疑为是失败构建的原因
  • 在遇到死锁时,可能会冻结构建,直到执行超时遇到能显著减慢构建。
  • 在构建环境可能是一个单一的CPU环境(想筹建正在运行的虚拟机),其中并发问题可能永远不会发生 - 无论有多少睡眠时间设置
  • 这违背某种程度上具有简单,独立的单位验证code的的的想法。
  • Lack of reproducibility: the tests will fail only once in a while, and won't be really helpful to pinpoint the problems.
  • Erratic failing build will annoy everybody in the team - because the last commit will always be wrongly suspected for being the cause of the failing build.
  • Deadlocks when encountered are likely to freeze the build until the execution timeout is encountered which can significantly slow down the build.
  • The build environment is likely to be a single CPU environment (think build being run in a VM) where concurrency issues may never happen - no matter how much sleeping time is set.
  • It defeats somehow the idea of having simple, isolated units of validating code.

这篇关于单元测试多线程应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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