fix(测试计划): 修复测试计划执行时获取不到接口案例的问题

修复测试计划执行时获取不到接口案例的问题
This commit is contained in:
song-tianyang 2021-11-10 12:22:43 +08:00 committed by song-tianyang
parent ca288fc05b
commit bf902538e0
4 changed files with 87 additions and 71 deletions

View File

@ -20,6 +20,9 @@ public class RunModeDataDTO {
private String apiCaseId; private String apiCaseId;
private Map<String, String> planEnvMap; private Map<String, String> planEnvMap;
private String debugReportId;
public RunModeDataDTO(){ public RunModeDataDTO(){
} }

View File

@ -242,39 +242,27 @@ public class ApiDefinitionExecResultService {
if (CollectionUtils.isNotEmpty(result.getScenarios())) { if (CollectionUtils.isNotEmpty(result.getScenarios())) {
result.getScenarios().forEach(scenarioResult -> { result.getScenarios().forEach(scenarioResult -> {
final boolean[] isFirst = {true};
if (scenarioResult != null && CollectionUtils.isNotEmpty(scenarioResult.getRequestResults())) { if (scenarioResult != null && CollectionUtils.isNotEmpty(scenarioResult.getRequestResults())) {
scenarioResult.getRequestResults().forEach(item -> { scenarioResult.getRequestResults().forEach(item -> {
String creator = TestPlanReportExecuteCatch.getCreator(testPlanReportId); String creator = TestPlanReportExecuteCatch.getCreator(testPlanReportId);
if (!StringUtils.startsWithAny(item.getName(), "PRE_PROCESSOR_ENV_", "POST_PROCESSOR_ENV_")) {
String status = item.isSuccess() ? "success" : "error"; ApiDefinitionExecResult saveResult = MessageCache.caseExecResourceLock.get(result.getTestId());
if (saveResult == null) {
saveResult = apiDefinitionExecResultMapper.selectByPrimaryKey(result.getTestId());
}
item.getResponseResult().setConsole(result.getConsole()); item.getResponseResult().setConsole(result.getConsole());
ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult(); boolean saved = true;
if (saveResult == null || scenarioResult.getRequestResults().size() > 1) {
saveResult = new ApiDefinitionExecResult();
if (isFirst[0]) {
isFirst[0] = false;
saveResult.setId(result.getTestId());
} else {
saveResult.setId(UUID.randomUUID().toString()); saveResult.setId(UUID.randomUUID().toString());
saveResult.setCreateTime(System.currentTimeMillis());
saveResult.setName(getName(type, item.getName(), status, saveResult.getCreateTime(), saveResult.getId()));
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = apiDefinitionMapper.selectByPrimaryKey(item.getName());
String caseId = null;
if (apiDefinitionWithBLOBs != null) {
saveResult.setName(apiDefinitionWithBLOBs.getName());
caseId = apiDefinitionWithBLOBs.getId();
} else {
ApiTestCaseWithBLOBs caseWithBLOBs = apiTestCaseMapper.selectByPrimaryKey(item.getName());
if (caseWithBLOBs != null) {
caseId = caseWithBLOBs.getId();
saveResult.setName(caseWithBLOBs.getName());
} else {
caseWithBLOBs = testPlanApiCaseService.getApiTestCaseById(item.getName());
if (caseWithBLOBs != null) {
saveResult.setName(caseWithBLOBs.getName());
caseId = caseWithBLOBs.getId();
} }
} saveResult.setActuator("LOCAL");
} saveResult.setName(item.getName());
if (StringUtils.isNotEmpty(caseId)) {
apiIdResultMap.put(caseId, item.isSuccess() ? TestPlanApiExecuteStatus.SUCCESS.name() : TestPlanApiExecuteStatus.FAILD.name());
}
if (StringUtils.equals(type, ApiRunMode.JENKINS_API_PLAN.name())) { if (StringUtils.equals(type, ApiRunMode.JENKINS_API_PLAN.name())) {
saveResult.setTriggerMode(TriggerMode.API.name()); saveResult.setTriggerMode(TriggerMode.API.name());
} else if (StringUtils.equals(type, ApiRunMode.MANUAL_PLAN.name())) { } else if (StringUtils.equals(type, ApiRunMode.MANUAL_PLAN.name())) {
@ -282,44 +270,64 @@ public class ApiDefinitionExecResultService {
} else { } else {
saveResult.setTriggerMode(TriggerMode.SCHEDULE.name()); saveResult.setTriggerMode(TriggerMode.SCHEDULE.name());
} }
saveResult.setType(type);
saveResult.setCreateTime(item.getStartTime());
if (StringUtils.isNotEmpty(result.getUserId())) {
saveResult.setUserId(result.getUserId());
} else {
if (SessionUtils.getUser() != null) {
saveResult.setUserId(SessionUtils.getUser().getId());
}
}
saved = false;
}
String status = item.isSuccess() ? "success" : "error";
saveResult.setName(getName(type, item.getName(), status, saveResult.getCreateTime(), saveResult.getId()));
saveResult.setStatus(status);
saveResult.setResourceId(item.getName()); saveResult.setResourceId(item.getName());
saveResult.setActuator("LOCAL");
saveResult.setContent(JSON.toJSONString(item)); saveResult.setContent(JSON.toJSONString(item));
saveResult.setStartTime(item.getStartTime()); saveResult.setStartTime(item.getStartTime());
saveResult.setEndTime(item.getResponseResult().getResponseTime()); saveResult.setEndTime(item.getResponseResult().getResponseTime());
saveResult.setType(finalSaveResultType);
saveResult.setStatus(status);
if (StringUtils.equals(type, ApiRunMode.SCHEDULE_API_PLAN.name())) { // 清空上次执行结果的内容只保留近五条结果
TestPlanApiCase apiCase = testPlanApiCaseService.getById(item.getName()); ApiDefinitionExecResult prevResult = extApiDefinitionExecResultMapper.selectMaxResultByResourceIdAndType(item.getName(), type);
if (StringUtils.isEmpty(creator)) {
creator = testPlanService.findScheduleCreateUserById(apiCase.getTestPlanId());
}
apiCase.setStatus(status);
apiCase.setUpdateTime(System.currentTimeMillis());
testPlanApiCaseService.updateByPrimaryKeySelective(apiCase);
} else if (StringUtils.equals(type, ApiRunMode.JENKINS_API_PLAN.name())) {
TestPlanApiCase apiCase = testPlanApiCaseService.getById(item.getName());
apiCase.setStatus(status);
apiCase.setUpdateTime(System.currentTimeMillis());
testPlanApiCaseService.updateByPrimaryKeySelective(apiCase);
} else {
testPlanApiCaseService.setExecResult(item.getName(), status, item.getStartTime());
testCaseReviewApiCaseService.setExecResult(item.getName(), status, item.getStartTime());
}
if (creator == null) {
creator = "";
}
saveResult.setUserId(creator);
// 前一条数据内容清空
ApiDefinitionExecResult prevResult = extApiDefinitionExecResultMapper.selectMaxResultByResourceIdAndType(item.getName(), finalSaveResultType);
if (prevResult != null) { if (prevResult != null) {
prevResult.setContent(null); prevResult.setContent(null);
apiDefinitionExecResultMapper.updateByPrimaryKeyWithBLOBs(prevResult); apiDefinitionExecResultMapper.updateByPrimaryKeyWithBLOBs(prevResult);
} }
if (StringUtils.isNotEmpty(saveResult.getTriggerMode()) && saveResult.getTriggerMode().equals("CASE")) {
saveResult.setTriggerMode(TriggerMode.MANUAL.name());
}
if (!saved) {
apiDefinitionExecResultMapper.insert(saveResult); apiDefinitionExecResultMapper.insert(saveResult);
} else {
apiDefinitionExecResultMapper.updateByPrimaryKeyWithBLOBs(saveResult);
}
apiDefinitionService.removeCache(result.getTestId());
if (StringUtils.isNotEmpty(result.getTestId())) {
MessageCache.caseExecResourceLock.remove(result.getTestId());
}
String caseId = item.getName();
if (StringUtils.equalsAny(type, ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name())) {
TestPlanApiCase apiCase = testPlanApiCaseService.getById(caseId);
apiCase.setStatus(status);
apiCase.setUpdateTime(System.currentTimeMillis());
testPlanApiCaseService.updateByPrimaryKeySelective(apiCase);
} else {
testPlanApiCaseService.setExecResult(caseId, status, item.getStartTime());
testCaseReviewApiCaseService.setExecResult(caseId, status, item.getStartTime());
}
if (StringUtils.isNotEmpty(caseId)) {
apiIdResultMap.put(caseId, item.isSuccess() ? TestPlanApiExecuteStatus.SUCCESS.name() : TestPlanApiExecuteStatus.FAILD.name());
}
caseReportMap.put(caseId, saveResult.getId()); caseReportMap.put(caseId, saveResult.getId());
}
}); });
} }
}); });

View File

@ -467,6 +467,7 @@ public class TestPlanApiCaseService {
} }
mapper.updateByPrimaryKey(execResult); mapper.updateByPrimaryKey(execResult);
modeDataDTO.setApiCaseId(execResult.getId()); modeDataDTO.setApiCaseId(execResult.getId());
modeDataDTO.setDebugReportId(request.getPlanReportId());
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();
// 如果开启失败结束执行则判断返回结果状态 // 如果开启失败结束执行则判断返回结果状态

View File

@ -39,9 +39,13 @@ public class SerialApiExecTask<T> implements Callable<T> {
return null; return null;
} }
if (config != null && StringUtils.isNotBlank(config.getResourcePoolId())) { if (config != null && StringUtils.isNotBlank(config.getResourcePoolId())) {
jMeterService.runTest(runModeDataDTO.getTestId(), runModeDataDTO.getApiCaseId(), runMode, null, config); jMeterService.runTest(runModeDataDTO.getTestId(), runModeDataDTO.getApiCaseId(), runMode, runModeDataDTO.getDebugReportId(), config);
} else { } else {
jMeterService.runLocal(runModeDataDTO.getApiCaseId(), config, runModeDataDTO.getHashTree(), runModeDataDTO.getReport() != null ? runModeDataDTO.getReport().getTriggerMode() : null, runMode); String debugId = runModeDataDTO.getDebugReportId();
if(debugId == null){
debugId = runModeDataDTO.getReport() != null ? runModeDataDTO.getReport().getTriggerMode() : null;
}
jMeterService.runLocal(runModeDataDTO.getApiCaseId(), config, runModeDataDTO.getHashTree(), debugId, runMode);
} }
// 轮询查看报告状态最多200次防止死循环 // 轮询查看报告状态最多200次防止死循环
ApiDefinitionExecResult report = null; ApiDefinitionExecResult report = null;