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 java.util.*;
import java.util.concurrent.atomic.AtomicLong;
/**
* 执行场景解析参数时的临时参数
@ -50,4 +51,8 @@ public class ApiScenarioParseTmpParam {
* 环境相关信息
*/
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) {
ExecutionQueue queue = getExecutionQueue(runModeConfig, resourceType, userId);
queue.setRequestCount(runModeConfig.isIntegratedReport() ? resourceIds.size() : 1L);
List<ExecutionQueueDetail> queueDetails = getExecutionQueueDetails(resourceIds, caseReportMap);
apiExecutionQueueService.insertQueue(queue, queueDetails);
return queue;
@ -57,6 +58,7 @@ public class ApiBatchRunBaseService {
ExecutionQueueDetail queueDetail = new ExecutionQueueDetail();
queueDetail.setResourceId(resourceId);
queueDetail.setSort(sort.getAndIncrement());
queueDetail.setRequestCount(1L);
// caseReportMap null 说明是集合报告生成一个虚拟的报告ID
queueDetail.setReportId(caseReportMap == null ? UUID.randomUUID().toString() : caseReportMap.get(resourceId));
queueDetails.add(queueDetail);

View File

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

View File

@ -1,7 +1,6 @@
package io.metersphere.api.service.scenario;
import io.metersphere.api.constants.ApiScenarioStepRefType;
import io.metersphere.api.constants.ApiScenarioStepType;
import io.metersphere.api.domain.ApiScenario;
import io.metersphere.api.domain.ApiScenarioRecord;
import io.metersphere.api.domain.ApiScenarioReport;
@ -209,7 +208,7 @@ public class ApiScenarioBatchRunService {
private Long getRequestCount(List<ApiScenarioStepDTO> steps) {
AtomicLong requestCount = new AtomicLong();
apiScenarioService.traversalStepTree(steps, step -> {
if (BooleanUtils.isTrue(step.getEnable()) && isRequestStep(step)) {
if (BooleanUtils.isTrue(step.getEnable()) && apiScenarioService.isRequestStep(step)) {
requestCount.getAndIncrement();
}
return true;
@ -217,10 +216,6 @@ public class ApiScenarioBatchRunService {
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
* @param runModeConfig

View File

@ -1251,6 +1251,7 @@ public class ApiScenarioService extends MoveNodeService {
apiExecuteService.getDebugRunModule(request.getFrontendDebug()));
taskRequest.setSaveResult(false);
taskRequest.setRealTime(true);
taskRequest.setRequestCount(tmpParam.getRequestCount().get());
ApiScenarioParamConfig parseConfig = getApiScenarioParamConfig(request, tmpParam.getScenarioParseEnvInfo());
parseConfig.setReportId(request.getReportId());
@ -1305,6 +1306,7 @@ public class ApiScenarioService extends MoveNodeService {
taskRequest.getRunModeConfig().setPoolId(poolId);
taskRequest.setSaveResult(true);
taskRequest.getRunModeConfig().setEnvironmentId(parseParam.getEnvironmentId());
taskRequest.setRequestCount(tmpParam.getRequestCount().get());
if (StringUtils.isEmpty(taskRequest.getReportId())) {
taskRequest.setRealTime(false);
@ -1327,6 +1329,10 @@ public class ApiScenarioService extends MoveNodeService {
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);
if (isRequestStep(step) && BooleanUtils.isTrue(step.getEnable())) {
// 记录待执行的请求总数
parseParam.getRequestCount().getAndIncrement();
}
// 将步骤详情解析生成对应的MsTestElement
AbstractMsTestElement msTestElement = stepParser.parseTestElement(step,
MapUtils.isNotEmpty(resourceDetailMap) ? resourceDetailMap.getOrDefault(step.getResourceId(), StringUtils.EMPTY) : StringUtils.EMPTY, stepDetailMap.get(step.getId()));