diff --git a/backend/src/main/java/io/metersphere/api/exec/api/ApiRetryOnFailureService.java b/backend/src/main/java/io/metersphere/api/exec/api/ApiRetryOnFailureService.java index e1709b7cee..164bdd5815 100644 --- a/backend/src/main/java/io/metersphere/api/exec/api/ApiRetryOnFailureService.java +++ b/backend/src/main/java/io/metersphere/api/exec/api/ApiRetryOnFailureService.java @@ -1,5 +1,5 @@ package io.metersphere.api.exec.api; public interface ApiRetryOnFailureService { - public String retry(String data, long retryNum); + public String retry(String data, long retryNum, boolean isCase); } diff --git a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioSerialService.java b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioSerialService.java index b795b29085..35acbfe1dc 100644 --- a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioSerialService.java +++ b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioSerialService.java @@ -35,6 +35,7 @@ import io.metersphere.dto.ResultDTO; import io.metersphere.plugin.core.MsTestElement; import io.metersphere.service.SystemParameterService; import io.metersphere.utils.LoggerUtil; +import io.metersphere.xpack.api.dto.MsRetryLoopController; import org.apache.commons.lang3.StringUtils; import org.apache.jorphan.collections.HashTree; import org.springframework.data.redis.core.RedisTemplate; @@ -138,6 +139,7 @@ public class ApiScenarioSerialService { SmoothWeighted.setServerConfig(runRequest.getPoolId(), redisTemplate); } // 开始执行 + LoggerUtil.info(new MsTestPlan().getJmx(runRequest.getHashTree())); jMeterService.run(runRequest); } catch (Exception e) { RemakeReportService remakeReportService = CommonBeanFactory.getBean(RemakeReportService.class); @@ -202,7 +204,7 @@ public class ApiScenarioSerialService { // 失败重试 if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) { 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; } @@ -214,8 +216,18 @@ public class ApiScenarioSerialService { group.setLabel(caseWithBLOBs.getName()); group.setName(runRequest.getReportId()); group.setProjectId(caseWithBLOBs.getProjectId()); - - MsTestElement testElement = parse(data, testId, envId, caseWithBLOBs.getProjectId()); + MsTestElement testElement = null; + 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.getHashTree().add(testElement); testPlan.getHashTree().add(group); diff --git a/backend/src/main/java/io/metersphere/api/exec/utils/GenerateHashTreeUtil.java b/backend/src/main/java/io/metersphere/api/exec/utils/GenerateHashTreeUtil.java index 12ef6a271f..621afd6921 100644 --- a/backend/src/main/java/io/metersphere/api/exec/utils/GenerateHashTreeUtil.java +++ b/backend/src/main/java/io/metersphere/api/exec/utils/GenerateHashTreeUtil.java @@ -27,6 +27,7 @@ import io.metersphere.plugin.core.MsTestElement; import io.metersphere.service.EnvironmentGroupProjectService; import io.metersphere.utils.LoggerUtil; import io.metersphere.vo.BooleanPool; +import io.metersphere.xpack.api.dto.MsRetryLoopController; import org.apache.commons.lang3.StringUtils; 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 elements = mapper.readValue(element.getString("hashTree"), + new TypeReference>() { + }); + retryObj.setHashTree(elements); + } + } catch (Exception e) { + LogUtil.error(e); + } + } + public static LinkedList getScenarioHashTree(String definition) { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); @@ -139,7 +157,7 @@ public class GenerateHashTreeUtil { // 失败重试 if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) { 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; } diff --git a/backend/src/main/java/io/metersphere/api/jmeter/APISingleResultListener.java b/backend/src/main/java/io/metersphere/api/jmeter/APISingleResultListener.java index 88c98b76b9..a54f187c96 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/APISingleResultListener.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/APISingleResultListener.java @@ -35,21 +35,21 @@ public class APISingleResultListener implements MsExecListener { @Override public void handleTeardownTest(List results, ResultDTO dto, Map kafkaConfig) { LoggerUtil.info("接收到执行结果开始处理报告【" + dto.getReportId() + " 】,资源【 " + dto.getTestId() + " 】"); - // 清理过程步骤 - results = RetryResultUtil.clearLoops(results); queues.addAll(results); } @Override public void testEnded(ResultDTO dto, Map kafkaConfig) { try { + // 清理过程步骤 + queues = RetryResultUtil.clearLoops(queues); + JMeterBase.resultFormatting(queues, dto); if (dto.isRetryEnable()) { LoggerUtil.info("重试结果处理【" + dto.getReportId() + " 】开始"); - RetryResultUtil.mergeRetryResults(queues); + RetryResultUtil.mergeRetryResults(dto.getRequestResults()); LoggerUtil.info("重试结果处理【" + dto.getReportId() + " 】结束"); } - JMeterBase.resultFormatting(queues, dto); dto.setConsole(FixedCapacityUtils.getJmeterLogger(dto.getReportId(), !StringUtils.equals(dto.getReportType(), RunModeConstants.SET_REPORT.toString()))); // 入库存储 CommonBeanFactory.getBean(TestResultService.class).saveResults(dto);