refactor(测试计划执行): 优化测试计划执行状态监听

优化测试计划执行状态监听,支持分布式部署下的执行结果查询
This commit is contained in:
song-tianyang 2021-12-15 20:13:56 +08:00 committed by song-tianyang
parent 6be05e2c17
commit a46696fd6a
2 changed files with 87 additions and 5 deletions

View File

@ -34,6 +34,7 @@ public class TestPlanExecuteInfo {
private Map<String, String> apiScenarioCaseExecInfo = new ConcurrentHashMap<>();
private Map<String, String> loadCaseExecInfo = new ConcurrentHashMap<>();
//案例线程是以reportID为id的 key:关联表ID value:reportID
private Map<String, String> apiCaseExecuteThreadMap = new ConcurrentHashMap<>();
private Map<String, String> apiScenarioThreadMap = new ConcurrentHashMap<>();
private Map<String, String> loadCaseReportIdMap = new ConcurrentHashMap<>();
@ -201,13 +202,13 @@ public class TestPlanExecuteInfo {
MessageCache.executionQueue.remove(apiScenarioThreadMap.get(resourceId));
}
}
if(CollectionUtils.isNotEmpty(updateScenarioReportList)){
if (CollectionUtils.isNotEmpty(updateScenarioReportList)) {
ApiScenarioReportMapper apiScenarioReportMapper = CommonBeanFactory.getBean(ApiScenarioReportMapper.class);
ApiScenarioReportExample example = new ApiScenarioReportExample();
example.createCriteria().andIdIn(updateScenarioReportList).andStatusEqualTo("Running");
ApiScenarioReport report = new ApiScenarioReport();
report.setStatus("Error");
apiScenarioReportMapper.updateByExampleSelective(report,example);
apiScenarioReportMapper.updateByExampleSelective(report, example);
}
for (Map.Entry<String, String> entry : loadCaseExecInfo.entrySet()) {
@ -238,4 +239,30 @@ public class TestPlanExecuteInfo {
}
}
public Map<String, String> getRunningApiCaseReportMap() {
//key: reportId, value: testPlanApiCaseId
Map<String, String> returnMap = new HashMap<>();
for (String result : apiCaseExecInfo.keySet()) {
if (StringUtils.equalsIgnoreCase(result, TestPlanApiExecuteStatus.RUNNING.name())) {
if (apiCaseExecuteThreadMap.containsKey(result)) {
returnMap.put(apiCaseExecuteThreadMap.get(result), result);
}
}
}
return returnMap;
}
public Map<String, String> getRunningScenarioReportMap() {
//key: reportId, value: testPlanApiScenarioId
Map<String, String> returnMap = new HashMap<>();
for (String result : apiScenarioCaseExecInfo.keySet()) {
if (StringUtils.equalsIgnoreCase(result, TestPlanApiExecuteStatus.RUNNING.name())) {
if (apiScenarioThreadMap.containsKey(result)) {
returnMap.put(apiScenarioThreadMap.get(result), result);
}
}
}
return returnMap;
}
}

View File

@ -81,11 +81,15 @@ public class TestPlanReportService {
@Resource
ExtTestPlanApiCaseMapper extTestPlanApiCaseMapper;
@Resource
ExtApiDefinitionExecResultMapper extApiDefinitionExecResultMapper;
@Resource
ApiTestCaseMapper apiTestCaseMapper;
@Resource
LoadTestReportMapper loadTestReportMapper;
@Resource
TestPlanLoadCaseMapper testPlanLoadCaseMapper;
@Resource
ExtApiScenarioReportMapper extApiScenarioReportMapper;
@Lazy
@Resource
TestPlanService testPlanService;
@ -959,7 +963,7 @@ public class TestPlanReportService {
testPlanLog.info("TestPlanReportId[" + testPlanReport.getId() + "] SELECT performance BATCH OVER:" + JSONArray.toJSONString(selectList));
if (performaneReportIDList.isEmpty()) {
testPlanLog.info("TestPlanReportId[" + testPlanReport.getId() + "] performance EXECUTE OVER. TRIGGER_MODE:" + triggerMode + ",REsult:" + JSONObject.toJSONString(finishLoadTestId));
if (StringUtils.equalsAnyIgnoreCase(triggerMode, ReportTriggerMode.API.name() ,ReportTriggerMode.MANUAL.name())) {
if (StringUtils.equalsAnyIgnoreCase(triggerMode, ReportTriggerMode.API.name(), ReportTriggerMode.MANUAL.name())) {
for (String string : finishLoadTestId.keySet()) {
String reportId = caseReportMap.get(string);
TestPlanLoadCase updateDTO = new TestPlanLoadCase();
@ -1222,7 +1226,7 @@ public class TestPlanReportService {
if (StringUtils.isNotBlank(testPlanReportContent.getLoadAllCases())) {
List<TestPlanLoadCaseDTO> allCases = JSONObject.parseArray(testPlanReportContent.getLoadAllCases(), TestPlanLoadCaseDTO.class);
if(!allCases.isEmpty()){
if (!allCases.isEmpty()) {
isTaskRunning = true;
}
}
@ -1247,10 +1251,18 @@ public class TestPlanReportService {
bloBs.setEndTime(endTime);
TestPlanReportContentExample example = new TestPlanReportContentExample();
example.createCriteria().andTestPlanReportIdEqualTo(testPlanReport.getId());
testPlanReportContentMapper.updateByExampleSelective(bloBs,example);
testPlanReportContentMapper.updateByExampleSelective(bloBs, example);
}
private boolean checkTestPlanReportIsTimeOut(String planReportId) {
//同步数据库更新状态信息
try {
this.syncReportStatus(planReportId);
} catch (Exception e) {
LogUtil.info("联动数据库同步执行状态失败! " + e.getMessage());
LogUtil.error(e);
}
TestPlanExecuteInfo executeInfo = TestPlanReportExecuteCatch.getTestPlanExecuteInfo(planReportId);
int unFinishNum = executeInfo.countUnFinishedNum();
if (unFinishNum > 0) {
@ -1264,6 +1276,49 @@ public class TestPlanReportService {
return false;
}
private void syncReportStatus(String planReportId) {
if (TestPlanReportExecuteCatch.containsReport(planReportId)) {
TestPlanExecuteInfo executeInfo = TestPlanReportExecuteCatch.getTestPlanExecuteInfo(planReportId);
if (executeInfo != null) {
//同步接口案例结果
Map<String, String> updateCaseStatusMap = new HashMap<>();
Map<String, String> apiCaseReportMap = executeInfo.getRunningApiCaseReportMap();
if (MapUtils.isNotEmpty(apiCaseReportMap)) {
List<ApiDefinitionExecResult> execList = extApiDefinitionExecResultMapper.selectStatusByIdList(apiCaseReportMap.keySet());
for (ApiDefinitionExecResult report : execList) {
String reportId = report.getId();
String status = report.getStatus();
if (!StringUtils.equalsAnyIgnoreCase(status, "Running", "Waiting")) {
String planCaseId = apiCaseReportMap.get(reportId);
if (StringUtils.isNotEmpty(planCaseId)) {
updateCaseStatusMap.put(planCaseId, status);
}
}
}
}
//同步场景结果
Map<String, String> updateScenarioStatusMap = new HashMap<>();
Map<String, String> scenarioReportMap = executeInfo.getRunningScenarioReportMap();
if (MapUtils.isNotEmpty(scenarioReportMap)) {
List<ApiScenarioReport> reportList = extApiScenarioReportMapper.selectStatusByIds(scenarioReportMap.keySet());
for (ApiScenarioReport report : reportList) {
String reportId = report.getId();
String status = report.getStatus();
if (!StringUtils.equalsAnyIgnoreCase(status, "Running", "Waiting")) {
String planScenarioId = scenarioReportMap.get(reportId);
if (StringUtils.isNotEmpty(planScenarioId)) {
updateScenarioStatusMap.put(planScenarioId, status);
}
}
}
}
TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(planReportId, updateCaseStatusMap, updateScenarioStatusMap, null);
}
}
}
private void finishTestPlanReport(String planReportId) {
TestPlanReport testPlanReport = testPlanReportMapper.selectByPrimaryKey(planReportId);
if (testPlanReport != null && StringUtils.equalsIgnoreCase("Running", testPlanReport.getStatus())) {