Junit4单元测试 (二) GroboUtils进行多线程测试

    @Test
    public void testCreateCommentsThreads() throws Throwable{
        //Runner数组,相当于并发多少个线程。
        TestRunnable[] trs = new TestRunnable [100];
        for(int i=0;i<100;i++){
            trs[i]=new createCommentsThread();
        }

        // 执行多线程测试用例的Runner
        MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);

        // 执行所有线程
        mttr.runTestRunnables();
    }

    // 自定义的线程
    private class createCommentsThread extends TestRunnable {
        @Override
        public void runTest() throws Throwable {
            // 测试内容
            createComments();
        }
    }

    // 测试方法
    public void createComments() throws Exception {

    }

TestRunnable.class创建自定义测试线程

MultiThreadedTestRunner.class执行多线程测试用例的Runner

public MultiThreadedTestRunner(TestRunnable[] tr) {
        this(tr, (TestRunnable[])null);
    }

runTestRunnables方法执行线程

 public void runTestRunnables(long maxTime) throws Throwable {
        Thread.interrupted();
        this.exception = null;
        this.coreThread = Thread.currentThread();
        this.threadGroup = new ThreadGroup(THIS_CLASS_NAME);
        this.threadsFinished = false;
        Thread[] monitorThreads = this.setupThreads(this.threadGroup, this.monitors);
        Thread[] runnerThreads = this.setupThreads(this.threadGroup, this.runners);

        boolean threadsStillRunning;
        try {
            threadsStillRunning = this.joinThreads(runnerThreads, maxTime);
        } catch (InterruptedException var21) {
            threadsStillRunning = true;
        } finally {
            Object var9 = this.synch;
            synchronized(var9) {
                if(!this.threadsFinished) {
                    this.interruptThreads();
                } else {
                    LOG.debug("All threads finished within timeframe.");
                }

            }
        }

        if(threadsStillRunning) {
            LOG.debug("Halting the test threads.");
            this.setTimeoutError(maxTime);

            try {
                this.joinThreads(runnerThreads, this.maxFinalJoinTime);
            } catch (InterruptedException var20) {
                ;
            }

            int killCount = this.killThreads(runnerThreads);
            if(killCount > 0) {
                LOG.fatal(killCount + " thread(s) did not stop themselves.");
                this.setTimeoutError(this.maxFinalJoinTime);
            }
        }

        LOG.debug("Halting the monitor threads.");

        try {
            this.joinThreads(monitorThreads, this.maxFinalJoinTime);
        } catch (InterruptedException var19) {
            ;
        }

        this.killThreads(monitorThreads);
        if(this.exception != null) {
            LOG.debug("Exception occurred during testing.", this.exception);
            throw this.exception;
        } else {
            LOG.debug("No exceptions caused during execution.");
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值