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

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

问题描述

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

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 it's 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. It's 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天全站免登陆