对多线程应用程序进行单元测试
有人对多线程应用程序的单元测试有任何建议或了解任何框架吗?
Has anyone got any advice or know of any frameworks for unit-testing of multithreaded applications?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要对多线程应用程序进行单元测试。重构代码以消除不同线程中完成的工作之间的耦合。然后分别进行测试。
Do not unit test multithreaded applications. Refactor the code to remove coupling between work that is done in different threads. Then test it separately.
通常,您不会对多线程应用程序的并发性进行单元测试,因为单元测试不可靠且不可重现 - 由于并发错误的性质,通常不可能编写始终失败或成功的单元测试,因此单元测试并发代码通常不会进行非常有用的单元测试。
相反,您可以像平常一样对应用程序的每个单线程组件进行单元测试,并依靠负载测试会话来识别并发问题。
也就是说,有一些用于测试并发应用程序的实验性负载测试框架,例如Microsoft CHESS - CHESS 重复运行给定的单元测试,并系统地探索并发测试的每种可能的交错。这使得您的单元测试可靠且可重复。
但目前 CHESS 仍处于实验阶段(可能无法与 JVM 一起使用)——目前坚持负载测试以消除并发问题。
Typically you don't unit test the concurrency of multi threaded applications as the unit tests aren't dependable and reproducible - because of the nature of concurrency bugs its not generally possible to write unit tests that consistently either fail or succeed, and so unit tests of concurrent code generally don't make very useful unit tests.
Instead you unit test each single threaded components of your application as normal and rely on load testing sessions to identify concurrency issues.
That said, there are some experimental load testing frameworks for testing concurrent applications, such as Microsoft CHESS - CHESS repeatedly runs a given unit test and systematically explores every possible interleaving of a concurrent test. This makes your unit tests dependable and reproducible.
For the moment however CHESS is still experimental (and probably not usable with the JVM) - for now stick with load testing to weed out concurrency issues.
尝试使用 multithreadedTC
http://code.google.com/p/multithreadedtc/
http://code.google.com/p/multithreadedtc-junit4/
Try multithreadedTC
http://code.google.com/p/multithreadedtc/
http://code.google.com/p/multithreadedtc-junit4/