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 @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,18 +4,14 @@ 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
@ -25,21 +21,18 @@ import javax.annotation.Resource;
@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")) {
@ -96,26 +89,10 @@ public class ApiEnvironmentRunningParamService {
configObj.put("commonConfig", commonConfig); configObj.put("commonConfig", commonConfig);
} }
apiTestEnvironmentWithBLOBs.setConfig(configObj.toJSONString()); environment.setConfig(configObj.toJSONString());
testEnvironmentMapper.updateByPrimaryKeyWithBLOBs(environment);
SqlSession sqlSession = null; } catch (Exception ex) {
try { LoggerUtil.error("设置环境变量异常:" + ex.getMessage());
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);
}
} }
} }
@ -140,6 +117,13 @@ public class ApiEnvironmentRunningParamService {
} }
} }
} }
public void parseEnvironment(List<String> evnStrList) {
for (String evnStr : evnStrList) {
this.parseEvn(evnStr);
}
}
public boolean checkValidity(String str, String regex) { public boolean checkValidity(String str, String regex) {
if (str == null) { if (str == null) {
return false; return false;

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

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