单元测试的线程安全的烦躁?线程、烦躁、单元测试、安全

2023-09-02 01:31:29 作者:欠莪一个拥抱

我写了一个类和许多单元测试,但我没有做它线程安全的。现在,我想使这个类线程安全的,但要证明这一点,并使用TDD的,我想在我开始重构写一些失败的单元测试。

I've written a class and many unit test, but I did not make it thread safe. Now, I want to make the class thread safe, but to prove it and use TDD, I want to write some failing unit tests before I start refactoring.

什么好办法做到这一点?

Any good way to do this?

我首先想到的是刚刚创建几个线程,让他们都使用了类以不安全的方式。做到这一点足够的时间与足够的线程,我一定会看到它打破。

My first thought is just create a couple threads and make them all use the class in an unsafe way. Do this enough times with enough threads and I'm bound to see it break.

推荐答案

有两种产品,可以帮助您有:

There are two products that can help you there:

微软象棋 Typemock赛车 Microsoft Chess Typemock Racer

这两种检查在code死锁(通过单元测试),我觉得国际象棋检查比赛条件为好。

Both check for deadlocks in your code (via unit test) and I think Chess checks for race conditions as well.

使用这两种工具是很容易 - 你写一个简单的单元测试和运行code几次,检查是否死锁/竞态条件有可能在你的code

Using both tools is easy - you write a simple unit test and the run your code several times and check if deadlocks/race conditions is possible in your code.

编辑: 谷歌已经发布了检查竞态条件在运行时(而不是在测试)调用的发新种族测试。 它不会找到所有的比赛条件,因为它只分析了当前的运行,而不是所有的类似的工具,可能的方案上面,但它可能会帮助你找到比赛状态,一旦它发生。

Google has released a tool that checks for race condition in runtime (not during tests) that called thread-race-test. it won't find all of the race conditions because it only analyse the current run and not all of the possible scenarios like the tool above but it might help you find the race condition once it happens.

更新: Typemock站点不再有一个链接到赛车手,并且它没有在过去的4年更新一次。我想这个项目被关闭了。

Update: Typemock site no longer had a link to Racer, and it was not been updated in the last 4 years. I guess the project was closed.