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 ba10d04449
commit 3ed9f3c772
2 changed files with 41 additions and 69 deletions

View File

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

View File

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