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();    }    }




原创粉丝点击