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