refactor(接口测试): 优化处理执行过程中服务重启,报告状态持续running情况

This commit is contained in:
fit2-zhao 2022-07-25 13:06:28 +08:00 committed by f2c-ci-robot[bot]
parent edf8d46ec2
commit f25d0880d4
10 changed files with 63 additions and 10 deletions

View File

@ -141,9 +141,8 @@ public class ApiCaseSerialService {
group.setLabel(caseWithBLOBs.getName());
group.setName(runRequest.getReportId());
group.setProjectId(caseWithBLOBs.getProjectId());
MsTestElement testElement = parse(data, testId, envId, caseWithBLOBs.getProjectId());
// 暂时保留用例的重试功能
/*if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) {
MsTestElement testElement;
if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) {
// 失败重试
ApiRetryOnFailureService apiRetryOnFailureService = CommonBeanFactory.getBean(ApiRetryOnFailureService.class);
String retryData = apiRetryOnFailureService.retry(data, runRequest.getRetryNum(), true);
@ -156,7 +155,7 @@ public class ApiCaseSerialService {
}});
} else {
testElement = parse(data, testId, envId, caseWithBLOBs.getProjectId());
}*/
}
group.setHashTree(new LinkedList<>());
group.getHashTree().add(testElement);

View File

@ -15,12 +15,14 @@ import io.metersphere.base.mapper.*;
import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper;
import io.metersphere.base.mapper.ext.ExtApiExecutionQueueMapper;
import io.metersphere.base.mapper.ext.ExtApiScenarioReportMapper;
import io.metersphere.base.mapper.ext.ExtTestPlanReportMapper;
import io.metersphere.commons.constants.APITestStatus;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.constants.ExecuteResult;
import io.metersphere.commons.constants.TestPlanReportStatus;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.ResultDTO;
import io.metersphere.dto.RunModeConfigDTO;
@ -69,6 +71,8 @@ public class ApiExecutionQueueService {
private ApiScenarioReportResultMapper apiScenarioReportResultMapper;
@Resource
private ApiCaseSerialService apiCaseSerialService;
@Resource
protected ExtTestPlanReportMapper extTestPlanReportMapper;
@Transactional(propagation = Propagation.REQUIRES_NEW)
public DBTestQueue add(Object runObj, String poolId, String type, String reportId, String reportType, String runMode, RunModeConfigDTO config) {
@ -528,4 +532,23 @@ public class ApiExecutionQueueService {
}
}
}
/**
* 服务异常重启处理
*/
public void exceptionHandling() {
LogUtil.info("开始处理服务重启导致执行未完成的报告状态");
// 清理残留队列
ApiExecutionQueueExample example = new ApiExecutionQueueExample();
queueMapper.deleteByExample(example);
ApiExecutionQueueDetailExample detailExample = new ApiExecutionQueueDetailExample();
executionQueueDetailMapper.deleteByExample(detailExample);
// 更新报告状态
extApiScenarioReportMapper.updateAllStatus();
// 更新用例报告状态
extApiDefinitionExecResultMapper.updateAllStatus();
// 更新测试计划报告状态
extTestPlanReportMapper.updateAllStatus();
LogUtil.info("处理服务重启导致执行未完成的报告状态完成");
}
}

View File

@ -214,10 +214,23 @@ public class ApiJmeterFileService {
return multipartFiles;
}
private String replaceJmx(String jmx) {
jmx = StringUtils.replace(jmx, "<DubboSample", "<io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample");
jmx = StringUtils.replace(jmx, "</DubboSample>", "</io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample>");
jmx = StringUtils.replace(jmx, " guiclass=\"DubboSampleGui\" ", " guiclass=\"io.github.ningyu.jmeter.plugin.dubbo.gui.DubboSampleGui\" ");
jmx = StringUtils.replace(jmx, " guiclass=\"DubboDefaultConfigGui\" ", " guiclass=\"io.github.ningyu.jmeter.plugin.dubbo.gui.DubboDefaultConfigGui\" ");
jmx = StringUtils.replace(jmx, " testclass=\"DubboSample\" ", " testclass=\"io.github.ningyu.jmeter.plugin.dubbo.sample.DubboSample\" ");
return jmx;
}
private byte[] zipFilesToByteArray(String testId, HashTree hashTree) {
String bodyFilePath = FileUtils.BODY_FILE_DIR;
String fileName = testId + ".jmx";
String jmx = new MsTestPlan().getJmx(hashTree);
// 处理dubbo请求生成jmx文件
if (StringUtils.isNotEmpty(jmx)) {
jmx = replaceJmx(jmx);
}
Map<String, byte[]> files = new HashMap<>();
// 每个测试生成一个文件夹
files.put(fileName, jmx.getBytes(StandardCharsets.UTF_8));

View File

@ -58,4 +58,6 @@ public interface ExtApiDefinitionExecResultMapper {
List<String> selectByProjectIdAndLessThanTime(@Param("projectId") String projectId, @Param("time") long time);
List<ApiDefinitionExecResultWithBLOBs> selectByResourceIdsAndMaxCreateTime(@Param("ids") List<String> resourceIds);
void updateAllStatus();
}

View File

@ -494,4 +494,8 @@
and create_time &lt; #{time}
</select>
<update id="updateAllStatus">
update api_definition_exec_result set status="STOP" where status in ('Rerunning','Running','Waiting')
</update>
</mapper>

View File

@ -48,4 +48,6 @@ public interface ExtApiScenarioReportMapper {
List<ApiScenarioReport> findByProjectIds(@Param("request") TaskCenterRequest request);
List<String> selectByProjectIdAndLessThanTime(@Param("projectId") String projectId, @Param("time") long time);
void updateAllStatus();
}

View File

@ -470,4 +470,8 @@
and project_id = #{projectId}
</select>
<update id="updateAllStatus">
update api_scenario_report set status="STOP" where status in ('Rerunning','Running','Waiting')
</update>
</mapper>

View File

@ -20,4 +20,6 @@ public interface ExtTestPlanReportMapper {
@MapKey("id")
Map<String, ParamsDTO> reportCount(@Param("planIds") Set<String> planIds);
void updateAllStatus();
}

View File

@ -153,4 +153,9 @@
</where>
GROUP BY t.test_plan_id
</select>
<update id="updateAllStatus">
update test_plan_report set status="COMPLETED" where status in ('Rerunning','Running')
</update>
</mapper>

View File

@ -3,10 +3,7 @@ package io.metersphere.listener;
import io.metersphere.api.exec.queue.ExecThreadPoolExecutor;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.NewDriverManager;
import io.metersphere.api.service.ApiAutomationService;
import io.metersphere.api.service.ApiDefinitionService;
import io.metersphere.api.service.ApiTestCaseService;
import io.metersphere.api.service.MockConfigService;
import io.metersphere.api.service.*;
import io.metersphere.base.domain.JarConfig;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
@ -50,8 +47,6 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
@Resource
private TestCaseService testCaseService;
@Resource
private AttachmentService attachmentService;
@Resource
private ApiTestCaseService apiTestCaseService;
@Resource
private TestPlanTestCaseService testPlanTestCaseService;
@ -69,6 +64,8 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
private MockConfigService mockConfigService;
@Resource
private CustomFieldResourceService customFieldResourceService;
@Resource
private ApiExecutionQueueService apiExecutionQueueService;
@Value("${jmeter.home}")
private String jmeterHome;
@ -89,6 +86,8 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
System.setProperty("jmeter.home", jmeterHome);
pluginService.loadPlugins();
// 处理重启导致未执行完成的报告
apiExecutionQueueService.exceptionHandling();
// 设置并发队列核心数
BaseSystemConfigDTO dto = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo();
if (StringUtils.isNotEmpty(dto.getConcurrency())) {