Junit测试并发

发布于 2024-09-29 08:33:08 字数 1569 浏览 3 评论 0原文

我正在尝试通过多个线程访问时测试 java.util.concurrent.ConcurrentLinkedQueue 。下面提到的是我使用 RepeatedTest 在两个并发线程中运行的 Junit 测试。我的问题是:使用 RepeatedTest 测试并发性是否正确,例如在 ConcurrentLinkedQueue 上?下面提到了源代码。

谢谢

import java.util.concurrent.ConcurrentLinkedQueue;
import junit.extensions.ActiveTestSuite;
import junit.extensions.RepeatedTest;
import junit.extensions.TestSetup;
import junit.framework.TestCase;

public class TestNonBlockingConcurrentQueue extends TestCase{

private static ConcurrentLinkedQueue clq;

public void testPut() throws Exception {
    int messageCounter = 0;
    for(;messageCounter <10000; messageCounter++){
        clq.offer(messageCounter);
    }
    assertEquals(clq.size(), messageCounter);
}

public void testGet() throws Exception {
    while(!clq.isEmpty()){
        clq.poll();
    }
    assertEquals("Size should be zero", clq.size(), 0);
}

public static junit.framework.Test suite( ) {
    ActiveTestSuite ats = new ActiveTestSuite();

    TestSetup setup = new TestSetup(ats) {
       protected void setUp() throws Exception {
            System.out.println("Creating ConcurrentLinkedQueue..");
            clq = new ConcurrentLinkedQueue();
        }
        protected void tearDown(  ) throws Exception {
            clq = null;
        }
    };
    ats.addTest(new RepeatedTest(new TestNonBlockingConcurrentQueue("testPut"), 2));
    ats.addTest(new RepeatedTest(new TestNonBlockingConcurrentQueue("testGet"), 2));
    return setup;

}

public TestNonBlockingConcurrentQueue(String testName){
    super(testName);
}

I'm trying to test java.util.concurrent.ConcurrentLinkedQueue when accessed via multiple threads. Mentioned below is my Junit test using RepeatedTest to run in two concurrent threads. My questions is: is it correct to use RepeatedTest to test concurrency for example on ConcurrentLinkedQueue? The source code is mentioned below.

Thanks

import java.util.concurrent.ConcurrentLinkedQueue;
import junit.extensions.ActiveTestSuite;
import junit.extensions.RepeatedTest;
import junit.extensions.TestSetup;
import junit.framework.TestCase;

public class TestNonBlockingConcurrentQueue extends TestCase{

private static ConcurrentLinkedQueue clq;

public void testPut() throws Exception {
    int messageCounter = 0;
    for(;messageCounter <10000; messageCounter++){
        clq.offer(messageCounter);
    }
    assertEquals(clq.size(), messageCounter);
}

public void testGet() throws Exception {
    while(!clq.isEmpty()){
        clq.poll();
    }
    assertEquals("Size should be zero", clq.size(), 0);
}

public static junit.framework.Test suite( ) {
    ActiveTestSuite ats = new ActiveTestSuite();

    TestSetup setup = new TestSetup(ats) {
       protected void setUp() throws Exception {
            System.out.println("Creating ConcurrentLinkedQueue..");
            clq = new ConcurrentLinkedQueue();
        }
        protected void tearDown(  ) throws Exception {
            clq = null;
        }
    };
    ats.addTest(new RepeatedTest(new TestNonBlockingConcurrentQueue("testPut"), 2));
    ats.addTest(new RepeatedTest(new TestNonBlockingConcurrentQueue("testGet"), 2));
    return setup;

}

public TestNonBlockingConcurrentQueue(String testName){
    super(testName);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

打小就很酷 2024-10-06 08:33:08

JUnitPerf 使用 RepeatedTest 来测试并发代码,因此使用它对上面的测试执行相同的操作似乎是合理的,请参阅:

http://www.clarkware.com/software/JUnitPerf.html

还有其他方法可以对并发代码进行单元测试,尽管它们都不能真正验证您的代码是否是线程安全的:

另请参阅: 单元测试并发代码

JUnitPerf uses RepeatedTest to test concurrent code so it seems reasonable to use it to do the same thing with your test above, see:

http://www.clarkware.com/software/JUnitPerf.html

There are other methods for unit testing concurrent code, although none of them can really verify that your code is thread safe:

Also see: Unit Testing Concurrent Code

灼疼热情 2024-10-06 08:33:08

您永远无法真正运行测试来检查并发问题。事实上,在特定的测试机器上(在给定的操作系统上,具有一定数量的内核或处理器,甚至只是同时运行的其他进程)上没有出现问题,并不意味着不存在问题。

You can never really run tests to check concurrency problems. The fact that no problem shows up on a particular test machine (on a given OS, with a certain number of cores or processors, or even just other processes running at the same time) doesn't mean that there isn't a problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文