diff --git a/backend/src/main/java/io/metersphere/api/cache/TestPlanExecuteInfo.java b/backend/src/main/java/io/metersphere/api/cache/TestPlanExecuteInfo.java index 6e750fbc6f..82031428dd 100644 --- a/backend/src/main/java/io/metersphere/api/cache/TestPlanExecuteInfo.java +++ b/backend/src/main/java/io/metersphere/api/cache/TestPlanExecuteInfo.java @@ -34,6 +34,7 @@ public class TestPlanExecuteInfo { private Map apiScenarioCaseExecInfo = new ConcurrentHashMap<>(); private Map loadCaseExecInfo = new ConcurrentHashMap<>(); + //案例线程是以reportID为id的。 key:关联表ID value:reportID private Map apiCaseExecuteThreadMap = new ConcurrentHashMap<>(); private Map apiScenarioThreadMap = new ConcurrentHashMap<>(); private Map 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 entry : loadCaseExecInfo.entrySet()) { @@ -238,4 +239,30 @@ public class TestPlanExecuteInfo { } } + + public Map getRunningApiCaseReportMap() { + //key: reportId, value: testPlanApiCaseId + Map 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 getRunningScenarioReportMap() { + //key: reportId, value: testPlanApiScenarioId + Map 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; + } } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java index 0f7f173529..849edc5eb2 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java @@ -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 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 updateCaseStatusMap = new HashMap<>(); + Map apiCaseReportMap = executeInfo.getRunningApiCaseReportMap(); + if (MapUtils.isNotEmpty(apiCaseReportMap)) { + List 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 updateScenarioStatusMap = new HashMap<>(); + Map scenarioReportMap = executeInfo.getRunningScenarioReportMap(); + if (MapUtils.isNotEmpty(scenarioReportMap)) { + List 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())) {