From d008f6f40aa1e8510012baaf229b4f50e84dc1b7 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Mon, 4 Jul 2022 18:44:08 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8DCASE=E9=87=8D=E8=AF=95=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1014528 --user=赵勇 【测试跟踪】测试计划选择失败重试n次,执行此接口用例后,会显示n个stop状态 https://www.tapd.cn/55049933/s/1194283 --- .../exec/api/ApiRetryOnFailureService.java | 2 +- .../scenario/ApiScenarioSerialService.java | 18 ++++++++++++++--- .../api/exec/utils/GenerateHashTreeUtil.java | 20 ++++++++++++++++++- .../api/jmeter/APISingleResultListener.java | 8 ++++---- 4 files changed, 39 insertions(+), 9 deletions(-) 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);