fix(接口测试): 修复环境变量设置造成阻塞问题

This commit is contained in:
fit2-zhao 2022-03-07 11:51:28 +08:00 committed by fit2-zhao
parent d4475d06ab
commit d5a5805344
7 changed files with 88 additions and 152 deletions

View File

@ -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<String> evnStrList) {
for (String evnStr: evnStrList) {
apiEnvironmentRunningParamService.parseEvn(evnStr);
}
}
}

View File

@ -98,7 +98,7 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
@Override @Override
public void testEnded(String host) { public void testEnded(String host) {
LoggerUtil.debug("TestEnded " + this.getName()); LoggerUtil.info("Debug TestEnded " + this.getName());
MsgDto dto = new MsgDto(); MsgDto dto = new MsgDto();
dto.setExecEnd(false); dto.setExecEnd(false);
dto.setContent(TEST_END); dto.setContent(TEST_END);

View File

@ -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);
}
}
}

View File

@ -4,148 +4,132 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs; import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.base.mapper.ApiTestEnvironmentMapper; import io.metersphere.base.mapper.ApiTestEnvironmentMapper;
import io.metersphere.utils.LoggerUtil;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils; 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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List;
/** /**
* @author song.tianyang * @author song.tianyang
* 2021/5/13 6:24 下午 * 2021/5/13 6:24 下午
*/ */
@Service @Service
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public class ApiEnvironmentRunningParamService { public class ApiEnvironmentRunningParamService {
@Resource @Resource
ApiTestEnvironmentService apiTestEnvironmentService; ApiTestEnvironmentMapper testEnvironmentMapper;
@Resource
SqlSessionFactory sqlSessionFactory;
public synchronized void addParam(String enviromentId, String key, String value) { public void addParam(String environmentId, String key, String value) {
if (StringUtils.isEmpty(key)) {
if(StringUtils.isEmpty(key)){
return; return;
} }
ApiTestEnvironmentWithBLOBs apiTestEnvironmentWithBLOBs = apiTestEnvironmentService.get(enviromentId); ApiTestEnvironmentWithBLOBs environment = testEnvironmentMapper.selectByPrimaryKey(environmentId);
if(apiTestEnvironmentWithBLOBs == null ){ if (environment == null) {
return; return;
} }
try {
JSONObject configObj = JSONObject.parseObject(apiTestEnvironmentWithBLOBs.getConfig()); JSONObject configObj = JSONObject.parseObject(environment.getConfig());
if(configObj.containsKey("commonConfig")){ if (configObj.containsKey("commonConfig")) {
JSONObject commonConfig = configObj.getJSONObject("commonConfig"); JSONObject commonConfig = configObj.getJSONObject("commonConfig");
if(commonConfig.containsKey("variables")){ if (commonConfig.containsKey("variables")) {
JSONArray variables = commonConfig.getJSONArray("variables"); JSONArray variables = commonConfig.getJSONArray("variables");
boolean contains = false; boolean contains = false;
for(int i = 0;i<variables.size();i++){ for (int i = 0; i < variables.size(); i++) {
JSONObject jsonObj = variables.getJSONObject(i); JSONObject jsonObj = variables.getJSONObject(i);
if(jsonObj.containsKey("name")&&StringUtils.equals(jsonObj.getString("name"), key)){ if (jsonObj.containsKey("name") && StringUtils.equals(jsonObj.getString("name"), key)) {
contains = true; contains = true;
jsonObj.put("value",value); jsonObj.put("value", value);
}
} }
} if (!contains) {
if(!contains){ JSONObject itemObj = new JSONObject();
itemObj.put("name", key);
itemObj.put("value", value);
itemObj.put("enable", true);
if (variables.size() == 0) {
variables.add(itemObj);
} else {
variables.add(variables.size() - 1, itemObj);
}
commonConfig.put("variables", variables);
}
} else {
JSONArray variables = new JSONArray();
JSONObject itemObj = new JSONObject(); JSONObject itemObj = new JSONObject();
itemObj.put("name",key); itemObj.put("name", key);
itemObj.put("value",value); itemObj.put("value", value);
itemObj.put("enable",true); itemObj.put("enable", true);
if(variables.size() == 0){ JSONObject emptyObj = new JSONObject();
variables.add(itemObj); emptyObj.put("enable", true);
}else {
variables.add(variables.size()-1,itemObj); variables.add(itemObj);
} variables.add(emptyObj);
commonConfig.put("variables",variables); commonConfig.put("variables", variables);
} }
}else { } else {
JSONObject commonConfig = new JSONObject();
JSONArray variables = new JSONArray(); JSONArray variables = new JSONArray();
JSONObject itemObj = new JSONObject(); JSONObject itemObj = new JSONObject();
itemObj.put("name",key); itemObj.put("name", key);
itemObj.put("value",value); itemObj.put("value", value);
itemObj.put("enable",true); itemObj.put("enable", true);
JSONObject emptyObj = new JSONObject(); JSONObject emptyObj = new JSONObject();
emptyObj.put("enable",true); emptyObj.put("enable", true);
variables.add(itemObj); variables.add(itemObj);
variables.add(emptyObj); variables.add(emptyObj);
commonConfig.put("variables",variables); commonConfig.put("variables", variables);
configObj.put("commonConfig", commonConfig);
} }
}else {
JSONObject commonConfig = new JSONObject();
JSONArray variables = new JSONArray();
JSONObject itemObj = new JSONObject();
itemObj.put("name",key);
itemObj.put("value",value);
itemObj.put("enable",true);
JSONObject emptyObj = new JSONObject(); environment.setConfig(configObj.toJSONString());
emptyObj.put("enable",true); testEnvironmentMapper.updateByPrimaryKeyWithBLOBs(environment);
} catch (Exception ex) {
variables.add(itemObj); LoggerUtil.error("设置环境变量异常:" + ex.getMessage());
variables.add(emptyObj);
commonConfig.put("variables",variables);
configObj.put("commonConfig",commonConfig);
}
apiTestEnvironmentWithBLOBs.setConfig(configObj.toJSONString());
SqlSession sqlSession = null;
try {
sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE);
ApiTestEnvironmentMapper batchMapper = sqlSession.getMapper(ApiTestEnvironmentMapper.class);
batchMapper.updateByPrimaryKeyWithBLOBs(apiTestEnvironmentWithBLOBs);
sqlSession.flushStatements();
if (sqlSession != null && sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}
}catch (Exception e){
sqlSession.rollback();
if (sqlSession != null && sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}
}finally {
if (sqlSession != null && sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}
} }
} }
public void parseEvn(String envStr) { public void parseEvn(String envStr) {
String [] envStringArr = envStr.split("\n"); String[] envStringArr = envStr.split("\n");
for (String env :envStringArr) { for (String env : envStringArr) {
if(StringUtils.contains(env,"=")){ if (StringUtils.contains(env, "=")) {
String [] envItem = env.split("="); String[] envItem = env.split("=");
if(envItem.length > 1){ if (envItem.length > 1) {
String jmeterVarKey = envItem[0]; String jmeterVarKey = envItem[0];
if(this.checkValidity(jmeterVarKey,"MS.ENV.")){ if (this.checkValidity(jmeterVarKey, "MS.ENV.")) {
String [] envAndKeyArr = jmeterVarKey.substring("MS.ENV.".length()).split("\\."); String[] envAndKeyArr = jmeterVarKey.substring("MS.ENV.".length()).split("\\.");
String envId = envAndKeyArr[0]; String envId = envAndKeyArr[0];
String [] keyArr = ArrayUtils.remove(envAndKeyArr,0); String[] keyArr = ArrayUtils.remove(envAndKeyArr, 0);
String key = StringUtils.join(keyArr,"."); String key = StringUtils.join(keyArr, ".");
String value = StringUtils.substring(env,jmeterVarKey.length()+1); String value = StringUtils.substring(env, jmeterVarKey.length() + 1);
if(StringUtils.isNoneEmpty(envId,key,value)){ if (StringUtils.isNoneEmpty(envId, key, value)) {
this.addParam(envId,key,value); this.addParam(envId, key, value);
} }
} }
} }
} }
} }
} }
public boolean checkValidity (String str, String regex) {
if(str == null){ public void parseEnvironment(List<String> evnStrList) {
for (String evnStr : evnStrList) {
this.parseEvn(evnStr);
}
}
public boolean checkValidity(String str, String regex) {
if (str == null) {
return false; return false;
} }
if(regex == null){ if (regex == null) {
return true; return true;
} }
if (str.startsWith(regex)) { if (str.startsWith(regex)) {

View File

@ -1,7 +1,6 @@
package io.metersphere.api.service; package io.metersphere.api.service;
import io.metersphere.api.dto.automation.ApiTestReportVariable; import io.metersphere.api.dto.automation.ApiTestReportVariable;
import io.metersphere.api.jmeter.ExecutedHandleSingleton;
import io.metersphere.base.domain.*; import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ApiDefinitionExecResultMapper;
import io.metersphere.base.mapper.ApiScenarioMapper; import io.metersphere.base.mapper.ApiScenarioMapper;
@ -54,6 +53,8 @@ public class TestResultService {
private ApiTestCaseService apiTestCaseService; private ApiTestCaseService apiTestCaseService;
@Resource @Resource
private ApiDefinitionExecResultMapper apiDefinitionExecResultMapper; private ApiDefinitionExecResultMapper apiDefinitionExecResultMapper;
@Resource
private ApiEnvironmentRunningParamService apiEnvironmentRunningParamService;
public void saveResults(ResultDTO dto) { public void saveResults(ResultDTO dto) {
// 处理环境 // 处理环境
@ -64,7 +65,8 @@ public class TestResultService {
List<RequestResult> requestResults = dto.getRequestResults(); List<RequestResult> requestResults = dto.getRequestResults();
//处理环境参数 //处理环境参数
if (CollectionUtils.isNotEmpty(environmentList)) { 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())) { 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)) { if (CollectionUtils.isNotEmpty(environmentList)) {
ExecutedHandleSingleton.parseEnvironment(environmentList); apiEnvironmentRunningParamService.parseEnvironment(environmentList);
} }
// 处理用例/场景和计划关系 // 处理用例/场景和计划关系
updateTestCaseStates(dto.getRequestResults(), dto.getRunMode()); updateTestCaseStates(dto.getRequestResults(), dto.getRunMode());

View File

@ -81,7 +81,7 @@ public class HashTreeUtil {
return returnMap; return returnMap;
} }
public synchronized void setEnvParamsMapToHashTree(HashTree hashTree, Map<String, Map<String, String>> envParamsMap) { public void setEnvParamsMapToHashTree(HashTree hashTree, Map<String, Map<String, String>> envParamsMap) {
if (hashTree != null) { if (hashTree != null) {
Map<String, String> allParamMap = new HashMap<>(); Map<String, String> allParamMap = new HashMap<>();
for (Map<String, String> paramMap : envParamsMap.values()) { for (Map<String, String> paramMap : envParamsMap.values()) {

View File

@ -400,7 +400,7 @@ public class TestPlanReportService {
return principalName; return principalName;
} }
public synchronized void updateReport(List<String> testPlanReportIdList, String runMode, String triggerMode) { public void updateReport(List<String> testPlanReportIdList, String runMode, String triggerMode) {
for (String planReportId : testPlanReportIdList) { for (String planReportId : testPlanReportIdList) {
this.countReportByTestPlanReportId(planReportId, runMode, triggerMode); this.countReportByTestPlanReportId(planReportId, runMode, triggerMode);
} }