Junit测试并发
我正在尝试通过多个线程访问时测试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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
您永远无法真正运行测试来检查并发问题。事实上,在特定的测试机器上(在给定的操作系统上,具有一定数量的内核或处理器,甚至只是同时运行的其他进程)上没有出现问题,并不意味着不存在问题。
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.