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

This commit is contained in:
fit2-zhao 2022-03-07 11:51:28 +08:00 committed by fit2-zhao
parent f53d427035
commit bf51986a61
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
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);

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 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<variables.size();i++){
JSONObject jsonObj = variables.getJSONObject(i);
if(jsonObj.containsKey("name")&&StringUtils.equals(jsonObj.getString("name"), key)){
contains = true;
jsonObj.put("value",value);
try {
JSONObject configObj = JSONObject.parseObject(environment.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 < variables.size(); i++) {
JSONObject jsonObj = variables.getJSONObject(i);
if (jsonObj.containsKey("name") && StringUtils.equals(jsonObj.getString("name"), key)) {
contains = true;
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();
itemObj.put("name",key);
itemObj.put("value",value);
itemObj.put("enable",true);
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);
JSONObject emptyObj = new JSONObject();
emptyObj.put("enable", true);
variables.add(itemObj);
variables.add(emptyObj);
commonConfig.put("variables", variables);
}
}else {
} 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);
itemObj.put("name", key);
itemObj.put("value", value);
itemObj.put("enable", true);
JSONObject emptyObj = new JSONObject();
emptyObj.put("enable",true);
emptyObj.put("enable", true);
variables.add(itemObj);
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();
emptyObj.put("enable",true);
variables.add(itemObj);
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);
}
environment.setConfig(configObj.toJSONString());
testEnvironmentMapper.updateByPrimaryKeyWithBLOBs(environment);
} catch (Exception ex) {
LoggerUtil.error("设置环境变量异常:" + ex.getMessage());
}
}
public void parseEvn(String envStr) {
String [] envStringArr = envStr.split("\n");
for (String env :envStringArr) {
if(StringUtils.contains(env,"=")){
String [] envItem = env.split("=");
if(envItem.length > 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<String> 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)) {

View File

@ -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<RequestResult> 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());

View File

@ -81,7 +81,7 @@ public class HashTreeUtil {
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) {
Map<String, String> allParamMap = new HashMap<>();
for (Map<String, String> paramMap : envParamsMap.values()) {

View File

@ -402,7 +402,7 @@ public class TestPlanReportService {
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) {
this.countReportByTestPlanReportId(planReportId, runMode, triggerMode);
}