原标题:测试案例:新的方法使用JUnit测试多线程代码
下面是一个简单的例子,给大家讲讲如何使用Junit测试多线程Java代码。假设我们创建一个可以同时使用的计数器(counter)。因此,我们同时使用class Counter和JUnit测试TestCounter:
public class Counter { private int count=0; public void addOne() { count++; } public int getCount() { return count; } }
import static org.junit.Assert.assertEquals; import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; import com.anarsoft.vmlens.concurrent.junit.ConcurrentTestRunner; @RunWith(ConcurrentTestRunner.class) public class TestCounter { private Counter counter = new Counter(); @Test public void addOne() { counter.addOne(); } @After public void testCount() { assertEquals("4 Threads running addOne in parallel should lead to 4" , 4 , counter); } }
通过使用RunWith注释,JUnit测试将运行在一个特殊的ConcurrentTestRunner。这个被注释为“Test”的测试运行器,有4个线程并行运行。
如果我们运行测试用例就会如下图:
我们有一个race condition可以访问field count。为了解决这个问题,我们将count声明为volatile,然后再次运行。
private volatile int count=0;
现在测试用例成功执行了。大部分时候都会运行成功。如果你运行测试用例很多次,会偶尔出现错误提示。想要知道怎么回事,我们可以在vmlens中启用“延迟同步单元测试(Delay synchronization for unit tests)”。
现在,你会看到以下异常:
java.lang.Asserti: 4 Threads running addOne in parallel should lead to 4 expected:<4> but was:<3> at org.junit.Assert.fail(Assert.java:88) at org.junit.Assert.failNotEquals(Assert.java:834) at org.junit.Assert.assertEquals(Assert.java:645) at TestCounter.testCount(TestCounter.java:21) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at com.anarsoft.vmlens.concurrent.junit.internal.InvokeListOfMethods.evaluate(InvokeListOfMethods.java:23) at com.anarsoft.vmlens.concurrent.junit.internal.ConcurrentStatement.evaluateStatement(ConcurrentStatement.java:12) at com.anarsoft.vmlens.concurrent.junit.ConcurrentTestRunner.evaluateStatement(ConcurrentTestRunner.java:212) at com.anarsoft.vmlens.concurrent.junit.ConcurrentTestRunner.runChildrenConcurrently(ConcurrentTestRunner.java:172) at com.anarsoft.vmlens.concurrent.junit.ConcurrentTestRunner.access$0(ConcurrentTestRunner.java:78) at com.anarsoft.vmlens.concurrent.junit.ConcurrentTestRunner$1.evaluate(ConcurrentTestRunner.java:72) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
count++并不是一个操作,而是一个6字节的代码操作。对于field count,包含一个读和一个写:
ALOAD 0: this DUP GETFIELD Counter.count : int ICONST_1 IADD PUTFIELD Counter.count : int
通过这三个操作之间的延迟,我们要确保两个线程并行执行这些操作。在并行执行时,计数器将一直小于4,有时是3,有时只有2。
为了解决这个问题,我们使用atomic方法。可以通过使用java.util.concurrent.atomic.AtomicInteger来完成:
import java.util.concurrent.atomic.AtomicInteger; public class Counter { private final AtomicInteger count= new AtomicInteger(); public void addOne() { count.incrementAndGet(); } public int getCount() { return count.get(); } }
现在测试用例成功了。在这个测试中,Test runner使用的concurrent-junit,race condition catcher使用的vmlens。
有用(0)没用(0)
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:慧都控件网 [http://www.evget.com]
本文地址:http://www.evget.com/article/2015/11/26/23065.html返回搜狐,查看更多
责任编辑: