From 69f886b23053cff5d059d5744cb5bdb636e4d62a Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Thu, 13 Apr 2023 15:04:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA)?= =?UTF-8?q?=EF=BC=9A=E6=89=B9=E9=87=8F=E6=8F=92=E5=85=A5=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=BF=87=E5=A4=9A=E5=AF=BC=E8=87=B4OOM=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E6=96=B0=E5=A2=9E=E6=89=B9=E9=87=8Futils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --story=1011417 --user=王旭 【Bug转需求】[测试跟踪] github#19980测试计划关联用例,当用例较多的时候崩溃(6000条) https://www.tapd.cn/55049933/s/1362368 --- .../plan/service/TestPlanService.java | 61 ++++++++++--------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanService.java b/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanService.java index ef525cde2a..ecc4d5b46e 100644 --- a/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanService.java +++ b/test-track/backend/src/main/java/io/metersphere/plan/service/TestPlanService.java @@ -81,6 +81,7 @@ import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.*; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -649,38 +650,42 @@ public class TestPlanService { // 尽量保持与用例顺序一致 Collections.reverse(testCaseIds); - Long nextOrder = ServiceUtils.getNextOrder(request.getPlanId(), extTestPlanTestCaseMapper::getLastOrder); - for (String caseId : testCaseIds) { - TestPlanTestCaseWithBLOBs testPlanTestCase = new TestPlanTestCaseWithBLOBs(); - testPlanTestCase.setId(UUID.randomUUID().toString()); - testPlanTestCase.setCreateUser(SessionUtils.getUserId()); - String maintainer = userMap.get(caseId); - if (StringUtils.isBlank(maintainer) || !projectMemberSet.contains(maintainer)) { - maintainer = SessionUtils.getUserId(); + AtomicReference nextOrder = new AtomicReference<>(ServiceUtils.getNextOrder(request.getPlanId(), extTestPlanTestCaseMapper::getLastOrder)); + try { + SubListUtil.dealForSubList(testCaseIds, 1000, (subList) -> { + for (Object caseId : subList) { + TestPlanTestCaseWithBLOBs testPlanTestCase = new TestPlanTestCaseWithBLOBs(); + testPlanTestCase.setId(UUID.randomUUID().toString()); + testPlanTestCase.setCreateUser(SessionUtils.getUserId()); + String maintainer = userMap.get(caseId); + if (StringUtils.isBlank(maintainer) || !projectMemberSet.contains(maintainer)) { + maintainer = SessionUtils.getUserId(); + } + testPlanTestCase.setExecutor(maintainer); + testPlanTestCase.setCaseId(caseId.toString()); + testPlanTestCase.setCreateTime(System.currentTimeMillis()); + testPlanTestCase.setUpdateTime(System.currentTimeMillis()); + testPlanTestCase.setPlanId(request.getPlanId()); + testPlanTestCase.setStatus(TestPlanStatus.Prepare.name()); + testPlanTestCase.setIsDel(false); + testPlanTestCase.setOrder(nextOrder.get()); + nextOrder.updateAndGet(v -> v + ServiceUtils.ORDER_STEP); + batchMapper.insert(testPlanTestCase); + } + sqlSession.flushStatements(); + caseTestRelevance(request, subList); + }); + } catch (Exception e) { + sqlSession.rollback(); + throw new RuntimeException(e); + }finally { + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); } - testPlanTestCase.setExecutor(maintainer); - testPlanTestCase.setCaseId(caseId); - testPlanTestCase.setCreateTime(System.currentTimeMillis()); - testPlanTestCase.setUpdateTime(System.currentTimeMillis()); - testPlanTestCase.setPlanId(request.getPlanId()); - testPlanTestCase.setStatus(TestPlanStatus.Prepare.name()); - testPlanTestCase.setIsDel(false); - testPlanTestCase.setOrder(nextOrder); - nextOrder += ServiceUtils.ORDER_STEP; - batchMapper.insert(testPlanTestCase); - } - - sqlSession.flushStatements(); - - caseTestRelevance(request, testCaseIds); - - resetStatus(testPlan.getId()); - sqlSession.flushStatements(); - if (sqlSession != null && sqlSessionFactory != null) { - SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); } } + public void caseTestRelevance(PlanCaseRelevanceRequest request, List testCaseIds) { //同步添加关联的接口和测试用例 if (!request.getChecked()) {