From bf51986a61977e6e70db8902bee979ace19c6a73 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Mon, 7 Mar 2022 11:51:28 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E9=80=A0=E6=88=90=E9=98=BB=E5=A1=9E=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/jmeter/ExecutedHandleSingleton.java | 24 --- .../api/jmeter/MsDebugListener.java | 2 +- .../metersphere/api/jmeter/RunningParam.java | 26 --- .../ApiEnvironmentRunningParamService.java | 176 ++++++++---------- .../api/service/TestResultService.java | 8 +- .../commons/utils/HashTreeUtil.java | 2 +- .../track/service/TestPlanReportService.java | 2 +- 7 files changed, 88 insertions(+), 152 deletions(-) delete mode 100644 backend/src/main/java/io/metersphere/api/jmeter/ExecutedHandleSingleton.java delete mode 100644 backend/src/main/java/io/metersphere/api/jmeter/RunningParam.java diff --git a/backend/src/main/java/io/metersphere/api/jmeter/ExecutedHandleSingleton.java b/backend/src/main/java/io/metersphere/api/jmeter/ExecutedHandleSingleton.java deleted file mode 100644 index 50d514a74b..0000000000 --- a/backend/src/main/java/io/metersphere/api/jmeter/ExecutedHandleSingleton.java +++ /dev/null @@ -1,24 +0,0 @@ -package io.metersphere.api.jmeter; - -import io.metersphere.api.service.ApiEnvironmentRunningParamService; -import io.metersphere.commons.utils.CommonBeanFactory; - -import java.util.List; - -/** - * 执行结束后的处理(单例类) - * - * @author song.tianyang - * @Date 2021/10/29 11:22 上午 - */ -public class ExecutedHandleSingleton { - private static volatile ApiEnvironmentRunningParamService apiEnvironmentRunningParamService = CommonBeanFactory.getBean(ApiEnvironmentRunningParamService.class); - private ExecutedHandleSingleton() { - } - - public static void parseEnvironment(List evnStrList) { - for (String evnStr: evnStrList) { - apiEnvironmentRunningParamService.parseEvn(evnStr); - } - } -} diff --git a/backend/src/main/java/io/metersphere/api/jmeter/MsDebugListener.java b/backend/src/main/java/io/metersphere/api/jmeter/MsDebugListener.java index d2b7cef2e1..f7171e4515 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/MsDebugListener.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/MsDebugListener.java @@ -98,7 +98,7 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi @Override public void testEnded(String host) { - LoggerUtil.debug("TestEnded " + this.getName()); + LoggerUtil.info("Debug TestEnded " + this.getName()); MsgDto dto = new MsgDto(); dto.setExecEnd(false); dto.setContent(TEST_END); diff --git a/backend/src/main/java/io/metersphere/api/jmeter/RunningParam.java b/backend/src/main/java/io/metersphere/api/jmeter/RunningParam.java deleted file mode 100644 index ef0c8fed6b..0000000000 --- a/backend/src/main/java/io/metersphere/api/jmeter/RunningParam.java +++ /dev/null @@ -1,26 +0,0 @@ -package io.metersphere.api.jmeter; - -import io.metersphere.api.service.ApiEnvironmentRunningParamService; -import io.metersphere.commons.utils.CommonBeanFactory; - -/** - * @author song.tianyang - * 2021/5/13 5:24 下午 - */ -public class RunningParam { - private static ApiEnvironmentRunningParamService apiEnvironmentRunningParamService; - - public static void setParam(String enviromentId, String key, String value){ - checkService(); - apiEnvironmentRunningParamService.addParam(enviromentId,key,value); - } - - public static void deleteParam(String enviromentId, String key){ - } - - public static void checkService(){ - if(apiEnvironmentRunningParamService == null){ - apiEnvironmentRunningParamService = CommonBeanFactory.getBean(ApiEnvironmentRunningParamService.class); - } - } -} diff --git a/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java b/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java index 069f3cc184..04fbf08fd1 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java @@ -4,148 +4,132 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs; import io.metersphere.base.mapper.ApiTestEnvironmentMapper; +import io.metersphere.utils.LoggerUtil; import org.apache.commons.lang3.ArrayUtils; 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.mybatis.spring.SqlSessionUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.util.List; /** * @author song.tianyang - * 2021/5/13 6:24 下午 + * 2021/5/13 6:24 下午 */ @Service @Transactional(rollbackFor = Exception.class) public class ApiEnvironmentRunningParamService { @Resource - ApiTestEnvironmentService apiTestEnvironmentService; - @Resource - SqlSessionFactory sqlSessionFactory; + ApiTestEnvironmentMapper testEnvironmentMapper; - public synchronized void addParam(String enviromentId, String key, String value) { - - if(StringUtils.isEmpty(key)){ + public void addParam(String environmentId, String key, String value) { + if (StringUtils.isEmpty(key)) { return; } - ApiTestEnvironmentWithBLOBs apiTestEnvironmentWithBLOBs = apiTestEnvironmentService.get(enviromentId); - if(apiTestEnvironmentWithBLOBs == null ){ + ApiTestEnvironmentWithBLOBs environment = testEnvironmentMapper.selectByPrimaryKey(environmentId); + if (environment == null) { return; } - - JSONObject configObj = JSONObject.parseObject(apiTestEnvironmentWithBLOBs.getConfig()); - if(configObj.containsKey("commonConfig")){ - JSONObject commonConfig = configObj.getJSONObject("commonConfig"); - if(commonConfig.containsKey("variables")){ - JSONArray variables = commonConfig.getJSONArray("variables"); - boolean contains = false; - for(int i = 0;i 1){ + String[] envStringArr = envStr.split("\n"); + for (String env : envStringArr) { + if (StringUtils.contains(env, "=")) { + String[] envItem = env.split("="); + if (envItem.length > 1) { String jmeterVarKey = envItem[0]; - if(this.checkValidity(jmeterVarKey,"MS.ENV.")){ - String [] envAndKeyArr = jmeterVarKey.substring("MS.ENV.".length()).split("\\."); + if (this.checkValidity(jmeterVarKey, "MS.ENV.")) { + String[] envAndKeyArr = jmeterVarKey.substring("MS.ENV.".length()).split("\\."); String envId = envAndKeyArr[0]; - String [] keyArr = ArrayUtils.remove(envAndKeyArr,0); - String key = StringUtils.join(keyArr,"."); - String value = StringUtils.substring(env,jmeterVarKey.length()+1); - if(StringUtils.isNoneEmpty(envId,key,value)){ - this.addParam(envId,key,value); + String[] keyArr = ArrayUtils.remove(envAndKeyArr, 0); + String key = StringUtils.join(keyArr, "."); + String value = StringUtils.substring(env, jmeterVarKey.length() + 1); + if (StringUtils.isNoneEmpty(envId, key, value)) { + this.addParam(envId, key, value); } } } } } } - public boolean checkValidity (String str, String regex) { - if(str == null){ + + public void parseEnvironment(List evnStrList) { + for (String evnStr : evnStrList) { + this.parseEvn(evnStr); + } + } + + public boolean checkValidity(String str, String regex) { + if (str == null) { return false; } - if(regex == null){ - return true; + if (regex == null) { + return true; } if (str.startsWith(regex)) { diff --git a/backend/src/main/java/io/metersphere/api/service/TestResultService.java b/backend/src/main/java/io/metersphere/api/service/TestResultService.java index 71af4fe90e..1df8d48cbd 100644 --- a/backend/src/main/java/io/metersphere/api/service/TestResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/TestResultService.java @@ -1,7 +1,6 @@ package io.metersphere.api.service; import io.metersphere.api.dto.automation.ApiTestReportVariable; -import io.metersphere.api.jmeter.ExecutedHandleSingleton; import io.metersphere.base.domain.*; import io.metersphere.base.mapper.ApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ApiScenarioMapper; @@ -54,6 +53,8 @@ public class TestResultService { private ApiTestCaseService apiTestCaseService; @Resource private ApiDefinitionExecResultMapper apiDefinitionExecResultMapper; + @Resource + private ApiEnvironmentRunningParamService apiEnvironmentRunningParamService; public void saveResults(ResultDTO dto) { // 处理环境 @@ -64,7 +65,8 @@ public class TestResultService { List requestResults = dto.getRequestResults(); //处理环境参数 if (CollectionUtils.isNotEmpty(environmentList)) { - ExecutedHandleSingleton.parseEnvironment(environmentList); + apiEnvironmentRunningParamService.parseEnvironment(environmentList); + } //测试计划定时任务-接口执行逻辑的话,需要同步测试计划的报告数据 if (StringUtils.equalsAny(dto.getRunMode(), ApiRunMode.SCHEDULE_API_PLAN.name(), ApiRunMode.JENKINS_API_PLAN.name(), ApiRunMode.MANUAL_PLAN.name())) { @@ -88,7 +90,7 @@ public class TestResultService { } //处理环境参数 if (CollectionUtils.isNotEmpty(environmentList)) { - ExecutedHandleSingleton.parseEnvironment(environmentList); + apiEnvironmentRunningParamService.parseEnvironment(environmentList); } // 处理用例/场景和计划关系 updateTestCaseStates(dto.getRequestResults(), dto.getRunMode()); diff --git a/backend/src/main/java/io/metersphere/commons/utils/HashTreeUtil.java b/backend/src/main/java/io/metersphere/commons/utils/HashTreeUtil.java index 3a1c5156c5..01ade0869a 100644 --- a/backend/src/main/java/io/metersphere/commons/utils/HashTreeUtil.java +++ b/backend/src/main/java/io/metersphere/commons/utils/HashTreeUtil.java @@ -81,7 +81,7 @@ public class HashTreeUtil { return returnMap; } - public synchronized void setEnvParamsMapToHashTree(HashTree hashTree, Map> envParamsMap) { + public void setEnvParamsMapToHashTree(HashTree hashTree, Map> envParamsMap) { if (hashTree != null) { Map allParamMap = new HashMap<>(); for (Map paramMap : envParamsMap.values()) { diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java index da55384c0d..0b0b7f36e9 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanReportService.java @@ -402,7 +402,7 @@ public class TestPlanReportService { return principalName; } - public synchronized void updateReport(List testPlanReportIdList, String runMode, String triggerMode) { + public void updateReport(List testPlanReportIdList, String runMode, String triggerMode) { for (String planReportId : testPlanReportIdList) { this.countReportByTestPlanReportId(planReportId, runMode, triggerMode); }