fix(场景自动化) 增加部分输出便与判断集成进度

This commit is contained in:
fit2-zhao 2021-09-16 15:59:44 +08:00 committed by fit2-zhao
parent 744b9895d5
commit 305db60aea
4 changed files with 48 additions and 47 deletions

View File

@ -24,28 +24,33 @@ public class FixedTask {
if (MessageCache.cache != null && MessageCache.cache.size() > 0) { if (MessageCache.cache != null && MessageCache.cache.size() > 0) {
for (String key : MessageCache.cache.keySet()) { for (String key : MessageCache.cache.keySet()) {
ReportCounter counter = MessageCache.cache.get(key); ReportCounter counter = MessageCache.cache.get(key);
LogUtil.info("集成报告:【" + key + "】总执行场景:【" + counter.getReportIds().size() + "】已经执行完成场景:【" + counter.getNumber() + "");
// 合并 // 合并
if (counter.getNumber() == counter.getReportIds().size()) { if (counter.getNumber() == counter.getReportIds().size()) {
scenarioReportService.margeReport(key, counter.getReportIds()); scenarioReportService.margeReport(key, counter.getReportIds());
guardTask.remove(key); guardTask.remove(key);
MessageCache.cache.remove(key); MessageCache.cache.remove(key);
} else { } else {
int number = 0; try {
if (guardTask.containsKey(key)) { int number = 0;
number = guardTask.get(key); if (guardTask.containsKey(key)) {
guardTask.put(key, number++); number = guardTask.get(key);
} else { guardTask.put(key, number++);
guardTask.put(key, number); } else {
} guardTask.put(key, number);
if (CollectionUtils.isNotEmpty(counter.getPoolUrls()) && counter.getNumber() > 0 && guardTask.get(key) > 200) {
// 资源池中已经没有执行的请求了
int runningCount = scenarioReportService.get(key, counter);
if (runningCount == 0) {
LogUtil.info("发生未知异常,进行资源合并,请检查资源池是否正常运行");
scenarioReportService.margeReport(key, counter.getReportIds());
guardTask.remove(key);
MessageCache.cache.remove(key);
} }
if (CollectionUtils.isNotEmpty(counter.getPoolUrls()) && counter.getNumber() > 0 && guardTask.get(key) > 200) {
// 资源池中已经没有执行的请求了
int runningCount = scenarioReportService.get(key, counter);
if (runningCount == 0) {
LogUtil.info("发生未知异常,进行资源合并,请检查资源池是否正常运行");
scenarioReportService.margeReport(key, counter.getReportIds());
guardTask.remove(key);
MessageCache.cache.remove(key);
}
}
} catch (Exception ex) {
LogUtil.error(ex.getMessage());
} }
} }
} }

View File

@ -44,13 +44,14 @@ public class TestResult {
private List<ScenarioResult> scenarios = new ArrayList<>(); private List<ScenarioResult> scenarios = new ArrayList<>();
private Map<String, Boolean> margeScenariMap = new HashMap<>(); private Map<String, Boolean> margeScenarioMap = new HashMap<>();
private Map<String, Boolean> scenarioStepMap = new HashMap<>(); private Map<String, Boolean> scenarioStepMap = new HashMap<>();
private int scenarioStepSuccess = 0; private int scenarioStepSuccess = 0;
private int scenarioStepError = 0; private int scenarioStepError = 0;
private int scenarioStepTotal = 0; private int scenarioStepTotal = 0;
public void addError(int count) { public void addError(int count) {
this.error += count; this.error += count;
} }
@ -72,16 +73,16 @@ public class TestResult {
private void setStatus(List<String> id_names, boolean status) { private void setStatus(List<String> id_names, boolean status) {
if (CollectionUtils.isNotEmpty(id_names)) { if (CollectionUtils.isNotEmpty(id_names)) {
id_names.forEach(item -> { id_names.forEach(item -> {
if (!margeScenariMap.containsKey(item) || status) { if (!margeScenarioMap.containsKey(item) || status) {
margeScenariMap.put(item, status); margeScenarioMap.put(item, status);
} }
}); });
} }
} }
private void setStatus(String id_names, boolean status) { private void setStatus(String id_names, boolean status) {
if (!margeScenariMap.containsKey(id_names) || status) { if (!margeScenarioMap.containsKey(id_names) || status) {
margeScenariMap.put(id_names, status); margeScenarioMap.put(id_names, status);
} }
} }
@ -101,18 +102,18 @@ public class TestResult {
if (result != null && CollectionUtils.isNotEmpty(result.getRequestResults())) { if (result != null && CollectionUtils.isNotEmpty(result.getRequestResults())) {
result.getRequestResults().forEach(item -> { result.getRequestResults().forEach(item -> {
String itemAndScenarioName = ""; String itemAndScenarioName = "";
if (StringUtils.isNotEmpty(item.getScenario())) { if (StringUtils.isNotEmpty(item.getScenario())) {
//第1个当前场景 第all_id_names个最后一层场景 //第1个当前场景 第all_id_names个最后一层场景
List<String> all_id_names = JSON.parseObject(item.getScenario(), List.class); List<String> all_id_names = JSON.parseObject(item.getScenario(), List.class);
if(all_id_names.size()>1){ if (all_id_names.size() > 1) {
StringBuffer scenarioNames = new StringBuffer(); StringBuffer scenarioNames = new StringBuffer();
//因为要进行步骤统计第一层级下的场景算作步骤所以统计视角只按照第一级别场景来计算 //因为要进行步骤统计第一层级下的场景算作步骤所以统计视角只按照第一级别场景来计算
scenarioNames.append(all_id_names.get(all_id_names.size()-1)+all_id_names.get(all_id_names.size()-2)); scenarioNames.append(all_id_names.get(all_id_names.size() - 1) + all_id_names.get(all_id_names.size() - 2));
this.setStatus(scenarioNames.toString(), item.getError() > 0); this.setStatus(scenarioNames.toString(), item.getError() > 0);
itemAndScenarioName = scenarioNames.toString(); itemAndScenarioName = scenarioNames.toString();
}else{ } else {
//不存在多场景时需要补上步骤名字做唯一判断 添加UUID进行处理 //不存在多场景时需要补上步骤名字做唯一判断 添加UUID进行处理
itemAndScenarioName = item.getName()+":"+JSONArray.toJSONString(all_id_names.get(0))+ UUID.randomUUID().toString(); itemAndScenarioName = item.getName() + ":" + JSONArray.toJSONString(all_id_names.get(0)) + UUID.randomUUID().toString();
this.setStatus(all_id_names, item.getError() > 0); this.setStatus(all_id_names, item.getError() > 0);
} }
@ -124,9 +125,9 @@ public class TestResult {
subItem.setName(array[0]); subItem.setName(array[0]);
}); });
} else { } else {
this.genScenarioInSubReqeustResult(item); this.genScenarioInSubRequestResult(item);
} }
this.setStepStatus(itemAndScenarioName,item.getError()>0); this.setStepStatus(itemAndScenarioName, item.getError() > 0);
}); });
scenarios.add(result); scenarios.add(result);
} }
@ -134,23 +135,23 @@ public class TestResult {
* 1.10.2 场景成功/失败统计不再按照请求为纬度按照场景为纬度 * 1.10.2 场景成功/失败统计不再按照请求为纬度按照场景为纬度
*/ */
for (String key : scenarioStepMap.keySet()) { for (String key : scenarioStepMap.keySet()) {
if (scenarioStepMap .get(key)) { if (scenarioStepMap.get(key)) {
this.scenarioStepError++; this.scenarioStepError++;
} else { } else {
this.scenarioStepSuccess++; this.scenarioStepSuccess++;
} }
} }
boolean hasError = false; boolean hasError = false;
for (String key : margeScenariMap.keySet()) { for (String key : margeScenarioMap.keySet()) {
if (margeScenariMap.get(key)) { if (margeScenarioMap.get(key)) {
hasError = true; hasError = true;
break; break;
} }
} }
if(!margeScenariMap.isEmpty()){ if (!margeScenarioMap.isEmpty()) {
if(hasError){ if (hasError) {
this.scenarioError ++; this.scenarioError++;
}else { } else {
this.scenarioSuccess++; this.scenarioSuccess++;
} }
this.scenarioTotal++; this.scenarioTotal++;
@ -162,21 +163,21 @@ public class TestResult {
} }
//一般多层的事务控制器会出现这种情况 //一般多层的事务控制器会出现这种情况
private String genScenarioInSubReqeustResult(RequestResult item) { private String genScenarioInSubRequestResult(RequestResult item) {
if (StringUtils.isNotEmpty(item.getName()) && item.getName().indexOf(SEPARATOR) != -1) { if (StringUtils.isNotEmpty(item.getName()) && item.getName().indexOf(SEPARATOR) != -1) {
String array[] = item.getName().split(SEPARATOR); String array[] = item.getName().split(SEPARATOR);
item.setName(array[0]); item.setName(array[0]);
} }
if(StringUtils.isNotEmpty(item.getScenario())){ if (StringUtils.isNotEmpty(item.getScenario())) {
List<String> id_names = JSON.parseObject(item.getScenario(), List.class); List<String> id_names = JSON.parseObject(item.getScenario(), List.class);
this.setStatus(id_names, item.getError() > 0); this.setStatus(id_names, item.getError() > 0);
return item.getScenario(); return item.getScenario();
}else{ } else {
if(CollectionUtils.isNotEmpty(item.getSubRequestResults())){ if (CollectionUtils.isNotEmpty(item.getSubRequestResults())) {
for (RequestResult requestResult:item.getSubRequestResults()) { for (RequestResult requestResult : item.getSubRequestResults()) {
String result = this.genScenarioInSubReqeustResult(requestResult); this.genScenarioInSubRequestResult(requestResult);
} }
} }
return null; return null;

View File

@ -1098,6 +1098,7 @@ public class ApiAutomationService {
counter.setPoolUrls(request.getConfig().getTestResources()); counter.setPoolUrls(request.getConfig().getTestResources());
} }
counter.setReportIds(reportIds); counter.setReportIds(reportIds);
request.getConfig().setAmassReport(serialReportId);
MessageCache.cache.put(serialReportId, counter); MessageCache.cache.put(serialReportId, counter);
} }
} }

View File

@ -41,7 +41,6 @@ import io.metersphere.log.vo.api.ModuleReference;
import io.metersphere.notice.sender.NoticeModel; import io.metersphere.notice.sender.NoticeModel;
import io.metersphere.notice.service.NoticeSendService; import io.metersphere.notice.service.NoticeSendService;
import io.metersphere.service.UserService; import io.metersphere.service.UserService;
import io.metersphere.track.service.TestPlanReportService;
import org.apache.commons.beanutils.BeanMap; import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -292,7 +291,6 @@ public class ApiScenarioReportService {
returnReport = report; returnReport = report;
reportIds.add(report.getId()); reportIds.add(report.getId());
} }
// margeReport(result, scenarioIds, scenarioNames, runMode, projectId, userId, reportIds);
return returnReport; return returnReport;
} }
@ -401,10 +399,7 @@ public class ApiScenarioReportService {
caseReportMap.put(testPlanApiScenario.getApiScenarioId(), reportResult); caseReportMap.put(testPlanApiScenario.getApiScenarioId(), reportResult);
reportIds.add(report.getId()); reportIds.add(report.getId());
} }
TestPlanReportService testPlanReportService = CommonBeanFactory.getBean(TestPlanReportService.class);
testPlanLog.info("TestPlanReportId" + JSONArray.toJSONString(testPlanReportIdList) + " EXECUTE OVER. SCENARIO STATUS : " + JSONObject.toJSONString(scenarioAndErrorMap)); testPlanLog.info("TestPlanReportId" + JSONArray.toJSONString(testPlanReportIdList) + " EXECUTE OVER. SCENARIO STATUS : " + JSONObject.toJSONString(scenarioAndErrorMap));
for (String reportId : testPlanReportIdList) { for (String reportId : testPlanReportIdList) {
TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(reportId, null, scenarioAndErrorMap, null); TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(reportId, null, scenarioAndErrorMap, null);
TestPlanReportExecuteCatch.updateTestPlanExecuteResultInfo(reportId, null, caseReportMap, null); TestPlanReportExecuteCatch.updateTestPlanExecuteResultInfo(reportId, null, caseReportMap, null);
@ -868,8 +863,7 @@ public class ApiScenarioReportService {
} }
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); LogUtil.error(e.getMessage());
MSException.throwException(e.getMessage());
} }
return count; return count;
} }