fix(测试计划): 修复测试计划用例批量执行问题

--bug=1007516 --user=赵勇 【测试计划】-批量并行+local资源池执行接口用例失败 https://www.tapd.cn/55049933/s/1059992
This commit is contained in:
fit2-zhao 2021-10-27 17:25:54 +08:00 committed by fit2-zhao
parent d8bf400e56
commit d01e9d748f
6 changed files with 43 additions and 36 deletions

View File

@ -7,6 +7,7 @@ import io.metersphere.api.dto.automation.*;
import io.metersphere.api.dto.automation.parse.ScenarioImport;
import io.metersphere.api.dto.definition.RunDefinitionRequest;
import io.metersphere.api.jmeter.LocalRunner;
import io.metersphere.api.jmeter.MessageCache;
import io.metersphere.api.service.ApiAutomationService;
import io.metersphere.base.domain.ApiScenario;
import io.metersphere.base.domain.ApiScenarioWithBLOBs;
@ -97,7 +98,7 @@ public class ApiAutomationController {
@PostMapping(value = "/create")
@MsAuditLog(module = "api_automation", type = OperLogConstants.CREATE, title = "#request.name", content = "#msClass.getLogDetails(#request.id)", msClass = ApiAutomationService.class)
@RequiresPermissions(value={PermissionConstants.PROJECT_API_SCENARIO_READ_CREATE, PermissionConstants.PROJECT_API_SCENARIO_READ_COPY}, logical = Logical.OR)
@RequiresPermissions(value = {PermissionConstants.PROJECT_API_SCENARIO_READ_CREATE, PermissionConstants.PROJECT_API_SCENARIO_READ_COPY}, logical = Logical.OR)
@SendNotice(taskType = NoticeConstants.TaskType.API_AUTOMATION_TASK, event = NoticeConstants.Event.CREATE, mailTemplate = "api/AutomationCreate", subject = "接口自动化通知")
public ApiScenario create(@RequestPart("request") SaveApiScenarioRequest request, @RequestPart(value = "bodyFiles", required = false) List<MultipartFile> bodyFiles,
@RequestPart(value = "scenarioFiles", required = false) List<MultipartFile> scenarioFiles) {
@ -106,7 +107,7 @@ public class ApiAutomationController {
@PostMapping(value = "/update")
@MsAuditLog(module = "api_automation", type = OperLogConstants.UPDATE, beforeEvent = "#msClass.getLogDetails(#request.id)", title = "#request.name", content = "#msClass.getLogDetails(#request.id)", msClass = ApiAutomationService.class)
@RequiresPermissions(value={PermissionConstants.PROJECT_API_SCENARIO_READ_EDIT, PermissionConstants.PROJECT_API_SCENARIO_READ_COPY}, logical = Logical.OR)
@RequiresPermissions(value = {PermissionConstants.PROJECT_API_SCENARIO_READ_EDIT, PermissionConstants.PROJECT_API_SCENARIO_READ_COPY}, logical = Logical.OR)
@SendNotice(taskType = NoticeConstants.TaskType.API_AUTOMATION_TASK, event = NoticeConstants.Event.UPDATE, mailTemplate = "api/AutomationUpdate", subject = "接口自动化通知")
public ApiScenario update(@RequestPart("request") SaveApiScenarioRequest request, @RequestPart(value = "bodyFiles", required = false) List<MultipartFile> bodyFiles,
@RequestPart(value = "scenarioFiles", required = false) List<MultipartFile> scenarioFiles) {
@ -323,8 +324,11 @@ public class ApiAutomationController {
@GetMapping(value = "/stop/{reportId}")
public void stop(@PathVariable String reportId) {
if (StringUtils.isNotEmpty(reportId)) {
MessageCache.batchTestCases.remove(reportId);
new LocalRunner().stop(reportId);
}
}
@PostMapping(value = "/stop/batch")
public String stopBatch(@RequestBody List<TaskRequest> reportIds) {

View File

@ -174,11 +174,6 @@ public class JMeterService {
}
} else {
try {
// SendResult result = kafkaTemplate.send(KafkaConfig.EXEC_TOPIC, JSON.toJSONString(runRequest)).get();
// if (result != null) {
// LogUtil.debug("获取ack 结果:" + result.getRecordMetadata());
// }
// kafkaTemplate.flush();
this.send(runRequest, config, reportId);
} catch (Exception e) {
e.printStackTrace();
@ -186,6 +181,7 @@ public class JMeterService {
&& MessageCache.cache.get(config.getAmassReport()).getReportIds() != null) {
MessageCache.cache.get(config.getAmassReport()).getReportIds().remove(reportId);
}
MessageCache.batchTestCases.remove(reportId);
}
}
}
@ -216,6 +212,7 @@ public class JMeterService {
&& MessageCache.cache.get(config.getAmassReport()).getReportIds() != null) {
MessageCache.cache.get(config.getAmassReport()).getReportIds().remove(reportId);
}
MessageCache.batchTestCases.remove(reportId);
}
}
}

View File

@ -25,9 +25,6 @@ import io.metersphere.track.service.TestPlanService;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@ -73,7 +70,7 @@ public class ApiDefinitionExecResultService {
result.getScenarios().forEach(scenarioResult -> {
if (scenarioResult != null && CollectionUtils.isNotEmpty(scenarioResult.getRequestResults())) {
scenarioResult.getRequestResults().forEach(item -> {
if(!StringUtils.startsWithAny(item.getName(),"PRE_PROCESSOR_ENV_","POST_PROCESSOR_ENV_")){
if (!StringUtils.startsWithAny(item.getName(), "PRE_PROCESSOR_ENV_", "POST_PROCESSOR_ENV_")) {
ApiDefinitionExecResult saveResult = MessageCache.batchTestCases.get(result.getTestId());
if (saveResult == null) {
saveResult = apiDefinitionExecResultMapper.selectByPrimaryKey(result.getTestId());
@ -96,10 +93,13 @@ public class ApiDefinitionExecResultService {
if (StringUtils.isNotEmpty(result.getUserId())) {
saveResult.setUserId(result.getUserId());
} else {
saveResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());
if (SessionUtils.getUser() != null) {
saveResult.setUserId(SessionUtils.getUser().getId());
}
}
saved = false;
}
String status = item.isSuccess() ? "success" : "error";
saveResult.setName(getName(type, item.getName(), status, saveResult.getCreateTime(), saveResult.getId()));
saveResult.setStatus(status);
@ -124,6 +124,9 @@ public class ApiDefinitionExecResultService {
apiDefinitionExecResultMapper.updateByPrimaryKeyWithBLOBs(saveResult);
}
apiDefinitionService.removeCache(result.getTestId());
if (StringUtils.isNotEmpty(result.getTestId())) {
MessageCache.batchTestCases.remove(result.getTestId());
}
// 发送通知
sendNotice(saveResult);
}

View File

@ -783,7 +783,8 @@ public class ApiTestCaseService {
batchMapper.insert(report);
executeQueue.add(runCaseRequest);
}
sqlSession.flushStatements();
sqlSession.commit();
for (RunCaseRequest runCaseRequest : executeQueue) {
run(runCaseRequest);
MessageCache.batchTestCases.put(runCaseRequest.getReportId(), runCaseRequest.getReport());

View File

@ -108,6 +108,7 @@ public class TaskService {
result.setStatus("STOP");
apiDefinitionExecResultMapper.updateByPrimaryKeySelective(result);
actuator = result.getActuator();
MessageCache.batchTestCases.remove(result.getId());
}
} else if (StringUtils.equals(request.getType(), "SCENARIO")) {
ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(request.getReportId());

View File

@ -380,7 +380,9 @@ public class TestPlanApiCaseService {
if (request.getConfig() != null && StringUtils.isNotEmpty(request.getConfig().getResourcePoolId())) {
apiResult.setActuator(request.getConfig().getResourcePoolId());
}
apiResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());
if (SessionUtils.getUser() != null) {
apiResult.setUserId(SessionUtils.getUser().getId());
}
apiResult.setResourceId(key.getApiCaseId());
apiResult.setStartTime(System.currentTimeMillis());
apiResult.setType(ApiRunMode.API_PLAN.name());
@ -413,7 +415,7 @@ public class TestPlanApiCaseService {
ApiDefinitionExecResult report = addResult(request, testPlanApiCase, APITestStatus.Waiting.name(), batchMapper);
executeQueue.put(testPlanApiCase, report);
});
sqlSession.flushStatements();
sqlSession.commit();
List<String> reportIds = new LinkedList<>();
// 开始串行执行
Thread thread = new Thread(new Runnable() {
@ -421,7 +423,7 @@ public class TestPlanApiCaseService {
public void run() {
for (TestPlanApiCase testPlanApiCase : executeQueue.keySet()) {
try {
if (executeQueue.get(testPlanApiCase)!=null && MessageCache.terminationOrderDeque.contains(executeQueue.get(testPlanApiCase).getId())) {
if (executeQueue.get(testPlanApiCase) != null && MessageCache.terminationOrderDeque.contains(executeQueue.get(testPlanApiCase).getId())) {
MessageCache.terminationOrderDeque.remove(executeQueue.get(testPlanApiCase).getId());
break;
}
@ -467,25 +469,23 @@ public class TestPlanApiCaseService {
});
thread.start();
} else {
Map<String, TestPlanApiCase> executeQueue = new HashMap<>();
planApiCases.forEach(testPlanApiCase -> {
ApiDefinitionExecResult report = addResult(request, testPlanApiCase, APITestStatus.Running.name(), batchMapper);
executeQueue.put(report.getId(), testPlanApiCase);
MessageCache.batchTestCases.put(report.getId(), report);
});
sqlSession.commit();
// 开始并发执行
for (TestPlanApiCase key : planApiCases) {
RunModeDataDTO modeDataDTO = null;
if (StringUtils.isNotBlank(request.getConfig().getResourcePoolId())) {
modeDataDTO = new RunModeDataDTO(key.getId(), UUID.randomUUID().toString());
} else {
// 生成报告和HashTree
HashTree hashTree = generateHashTree(key.getId());
modeDataDTO = new RunModeDataDTO(hashTree, UUID.randomUUID().toString());
}
ApiDefinitionExecResult report = addResult(request, key, APITestStatus.Running.name(), batchMapper);
modeDataDTO.setApiCaseId(report.getId());
for (String reportId : executeQueue.keySet()) {
if (request.getConfig() != null && StringUtils.isNotEmpty(request.getConfig().getResourcePoolId())) {
jMeterService.runTest(report.getId(), modeDataDTO.getApiCaseId(), ApiRunMode.API_PLAN.name(), null, request.getConfig());
jMeterService.runTest(executeQueue.get(reportId).getId(), reportId, ApiRunMode.API_PLAN.name(), null, request.getConfig());
} else {
jMeterService.runLocal(report.getId(), modeDataDTO.getHashTree(), TriggerMode.BATCH.name() , ApiRunMode.API_PLAN.name());
HashTree hashTree = generateHashTree(executeQueue.get(reportId).getId());
jMeterService.runLocal(reportId, hashTree, TriggerMode.BATCH.name(), ApiRunMode.API_PLAN.name());
}
}
sqlSession.flushStatements();
}
return request.getId();
}
@ -593,11 +593,11 @@ public class TestPlanApiCaseService {
return buildCases(apiTestCases);
}
public List<TestPlanFailureApiDTO> getAllCases(Collection<String> caseIdList,String planId,String status) {
if(caseIdList.isEmpty()){
public List<TestPlanFailureApiDTO> getAllCases(Collection<String> caseIdList, String planId, String status) {
if (caseIdList.isEmpty()) {
return new ArrayList<>();
}
List<TestPlanFailureApiDTO> apiTestCases = extTestPlanApiCaseMapper.getFailureListByIds(caseIdList,planId, status);
List<TestPlanFailureApiDTO> apiTestCases = extTestPlanApiCaseMapper.getFailureListByIds(caseIdList, planId, status);
return buildCases(apiTestCases);
}
@ -617,6 +617,7 @@ public class TestPlanApiCaseService {
/**
* 用例自定义排序
*
* @param request
*/
public void updateOrder(ResetOrderRequest request) {