JUnit 实现多并发
来源:互联网 发布:电脑魔方计时器软件 编辑:程序博客网 时间:2024/05/23 02:05
1.pom 文件
<dependency> <groupId>net.sourceforge.groboutils</groupId> <artifactId>groboutils-core</artifactId> <version>5</version> </dependency>
2.代码实现
2.1 MultiThreadedRunner.java
package com.junit.test;import java.util.concurrent.atomic.AtomicInteger;import org.junit.runner.notification.RunNotifier;import org.junit.runners.BlockJUnit4ClassRunner;import org.junit.runners.model.FrameworkMethod;import org.junit.runners.model.InitializationError;import org.junit.runners.model.Statement;public class MultiThreadedRunner extends BlockJUnit4ClassRunner { private AtomicInteger numThreads; public static int maxThreads = 1000; public MultiThreadedRunner (Class<?> klass) throws InitializationError { super (klass); numThreads = new AtomicInteger(0); } @Override protected void runChild(final FrameworkMethod method, final RunNotifier notifier) { while (numThreads.get() > maxThreads) { try { Thread.sleep(1000); } catch (InterruptedException e) { System.err.println ("Interrupted: " + method.getName()); e.printStackTrace(); return; // The user may have interrupted us; this won't happen normally } } numThreads.incrementAndGet(); // 用线程执行父类runChild(method, notifier) new Thread (new Test(method, notifier)).start(); } @Override protected Statement childrenInvoker(final RunNotifier notifier) { return new Statement() { @Override public void evaluate() throws Throwable { MultiThreadedRunner.super.childrenInvoker(notifier).evaluate(); // wait for all child threads (tests) to complete while (numThreads.get() > 0) { Thread.sleep(1000); } } }; } class Test implements Runnable { private final FrameworkMethod method; private final RunNotifier notifier; public Test (final FrameworkMethod method, final RunNotifier notifier) { this.method = method; this.notifier = notifier; } @Override public void run () { System.err.println (method.getName()); MultiThreadedRunner.super.runChild(method, notifier); numThreads.decrementAndGet(); } }}
2.2 AutoMakSealTestDemo.java
package com.junit.test;import java.util.Date;import java.util.Random;import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;import net.sourceforge.groboutils.junit.v1.TestRunnable;import org.junit.Test;import org.junit.runner.RunWith;import cfca.sealclient.util.GUID;import example.servlet.AutoGenerateSealImageTest;import example.servlet.AutoMakeSealTest;import example.servlet.GetPdfParamsTest;import example.servlet.GetSealInfoTest;import junit.framework.TestCase;@RunWith(MultiThreadedRunner.class)public class AutoMakSealTestDemo extends TestCase { @Test public void testDoAutomakeSeal() throws Throwable { long start = new Date().getTime(); int threads = 2; TestRunnable[] trs = new TestRunnable [threads]; for(int i=0;i<threads;i++){ trs[i]=new ThreadDoAutoMakeSeal(); } // 用于执行多线程测试用例的Runner,将前面定义的单个Runner组成的数组传入 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs); // 开发并发执行数组里定义的内容 mttr.runTestRunnables(); System.out.println("执行"+threads+"次,共用时:["+(new Date().getTime() - start)+"]"); } private class ThreadDoAutoMakeSeal extends TestRunnable { @Override public void runTest() throws Throwable { // 测试内容 autoMakeSeal(); //getPdfParams(); //autoGenerateSealImage(); //GetSealInfo(); } } private void autoMakeSeal() throws Exception { AutoMakeSealTest test = new AutoMakeSealTest(); String sealCode =""; int max=2; int min=1; Random random = new Random(); int s = random.nextInt(max)%(max-min+1) + min; String customerType ="" + s; String identificationNo = GUID.generateId(); String userName = "test0"+GUID.getRandomNumber(4); test.doAutomakeSeal(sealCode,customerType,userName,identificationNo); System.out.println("===" + Thread.currentThread().getId() + "autoMakeSeal"); } private void getPdfParams() throws Exception{ GetPdfParamsTest test = new GetPdfParamsTest(); test.getPdfParams(); } private void autoGenerateSealImage() throws Exception{ AutoGenerateSealImageTest test = new AutoGenerateSealImageTest(); int max=2; int min=1; Random random = new Random(); int s = random.nextInt(max)%(max-min+1) + min; String type ="" + s; test.autoGenerateSealImage("1"); } private void GetSealInfo() throws Exception{ GetSealInfoTest test = new GetSealInfoTest(); test.GetSealInfo(); } }
阅读全文
0 0
- JUnit 实现多并发
- junit 测试用例多并发
- 使用junit进行并发测试
- JUnit最佳实现
- junit实现过程
- JUnit实现单元测试
- Jetty+JUnit实现单元测试
- 单元测试实现-JUnit
- junit实现android单元测试
- JUnit/Mockito实现单元测试
- 使用junit进行多线程并发测试
- junit并发访问数据库引发的问题
- Shell 实现多任务并发
- 实现并发多进程服务器端
- 多进程实现并发服务器
- Android junit实现多个Activity跳转测试
- Android junit实现多个Activity跳转测试
- Junit实现spring的单元测试
- B
- java -xml文件读取
- mysql左连接右连接(查询两张表不同的数据)
- tesseract 遇到的一些问题
- 开启子线程的方法(二)
- JUnit 实现多并发
- 查找 -- 红黑树的删除操作
- 001-js基础-变量提升练习题
- wx.toast 页面跳转延时的问题
- TCP/IP 小结
- 打开谷歌浏览器(chrome)线程lsass.exe的cpu占用高的解决办法
- <s:iterator>和<s:if>结合使用
- 【BzoJ 3319】【黑白树】【并查集】【”猥琐欲为“】
- java学习笔记-初探字节码和命令执行效率