Skip to content

Commit

Permalink
fix(测试计划): 修复测试计划执行时获取不到接口案例的问题
Browse files Browse the repository at this point in the history
修复测试计划执行时获取不到接口案例的问题
  • Loading branch information
Somebody-JIAN committed Nov 10, 2021
1 parent 12036e5 commit 811e3e3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class RunModeDataDTO {
private String apiCaseId;

private Map<String, String> planEnvMap;

private String debugReportId;

public RunModeDataDTO(){

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,84 +242,92 @@ public void saveApiResultByScheduleTask(TestResult result, String testPlanReport

if (CollectionUtils.isNotEmpty(result.getScenarios())) {
result.getScenarios().forEach(scenarioResult -> {
final boolean[] isFirst = {true};
if (scenarioResult != null && CollectionUtils.isNotEmpty(scenarioResult.getRequestResults())) {
scenarioResult.getRequestResults().forEach(item -> {

String creator = TestPlanReportExecuteCatch.getCreator(testPlanReportId);
if (!StringUtils.startsWithAny(item.getName(), "PRE_PROCESSOR_ENV_", "POST_PROCESSOR_ENV_")) {
ApiDefinitionExecResult saveResult = MessageCache.caseExecResourceLock.get(result.getTestId());
if (saveResult == null) {
saveResult = apiDefinitionExecResultMapper.selectByPrimaryKey(result.getTestId());
}
item.getResponseResult().setConsole(result.getConsole());
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.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;
}

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.setContent(JSON.toJSONString(item));
saveResult.setStartTime(item.getStartTime());
saveResult.setEndTime(item.getResponseResult().getResponseTime());

String status = item.isSuccess() ? "success" : "error";
item.getResponseResult().setConsole(result.getConsole());
ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult();
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());
// 清空上次执行结果的内容,只保留近五条结果
ApiDefinitionExecResult prevResult = extApiDefinitionExecResultMapper.selectMaxResultByResourceIdAndType(item.getName(), type);
if (prevResult != null) {
prevResult.setContent(null);
apiDefinitionExecResultMapper.updateByPrimaryKeyWithBLOBs(prevResult);
}

if (StringUtils.isNotEmpty(saveResult.getTriggerMode()) && saveResult.getTriggerMode().equals("CASE")) {
saveResult.setTriggerMode(TriggerMode.MANUAL.name());
}
if (!saved) {
apiDefinitionExecResultMapper.insert(saveResult);
} else {
caseWithBLOBs = testPlanApiCaseService.getApiTestCaseById(item.getName());
if (caseWithBLOBs != null) {
saveResult.setName(caseWithBLOBs.getName());
caseId = caseWithBLOBs.getId();
}
apiDefinitionExecResultMapper.updateByPrimaryKeyWithBLOBs(saveResult);
}
apiDefinitionService.removeCache(result.getTestId());
if (StringUtils.isNotEmpty(result.getTestId())) {
MessageCache.caseExecResourceLock.remove(result.getTestId());
}
}
if (StringUtils.isNotEmpty(caseId)) {
apiIdResultMap.put(caseId, item.isSuccess() ? TestPlanApiExecuteStatus.SUCCESS.name() : TestPlanApiExecuteStatus.FAILD.name());
}

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());
}
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());
}

saveResult.setResourceId(item.getName());
saveResult.setActuator("LOCAL");
saveResult.setContent(JSON.toJSONString(item));
saveResult.setStartTime(item.getStartTime());
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());
if (StringUtils.isEmpty(creator)) {
creator = testPlanService.findScheduleCreateUserById(apiCase.getTestPlanId());
if (StringUtils.isNotEmpty(caseId)) {
apiIdResultMap.put(caseId, item.isSuccess() ? TestPlanApiExecuteStatus.SUCCESS.name() : TestPlanApiExecuteStatus.FAILD.name());
}
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) {
prevResult.setContent(null);
apiDefinitionExecResultMapper.updateByPrimaryKeyWithBLOBs(prevResult);

caseReportMap.put(caseId, saveResult.getId());
}
apiDefinitionExecResultMapper.insert(saveResult);
caseReportMap.put(caseId, saveResult.getId());

});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ public void run() {
}
mapper.updateByPrimaryKey(execResult);
modeDataDTO.setApiCaseId(execResult.getId());
modeDataDTO.setDebugReportId(request.getPlanReportId());
Future<ApiDefinitionExecResult> future = executorService.submit(new SerialApiExecTask(jMeterService, mapper, modeDataDTO, request.getConfig(), request.getTriggerMode()));
ApiDefinitionExecResult report = future.get();
// 如果开启失败结束执行,则判断返回结果状态
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ public T call() {
return null;
}
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 {
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次,防止死循环
ApiDefinitionExecResult report = null;
Expand Down

0 comments on commit 811e3e3

Please sign in to comment.