Junit4 多線程運行測試用例

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章