Junit4 多线程运行测试用例

38 篇文章 0 订阅

Junit4 测试默认不能开启多线程,这里借助Groboutils Core可以实现多线程

下载jar包(不知道为什么我这边maven依赖不进去,只能导入jar包了):

https://mvnrepository.com/artifact/net.sourceforge.groboutils/groboutils-core/5

导入lib:

编写代码:

package com.wjj.application.utils; 

import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
import net.sourceforge.groboutils.junit.v1.TestRunnable;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.Before; 
import org.junit.After;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

/** 
* RedLockUtils Tester. 
* 锁测试
* @author hank 
* @since <pre>11/05/2019</pre> 
* @version 1.0 
*/ 
@RunWith(SpringRunner.class)
@SpringBootTest
//@ContextConfiguration("/bootstrap.properties")
@ActiveProfiles(value="local")
@Transactional
@Rollback(false)
public class RedLockUtilsTest {
    private String accountUnifiedorderLockKey = "ACCOUNT_PAY_UNIFIEDORDER_LOCK_KEY_TRADE_NO";

    @Autowired
    RedLockUtils redLockUtils;

    @Autowired
    DistributedLock distributedLock;

    @Before
    public void before() throws Exception {
    }

    @After
    public void after() throws Exception {
    }

    @Test
    public void testLock1() throws Throwable {
        int sum = 8;
        int Nsum = 100;
        TestRunnable[] testRunnable = new TestRunnable[sum+Nsum];
        for(int i=0; i< sum; i++){
            //锁1线程
            testRunnable[i] = new TestRunnable(){

                @Override
                public void runTest() throws Throwable {
                    System.out.println("-----"+Thread.currentThread().getName()+Thread.currentThread().getId()+ " "+ System.currentTimeMillis()/1000 +" start -----");
                    String lockKey = accountUnifiedorderLockKey + "unifiedorder" + "231123" + "sn1111125246546454564";
                    String identifier = distributedLock.lockWithTimeout(lockKey, 8000, 10000);
                    System.out.println("-----"+Thread.currentThread().getName()+Thread.currentThread().getId()+ " "+ System.currentTimeMillis()/1000+" lock :" + identifier + " -----");
                    Thread.sleep(2000);
                    if(StringUtils.isNotBlank(identifier)) {
                        distributedLock.releaseLock(lockKey, identifier);
                        System.out.println("-----"+Thread.currentThread().getName()+Thread.currentThread().getId()+ " "+ System.currentTimeMillis()/1000+" unlock -----");
                    }
                }
            };
        }

        for(int i=0; i< Nsum; i++){
            //锁2线程
            testRunnable[i+sum] = new TestRunnable(){

                @Override
                public void runTest() throws Throwable {
                    System.out.println("-----"+Thread.currentThread().getName()+Thread.currentThread().getId()+ " "+ System.currentTimeMillis()/1000 +" Nstart -----");
                    String lockKey = accountUnifiedorderLockKey + "unifiedorder" + "an465465464131232132" + "sn1111125246546454564";
                    boolean identifier = redLockUtils.tryLockTimeout(lockKey, 8000, 10000);
                    System.out.println("-----"+Thread.currentThread().getName()+Thread.currentThread().getId()+ " "+ System.currentTimeMillis()/1000+" Nlock :" + identifier + " -----");
                    Thread.sleep(2000);
                    if(identifier) {
                        redLockUtils.unLock(lockKey);
                        System.out.println("-----"+Thread.currentThread().getName()+Thread.currentThread().getId()+ " "+ System.currentTimeMillis()/1000+" Nunlock -----");
                    }

                }
            };
        }
       //加入并运行测试线程
       new MultiThreadedTestRunner(testRunnable).runTestRunnables();
    }
} 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值