refactor(测试计划): 停止测试计划

This commit is contained in:
wxg0103 2024-06-15 20:30:47 +08:00 committed by Craftsman
parent 6f30f3476e
commit ffc3bfc934
2 changed files with 57 additions and 44 deletions

View File

@ -1,8 +1,8 @@
package io.metersphere.plan.service;
import com.esotericsoftware.minlog.Log;
import io.metersphere.plan.dto.TestPlanReportPostParam;
import io.metersphere.plan.domain.*;
import io.metersphere.plan.dto.TestPlanReportPostParam;
import io.metersphere.plan.dto.request.TestPlanBatchExecuteRequest;
import io.metersphere.plan.dto.request.TestPlanExecuteRequest;
import io.metersphere.plan.dto.request.TestPlanReportGenRequest;
@ -66,12 +66,12 @@ public class TestPlanExecuteService {
private TestPlanReportMapper testPlanReportMapper;
// 停止测试计划的执行
public void stopTestPlanRunning(String testPlanReportId){
public void stopTestPlanRunning(String testPlanReportId) {
TestPlanReport testPlanReport = testPlanReportMapper.selectByPrimaryKey(testPlanReportId);
if(testPlanReport ==null){
if (testPlanReport == null) {
return;
}
if(testPlanReport.getIntegrated()){
if (testPlanReport.getIntegrated()) {
TestPlanReportExample reportExample = new TestPlanReportExample();
reportExample.createCriteria().andParentIdEqualTo(testPlanReportId);
List<TestPlanReport> testPlanItemReport = testPlanReportMapper.selectByExample(reportExample);
@ -86,21 +86,21 @@ public class TestPlanExecuteService {
继续执行 test-plan-batch-execute:randomId 队列的下一条
*/
// 获取下一个要执行的测试计划节点目的是得到最后一条的queueId
TestPlanExecutionQueue nextTestPlanQueue = this.getNextQueue(testPlanReportId,QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE);
if(nextTestPlanQueue == null || !StringUtils.equalsIgnoreCase(nextTestPlanQueue.getParentQueueType(),QUEUE_PREFIX_TEST_PLAN_BATCH_EXECUTE)){
throw new MSException("停止执行测试计划组失败!无法获取到执行队列!");
TestPlanExecutionQueue nextTestPlanQueue = this.getNextQueue(testPlanReportId, QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE);
if (nextTestPlanQueue == null || !StringUtils.equalsIgnoreCase(nextTestPlanQueue.getParentQueueType(), QUEUE_PREFIX_TEST_PLAN_BATCH_EXECUTE)) {
return;
}
String groupExecuteQueueId = genQueueKey(testPlanReportId,QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE);
String groupExecuteQueueId = genQueueKey(testPlanReportId, QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE);
this.deleteRedisKey(groupExecuteQueueId);
testPlanItemReport.forEach(item -> {
this.deepDeleteTestPlanCaseType(item);
//统计子测试计划报告
summaryTestPlanReport(item.getId(),false);
summaryTestPlanReport(item.getId(), false);
});
summaryTestPlanReport(testPlanReportId,true);
this.testPlanExecuteQueueFinish(nextTestPlanQueue.getParentQueueId(),nextTestPlanQueue.getParentQueueType());
}else {
summaryTestPlanReport(testPlanReportId, true);
this.testPlanExecuteQueueFinish(nextTestPlanQueue.getParentQueueId(), nextTestPlanQueue.getParentQueueType());
} else {
/*
独立报告中停止的是单独测试计划的执行
这条测试计划是在批量执行队列中还是在测试计划组执行队中 通过要删除的队列1因为当前节点在执行之前就被弹出来确定
@ -111,26 +111,26 @@ public class TestPlanExecuteService {
进行当前报告结算
继续执行 队列的下一条
*/
TestPlanExecutionQueue nextTestPlanQueue = this.getNextQueue(testPlanReportId,QUEUE_PREFIX_TEST_PLAN_CASE_TYPE);
if(nextTestPlanQueue == null || !StringUtils.equalsAnyIgnoreCase(nextTestPlanQueue.getParentQueueType(),QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE,QUEUE_PREFIX_TEST_PLAN_BATCH_EXECUTE)){
throw new MSException("停止测试计划执行失败!无法获取到执行队列!");
TestPlanExecutionQueue nextTestPlanQueue = this.getNextQueue(testPlanReportId, QUEUE_PREFIX_TEST_PLAN_CASE_TYPE);
if (nextTestPlanQueue == null || !StringUtils.equalsAnyIgnoreCase(nextTestPlanQueue.getParentQueueType(), QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE, QUEUE_PREFIX_TEST_PLAN_BATCH_EXECUTE)) {
return;
}
this.deepDeleteTestPlanCaseType(testPlanReport);
summaryTestPlanReport(testPlanReportId,false);
this.testPlanExecuteQueueFinish(nextTestPlanQueue.getParentQueueId(),nextTestPlanQueue.getParentQueueType());
summaryTestPlanReport(testPlanReportId, false);
this.testPlanExecuteQueueFinish(nextTestPlanQueue.getParentQueueId(), nextTestPlanQueue.getParentQueueType());
}
// todo @wxg 是在 deepDeleteTestPlanCaseType()方法中删除用例执行队列时同步到执行机停止执行任务还是其它操作 由你来决定了
}
private void deepDeleteTestPlanCaseType(TestPlanReport report){
this.deleteRedisKey(genQueueKey(report.getId(),QUEUE_PREFIX_TEST_PLAN_CASE_TYPE));
private void deepDeleteTestPlanCaseType(TestPlanReport report) {
this.deleteRedisKey(genQueueKey(report.getId(), QUEUE_PREFIX_TEST_PLAN_CASE_TYPE));
TestPlanCollectionExample collectionExample = new TestPlanCollectionExample();
collectionExample.createCriteria().andTestPlanIdEqualTo(report.getTestPlanId()).andParentIdEqualTo(TestPlanConstants.DEFAULT_PARENT_ID);
List<TestPlanCollection> parentTestPlanCollectionList = testPlanCollectionMapper.selectByExample(collectionExample);
parentTestPlanCollectionList.forEach( parentCollection -> {
parentTestPlanCollectionList.forEach(parentCollection -> {
this.deleteRedisKey(genQueueKey(report.getId()+"_"+parentCollection.getId(),QUEUE_PREFIX_TEST_PLAN_COLLECTION));
this.deleteRedisKey(genQueueKey(report.getId() + "_" + parentCollection.getId(), QUEUE_PREFIX_TEST_PLAN_COLLECTION));
//todo @Chen-Jianxing 这里要同步清理用例/场景的执行队列
});
@ -159,6 +159,7 @@ public class TestPlanExecuteService {
redisTemplate.delete(redisKey);
redisTemplate.delete(genQueueKey(redisKey, LAST_QUEUE_PREFIX));
}
//批量执行测试计划组
public void batchExecuteTestPlan(TestPlanBatchExecuteRequest request, String userId) {
List<String> rightfulIds = testPlanService.selectRightfulIds(request.getExecuteIds());
@ -218,7 +219,7 @@ public class TestPlanExecuteService {
if (StringUtils.equalsIgnoreCase(testPlan.getType(), TestPlanConstants.TEST_PLAN_TYPE_GROUP)) {
List<TestPlan> children = testPlanService.selectNotArchivedChildren(testPlan.getId());
// 预生成计划组报告
Map<String, String> reportMap = testPlanReportService.genReportByExecution(executionQueue.getPrepareReportId(),genReportRequest, executionQueue.getCreateUser());
Map<String, String> reportMap = testPlanReportService.genReportByExecution(executionQueue.getPrepareReportId(), genReportRequest, executionQueue.getCreateUser());
long pos = 0;
List<TestPlanExecutionQueue> childrenQueue = new ArrayList<>();
@ -248,7 +249,7 @@ public class TestPlanExecuteService {
this.setRedisForList(genQueueKey(queueId, queueType), childrenQueue.stream().map(JSON::toJSONString).toList());
// 更新报告的执行时间
extTestPlanReportMapper.batchUpdateExecuteTime(System.currentTimeMillis(),reportMap.values().stream().toList());
extTestPlanReportMapper.batchUpdateExecuteTime(System.currentTimeMillis(), reportMap.values().stream().toList());
if (StringUtils.equalsIgnoreCase(executionQueue.getRunMode(), ApiBatchRunMode.SERIAL.name())) {
//串行
@ -264,9 +265,9 @@ public class TestPlanExecuteService {
return executionQueue.getPrepareReportId();
} else {
Map<String, String> reportMap = testPlanReportService.genReportByExecution(executionQueue.getPrepareReportId(),genReportRequest, executionQueue.getCreateUser());
Map<String, String> reportMap = testPlanReportService.genReportByExecution(executionQueue.getPrepareReportId(), genReportRequest, executionQueue.getCreateUser());
executionQueue.setPrepareReportId(reportMap.get(executionQueue.getSourceID()));
extTestPlanReportMapper.batchUpdateExecuteTime(System.currentTimeMillis(),reportMap.values().stream().toList());
extTestPlanReportMapper.batchUpdateExecuteTime(System.currentTimeMillis(), reportMap.values().stream().toList());
this.executeTestPlan(executionQueue);
return executionQueue.getPrepareReportId();
}
@ -338,7 +339,7 @@ public class TestPlanExecuteService {
int pos = 0;
List<TestPlanExecutionQueue> childrenQueue = new ArrayList<>();
String queueId = executionQueue.getPrepareReportId()+"_"+parentCollection.getId();
String queueId = executionQueue.getPrepareReportId() + "_" + parentCollection.getId();
String queueType = QUEUE_PREFIX_TEST_PLAN_COLLECTION;
for (TestPlanCollection collection : childrenList) {
childrenQueue.add(
@ -379,6 +380,7 @@ public class TestPlanExecuteService {
/**
* 批量执行单个测试集的用例
*
* @param testPlanExecutionQueue
*/
private void executeCase(TestPlanExecutionQueue testPlanExecutionQueue) {
@ -504,11 +506,11 @@ public class TestPlanExecuteService {
}
}
private void summaryTestPlanReport(String reportId,boolean isGroupReport){
private void summaryTestPlanReport(String reportId, boolean isGroupReport) {
try {
if(isGroupReport){
if (isGroupReport) {
testPlanReportService.summaryGroupReport(reportId);
}else {
} else {
testPlanReportService.summaryPlanReport(reportId);
}
@ -519,34 +521,34 @@ public class TestPlanExecuteService {
postParam.setExecStatus(ExecStatus.COMPLETED.name());
testPlanReportService.postHandleReport(postParam);
if(!isGroupReport){
if (!isGroupReport) {
TestPlanReport testPlanReport = testPlanReportService.selectById(reportId);
if(testPlanReport!=null){
if (testPlanReport != null) {
testPlanService.refreshTestPlanStatus(testPlanReport.getTestPlanId());
}
}
}catch (Exception e){
} catch (Exception e) {
LogUtils.error("Cannot find test plan report for " + reportId, e);
}
}
private void queueExecuteFinish(TestPlanExecutionQueue queue) {
if (StringUtils.equalsIgnoreCase(queue.getParentQueueType(), QUEUE_PREFIX_TEST_PLAN_BATCH_EXECUTE)) {
if(StringUtils.equalsIgnoreCase(queue.getQueueType(),QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE)){
if (StringUtils.equalsIgnoreCase(queue.getQueueType(), QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE)) {
// 计划组报告汇总并统计
this.summaryTestPlanReport(queue.getQueueId(),true);
}else if(StringUtils.equalsIgnoreCase(queue.getQueueType(),QUEUE_PREFIX_TEST_PLAN_CASE_TYPE)){
this.summaryTestPlanReport(queue.getQueueId(), true);
} else if (StringUtils.equalsIgnoreCase(queue.getQueueType(), QUEUE_PREFIX_TEST_PLAN_CASE_TYPE)) {
/*
此时处于批量勾选执行中的游离态测试计划执行所以队列顺序为QUEUE_PREFIX_TEST_PLAN_BATCH_EXECUTE -> QUEUE_PREFIX_TEST_PLAN_CASE_TYPE
此时queue节点为testPlanCollection的节点 而测试计划节点串行状态下在执行之前就被弹出了
所以获取报告ID的方式为读取queueId caseType队列和collection队列的queueId都是报告ID
*/
this.summaryTestPlanReport(queue.getQueueId(),false);
this.summaryTestPlanReport(queue.getQueueId(), false);
}
this.testPlanGroupQueueFinish(queue.getParentQueueId(), queue.getParentQueueType());
} else if (StringUtils.equalsIgnoreCase(queue.getParentQueueType(), QUEUE_PREFIX_TEST_PLAN_GROUP_EXECUTE)) {
// 计划报告汇总并统计
this.summaryTestPlanReport(queue.getQueueId(),false);
this.summaryTestPlanReport(queue.getQueueId(), false);
this.testPlanExecuteQueueFinish(queue.getParentQueueId(), queue.getParentQueueType());
} else if (StringUtils.equalsIgnoreCase(queue.getParentQueueType(), QUEUE_PREFIX_TEST_PLAN_CASE_TYPE)) {
this.caseTypeExecuteQueueFinish(queue.getParentQueueId(), queue.getParentQueueType());

View File

@ -82,10 +82,9 @@ public class TestPlanTaskCenterService {
ExtApiReportMapper extApiReportMapper;
@Resource
ExtApiScenarioReportMapper extApiScenarioReportMapper;
@Resource
TestPlanExecuteService testPlanExecuteService;
private static final String DEFAULT_SORT = "start_time desc";
private final static String PROJECT_STOP = "/task/center/api/project/stop";
private final static String ORG_STOP = "/task/center/api/org/stop";
private final static String SYSTEM_STOP = "/task/center/api/system/stop";
/**
* 任务中心实时任务列表-项目级
@ -275,12 +274,24 @@ public class TestPlanTaskCenterService {
}
//根据id去重
List<String> allReportIds = reports.stream().distinct().map(ReportDTO::getId).toList();
if (CollectionUtils.isNotEmpty(allReportIds)) {
// 查找和测试计划管理的接口用例的所有数据
List<ReportDTO> apiReports = extTestPlanReportMapper.getCaseReports(allReportIds);
detailReport(request, apiReports, userId, module, ApiExecuteResourceType.TEST_PLAN_API_CASE.name());
List<ReportDTO> scenarioReports = extTestPlanReportMapper.getScenarioReports(allReportIds);
detailReport(request, scenarioReports, userId, module, ApiExecuteResourceType.TEST_PLAN_API_SCENARIO.name());
//停止测试计划的队列
SubListUtils.dealForSubList(allReportIds, 10, (subList) -> {
subList.forEach(item -> {
try {
LogUtils.info(String.format("开始停止测试计划队列:%s", item));
testPlanExecuteService.stopTestPlanRunning(item);
} catch (Exception e) {
LogUtils.error("停止测试计划队列异常", e);
}
});
List<ReportDTO> apiReports = extTestPlanReportMapper.getCaseReports(subList);
detailReport(request, apiReports, userId, module, ApiExecuteResourceType.TEST_PLAN_API_CASE.name());
List<ReportDTO> scenarioReports = extTestPlanReportMapper.getScenarioReports(subList);
detailReport(request, scenarioReports, userId, module, ApiExecuteResourceType.TEST_PLAN_API_SCENARIO.name());
});
}
}