From 06a3a274aff096a60fc98c481ef358188aad16d8 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Mon, 26 Jun 2023 16:04:14 +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=8D=E5=90=8C=E6=AD=A5=E6=8E=A5=E6=94=B6=E6=8A=A5?= =?UTF-8?q?=E5=91=8A=E7=BB=93=E6=9E=9C=E6=9C=89=E6=97=B6=E9=97=B4=E5=B7=AE?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1027294 --user=赵勇 【接口测试】场景-生成报告-步骤显示返回结果未执行 https://www.tapd.cn/55049933/s/1386310 Signed-off-by: fit2-zhao --- .../api/exec/api/ApiCaseSerialService.java | 28 +------ .../scenario/ApiScenarioSerialService.java | 43 +---------- .../service/RemakeReportService.java | 74 ++++++++++++++++++- .../automation/report/SyncApiReportDetail.vue | 2 +- .../automation/scenario/EditApiScenario.vue | 5 +- 5 files changed, 79 insertions(+), 73 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java b/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java index 2f3543e525..19dfbc374f 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java @@ -10,17 +10,13 @@ import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.jmeter.NewDriverManager; import io.metersphere.api.jmeter.utils.ApiFakeErrorUtil; import io.metersphere.api.jmeter.utils.SmoothWeighted; -import io.metersphere.base.domain.ApiDefinitionExecResultWithBLOBs; import io.metersphere.base.domain.ApiExecutionQueueDetail; import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import io.metersphere.base.domain.TestPlanApiCase; -import io.metersphere.base.mapper.ApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ApiTestCaseMapper; import io.metersphere.base.mapper.plan.TestPlanApiCaseMapper; import io.metersphere.commons.constants.ApiRunMode; -import io.metersphere.commons.constants.CommonConstants; import io.metersphere.commons.constants.PropertyConstant; -import io.metersphere.commons.enums.ApiReportStatus; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.*; import io.metersphere.constants.RunModeConstants; @@ -31,13 +27,11 @@ import io.metersphere.plugin.core.MsTestElement; import io.metersphere.service.RemakeReportService; import io.metersphere.utils.LoggerUtil; import jakarta.annotation.Resource; -import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.jorphan.collections.HashTree; import org.json.JSONObject; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.util.*; @@ -50,8 +44,6 @@ public class ApiCaseSerialService { @Resource private JMeterService jMeterService; @Resource - private ApiDefinitionExecResultMapper apiDefinitionExecResultMapper; - @Resource private ApiTestCaseMapper apiTestCaseMapper; @Resource private RedisTemplate redisTemplate; @@ -91,29 +83,11 @@ public class ApiCaseSerialService { // 判断触发资源对象是用例 if (!GenerateHashTreeUtil.isSetReport(executionQueue.getReportType()) || StringUtils.equalsIgnoreCase(executionQueue.getRunMode(), ApiRunMode.DEFINITION.name())) { - updateDefinitionExecResultToRunning(queue, runRequest); + remakeReportService.updateApiReport(queue, runRequest); } jMeterService.run(runRequest); } - @Transactional(propagation = Propagation.NOT_SUPPORTED) - protected void updateDefinitionExecResultToRunning(ApiExecutionQueueDetail queue, JmeterRunRequestDTO runRequest) { - ApiDefinitionExecResultWithBLOBs execResult = apiDefinitionExecResultMapper.selectByPrimaryKey(queue.getReportId()); - if (execResult != null) { - if (MapUtils.isNotEmpty(runRequest.getExtendedParameters())) { - runRequest.getExtendedParameters().put(CommonConstants.USER_ID, execResult.getUserId()); - } else { - runRequest.setExtendedParameters(new HashMap() {{ - this.put(CommonConstants.USER_ID, execResult.getUserId()); - }}); - } - execResult.setStartTime(System.currentTimeMillis()); - execResult.setStatus(ApiReportStatus.RUNNING.name()); - apiDefinitionExecResultMapper.updateByPrimaryKeySelective(execResult); - LoggerUtil.info("进入串行模式,准备执行资源:[" + execResult.getName() + " ]", execResult.getId()); - } - } - private void initEnv(HashTree hashTree) { BaseEnvironmentService apiTestEnvironmentService = CommonBeanFactory.getBean(BaseEnvironmentService.class); HashTreeUtil hashTreeUtil = new HashTreeUtil(); diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioSerialService.java b/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioSerialService.java index c0ab2eda78..22ffe94518 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioSerialService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioSerialService.java @@ -6,15 +6,11 @@ import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.jmeter.utils.ApiFakeErrorUtil; import io.metersphere.api.jmeter.utils.SmoothWeighted; import io.metersphere.base.domain.ApiExecutionQueueDetail; -import io.metersphere.base.domain.ApiScenarioReport; import io.metersphere.base.domain.ApiScenarioWithBLOBs; import io.metersphere.base.domain.TestPlanApiScenario; import io.metersphere.base.mapper.ApiScenarioMapper; -import io.metersphere.base.mapper.ApiScenarioReportMapper; import io.metersphere.base.mapper.plan.TestPlanApiScenarioMapper; import io.metersphere.commons.constants.ApiRunMode; -import io.metersphere.commons.constants.CommonConstants; -import io.metersphere.commons.enums.ApiReportStatus; import io.metersphere.commons.utils.GenerateHashTreeUtil; import io.metersphere.commons.utils.HashTreeUtil; import io.metersphere.commons.utils.JSON; @@ -33,15 +29,12 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @Service @Transactional(rollbackFor = Exception.class) public class ApiScenarioSerialService { - @Resource - private ApiScenarioReportMapper apiScenarioReportMapper; @Resource private JMeterService jMeterService; @Resource @@ -108,44 +101,10 @@ public class ApiScenarioSerialService { return; } // 更新报告状态 - updateReportToRunning(queue, runRequest); + remakeReportService.updateScenarioReport(queue, runRequest); jMeterService.run(runRequest); } - /** - * 更新报告状态 - * - * @param queue - * @param runRequest - */ - public void updateReportToRunning(ApiExecutionQueueDetail queue, JmeterRunRequestDTO runRequest) { - if (!GenerateHashTreeUtil.isSetReport(runRequest.getReportType()) && - StringUtils.equalsAny(runRequest.getRunMode(), - ApiRunMode.SCENARIO.name(), - ApiRunMode.SCENARIO_PLAN.name(), - ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), - ApiRunMode.SCHEDULE_SCENARIO.name(), - ApiRunMode.JENKINS_SCENARIO_PLAN.name(), - ApiRunMode.UI_SCENARIO.name(), - ApiRunMode.UI_SCENARIO_PLAN.name(), - ApiRunMode.UI_JENKINS_SCENARIO_PLAN.name(), - ApiRunMode.UI_SCHEDULE_SCENARIO.name(), - ApiRunMode.UI_SCHEDULE_SCENARIO_PLAN.name()) - ) { - ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(queue.getReportId()); - if (report != null) { - report.setStatus(ApiReportStatus.RUNNING.name()); - report.setCreateTime(System.currentTimeMillis()); - report.setUpdateTime(System.currentTimeMillis()); - runRequest.setExtendedParameters(new HashMap<>() {{ - this.put(CommonConstants.USER_ID, report.getCreateUser()); - }}); - apiScenarioReportMapper.updateByPrimaryKey(report); - LoggerUtil.info("进入串行模式,准备执行资源:[ " + report.getName() + " ]", report.getId()); - } - } - } - private void initEnv(HashTree hashTree) { HashTreeUtil hashTreeUtil = new HashTreeUtil(); Map> envParamsMap = hashTreeUtil.getEnvParamsDataByHashTree(hashTree, apiTestEnvironmentService); diff --git a/api-test/backend/src/main/java/io/metersphere/service/RemakeReportService.java b/api-test/backend/src/main/java/io/metersphere/service/RemakeReportService.java index 3b445883d1..1f20d0664c 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/RemakeReportService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/RemakeReportService.java @@ -2,15 +2,29 @@ package io.metersphere.service; import io.metersphere.api.exec.queue.PoolExecBlockingQueueUtil; import io.metersphere.api.jmeter.ApiLocalRunner; +import io.metersphere.base.domain.ApiDefinitionExecResultWithBLOBs; +import io.metersphere.base.domain.ApiExecutionQueueDetail; +import io.metersphere.base.domain.ApiScenarioReport; +import io.metersphere.base.mapper.ApiDefinitionExecResultMapper; +import io.metersphere.base.mapper.ApiScenarioReportMapper; +import io.metersphere.commons.constants.ApiRunMode; +import io.metersphere.commons.constants.CommonConstants; +import io.metersphere.commons.enums.ApiReportStatus; import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.FixedCapacityUtil; +import io.metersphere.commons.utils.GenerateHashTreeUtil; import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.ResultDTO; import io.metersphere.utils.LoggerUtil; import jakarta.annotation.Resource; +import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +import java.util.HashMap; @Service public class RemakeReportService { @@ -20,6 +34,10 @@ public class RemakeReportService { @Resource @Lazy private TestResultService testResultService; + @Resource + private ApiDefinitionExecResultMapper apiDefinitionExecResultMapper; + @Resource + private ApiScenarioReportMapper apiScenarioReportMapper; public void queueNext(JmeterRunRequestDTO request, String errorMsg) { try { @@ -41,7 +59,7 @@ public class RemakeReportService { queueService.checkTestPlanCaseTestEnd(dto.getTestId(), dto.getRunMode(), dto.getTestPlanReportId()); } catch (Exception e) { LoggerUtil.error("回退报告异常", request.getReportId(), e); - }finally { + } finally { ApiLocalRunner.clearCache(request.getReportId()); } } @@ -61,4 +79,58 @@ public class RemakeReportService { updateReport(request, errorMsg); queueNext(request, errorMsg); } + + @Transactional(propagation = Propagation.REQUIRES_NEW) + public void updateApiReport(ApiExecutionQueueDetail queue, JmeterRunRequestDTO runRequest) { + ApiDefinitionExecResultWithBLOBs execResult = apiDefinitionExecResultMapper.selectByPrimaryKey(queue.getReportId()); + if (execResult != null) { + if (MapUtils.isNotEmpty(runRequest.getExtendedParameters())) { + runRequest.getExtendedParameters().put(CommonConstants.USER_ID, execResult.getUserId()); + } else { + runRequest.setExtendedParameters(new HashMap<>() {{ + this.put(CommonConstants.USER_ID, execResult.getUserId()); + }}); + } + execResult.setStartTime(System.currentTimeMillis()); + execResult.setStatus(ApiReportStatus.RUNNING.name()); + apiDefinitionExecResultMapper.updateByPrimaryKeySelective(execResult); + LoggerUtil.info("进入串行模式,准备执行资源:[" + execResult.getName() + " ]", execResult.getId()); + } + } + + /** + * 更新报告状态 + * + * @param queue + * @param runRequest + */ + @Transactional(propagation = Propagation.REQUIRES_NEW) + public void updateScenarioReport(ApiExecutionQueueDetail queue, JmeterRunRequestDTO runRequest) { + if (!GenerateHashTreeUtil.isSetReport(runRequest.getReportType()) && + StringUtils.equalsAny(runRequest.getRunMode(), + ApiRunMode.SCENARIO.name(), + ApiRunMode.SCENARIO_PLAN.name(), + ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), + ApiRunMode.SCHEDULE_SCENARIO.name(), + ApiRunMode.JENKINS_SCENARIO_PLAN.name(), + ApiRunMode.UI_SCENARIO.name(), + ApiRunMode.UI_SCENARIO_PLAN.name(), + ApiRunMode.UI_JENKINS_SCENARIO_PLAN.name(), + ApiRunMode.UI_SCHEDULE_SCENARIO.name(), + ApiRunMode.UI_SCHEDULE_SCENARIO_PLAN.name()) + ) { + ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(queue.getReportId()); + if (report != null) { + report.setStatus(ApiReportStatus.RUNNING.name()); + report.setCreateTime(System.currentTimeMillis()); + report.setUpdateTime(System.currentTimeMillis()); + runRequest.setExtendedParameters(new HashMap<>() {{ + this.put(CommonConstants.USER_ID, report.getCreateUser()); + }}); + apiScenarioReportMapper.updateByPrimaryKey(report); + LoggerUtil.info("进入串行模式,准备执行资源:[ " + report.getName() + " ]", report.getId()); + } + } + } + } diff --git a/api-test/frontend/src/business/automation/report/SyncApiReportDetail.vue b/api-test/frontend/src/business/automation/report/SyncApiReportDetail.vue index 5eb7160cb4..978232c3da 100644 --- a/api-test/frontend/src/business/automation/report/SyncApiReportDetail.vue +++ b/api-test/frontend/src/business/automation/report/SyncApiReportDetail.vue @@ -348,7 +348,7 @@ export default { this.report = response.data || {}; if (response.data) { if (response.data.status === 'RUNNING') { - setTimeout(this.getReport, 2000); + setTimeout(this.getReport, 5000); } else { this.content = JSON.parse(response.data.content); if (!this.content) { diff --git a/api-test/frontend/src/business/automation/scenario/EditApiScenario.vue b/api-test/frontend/src/business/automation/scenario/EditApiScenario.vue index 1d73c5decd..0f1190dae0 100644 --- a/api-test/frontend/src/business/automation/scenario/EditApiScenario.vue +++ b/api-test/frontend/src/business/automation/scenario/EditApiScenario.vue @@ -1171,7 +1171,8 @@ export default { }, margeTransaction(item, console, arr) { arr.forEach((sub) => { - if (item.data && item.data.id + '_' + item.data.parentIndex === sub.resourceId) { + if (item.data && item.data.id + '_' + item.data.parentIndex === sub.resourceId + && item.data.requestResult.length === 0) { sub.responseResult.console = console; item.data.requestResult.push(sub); // 更新父节点状态 @@ -1179,7 +1180,7 @@ export default { item.data.testing = false; item.data.debug = true; } - if (sub.subRequestResults && sub.subRequestResults.length > 0) { + if (sub.method === 'Request' && sub.subRequestResults && sub.subRequestResults.length > 0) { this.margeTransaction(item, console, sub.subRequestResults); } });