线程安全性的单元测试? [英] Unit test for thread safe-ness?

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

问题描述

我写了一个类和许多单元测试,但我没有使它线程安全.现在,我想让类线程安全,但为了证明它并使用 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.

有什么好办法吗?

我的第一个想法是创建几个线程并让它们都以不安全的方式使用该类.用足够多的线程做足够多的时间,我一定会看到它坏掉.

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:

两者都检查代码中的死锁(通过单元测试),我认为 Chess 也会检查竞争条件.

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

使用这两种工具很容易 - 您编写一个简单的单元测试并多次运行您的代码并检查您的代码中是否可能出现死锁/竞争条件.

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 发布了一个工具,可以在运行时(而不是在测试期间)检查竞争条件,该工具称为 线程竞争测试.
它不会找到所有的竞争条件,因为它只分析当前的运行,而不是像上面的工具那样分析所有可能的场景,但它可能会帮助您找到发生的竞争条件.

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 站点不再有 Racer 的链接,并且在过去 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.

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

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