Skip to content

Commit

Permalink
feat(测试计划): 修复测试计划定时任务总是Running的问题
Browse files Browse the repository at this point in the history
--bug=1005249 --user=宋天阳 【定时任务】测试计划定时任务,状态一直是running
https://www.tapd.cn/55049933/s/1026590
  • Loading branch information
Somebody-JIAN authored and liuruibin committed Jul 20, 2021
1 parent 47558a6 commit b4a5a00
Show file tree
Hide file tree
Showing 17 changed files with 765 additions and 286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class TestPlanScenarioRequest {
private String projectId;
private String moduleId;
private List<String> moduleIds;
private List<String> scenarioIds;
private String name;
private String status;
private String workspaceId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2342,7 +2342,6 @@ public void batchCopy(ApiScenarioBatchRequest batchRequest) {
}
}
}
// uploadFiles(request, bodyFiles, scenarioFiles);
}

public DeleteCheckResult checkBeforeDelete(ApiScenarioBatchRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import io.metersphere.base.mapper.ApiTestCaseMapper;
import io.metersphere.base.mapper.TestCaseReviewApiCaseMapper;
import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.constants.DelimiterConstants;
import io.metersphere.commons.constants.TriggerMode;
import io.metersphere.commons.constants.*;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.DateUtils;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.track.dto.TestPlanDTO;
import io.metersphere.track.request.testcase.QueryTestPlanRequest;
import io.metersphere.track.service.TestCaseReviewApiCaseService;
import io.metersphere.track.service.TestPlanApiCaseService;
import io.metersphere.track.service.TestPlanReportService;
import io.metersphere.track.service.TestPlanService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -157,69 +157,89 @@ private String getName(String type, String id, String status, Long time) {
* @param result
* @param type
*/
public void saveApiResultByScheduleTask(TestResult result, String type) {
public void saveApiResultByScheduleTask(TestResult result,String testPlanReportId, String type,String trigeMode) {
String saveResultType = type;
if (StringUtils.equalsAny(saveResultType, ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name())) {
saveResultType = ApiRunMode.API_PLAN.name();
}
String finalSaveResultType = saveResultType;
result.getScenarios().get(0).getRequestResults().forEach(item -> {
ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult();
saveResult.setId(UUID.randomUUID().toString());
saveResult.setCreateTime(System.currentTimeMillis());
saveResult.setName(item.getName());
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = apiDefinitionMapper.selectByPrimaryKey(item.getName());
if (apiDefinitionWithBLOBs != null) {
saveResult.setName(apiDefinitionWithBLOBs.getName());
} else {
ApiTestCaseWithBLOBs caseWithBLOBs = apiTestCaseMapper.selectByPrimaryKey(item.getName());
if (caseWithBLOBs != null) {
saveResult.setName(caseWithBLOBs.getName());
}
}
if (StringUtils.equals(type, ApiRunMode.JENKINS_API_PLAN.name())) {
saveResult.setTriggerMode(TriggerMode.API.name());
} else {
saveResult.setTriggerMode(TriggerMode.SCHEDULE.name());
}

saveResult.setResourceId(item.getName());
saveResult.setActuator("LOCAL");
saveResult.setContent(JSON.toJSONString(item));
saveResult.setStartTime(item.getStartTime());
String status = item.isSuccess() ? "success" : "error";
saveResult.setEndTime(item.getResponseResult().getResponseTime());
saveResult.setType(finalSaveResultType);
saveResult.setStatus(status);
Map<String,String> apiIdResultMap = new HashMap<>();

String userID = null;
if (StringUtils.equals(type, ApiRunMode.SCHEDULE_API_PLAN.name())) {
TestPlanApiCase apiCase = testPlanApiCaseService.getById(item.getName());
String scheduleCreateUser = testPlanService.findScheduleCreateUserById(apiCase.getTestPlanId());
userID = scheduleCreateUser;
apiCase.setStatus(status);
apiCase.setUpdateTime(System.currentTimeMillis());
testPlanApiCaseService.updateByPrimaryKeySelective(apiCase);
} else if (StringUtils.equals(type, ApiRunMode.JENKINS_SCENARIO_PLAN.name())) {
TestPlanApiCase apiCase = testPlanApiCaseService.getById(item.getName());
userID = Objects.requireNonNull(SessionUtils.getUser()).getId();
apiCase.setStatus(status);
apiCase.setUpdateTime(System.currentTimeMillis());
testPlanApiCaseService.updateByPrimaryKeySelective(apiCase);
} else {
userID = Objects.requireNonNull(SessionUtils.getUser()).getId();
testPlanApiCaseService.setExecResult(item.getName(), status, item.getStartTime());
testCaseReviewApiCaseService.setExecResult(item.getName(), status, item.getStartTime());
}
saveResult.setUserId(userID);
// 前一条数据内容清空
ApiDefinitionExecResult prevResult = extApiDefinitionExecResultMapper.selectMaxResultByResourceIdAndType(item.getName(), finalSaveResultType);
if (prevResult != null) {
prevResult.setContent(null);
apiDefinitionExecResultMapper.updateByPrimaryKeyWithBLOBs(prevResult);
}
apiDefinitionExecResultMapper.insert(saveResult);
});
if (CollectionUtils.isNotEmpty(result.getScenarios())) {
result.getScenarios().forEach(scenarioResult -> {
if (scenarioResult != null && CollectionUtils.isNotEmpty(scenarioResult.getRequestResults())) {
scenarioResult.getRequestResults().forEach(item -> {
String status = item.isSuccess() ? "success" : "error";
ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult();
saveResult.setId(UUID.randomUUID().toString());
saveResult.setCreateTime(System.currentTimeMillis());
saveResult.setName(item.getName());
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = apiDefinitionMapper.selectByPrimaryKey(item.getName());
if (apiDefinitionWithBLOBs != null) {
saveResult.setName(apiDefinitionWithBLOBs.getName());
apiIdResultMap.put(apiDefinitionWithBLOBs.getId(),item.isSuccess()? TestPlanApiExecuteStatus.SUCCESS.name() : TestPlanApiExecuteStatus.FAILD.name());
} else {
ApiTestCaseWithBLOBs caseWithBLOBs = apiTestCaseMapper.selectByPrimaryKey(item.getName());
if (caseWithBLOBs != null) {
saveResult.setName(caseWithBLOBs.getName());
apiIdResultMap.put(caseWithBLOBs.getId(),item.isSuccess()? TestPlanApiExecuteStatus.SUCCESS.name() : TestPlanApiExecuteStatus.FAILD.name());
}else {
caseWithBLOBs = testPlanApiCaseService.getApiTestCaseById(item.getName());
if (caseWithBLOBs != null) {
saveResult.setName(caseWithBLOBs.getName());
apiIdResultMap.put(caseWithBLOBs.getId(), item.isSuccess() ? TestPlanApiExecuteStatus.SUCCESS.name() : TestPlanApiExecuteStatus.FAILD.name());
}
}
}
if (StringUtils.equals(type, ApiRunMode.JENKINS_API_PLAN.name())) {
saveResult.setTriggerMode(TriggerMode.API.name());
} else {
saveResult.setTriggerMode(TriggerMode.SCHEDULE.name());
}

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

String userID = null;
if (StringUtils.equals(type, ApiRunMode.SCHEDULE_API_PLAN.name())) {
TestPlanApiCase apiCase = testPlanApiCaseService.getById(item.getName());
String scheduleCreateUser = testPlanService.findScheduleCreateUserById(apiCase.getTestPlanId());
userID = scheduleCreateUser;
apiCase.setStatus(status);
apiCase.setUpdateTime(System.currentTimeMillis());
testPlanApiCaseService.updateByPrimaryKeySelective(apiCase);
} else if (StringUtils.equals(type, ApiRunMode.JENKINS_SCENARIO_PLAN.name())) {
TestPlanApiCase apiCase = testPlanApiCaseService.getById(item.getName());
userID = Objects.requireNonNull(SessionUtils.getUser()).getId();
apiCase.setStatus(status);
apiCase.setUpdateTime(System.currentTimeMillis());
testPlanApiCaseService.updateByPrimaryKeySelective(apiCase);
} else {
userID = Objects.requireNonNull(SessionUtils.getUser()).getId();
testPlanApiCaseService.setExecResult(item.getName(), status, item.getStartTime());
testCaseReviewApiCaseService.setExecResult(item.getName(), status, item.getStartTime());
}
saveResult.setUserId(userID);
// 前一条数据内容清空
ApiDefinitionExecResult prevResult = extApiDefinitionExecResultMapper.selectMaxResultByResourceIdAndType(item.getName(), finalSaveResultType);
if (prevResult != null) {
prevResult.setContent(null);
apiDefinitionExecResultMapper.updateByPrimaryKeyWithBLOBs(prevResult);
}
apiDefinitionExecResultMapper.insert(saveResult);
});
}
});
}

TestPlanReportService testPlanReportService = CommonBeanFactory.getBean(TestPlanReportService.class);
testPlanReportService.updateExecuteApis(testPlanReportId,apiIdResultMap,null,null);
}

public void deleteByResourceId(String resourceId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.metersphere.base.mapper.ext.ExtApiScenarioReportMapper;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.constants.ReportTriggerMode;
import io.metersphere.commons.constants.TestPlanApiExecuteStatus;
import io.metersphere.commons.constants.TriggerMode;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*;
Expand Down Expand Up @@ -252,7 +253,7 @@ public ApiScenarioReport updatePlanCase(TestResult result, String runMode) {
return returnReport;
}

public ApiScenarioReport updateSchedulePlanCase(TestResult result, String runMode) {
public ApiScenarioReport updateSchedulePlanCase(TestResult result, String runMode) {
ApiScenarioReport lastReport = null;
List<ScenarioResult> scenarioResultList = result.getScenarios();

Expand All @@ -261,6 +262,7 @@ public ApiScenarioReport updateSchedulePlanCase(TestResult result, String runMod

List<String> reportIds = new ArrayList<>();
List<String> scenarioIdList = new ArrayList<>();
Map<String,String> scenarioAndErrorMap = new HashMap<>();
for (ScenarioResult scenarioResult : scenarioResultList) {

// 存储场景报告
Expand Down Expand Up @@ -296,9 +298,12 @@ public ApiScenarioReport updateSchedulePlanCase(TestResult result, String runMod
report.setScenarioId(testPlanApiScenario.getApiScenarioId());
report.setTestPlanScenarioId(planScenarioId);
apiScenarioReportMapper.updateByPrimaryKeySelective(report);

if (scenarioResult.getError() > 0) {
scenarioAndErrorMap.put(testPlanApiScenario.getApiScenarioId(), TestPlanApiExecuteStatus.FAILD.name());
testPlanApiScenario.setLastResult(ScenarioStatus.Fail.name());
} else {
scenarioAndErrorMap.put(testPlanApiScenario.getApiScenarioId(), TestPlanApiExecuteStatus.SUCCESS.name());
testPlanApiScenario.setLastResult(ScenarioStatus.Success.name());
}
String passRate = new DecimalFormat("0%").format((float) scenarioResult.getSuccess() / (scenarioResult.getSuccess() + scenarioResult.getError()));
Expand All @@ -317,18 +322,17 @@ public ApiScenarioReport updateSchedulePlanCase(TestResult result, String runMod
testPlanApiScenario.setReportId(report.getId());
testPlanApiScenario.setUpdateTime(System.currentTimeMillis());
testPlanApiScenarioMapper.updateByPrimaryKeySelective(testPlanApiScenario);
// scenarioIds.append(scenarioResult.getName()).append(",");
scenarioIdList.add(testPlanApiScenario.getApiScenarioId());
scenarioNames.append(report.getName()).append(",");

lastReport = report;
reportIds.add(report.getId());
}
// 合并报告
// margeReport(result, scenarioIds, scenarioNames, runMode, projectId, userId, reportIds);

TestPlanReportService testPlanReportService = CommonBeanFactory.getBean(TestPlanReportService.class);
testPlanReportService.updateReport(testPlanReportIdList, runMode, lastReport.getTriggerMode(), scenarioIdList);
for (String planId :testPlanReportIdList) {
testPlanReportService.updateExecuteApis(planId,null,scenarioAndErrorMap,null);
}
// testPlanReportService.updateReport(testPlanReportIdList, runMode, lastReport.getTriggerMode(), scenarioIdList);

return lastReport;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,9 @@ public void saveResult(TestResult testResult, String runMode, String debugReport
} else if (StringUtils.equalsAny(runMode, ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name())) {
//测试计划定时任务-接口执行逻辑的话,需要同步测试计划的报告数据
if (StringUtils.equals(runMode, ApiRunMode.SCHEDULE_API_PLAN.name())) {
apiDefinitionExecResultService.saveApiResultByScheduleTask(testResult, ApiRunMode.SCHEDULE_API_PLAN.name());
List<String> testPlanReportIdList = new ArrayList<>();
testPlanReportIdList.add(debugReportId);
for (String testPlanReportId : testPlanReportIdList) { // 更新每个测试计划的状态
testPlanReportService.checkTestPlanStatus(testPlanReportId);
}
testPlanReportService.updateReport(testPlanReportIdList, ApiRunMode.SCHEDULE_API_PLAN.name(), ReportTriggerMode.SCHEDULE.name());
apiDefinitionExecResultService.saveApiResultByScheduleTask(testResult,debugReportId, ApiRunMode.SCHEDULE_API_PLAN.name(),ReportTriggerMode.SCHEDULE.name());
} else if (StringUtils.equals(runMode, ApiRunMode.JENKINS_API_PLAN.name())) {
apiDefinitionExecResultService.saveApiResultByScheduleTask(testResult, ApiRunMode.JENKINS_API_PLAN.name());
List<String> testPlanReportIdList = new ArrayList<>();
testPlanReportIdList.add(debugReportId);
for (String testPlanReportId : testPlanReportIdList) { // 更新每个测试计划的状态
testPlanReportService.checkTestPlanStatus(testPlanReportId);
}
testPlanReportService.updateReport(testPlanReportIdList, ApiRunMode.JENKINS_API_PLAN.name(), ReportTriggerMode.API.name());
apiDefinitionExecResultService.saveApiResultByScheduleTask(testResult,debugReportId, ApiRunMode.JENKINS_API_PLAN.name(),ReportTriggerMode.API.name());
} else {
apiDefinitionExecResultService.saveApiResult(testResult, ApiRunMode.API_PLAN.name(), TriggerMode.MANUAL.name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.metersphere.api.dto.definition.ApiTestCaseRequest;
import io.metersphere.api.dto.definition.TestPlanApiCaseDTO;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.base.domain.TestPlanApiCase;
import org.apache.ibatis.annotations.Param;

Expand All @@ -21,4 +22,6 @@ public interface ExtTestPlanApiCaseMapper {
List<String> getStatusByTestPlanId(String id);

List<String> selectIds(@Param("request") ApiTestCaseRequest request);

ApiTestCaseWithBLOBs getApiTestCaseById(String testPlanApiCaseId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
WHERE test_plan_id = #{request.testPlanId} and api_case_id = #{request.apiCaseId}
)
</insert>

<select id="getApiTestCaseById" resultType="io.metersphere.base.domain.ApiTestCaseWithBLOBs">
SELECT t.* FROM api_test_case t
INNER JOIN test_plan_api_case tpac ON t.id = tpac.api_case_id
WHERE tpac.id = #{0}
</select>
<select id="list" resultType="io.metersphere.api.dto.definition.TestPlanApiCaseDTO">
select
t.id, t.environment_id, t.create_time, t.update_time,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
inner join project p on lt.project_id = p.id
<where>
tplc.test_plan_id = #{request.testPlanId}
<if test="request.caseIds != null and request.caseIds.size > 0">
AND lt.id IN
<foreach collection="request.caseIds" item="value" separator="," open="(" close=")">
#{value}
</foreach>
</if>
<if test="request.projectId != null and request.projectId != ''">
and lt.project_id = #{request.projectId}
</if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
and t.test_plan_id = #{request.planId}
</if>
where 1

<if test="request.scenarioIds != null and request.scenarioIds.size() > 0">
and c.id in
<foreach collection="request.scenarioIds" item="caseId" separator="," open="(" close=")">
#{caseId}
</foreach>
</if>
<if test="request.ids != null and request.ids.size() > 0">
<if test="request.projectId != null and request.projectId!=''">
and c.project_id = #{request.projectId}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.metersphere.commons.constants;

public enum TestPlanApiExecuteStatus {
PREPARE,RUNNING,SUCCESS,FAILD
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import io.metersphere.commons.constants.PermissionConstants;
import io.metersphere.commons.utils.PageUtils;
import io.metersphere.commons.utils.Pager;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.log.annotation.MsAuditLog;
import io.metersphere.service.CheckPermissionService;
import io.metersphere.track.dto.ApiRunConfigDTO;
Expand Down Expand Up @@ -162,4 +161,5 @@ public String runJenkins(@RequestBody TestplanRunRequest testplanRunRequest) {
String apiRunConfig = JSONObject.toJSONString(api);
return testPlanService.run(testplanRunRequest.getTestPlanId(), testplanRunRequest.getProjectId(), testplanRunRequest.getUserId(), testplanRunRequest.getTriggerMode(), apiRunConfig);
}

}
Loading

0 comments on commit b4a5a00

Please sign in to comment.