fix(接口测试): 修复CASE重试结果存储问题

--bug=1014528 --user=赵勇 【测试跟踪】测试计划选择失败重试n次,执行此接口用例后,会显示n个stop状态 https://www.tapd.cn/55049933/s/1194283
This commit is contained in:
fit2-zhao 2022-07-04 18:44:08 +08:00 committed by fit2-zhao
parent 994e30e9d5
commit d008f6f40a
4 changed files with 39 additions and 9 deletions

View File

@ -1,5 +1,5 @@
package io.metersphere.api.exec.api; package io.metersphere.api.exec.api;
public interface ApiRetryOnFailureService { public interface ApiRetryOnFailureService {
public String retry(String data, long retryNum); public String retry(String data, long retryNum, boolean isCase);
} }

View File

@ -35,6 +35,7 @@ import io.metersphere.dto.ResultDTO;
import io.metersphere.plugin.core.MsTestElement; import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.SystemParameterService; import io.metersphere.service.SystemParameterService;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import io.metersphere.xpack.api.dto.MsRetryLoopController;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
@ -138,6 +139,7 @@ public class ApiScenarioSerialService {
SmoothWeighted.setServerConfig(runRequest.getPoolId(), redisTemplate); SmoothWeighted.setServerConfig(runRequest.getPoolId(), redisTemplate);
} }
// 开始执行 // 开始执行
LoggerUtil.info(new MsTestPlan().getJmx(runRequest.getHashTree()));
jMeterService.run(runRequest); jMeterService.run(runRequest);
} catch (Exception e) { } catch (Exception e) {
RemakeReportService remakeReportService = CommonBeanFactory.getBean(RemakeReportService.class); RemakeReportService remakeReportService = CommonBeanFactory.getBean(RemakeReportService.class);
@ -202,7 +204,7 @@ public class ApiScenarioSerialService {
// 失败重试 // 失败重试
if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) { if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) {
ApiRetryOnFailureService apiRetryOnFailureService = CommonBeanFactory.getBean(ApiRetryOnFailureService.class); ApiRetryOnFailureService apiRetryOnFailureService = CommonBeanFactory.getBean(ApiRetryOnFailureService.class);
String retryData = apiRetryOnFailureService.retry(data, runRequest.getRetryNum()); String retryData = apiRetryOnFailureService.retry(data, runRequest.getRetryNum(), true);
data = StringUtils.isNotEmpty(retryData) ? retryData : data; data = StringUtils.isNotEmpty(retryData) ? retryData : data;
} }
@ -214,8 +216,18 @@ public class ApiScenarioSerialService {
group.setLabel(caseWithBLOBs.getName()); group.setLabel(caseWithBLOBs.getName());
group.setName(runRequest.getReportId()); group.setName(runRequest.getReportId());
group.setProjectId(caseWithBLOBs.getProjectId()); group.setProjectId(caseWithBLOBs.getProjectId());
MsTestElement testElement = null;
MsTestElement testElement = parse(data, testId, envId, caseWithBLOBs.getProjectId()); if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) {
MsRetryLoopController controller = JSON.parseObject(data, MsRetryLoopController.class);
GenerateHashTreeUtil.parse(data, controller);
MsTestElement element = parse(JSON.toJSONString(controller.getHashTree().get(0)), testId, envId, caseWithBLOBs.getProjectId());
controller.setHashTree(new LinkedList<>() {{
this.add(element);
}});
testElement = controller;
} else {
testElement = parse(data, testId, envId, caseWithBLOBs.getProjectId());
}
group.setHashTree(new LinkedList<>()); group.setHashTree(new LinkedList<>());
group.getHashTree().add(testElement); group.getHashTree().add(testElement);
testPlan.getHashTree().add(group); testPlan.getHashTree().add(group);

View File

@ -27,6 +27,7 @@ import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.EnvironmentGroupProjectService; import io.metersphere.service.EnvironmentGroupProjectService;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import io.metersphere.vo.BooleanPool; import io.metersphere.vo.BooleanPool;
import io.metersphere.xpack.api.dto.MsRetryLoopController;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
@ -71,6 +72,23 @@ public class GenerateHashTreeUtil {
} }
} }
public static void parse(String retryCase, MsRetryLoopController retryObj) {
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JSONObject element = JSON.parseObject(retryCase, Feature.DisableSpecialKeyDetect);
ElementUtil.dataFormatting(element);
if (element != null && StringUtils.isNotEmpty(element.getString("hashTree"))) {
LinkedList<MsTestElement> elements = mapper.readValue(element.getString("hashTree"),
new TypeReference<LinkedList<MsTestElement>>() {
});
retryObj.setHashTree(elements);
}
} catch (Exception e) {
LogUtil.error(e);
}
}
public static LinkedList<MsTestElement> getScenarioHashTree(String definition) { public static LinkedList<MsTestElement> getScenarioHashTree(String definition) {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@ -139,7 +157,7 @@ public class GenerateHashTreeUtil {
// 失败重试 // 失败重试
if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) { if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) {
ApiRetryOnFailureService apiRetryOnFailureService = CommonBeanFactory.getBean(ApiRetryOnFailureService.class); ApiRetryOnFailureService apiRetryOnFailureService = CommonBeanFactory.getBean(ApiRetryOnFailureService.class);
String retryData = apiRetryOnFailureService.retry(data, runRequest.getRetryNum()); String retryData = apiRetryOnFailureService.retry(data, runRequest.getRetryNum(), false);
data = StringUtils.isNotEmpty(retryData) ? retryData : data; data = StringUtils.isNotEmpty(retryData) ? retryData : data;
} }

View File

@ -35,21 +35,21 @@ public class APISingleResultListener implements MsExecListener {
@Override @Override
public void handleTeardownTest(List<SampleResult> results, ResultDTO dto, Map<String, Object> kafkaConfig) { public void handleTeardownTest(List<SampleResult> results, ResultDTO dto, Map<String, Object> kafkaConfig) {
LoggerUtil.info("接收到执行结果开始处理报告【" + dto.getReportId() + " 】,资源【 " + dto.getTestId() + ""); LoggerUtil.info("接收到执行结果开始处理报告【" + dto.getReportId() + " 】,资源【 " + dto.getTestId() + "");
// 清理过程步骤
results = RetryResultUtil.clearLoops(results);
queues.addAll(results); queues.addAll(results);
} }
@Override @Override
public void testEnded(ResultDTO dto, Map<String, Object> kafkaConfig) { public void testEnded(ResultDTO dto, Map<String, Object> kafkaConfig) {
try { try {
// 清理过程步骤
queues = RetryResultUtil.clearLoops(queues);
JMeterBase.resultFormatting(queues, dto);
if (dto.isRetryEnable()) { if (dto.isRetryEnable()) {
LoggerUtil.info("重试结果处理【" + dto.getReportId() + " 】开始"); LoggerUtil.info("重试结果处理【" + dto.getReportId() + " 】开始");
RetryResultUtil.mergeRetryResults(queues); RetryResultUtil.mergeRetryResults(dto.getRequestResults());
LoggerUtil.info("重试结果处理【" + dto.getReportId() + " 】结束"); LoggerUtil.info("重试结果处理【" + dto.getReportId() + " 】结束");
} }
JMeterBase.resultFormatting(queues, dto);
dto.setConsole(FixedCapacityUtils.getJmeterLogger(dto.getReportId(), !StringUtils.equals(dto.getReportType(), RunModeConstants.SET_REPORT.toString()))); dto.setConsole(FixedCapacityUtils.getJmeterLogger(dto.getReportId(), !StringUtils.equals(dto.getReportType(), RunModeConstants.SET_REPORT.toString())));
// 入库存储 // 入库存储
CommonBeanFactory.getBean(TestResultService.class).saveResults(dto); CommonBeanFactory.getBean(TestResultService.class).saveResults(dto);