From abda752b585eb794267d6e087d0a1b59536ed799 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 13 Jan 2022 12:52:54 +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=A4=B1=E8=B4=A5=E5=81=9C=E6=AD=A2=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E4=B8=8D=E7=94=9F=E6=95=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1009551 --user=赵勇 【接口测试】批量执行接口用例、场景,失败停止没有生效 https://www.tapd.cn/55049933/s/1091264 --- .../api/exec/api/ApiCaseExecuteService.java | 7 +-- .../api/exec/queue/DBTestQueue.java | 1 + .../scenario/ApiScenarioExecuteService.java | 12 +++- .../api/service/ApiExecutionQueueService.java | 47 ++++++++++++++- .../base/domain/ApiExecutionQueue.java | 5 +- .../base/domain/ApiExecutionQueueExample.java | 60 +++++++++++++++++++ .../base/mapper/ApiExecutionQueueMapper.xml | 29 ++++++--- .../ext/ExtApiDefinitionExecResultMapper.java | 3 + .../ext/ExtApiDefinitionExecResultMapper.xml | 6 ++ .../ext/ExtApiScenarioReportMapper.java | 2 + .../mapper/ext/ExtApiScenarioReportMapper.xml | 19 ++++-- .../db/migration/V105__v1.16.5_release.sql | 1 + 12 files changed, 170 insertions(+), 22 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java index 092260850a..aefea80e15 100644 --- a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java +++ b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseExecuteService.java @@ -16,13 +16,11 @@ import io.metersphere.base.mapper.ext.ExtApiTestCaseMapper; import io.metersphere.commons.constants.APITestStatus; import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.TriggerMode; -import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.ServiceUtils; import io.metersphere.constants.RunModeConstants; import io.metersphere.dto.MsExecResponseDTO; import io.metersphere.dto.RunModeConfigDTO; import io.metersphere.service.EnvironmentGroupProjectService; -import io.metersphere.track.service.TestPlanReportService; import io.metersphere.utils.LoggerUtil; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -104,7 +102,7 @@ public class ApiCaseExecuteService { String reportType = request.getConfig().getReportType(); String poolId = request.getConfig().getResourcePoolId(); String runMode = StringUtils.equals(request.getTriggerMode(), TriggerMode.MANUAL.name()) ? ApiRunMode.API_PLAN.name() : ApiRunMode.SCHEDULE_API_PLAN.name(); - DBTestQueue deQueue = apiExecutionQueueService.add(executeQueue, poolId, ApiRunMode.API_PLAN.name(), request.getPlanReportId(), reportType, runMode, request.getConfig().getEnvMap()); + DBTestQueue deQueue = apiExecutionQueueService.add(executeQueue, poolId, ApiRunMode.API_PLAN.name(), request.getPlanReportId(), reportType, runMode, request.getConfig().getEnvMap(), request.getConfig().isOnSampleError()); // 开始选择执行模式 if (request.getConfig() != null && request.getConfig().getMode().equals(RunModeConstants.SERIAL.toString())) { @@ -159,6 +157,7 @@ public class ApiCaseExecuteService { list.forEach(caseWithBLOBs -> { ApiDefinitionExecResult report = ApiDefinitionExecResultUtil.initBase(caseWithBLOBs.getId(), APITestStatus.Running.name(), null, request.getConfig()); report.setStatus(status); + report.setName(caseWithBLOBs.getName()); batchMapper.insert(report); executeQueue.put(caseWithBLOBs.getId(), report); responseDTOS.add(new MsExecResponseDTO(caseWithBLOBs.getId(), report.getId(), request.getTriggerMode())); @@ -170,7 +169,7 @@ public class ApiCaseExecuteService { String reportType = request.getConfig().getReportType(); String poolId = request.getConfig().getResourcePoolId(); - DBTestQueue deQueue = apiExecutionQueueService.add(executeQueue, poolId, ApiRunMode.DEFINITION.name(), null, reportType, ApiRunMode.DEFINITION.name(), request.getConfig().getEnvMap()); + DBTestQueue deQueue = apiExecutionQueueService.add(executeQueue, poolId, ApiRunMode.DEFINITION.name(), null, reportType, ApiRunMode.DEFINITION.name(), request.getConfig().getEnvMap(), request.getConfig().isOnSampleError()); // 开始选择执行模式 if (request.getConfig().getMode().equals(RunModeConstants.SERIAL.toString())) { LoggerUtil.debug("开始串行执行"); diff --git a/backend/src/main/java/io/metersphere/api/exec/queue/DBTestQueue.java b/backend/src/main/java/io/metersphere/api/exec/queue/DBTestQueue.java index 088c14c7b0..a734b5bb6a 100644 --- a/backend/src/main/java/io/metersphere/api/exec/queue/DBTestQueue.java +++ b/backend/src/main/java/io/metersphere/api/exec/queue/DBTestQueue.java @@ -9,6 +9,7 @@ import java.util.Map; @Data public class DBTestQueue extends ApiExecutionQueue { + private String nowReportId; private ApiExecutionQueueDetail queue; private Map detailMap = new HashMap<>(); } diff --git a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java index 57ca19229f..65cbf7dfbc 100644 --- a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java +++ b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioExecuteService.java @@ -24,7 +24,10 @@ import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.ReportTriggerMode; import io.metersphere.commons.constants.TriggerMode; import io.metersphere.commons.exception.MSException; -import io.metersphere.commons.utils.*; +import io.metersphere.commons.utils.FileUtils; +import io.metersphere.commons.utils.LogUtil; +import io.metersphere.commons.utils.ServiceUtils; +import io.metersphere.commons.utils.SessionUtils; import io.metersphere.constants.RunModeConstants; import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.MsExecResponseDTO; @@ -32,7 +35,6 @@ import io.metersphere.dto.RunModeConfigDTO; import io.metersphere.i18n.Translator; import io.metersphere.plugin.core.MsTestElement; import io.metersphere.service.EnvironmentGroupProjectService; -import io.metersphere.track.service.TestPlanReportService; import io.metersphere.track.service.TestPlanScenarioCaseService; import io.metersphere.utils.LoggerUtil; import org.apache.commons.beanutils.BeanComparator; @@ -163,7 +165,11 @@ public class ApiScenarioExecuteService { // 开始执行 if (executeQueue != null && executeQueue.size() > 0) { String reportType = request.getConfig().getReportType(); - DBTestQueue executionQueue = apiExecutionQueueService.add(executeQueue, request.getConfig().getResourcePoolId(), ApiRunMode.SCENARIO.name(), serialReportId, reportType, request.getRunMode(), request.getConfig().getEnvMap()); + String planReportId = StringUtils.isNotEmpty(request.getTestPlanReportId()) ? request.getTestPlanReportId() : serialReportId; + DBTestQueue executionQueue = apiExecutionQueueService.add(executeQueue, request.getConfig().getResourcePoolId() + , ApiRunMode.SCENARIO.name(), planReportId, reportType, request.getRunMode(), request.getConfig().getEnvMap() + , request.getConfig().isOnSampleError()); + if (request.getConfig() != null && request.getConfig().getMode().equals(RunModeConstants.SERIAL.toString())) { if (StringUtils.isNotEmpty(serialReportId)) { apiScenarioReportStructureService.save(apiScenarios, serialReportId, request.getConfig() != null ? request.getConfig().getReportType() : null); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java b/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java index 79946e56ea..96a08a3414 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java @@ -10,7 +10,9 @@ import io.metersphere.base.mapper.ApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ApiExecutionQueueDetailMapper; import io.metersphere.base.mapper.ApiExecutionQueueMapper; import io.metersphere.base.mapper.ApiScenarioReportMapper; +import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ext.ExtApiExecutionQueueMapper; +import io.metersphere.base.mapper.ext.ExtApiScenarioReportMapper; import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.TestPlanReportStatus; import io.metersphere.commons.utils.BeanUtils; @@ -47,14 +49,20 @@ public class ApiExecutionQueueService { private ApiScenarioReportMapper apiScenarioReportMapper; @Resource private ApiDefinitionExecResultMapper apiDefinitionExecResultMapper; + @Resource + private ExtApiDefinitionExecResultMapper extApiDefinitionExecResultMapper; + @Resource + private ExtApiScenarioReportMapper extApiScenarioReportMapper; + @Resource private ExtApiExecutionQueueMapper extApiExecutionQueueMapper; - public DBTestQueue add(Object runObj, String poolId, String type, String reportId, String reportType, String runMode, Map envMap) { + public DBTestQueue add(Object runObj, String poolId, String type, String reportId, String reportType, String runMode, Map envMap, boolean failure) { ApiExecutionQueue executionQueue = new ApiExecutionQueue(); executionQueue.setId(UUID.randomUUID().toString()); executionQueue.setCreateTime(System.currentTimeMillis()); executionQueue.setPoolId(poolId); + executionQueue.setFailure(failure); executionQueue.setReportId(reportId); executionQueue.setReportType(StringUtils.isNotEmpty(reportType) ? reportType : RunModeConstants.INDEPENDENCE.toString()); executionQueue.setRunMode(runMode); @@ -129,6 +137,7 @@ public class ApiExecutionQueueService { if (CollectionUtils.isNotEmpty(queues)) { List list = queues.stream().filter(item -> StringUtils.equals(item.getTestId(), testId)).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(list)) { + queue.setNowReportId(list.get(0).getReportId()); executionQueueDetailMapper.deleteByPrimaryKey(list.get(0).getId()); queues.remove(list.get(0)); BeanUtils.copyBean(queue, executionQueue); @@ -151,6 +160,42 @@ public class ApiExecutionQueueService { public void queueNext(ResultDTO dto) { DBTestQueue executionQueue = this.edit(dto.getQueueId(), dto.getTestId()); if (executionQueue != null) { + if (executionQueue.getFailure()) { + LoggerUtil.info("进入失败停止处理:" + executionQueue.getId()); + boolean isError = false; + if (StringUtils.equalsAny(dto.getRunMode(), ApiRunMode.SCENARIO.name(), + ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), + ApiRunMode.SCHEDULE_SCENARIO.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) { + ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(executionQueue.getNowReportId()); + if (report != null && StringUtils.equalsIgnoreCase(report.getStatus(), "Error")) { + isError = true; + } + } else { + ApiDefinitionExecResult result = apiDefinitionExecResultMapper.selectByPrimaryKey(executionQueue.getNowReportId()); + if (result != null && StringUtils.equalsIgnoreCase(result.getStatus(), "Error")) { + isError = true; + } + } + if (isError) { + ApiExecutionQueueDetailExample example = new ApiExecutionQueueDetailExample(); + example.createCriteria().andQueueIdEqualTo(dto.getQueueId()); + + if (StringUtils.isNotEmpty(dto.getTestPlanReportId())) { + CommonBeanFactory.getBean(TestPlanReportService.class).finishedTestPlanReport(dto.getTestPlanReportId(), "Stopped"); + } + // 更新未执行的报告状态 + List details = executionQueueDetailMapper.selectByExample(example); + List reportIds = details.stream().map(ApiExecutionQueueDetail::getReportId).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(reportIds)) { + extApiDefinitionExecResultMapper.update(reportIds); + extApiScenarioReportMapper.update(reportIds); + } + // 清除队列 + executionQueueDetailMapper.deleteByExample(example); + queueMapper.deleteByPrimaryKey(executionQueue.getId()); + return; + } + } LoggerUtil.info("开始处理执行队列:" + executionQueue.getId()); if (executionQueue.getQueue() != null && StringUtils.isNotEmpty(executionQueue.getQueue().getTestId())) { if (StringUtils.equals(dto.getRunType(), RunModeConstants.SERIAL.toString())) { diff --git a/backend/src/main/java/io/metersphere/base/domain/ApiExecutionQueue.java b/backend/src/main/java/io/metersphere/base/domain/ApiExecutionQueue.java index 8c62136d80..485514a5e0 100644 --- a/backend/src/main/java/io/metersphere/base/domain/ApiExecutionQueue.java +++ b/backend/src/main/java/io/metersphere/base/domain/ApiExecutionQueue.java @@ -1,8 +1,9 @@ package io.metersphere.base.domain; -import java.io.Serializable; import lombok.Data; +import java.io.Serializable; + @Data public class ApiExecutionQueue implements Serializable { private String id; @@ -17,5 +18,7 @@ public class ApiExecutionQueue implements Serializable { private Long createTime; + private Boolean failure; + private static final long serialVersionUID = 1L; } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/domain/ApiExecutionQueueExample.java b/backend/src/main/java/io/metersphere/base/domain/ApiExecutionQueueExample.java index dbcd79af0c..479c6ee1c1 100644 --- a/backend/src/main/java/io/metersphere/base/domain/ApiExecutionQueueExample.java +++ b/backend/src/main/java/io/metersphere/base/domain/ApiExecutionQueueExample.java @@ -513,6 +513,66 @@ public class ApiExecutionQueueExample { addCriterion("create_time not between", value1, value2, "createTime"); return (Criteria) this; } + + public Criteria andFailureIsNull() { + addCriterion("failure is null"); + return (Criteria) this; + } + + public Criteria andFailureIsNotNull() { + addCriterion("failure is not null"); + return (Criteria) this; + } + + public Criteria andFailureEqualTo(Boolean value) { + addCriterion("failure =", value, "failure"); + return (Criteria) this; + } + + public Criteria andFailureNotEqualTo(Boolean value) { + addCriterion("failure <>", value, "failure"); + return (Criteria) this; + } + + public Criteria andFailureGreaterThan(Boolean value) { + addCriterion("failure >", value, "failure"); + return (Criteria) this; + } + + public Criteria andFailureGreaterThanOrEqualTo(Boolean value) { + addCriterion("failure >=", value, "failure"); + return (Criteria) this; + } + + public Criteria andFailureLessThan(Boolean value) { + addCriterion("failure <", value, "failure"); + return (Criteria) this; + } + + public Criteria andFailureLessThanOrEqualTo(Boolean value) { + addCriterion("failure <=", value, "failure"); + return (Criteria) this; + } + + public Criteria andFailureIn(List values) { + addCriterion("failure in", values, "failure"); + return (Criteria) this; + } + + public Criteria andFailureNotIn(List values) { + addCriterion("failure not in", values, "failure"); + return (Criteria) this; + } + + public Criteria andFailureBetween(Boolean value1, Boolean value2) { + addCriterion("failure between", value1, value2, "failure"); + return (Criteria) this; + } + + public Criteria andFailureNotBetween(Boolean value1, Boolean value2) { + addCriterion("failure not between", value1, value2, "failure"); + return (Criteria) this; + } } public static class Criteria extends GeneratedCriteria { diff --git a/backend/src/main/java/io/metersphere/base/mapper/ApiExecutionQueueMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ApiExecutionQueueMapper.xml index 9fc157c7f0..4f1f339b64 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ApiExecutionQueueMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ApiExecutionQueueMapper.xml @@ -8,6 +8,7 @@ + @@ -68,7 +69,7 @@ - id, report_id, report_type, run_mode, pool_id, create_time + id, report_id, report_type, run_mode, pool_id, create_time, failure @@ -178,6 +185,9 @@ create_time = #{record.createTime,jdbcType=BIGINT}, + + failure = #{record.failure,jdbcType=BIT}, + @@ -190,7 +200,8 @@ report_type = #{record.reportType,jdbcType=VARCHAR}, run_mode = #{record.runMode,jdbcType=VARCHAR}, pool_id = #{record.poolId,jdbcType=VARCHAR}, - create_time = #{record.createTime,jdbcType=BIGINT} + create_time = #{record.createTime,jdbcType=BIGINT}, + failure = #{record.failure,jdbcType=BIT} @@ -213,6 +224,9 @@ create_time = #{createTime,jdbcType=BIGINT}, + + failure = #{failure,jdbcType=BIT}, + where id = #{id,jdbcType=VARCHAR} @@ -222,7 +236,8 @@ report_type = #{reportType,jdbcType=VARCHAR}, run_mode = #{runMode,jdbcType=VARCHAR}, pool_id = #{poolId,jdbcType=VARCHAR}, - create_time = #{createTime,jdbcType=BIGINT} + create_time = #{createTime,jdbcType=BIGINT}, + failure = #{failure,jdbcType=BIT} where id = #{id,jdbcType=VARCHAR} \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.java index 14aabfb7e1..4e67ec94f6 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.java @@ -32,4 +32,7 @@ public interface ExtApiDefinitionExecResultMapper { List selectApiResultByProjectId(String projectId); List selectForPlanReport(@Param("ids") List apiReportIds); + + void update(@Param("ids") List ids); + } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.xml index c0aaccdaaa..323b1cb9c0 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiDefinitionExecResultMapper.xml @@ -124,4 +124,10 @@ + + update api_definition_exec_result set status="STOP" where id in + + #{value} + + diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioReportMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioReportMapper.java index 0550580099..a028b4861e 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioReportMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioReportMapper.java @@ -37,4 +37,6 @@ public interface ExtApiScenarioReportMapper { List selectReportByProjectId(String projectId); List selectForPlanReport(@Param("ids") List reportIds); + + void update(@Param("ids") List ids); } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioReportMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioReportMapper.xml index 60ae4e50e7..27cc0455be 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioReportMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioReportMapper.xml @@ -252,18 +252,18 @@ SELECT id AS id,create_time AS createTime, scenario_id AS scenario_id FROM api_scenario_report WHERE execute_type = 'Saved' AND scenario_id in - #{value} - + #{value} + UNION SELECT report.id AS id,report.create_time AS createTime,planScenario.api_scenario_id AS scenario_id FROM api_scenario_report report INNER JOIN test_plan_api_scenario planScenario ON report.scenario_id = planScenario.id WHERE report.execute_type = 'Saved' AND planScenario.api_scenario_id in - #{value} - - ORDER BY createTime DESC - ) a GROUP BY a.scenario_id + #{value} + + ORDER BY createTime DESC + ) a GROUP BY a.scenario_id ) orderData ON orderData.id = report.id; @@ -309,4 +309,11 @@ #{value} + + + update api_scenario_report set status="STOP" where id in + + #{value} + + diff --git a/backend/src/main/resources/db/migration/V105__v1.16.5_release.sql b/backend/src/main/resources/db/migration/V105__v1.16.5_release.sql index b7f226d171..70e69ee6c3 100644 --- a/backend/src/main/resources/db/migration/V105__v1.16.5_release.sql +++ b/backend/src/main/resources/db/migration/V105__v1.16.5_release.sql @@ -1,5 +1,6 @@ ALTER TABLE test_plan_report_content ADD plan_scenario_report_struct longtext NULL; ALTER TABLE test_plan_report_content ADD plan_api_case_report_struct longtext NULL; ALTER TABLE test_plan_report_content ADD plan_load_case_report_struct longtext NULL; +ALTER TABLE api_execution_queue ADD failure TINYINT(1); CREATE INDEX api_scenario_report_result_report_id_IDX ON api_scenario_report_result (report_id);