我应该如何对线程代码进行单元测试? [英] How should I unit test threaded code?

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

问题描述

到目前为止,我避免了测试多线程代码的噩梦,因为它似乎是雷区中的太多内容.我想问一下人们如何去测试依赖于线程的代码才能成功执行,或者人们如何去测试那些仅在两个线程以给定方式交互时才会出现的问题?

I have thus far avoided the nightmare that is testing multi-threaded code since it just seems like too much of a minefield. I'd like to ask how people have gone about testing code that relies on threads for successful execution, or just how people have gone about testing those kinds of issues that only show up when two threads interact in a given manner?

对于当今的程序员来说,这似乎是一个非常关键的问题,将我们的知识集中在这一恕我直言上将很有用.

This seems like a really key problem for programmers today, it would be useful to pool our knowledge on this one imho.

推荐答案

看,没有简单的方法可以做到这一点.我正在开发一个本质上是多线程的项目.事件来自操作系统,我必须同时处理它们.

Look, there's no easy way to do this. I'm working on a project that is inherently multithreaded. Events come in from the operating system and I have to process them concurrently.

处理测试复杂的多线程应用程序代码的最简单方法是:如果太复杂而无法测试,则说明您做错了.如果您有一个实例,该实例具有作用于其上的多个线程,并且您无法测试这些线程彼此间步调一致的情况,则需要重新设计.它既简单又复杂.

The simplest way to deal with testing complex, multithreaded application code is this: If its too complex to test, you're doing it wrong. If you have a single instance that has multiple threads acting upon it, and you can't test situations where these threads step all over each other, then your design needs to be redone. Its both as simple and as complex as this.

为多线程编程的方法有很多,可以避免线程同时在实例中运行.最简单的是使所有对象不可变.当然,通常是不可能的.因此,您必须确定设计中线程与同一实例交互的那些位置,并减少这些位置的数量.通过这样做,您隔离了实际发生多线程的几个类,从而降低了测试系统的总体复杂性.

There are many ways to program for multithreading that avoids threads running through instances at the same time. The simplest is to make all your objects immutable. Of course, that's not usually possible. So you have to identify those places in your design where threads interact with the same instance and reduce the number of those places. By doing this, you isolate a few classes where multithreading actually occurs, reducing the overall complexity of testing your system.

但是您必须意识到,即使这样做,您仍然无法测试所有两个线程相互踩踏的情况.为此,您必须在同一测试中同时运行两个线程,然后精确控制它们在任何给定时刻执行的行.您能做的最好的就是模拟这种情况.但这可能需要您专门为测试编写代码,而这仅是迈向真正解决方案的一半.

But you have to realize that even by doing this you still can't test every situation where two threads step on each other. To do that, you'd have to run two threads concurrently in the same test, then control exactly what lines they are executing at any given moment. The best you can do is simulate this situation. But this might require you to code specifically for testing, and that's at best a half step towards a true solution.

测试代码中是否存在线程问题的最佳方法可能是对代码进行静态分析.如果您的线程代码没有遵循一组有限的线程安全模式,那么您可能会遇到问题.我相信VS中的代码分析确实包含一些线程知识,但可能不多.

Probably the best way to test code for threading issues is through static analysis of the code. If your threaded code doesn't follow a finite set of thread safe patterns, then you might have a problem. I believe Code Analysis in VS does contain some knowledge of threading, but probably not much.

看起来,就目前而言(可能会是一个好时机),测试多线程应用程序的最佳方法是尽可能降低线程代码的复杂性.尽量减少线程交互的区域,尽可能地进行测试,并使用代码分析来识别危险区域.

Look, as things stand currently (and probably will stand for a good time to come), the best way to test multithreaded apps is to reduce the complexity of threaded code as much as possible. Minimize areas where threads interact, test as best as possible, and use code analysis to identify danger areas.

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

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