java使用线程池实现接口自动化中的并发测试

发布时间 2023-07-30 14:16:05作者: as阿水

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


public
void test() throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(2); Future<Throwable> future1 = executorService.submit(() -> { try { // 接口请求 return null; }catch (Throwable e){ return e; } }); Future<Throwable> future2 = executorService.submit(() -> { try { // 接口请求 return null; }catch (Throwable e){ return e; } }); Throwable exception1 = future1.get(); Throwable exception2 = future2.get(); Assert.assertTrue((exception1 == null && exception2 != null) || (exception1 != null && exception2 == null)); if (exception1 != null) { Assert.assertTrue(exception1 instanceof AssertionError); } if (exception2 != null) { Assert.assertTrue(exception2 instanceof AssertionError); } }