fix(测试计划执行): 修复串行测试计划时接口用例执行顺序不对的问题

--bug=1007796 --user=宋天阳
【Github#7579】测试计划中拖拽编排场景用例,只能调整显示顺序,但无法作用于执行顺序【+github#8144】
https://www.tapd.cn/55049933/s/1075681
This commit is contained in:
song-tianyang 2021-11-26 18:01:55 +08:00 committed by song-tianyang
parent 6eca1344bd
commit 7bfb92b5f7
2 changed files with 41 additions and 69 deletions

View File

@ -391,22 +391,22 @@ public class TestPlanApiCaseService {
if (caseWithBLOBs != null) { if (caseWithBLOBs != null) {
apiResult.setName(caseWithBLOBs.getName()); apiResult.setName(caseWithBLOBs.getName());
} }
if(StringUtils.equalsIgnoreCase(request.getTriggerMode(),ApiRunMode.SCHEDULE_API_PLAN.name())) { if (StringUtils.equalsIgnoreCase(request.getTriggerMode(), ApiRunMode.SCHEDULE_API_PLAN.name())) {
apiResult.setTriggerMode(TriggerMode.SCHEDULE.name()); apiResult.setTriggerMode(TriggerMode.SCHEDULE.name());
}else if(StringUtils.equalsIgnoreCase(request.getTriggerMode(),ApiRunMode.JENKINS_API_PLAN.name())){ } else if (StringUtils.equalsIgnoreCase(request.getTriggerMode(), ApiRunMode.JENKINS_API_PLAN.name())) {
apiResult.setTriggerMode(TriggerMode.MANUAL.name()); apiResult.setTriggerMode(TriggerMode.MANUAL.name());
}else { } else {
apiResult.setTriggerMode(TriggerMode.BATCH.name()); apiResult.setTriggerMode(TriggerMode.BATCH.name());
} }
apiResult.setActuator("LOCAL"); apiResult.setActuator("LOCAL");
if (request.getConfig() != null && StringUtils.isNotEmpty(request.getConfig().getResourcePoolId())) { if (request.getConfig() != null && StringUtils.isNotEmpty(request.getConfig().getResourcePoolId())) {
apiResult.setActuator(request.getConfig().getResourcePoolId()); apiResult.setActuator(request.getConfig().getResourcePoolId());
} }
if(StringUtils.isEmpty(request.getUserId())){ if (StringUtils.isEmpty(request.getUserId())) {
if (SessionUtils.getUser() != null) { if (SessionUtils.getUser() != null) {
apiResult.setUserId(SessionUtils.getUser().getId()); apiResult.setUserId(SessionUtils.getUser().getId());
} }
}else { } else {
apiResult.setUserId(request.getUserId()); apiResult.setUserId(request.getUserId());
} }
@ -423,6 +423,7 @@ public class TestPlanApiCaseService {
List<String> ids = request.getPlanIds(); List<String> ids = request.getPlanIds();
TestPlanApiCaseExample example = new TestPlanApiCaseExample(); TestPlanApiCaseExample example = new TestPlanApiCaseExample();
example.createCriteria().andIdIn(ids); example.createCriteria().andIdIn(ids);
example.setOrderByClause("`order` DESC");
List<TestPlanApiCase> planApiCases = testPlanApiCaseMapper.selectByExample(example); List<TestPlanApiCase> planApiCases = testPlanApiCaseMapper.selectByExample(example);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ApiDefinitionExecResultMapper batchMapper = sqlSession.getMapper(ApiDefinitionExecResultMapper.class); ApiDefinitionExecResultMapper batchMapper = sqlSession.getMapper(ApiDefinitionExecResultMapper.class);
@ -449,13 +450,13 @@ public class TestPlanApiCaseService {
planApiCases.forEach(testPlanApiCase -> { planApiCases.forEach(testPlanApiCase -> {
ApiDefinitionExecResult report = addResult(request, testPlanApiCase, APITestStatus.Waiting.name(), batchMapper); ApiDefinitionExecResult report = addResult(request, testPlanApiCase, APITestStatus.Waiting.name(), batchMapper);
executeQueue.put(testPlanApiCase, report); executeQueue.put(testPlanApiCase, report);
executeThreadIdMap.put(testPlanApiCase.getId(),report.getId()); executeThreadIdMap.put(testPlanApiCase.getId(), report.getId());
}); });
//如果是测试计划生成报告的执行则更新执行信息执行线程信息 //如果是测试计划生成报告的执行则更新执行信息执行线程信息
if(TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())){ if (TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())) {
if (!executeThreadIdMap.isEmpty()) { if (!executeThreadIdMap.isEmpty()) {
TestPlanReportExecuteCatch.updateTestPlanThreadInfo(request.getPlanReportId(), executeThreadIdMap,null, null); TestPlanReportExecuteCatch.updateTestPlanThreadInfo(request.getPlanReportId(), executeThreadIdMap, null, null);
} }
} }
@ -503,7 +504,7 @@ public class TestPlanApiCaseService {
mapper.updateByPrimaryKey(execResult); mapper.updateByPrimaryKey(execResult);
modeDataDTO.setApiCaseId(execResult.getId()); modeDataDTO.setApiCaseId(execResult.getId());
modeDataDTO.setDebugReportId(request.getPlanReportId()); modeDataDTO.setDebugReportId(request.getPlanReportId());
modeDataDTO.setTestId(modeDataDTO.getTestId()+":"+request.getPlanReportId()+ ":"+ execResult.getId()); modeDataDTO.setTestId(modeDataDTO.getTestId() + ":" + request.getPlanReportId() + ":" + execResult.getId());
Future<ApiDefinitionExecResult> future = executorService.submit(new SerialApiExecTask(jMeterService, mapper, modeDataDTO, request.getConfig(), request.getTriggerMode())); Future<ApiDefinitionExecResult> future = executorService.submit(new SerialApiExecTask(jMeterService, mapper, modeDataDTO, request.getConfig(), request.getTriggerMode()));
ApiDefinitionExecResult report = future.get(); ApiDefinitionExecResult report = future.get();
// 如果开启失败结束执行则判断返回结果状态 // 如果开启失败结束执行则判断返回结果状态
@ -533,11 +534,11 @@ public class TestPlanApiCaseService {
} }
//如果是测试计划生成报告的执行则更新执行信息执行线程信息 //如果是测试计划生成报告的执行则更新执行信息执行线程信息
if(TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())){ if (TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())) {
if (!executeErrorList.isEmpty()) { if (!executeErrorList.isEmpty()) {
Map<String,String> executeErrorMap = new HashMap<>(); Map<String, String> executeErrorMap = new HashMap<>();
for(String id : executeErrorList){ for (String id : executeErrorList) {
executeErrorMap.put(id,TestPlanApiExecuteStatus.FAILD.name()); executeErrorMap.put(id, TestPlanApiExecuteStatus.FAILD.name());
} }
TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(request.getPlanReportId(), executeErrorMap, null, null); TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(request.getPlanReportId(), executeErrorMap, null, null);
} }
@ -558,14 +559,14 @@ public class TestPlanApiCaseService {
planApiCases.forEach(testPlanApiCase -> { planApiCases.forEach(testPlanApiCase -> {
ApiDefinitionExecResult report = addResult(request, testPlanApiCase, APITestStatus.Running.name(), batchMapper); ApiDefinitionExecResult report = addResult(request, testPlanApiCase, APITestStatus.Running.name(), batchMapper);
executeQueue.put(report.getId(), testPlanApiCase); executeQueue.put(report.getId(), testPlanApiCase);
executeThreadIdMap.put(testPlanApiCase.getId(),report.getId()); executeThreadIdMap.put(testPlanApiCase.getId(), report.getId());
MessageCache.caseExecResourceLock.put(report.getId(), report); MessageCache.caseExecResourceLock.put(report.getId(), report);
}); });
sqlSession.commit(); sqlSession.commit();
//如果是测试计划生成报告的执行则更新执行信息执行线程信息 //如果是测试计划生成报告的执行则更新执行信息执行线程信息
if(TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())){ if (TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())) {
if (!executeThreadIdMap.isEmpty()) { if (!executeThreadIdMap.isEmpty()) {
TestPlanReportExecuteCatch.updateTestPlanThreadInfo(request.getPlanReportId(), executeThreadIdMap,null, null); TestPlanReportExecuteCatch.updateTestPlanThreadInfo(request.getPlanReportId(), executeThreadIdMap, null, null);
} }
} }
@ -577,28 +578,28 @@ public class TestPlanApiCaseService {
try { try {
String debugId = request.getPlanReportId(); String debugId = request.getPlanReportId();
if (request.getConfig() != null && StringUtils.isNotEmpty(request.getConfig().getResourcePoolId())) { if (request.getConfig() != null && StringUtils.isNotEmpty(request.getConfig().getResourcePoolId())) {
String testId = testPlanApiCase.getId()+":"+ request.getPlanReportId() + ":" +reportId; String testId = testPlanApiCase.getId() + ":" + request.getPlanReportId() + ":" + reportId;
jMeterService.runTest(testId, reportId, request.getTriggerMode(), request.getPlanReportId(), request.getConfig()); jMeterService.runTest(testId, reportId, request.getTriggerMode(), request.getPlanReportId(), request.getConfig());
} else { } else {
HashTree hashTree = generateHashTree(testPlanApiCase.getId()); HashTree hashTree = generateHashTree(testPlanApiCase.getId());
if(StringUtils.isEmpty(debugId)){ if (StringUtils.isEmpty(debugId)) {
debugId = TriggerMode.BATCH.name(); debugId = TriggerMode.BATCH.name();
} }
String testId = reportId+":"+ request.getPlanReportId(); String testId = reportId + ":" + request.getPlanReportId();
jMeterService.runLocal(testId,request.getConfig(), hashTree, debugId, request.getTriggerMode()); jMeterService.runLocal(testId, request.getConfig(), hashTree, debugId, request.getTriggerMode());
} }
}catch (Exception e){ } catch (Exception e) {
executeErrorList.add(testPlanApiCase.getId()); executeErrorList.add(testPlanApiCase.getId());
} }
} }
//如果是测试计划生成报告的执行则更新执行信息执行线程信息 //如果是测试计划生成报告的执行则更新执行信息执行线程信息
if(TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())){ if (TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())) {
if (!executeErrorList.isEmpty()) { if (!executeErrorList.isEmpty()) {
Map<String,String> executeErrorMap = new HashMap<>(); Map<String, String> executeErrorMap = new HashMap<>();
for(String id : executeErrorList){ for (String id : executeErrorList) {
executeErrorMap.put(id,TestPlanApiExecuteStatus.FAILD.name()); executeErrorMap.put(id, TestPlanApiExecuteStatus.FAILD.name());
} }
TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(request.getPlanReportId(), executeErrorMap, null, null); TestPlanReportExecuteCatch.updateApiTestPlanExecuteInfo(request.getPlanReportId(), executeErrorMap, null, null);
} }
@ -745,25 +746,25 @@ public class TestPlanApiCaseService {
testPlanApiCaseMapper::updateByPrimaryKeySelective); testPlanApiCaseMapper::updateByPrimaryKeySelective);
} }
public List<TestPlanFailureApiDTO> getByApiExecReportIds(Map<String,String> testPlanApiCaseReportMap,boolean isFinish) { public List<TestPlanFailureApiDTO> getByApiExecReportIds(Map<String, String> testPlanApiCaseReportMap, boolean isFinish) {
if (testPlanApiCaseReportMap.isEmpty()) { if (testPlanApiCaseReportMap.isEmpty()) {
return new ArrayList<>(); return new ArrayList<>();
} }
String defaultStatus = "Running"; String defaultStatus = "Running";
if(isFinish){ if (isFinish) {
defaultStatus = "error"; defaultStatus = "error";
} }
List<TestPlanFailureApiDTO> apiTestCases = extTestPlanApiCaseMapper.getFailureListByIds(testPlanApiCaseReportMap.keySet(),null); List<TestPlanFailureApiDTO> apiTestCases = extTestPlanApiCaseMapper.getFailureListByIds(testPlanApiCaseReportMap.keySet(), null);
Map<String,String> reportResult = apiDefinitionExecResultService.selectReportResultByReportIds(testPlanApiCaseReportMap.values()); Map<String, String> reportResult = apiDefinitionExecResultService.selectReportResultByReportIds(testPlanApiCaseReportMap.values());
for (TestPlanFailureApiDTO dto : apiTestCases) { for (TestPlanFailureApiDTO dto : apiTestCases) {
String testPlanApiCaseId = dto.getId(); String testPlanApiCaseId = dto.getId();
String reportId = testPlanApiCaseReportMap.get(testPlanApiCaseId); String reportId = testPlanApiCaseReportMap.get(testPlanApiCaseId);
dto.setReportId(reportId); dto.setReportId(reportId);
if(StringUtils.isEmpty(reportId)){ if (StringUtils.isEmpty(reportId)) {
dto.setExecResult(defaultStatus); dto.setExecResult(defaultStatus);
}else { } else {
String status = reportResult.get(reportId); String status = reportResult.get(reportId);
if(status == null){ if (status == null) {
status = defaultStatus; status = defaultStatus;
} }
dto.setExecResult(status); dto.setExecResult(status);

View File

@ -131,28 +131,17 @@ public class TestPlanReportService {
} }
public TestPlanScheduleReportInfoDTO genTestPlanReportBySchedule(String projectID, String planId, String userId, String triggerMode) { public TestPlanScheduleReportInfoDTO genTestPlanReportBySchedule(String projectID, String planId, String userId, String triggerMode) {
Map<String, List<String>> apiTestCaseIdMap = new LinkedHashMap<>();
Map<String, String> planScenarioIdMap = new LinkedHashMap<>(); Map<String, String> planScenarioIdMap = new LinkedHashMap<>();
Map<String, String> apiTestCaseDataMap = new LinkedHashMap<>(); Map<String, String> planTestCaseIdMap = new LinkedHashMap<>();
Map<String, String> performanceIdMap = new LinkedHashMap<>(); Map<String, String> performanceIdMap = new LinkedHashMap<>();
List<TestPlanApiScenario> testPlanApiScenarioList = extTestPlanScenarioCaseMapper.selectLegalDataByTestPlanId(planId); List<TestPlanApiScenario> testPlanApiScenarioList = extTestPlanScenarioCaseMapper.selectLegalDataByTestPlanId(planId);
for (TestPlanApiScenario model : testPlanApiScenarioList) { for (TestPlanApiScenario model : testPlanApiScenarioList) {
planScenarioIdMap.put(model.getId(), model.getApiScenarioId()); planScenarioIdMap.put(model.getId(), model.getApiScenarioId());
} }
List<TestPlanApiCase> testPlanApiCaseList = extTestPlanApiCaseMapper.selectLegalDataByTestPlanId(planId); List<TestPlanApiCase> testPlanApiCaseList = extTestPlanApiCaseMapper.selectLegalDataByTestPlanId(planId);
for (TestPlanApiCase model : testPlanApiCaseList) { for (TestPlanApiCase model : testPlanApiCaseList) {
if (apiTestCaseIdMap.containsKey(model.getApiCaseId())) { planTestCaseIdMap.put(model.getId(), model.getApiCaseId());
apiTestCaseIdMap.get(model.getApiCaseId()).add(model.getId());
} else {
List<String> list = new ArrayList<>();
list.add(model.getId());
apiTestCaseIdMap.put(model.getApiCaseId(), list);
}
} }
LoadCaseRequest loadCaseRequest = new LoadCaseRequest(); LoadCaseRequest loadCaseRequest = new LoadCaseRequest();
loadCaseRequest.setTestPlanId(planId); loadCaseRequest.setTestPlanId(planId);
loadCaseRequest.setProjectId(projectID); loadCaseRequest.setProjectId(projectID);
@ -160,29 +149,12 @@ public class TestPlanReportService {
for (TestPlanLoadCaseDTO dto : testPlanLoadCaseDTOList) { for (TestPlanLoadCaseDTO dto : testPlanLoadCaseDTOList) {
performanceIdMap.put(dto.getId(), dto.getLoadCaseId()); performanceIdMap.put(dto.getId(), dto.getLoadCaseId());
} }
String planReportId = UUID.randomUUID().toString(); String planReportId = UUID.randomUUID().toString();
Map<String, String> apiCaseInfoMap = new HashMap<>(); Map<String, String> apiCaseInfoMap = new HashMap<>();
if (!apiTestCaseIdMap.isEmpty()) { for (String id : planTestCaseIdMap.keySet()) {
//筛选出未被删除/放入回收站的案例 apiCaseInfoMap.put(id, TestPlanApiExecuteStatus.PREPARE.name());
ApiTestCaseExample apiTestCaseExample = new ApiTestCaseExample();
apiTestCaseExample.createCriteria().andIdIn(new ArrayList<>(apiTestCaseIdMap.keySet()));
List<ApiTestCase> apiCaseList = apiTestCaseMapper.selectByExample(apiTestCaseExample);
if (!apiCaseList.isEmpty()) {
Map<String, ApiTestCase> apiCaseDataMap = apiCaseList.stream().collect(Collectors.toMap(ApiTestCase::getId, k -> k));
for (String id : apiCaseDataMap.keySet()) {
List<String> testPlanCaseIdList = apiTestCaseIdMap.get(id);
if (CollectionUtils.isNotEmpty(testPlanCaseIdList)) {
for (String testPlanCaseId : testPlanCaseIdList) {
apiCaseInfoMap.put(testPlanCaseId, TestPlanApiExecuteStatus.PREPARE.name());
apiTestCaseDataMap.put(testPlanCaseId, id);
}
}
}
}
} }
Map<String, String> scenarioInfoMap = new HashMap<>(); Map<String, String> scenarioInfoMap = new HashMap<>();
for (String id : planScenarioIdMap.keySet()) { for (String id : planScenarioIdMap.keySet()) {
scenarioInfoMap.put(id, TestPlanApiExecuteStatus.PREPARE.name()); scenarioInfoMap.put(id, TestPlanApiExecuteStatus.PREPARE.name());
@ -192,14 +164,13 @@ public class TestPlanReportService {
performanceInfoMap.put(id, TestPlanApiExecuteStatus.PREPARE.name()); performanceInfoMap.put(id, TestPlanApiExecuteStatus.PREPARE.name());
} }
TestPlanReportSaveRequest saveRequest = new TestPlanReportSaveRequest(planReportId, planId, userId, triggerMode, TestPlanReportSaveRequest saveRequest = new TestPlanReportSaveRequest(planReportId, planId, userId, triggerMode,
apiTestCaseDataMap.size() > 0, planScenarioIdMap.size() > 0, performanceIdMap.size() > 0, planTestCaseIdMap.size() > 0, planScenarioIdMap.size() > 0, performanceIdMap.size() > 0,
apiCaseInfoMap, scenarioInfoMap, performanceInfoMap); apiCaseInfoMap, scenarioInfoMap, performanceInfoMap);
TestPlanReport report = this.genTestPlanReport(saveRequest); TestPlanReport report = this.genTestPlanReport(saveRequest);
TestPlanScheduleReportInfoDTO returnDTO = new TestPlanScheduleReportInfoDTO(); TestPlanScheduleReportInfoDTO returnDTO = new TestPlanScheduleReportInfoDTO();
returnDTO.setTestPlanReport(report); returnDTO.setTestPlanReport(report);
returnDTO.setPlanScenarioIdMap(planScenarioIdMap); returnDTO.setPlanScenarioIdMap(planScenarioIdMap);
returnDTO.setApiTestCaseDataMap(apiTestCaseDataMap); returnDTO.setApiTestCaseDataMap(planTestCaseIdMap);
returnDTO.setPerformanceIdMap(performanceIdMap); returnDTO.setPerformanceIdMap(performanceIdMap);
return returnDTO; return returnDTO;
} }
@ -1171,14 +1142,14 @@ public class TestPlanReportService {
if (StringUtils.isNotBlank(testPlanReportContent.getScenarioAllCases())) { if (StringUtils.isNotBlank(testPlanReportContent.getScenarioAllCases())) {
List<TestPlanFailureScenarioDTO> allCases = JSONObject.parseArray(testPlanReportContent.getScenarioAllCases(), TestPlanFailureScenarioDTO.class); List<TestPlanFailureScenarioDTO> allCases = JSONObject.parseArray(testPlanReportContent.getScenarioAllCases(), TestPlanFailureScenarioDTO.class);
for (TestPlanFailureScenarioDTO dto : allCases) { for (TestPlanFailureScenarioDTO dto : allCases) {
if (StringUtils.equalsAnyIgnoreCase("Underway",dto.getStatus(), dto.getLastResult())) { if (StringUtils.equalsAnyIgnoreCase("Underway", dto.getStatus(), dto.getLastResult())) {
isUpdate = true; isUpdate = true;
ApiScenarioReport apiReport = apiScenarioReportMapper.selectByPrimaryKey(dto.getReportId()); ApiScenarioReport apiReport = apiScenarioReportMapper.selectByPrimaryKey(dto.getReportId());
if (apiReport != null) { if (apiReport != null) {
dto.setLastResult(apiReport.getStatus()); dto.setLastResult(apiReport.getStatus());
dto.setStatus(apiReport.getStatus()); dto.setStatus(apiReport.getStatus());
} }
}else if (StringUtils.equalsAnyIgnoreCase("Error",dto.getStatus(), dto.getLastResult())) { } else if (StringUtils.equalsAnyIgnoreCase("Error", dto.getStatus(), dto.getLastResult())) {
isUpdate = true; isUpdate = true;
dto.setLastResult("Fail"); dto.setLastResult("Fail");
dto.setStatus("Fail"); dto.setStatus("Fail");