如何运行并发单元测试? [英] How to run concurrency unit test?

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

问题描述

如何使用junit执行并发测试?

How to use junit to run concurrency test?

假设我有一个类

public class MessageBoard
{
    public synchronized void postMessage(String message)
    {
        ....
    }

    public void updateMessage(Long id, String message)
    {
        ....
    }
}

我要同时测试对这个postMessage的多个访问。
对此有什么建议?我想对所有我的setter函数(或任何涉及create / update / delete操作的方法)运行这种并发测试。

I wan to test multiple access to this postMessage concurrently. Any advice on this? I wish to run this kind of concurrency test against all my setter functions (or any method that involves create/update/delete operation).

推荐答案

很遗憾,我不相信您可以通过运行时测试来确定您的代码是否是线程安全的。你可以抛出任意多的线程,因为它,它可能/可能不会通过,具体取决于调度。

Unfortunately I don't believe you can definitively prove that your code is thread-safe by using run-time testing. You can throw as many threads as you like against it, and it may/may not pass depending on the scheduling.

也许您应该查看一些静态分析工具,例如 PMD ,可以确定您如何使用同步和识别使用问题。

Perhaps you should look at some static analysis tools, such as PMD, that can determine how you're using synchronisation and identify usage problems.

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

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