fix(接口测试): 执行时设置请求数量参数

This commit is contained in:
AgAngle 2024-04-01 20:58:15 +08:00 committed by Craftsman
parent 987df3fa75
commit 17cb9cc57b
5 changed files with 23 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import io.metersphere.api.dto.request.http.MsHTTPElement;
import lombok.Data; import lombok.Data;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
/** /**
* 执行场景解析参数时的临时参数 * 执行场景解析参数时的临时参数
@ -50,4 +51,8 @@ public class ApiScenarioParseTmpParam {
* 环境相关信息 * 环境相关信息
*/ */
private ApiScenarioParseEnvInfo scenarioParseEnvInfo; private ApiScenarioParseEnvInfo scenarioParseEnvInfo;
/**
* 场景中的待执行的请求总数
*/
private AtomicLong requestCount = new AtomicLong(0);
} }

View File

@ -26,6 +26,7 @@ public class ApiBatchRunBaseService {
*/ */
public ExecutionQueue initExecutionqueue(List<String> resourceIds, ApiRunModeConfigDTO runModeConfig, String resourceType, Map<String, String> caseReportMap, String userId) { public ExecutionQueue initExecutionqueue(List<String> resourceIds, ApiRunModeConfigDTO runModeConfig, String resourceType, Map<String, String> caseReportMap, String userId) {
ExecutionQueue queue = getExecutionQueue(runModeConfig, resourceType, userId); ExecutionQueue queue = getExecutionQueue(runModeConfig, resourceType, userId);
queue.setRequestCount(runModeConfig.isIntegratedReport() ? resourceIds.size() : 1L);
List<ExecutionQueueDetail> queueDetails = getExecutionQueueDetails(resourceIds, caseReportMap); List<ExecutionQueueDetail> queueDetails = getExecutionQueueDetails(resourceIds, caseReportMap);
apiExecutionQueueService.insertQueue(queue, queueDetails); apiExecutionQueueService.insertQueue(queue, queueDetails);
return queue; return queue;
@ -57,6 +58,7 @@ public class ApiBatchRunBaseService {
ExecutionQueueDetail queueDetail = new ExecutionQueueDetail(); ExecutionQueueDetail queueDetail = new ExecutionQueueDetail();
queueDetail.setResourceId(resourceId); queueDetail.setResourceId(resourceId);
queueDetail.setSort(sort.getAndIncrement()); queueDetail.setSort(sort.getAndIncrement());
queueDetail.setRequestCount(1L);
// caseReportMap null 说明是集合报告生成一个虚拟的报告ID // caseReportMap null 说明是集合报告生成一个虚拟的报告ID
queueDetail.setReportId(caseReportMap == null ? UUID.randomUUID().toString() : caseReportMap.get(resourceId)); queueDetail.setReportId(caseReportMap == null ? UUID.randomUUID().toString() : caseReportMap.get(resourceId));
queueDetails.add(queueDetail); queueDetails.add(queueDetail);

View File

@ -123,6 +123,7 @@ public class ApiTestCaseBatchRunService {
// 先初始化集成报告设置好报告ID再初始化执行队列 // 先初始化集成报告设置好报告ID再初始化执行队列
ExecutionQueue queue = apiBatchRunBaseService.initExecutionqueue(ids, runModeConfig, ApiExecuteResourceType.API_CASE.name(), caseReportMap, userId); ExecutionQueue queue = apiBatchRunBaseService.initExecutionqueue(ids, runModeConfig, ApiExecuteResourceType.API_CASE.name(), caseReportMap, userId);
// 执行第一个任务 // 执行第一个任务
ExecutionQueueDetail nextDetail = apiExecutionQueueService.getNextDetail(queue.getQueueId()); ExecutionQueueDetail nextDetail = apiExecutionQueueService.getNextDetail(queue.getQueueId());
@ -192,7 +193,9 @@ public class ApiTestCaseBatchRunService {
// 如果是集成报告则生成唯一的虚拟ID非集成报告使用单用例的报告ID // 如果是集成报告则生成唯一的虚拟ID非集成报告使用单用例的报告ID
reportId = runModeConfig.isIntegratedReport() ? UUID.randomUUID().toString() : caseReportMap.get(id); reportId = runModeConfig.isIntegratedReport() ? UUID.randomUUID().toString() : caseReportMap.get(id);
Long requestCount = runModeConfig.isIntegratedReport() ? ids.size() : 1L;
TaskRequestDTO taskRequest = getTaskRequestDTO(reportId, apiTestCase, runModeConfig); TaskRequestDTO taskRequest = getTaskRequestDTO(reportId, apiTestCase, runModeConfig);
taskRequest.setRequestCount(requestCount);
execute(taskRequest, apiTestCase, apiTestCaseBlob, definitionExecuteInfoMap.get(apiTestCase.getId())); execute(taskRequest, apiTestCase, apiTestCaseBlob, definitionExecuteInfoMap.get(apiTestCase.getId()));
} catch (Exception e) { } catch (Exception e) {
LogUtils.error("执行用例失败 {}-{}", reportId, id); LogUtils.error("执行用例失败 {}-{}", reportId, id);
@ -312,6 +315,7 @@ public class ApiTestCaseBatchRunService {
TaskRequestDTO taskRequest = getTaskRequestDTO(reportId, apiTestCase, runModeConfig); TaskRequestDTO taskRequest = getTaskRequestDTO(reportId, apiTestCase, runModeConfig);
taskRequest.setQueueId(queue.getQueueId()); taskRequest.setQueueId(queue.getQueueId());
taskRequest.setRequestCount(queue.getRequestCount());
execute(taskRequest, apiTestCase, apiTestCaseBlob, BeanUtils.copyBean(new ApiDefinitionExecuteInfo(), apiDefinition)); execute(taskRequest, apiTestCase, apiTestCaseBlob, BeanUtils.copyBean(new ApiDefinitionExecuteInfo(), apiDefinition));
} }

View File

@ -1,7 +1,6 @@
package io.metersphere.api.service.scenario; package io.metersphere.api.service.scenario;
import io.metersphere.api.constants.ApiScenarioStepRefType; import io.metersphere.api.constants.ApiScenarioStepRefType;
import io.metersphere.api.constants.ApiScenarioStepType;
import io.metersphere.api.domain.ApiScenario; import io.metersphere.api.domain.ApiScenario;
import io.metersphere.api.domain.ApiScenarioRecord; import io.metersphere.api.domain.ApiScenarioRecord;
import io.metersphere.api.domain.ApiScenarioReport; import io.metersphere.api.domain.ApiScenarioReport;
@ -209,7 +208,7 @@ public class ApiScenarioBatchRunService {
private Long getRequestCount(List<ApiScenarioStepDTO> steps) { private Long getRequestCount(List<ApiScenarioStepDTO> steps) {
AtomicLong requestCount = new AtomicLong(); AtomicLong requestCount = new AtomicLong();
apiScenarioService.traversalStepTree(steps, step -> { apiScenarioService.traversalStepTree(steps, step -> {
if (BooleanUtils.isTrue(step.getEnable()) && isRequestStep(step)) { if (BooleanUtils.isTrue(step.getEnable()) && apiScenarioService.isRequestStep(step)) {
requestCount.getAndIncrement(); requestCount.getAndIncrement();
} }
return true; return true;
@ -217,10 +216,6 @@ public class ApiScenarioBatchRunService {
return requestCount.get(); return requestCount.get();
} }
private boolean isRequestStep(ApiScenarioStepCommonDTO step) {
return StringUtils.equalsAny(step.getStepType(), ApiScenarioStepType.API.name(), ApiScenarioStepType.API_CASE.name(), ApiScenarioStepType.CUSTOM_REQUEST.name());
}
/** /**
* 集成报告执行前先设置成 RUNNING * 集成报告执行前先设置成 RUNNING
* @param runModeConfig * @param runModeConfig

View File

@ -1251,6 +1251,7 @@ public class ApiScenarioService extends MoveNodeService {
apiExecuteService.getDebugRunModule(request.getFrontendDebug())); apiExecuteService.getDebugRunModule(request.getFrontendDebug()));
taskRequest.setSaveResult(false); taskRequest.setSaveResult(false);
taskRequest.setRealTime(true); taskRequest.setRealTime(true);
taskRequest.setRequestCount(tmpParam.getRequestCount().get());
ApiScenarioParamConfig parseConfig = getApiScenarioParamConfig(request, tmpParam.getScenarioParseEnvInfo()); ApiScenarioParamConfig parseConfig = getApiScenarioParamConfig(request, tmpParam.getScenarioParseEnvInfo());
parseConfig.setReportId(request.getReportId()); parseConfig.setReportId(request.getReportId());
@ -1305,6 +1306,7 @@ public class ApiScenarioService extends MoveNodeService {
taskRequest.getRunModeConfig().setPoolId(poolId); taskRequest.getRunModeConfig().setPoolId(poolId);
taskRequest.setSaveResult(true); taskRequest.setSaveResult(true);
taskRequest.getRunModeConfig().setEnvironmentId(parseParam.getEnvironmentId()); taskRequest.getRunModeConfig().setEnvironmentId(parseParam.getEnvironmentId());
taskRequest.setRequestCount(tmpParam.getRequestCount().get());
if (StringUtils.isEmpty(taskRequest.getReportId())) { if (StringUtils.isEmpty(taskRequest.getReportId())) {
taskRequest.setRealTime(false); taskRequest.setRealTime(false);
@ -1327,6 +1329,10 @@ public class ApiScenarioService extends MoveNodeService {
return apiExecuteService.execute(runRequest, taskRequest, parseConfig); return apiExecuteService.execute(runRequest, taskRequest, parseConfig);
} }
public boolean isRequestStep(ApiScenarioStepCommonDTO step) {
return StringUtils.equalsAny(step.getStepType(), ApiScenarioStepType.API.name(), ApiScenarioStepType.API_CASE.name(), ApiScenarioStepType.CUSTOM_REQUEST.name());
}
/** /**
* 预生成用例的执行报告 * 预生成用例的执行报告
* *
@ -1614,6 +1620,11 @@ public class ApiScenarioService extends MoveNodeService {
} }
setPartialRefStepEnable(step, stepDetailMap); setPartialRefStepEnable(step, stepDetailMap);
if (isRequestStep(step) && BooleanUtils.isTrue(step.getEnable())) {
// 记录待执行的请求总数
parseParam.getRequestCount().getAndIncrement();
}
// 将步骤详情解析生成对应的MsTestElement // 将步骤详情解析生成对应的MsTestElement
AbstractMsTestElement msTestElement = stepParser.parseTestElement(step, AbstractMsTestElement msTestElement = stepParser.parseTestElement(step,
MapUtils.isNotEmpty(resourceDetailMap) ? resourceDetailMap.getOrDefault(step.getResourceId(), StringUtils.EMPTY) : StringUtils.EMPTY, stepDetailMap.get(step.getId())); MapUtils.isNotEmpty(resourceDetailMap) ? resourceDetailMap.getOrDefault(step.getResourceId(), StringUtils.EMPTY) : StringUtils.EMPTY, stepDetailMap.get(step.getId()));