fix (接口自动化): 修复串行问题

This commit is contained in:
fit2-zhao 2021-09-30 11:15:03 +08:00 committed by fit2-zhao
parent 173532ee2f
commit c237e24935
3 changed files with 13 additions and 18 deletions

View File

@ -2,10 +2,11 @@ package io.metersphere.api.jmeter;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class MessageCache {
public static Map<String, ReportCounter> cache = new HashMap<>();
// 串行执行队列 KEY=报告ID VALUE=开始时间
public static Map<String, Long> executionQueue = new HashMap<>();
public static Map<String, Long> executionQueue = new ConcurrentHashMap<>();
}

View File

@ -1059,13 +1059,14 @@ public class ApiAutomationService {
public void run() {
List<String> reportIds = new LinkedList<>();
for (APIScenarioReportResult key : map.keySet()) {
apiScenarioReportMapper.insert(key);
reportIds.add(key.getId());
try {
apiScenarioReportMapper.insert(key);
reportIds.add(key.getId());
// 进入执行队列
MessageCache.executionQueue.put(key.getId(), System.currentTimeMillis());
Future<ApiScenarioReport> future = executorService.submit(new SerialScenarioExecTask(jMeterService, apiScenarioReportMapper, key.getId(), map.get(key), request));
ApiScenarioReport report = future.get();
Future<String> future = executorService.submit(new SerialScenarioExecTask(jMeterService, key.getId(), map.get(key), request));
future.get();
ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(key.getId());
// 如果开启失败结束执行则判断返回结果状态
if (request.getConfig().isOnSampleError()) {
if (report == null || !report.getStatus().equals("Success")) {

View File

@ -6,10 +6,6 @@ package io.metersphere.api.service.task;
import io.metersphere.api.dto.automation.RunScenarioRequest;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.MessageCache;
import io.metersphere.base.domain.ApiScenarioReport;
import io.metersphere.base.mapper.ApiScenarioReportMapper;
import io.metersphere.commons.constants.APITestStatus;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.LogUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree;
@ -19,14 +15,11 @@ import java.util.concurrent.Callable;
public class SerialScenarioExecTask<T> implements Callable<T> {
private RunScenarioRequest request;
private JMeterService jMeterService;
private ApiScenarioReportMapper apiScenarioReportMapper;
private HashTree hashTree;
ApiScenarioReport report = null;
private String id;
public SerialScenarioExecTask(JMeterService jMeterService, ApiScenarioReportMapper apiScenarioReportMapper, String id, HashTree hashTree, RunScenarioRequest request) {
public SerialScenarioExecTask(JMeterService jMeterService, String id, HashTree hashTree, RunScenarioRequest request) {
this.jMeterService = jMeterService;
this.apiScenarioReportMapper = apiScenarioReportMapper;
this.request = request;
this.hashTree = hashTree;
this.id = id;
@ -41,19 +34,19 @@ public class SerialScenarioExecTask<T> implements Callable<T> {
jMeterService.runSerial(id, hashTree, request.getReportId(), request.getRunMode(), request.getConfig());
}
while (MessageCache.executionQueue.containsKey(id)) {
long currentSecond = (System.currentTimeMillis() - MessageCache.executionQueue.get(id)) / 1000 / 60;
Thread.sleep(1000);
long time = MessageCache.executionQueue.get(id);
long currentSecond = (System.currentTimeMillis() - time) / 1000 / 60;
// 设置五分钟超时
if (currentSecond > 5) {
// 执行失败了恢复报告状态
report.setStatus(APITestStatus.Error.name());
apiScenarioReportMapper.updateByPrimaryKey(report);
break;
}
}
return (T) report;
return null;
} catch (Exception ex) {
LogUtil.error(ex.getMessage());
MSException.throwException(ex.getMessage());
ex.printStackTrace();
return null;
}
}