fix(接口测试): 修复case编辑时日志中出现异常信息

--bug=1020668 --user=赵勇 【接口测试】case编辑-未选择环境保存-后端报sql异常 https://www.tapd.cn/55049933/s/1313316
This commit is contained in:
fit2-zhao 2022-12-06 11:17:48 +08:00 committed by fit2-zhao
parent c9708dec81
commit 951146df96
9 changed files with 18 additions and 201 deletions

View File

@ -2,7 +2,6 @@ package io.metersphere.api.exec.queue;
import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.JMeterThreadUtils; import io.metersphere.api.jmeter.JMeterThreadUtils;
import io.metersphere.cache.JMeterEngineCache;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.dto.JmeterRunRequestDTO; import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
@ -24,9 +23,6 @@ public class ExecTask implements Runnable {
Object res = PoolExecBlockingQueueUtil.take(request.getReportId()); Object res = PoolExecBlockingQueueUtil.take(request.getReportId());
if (res == null && !JMeterThreadUtils.isRunning(request.getReportId(), request.getTestId())) { if (res == null && !JMeterThreadUtils.isRunning(request.getReportId(), request.getTestId())) {
LoggerUtil.info("任务执行超时", request.getReportId()); LoggerUtil.info("任务执行超时", request.getReportId());
if (JMeterEngineCache.runningEngine.containsKey(request.getReportId())) {
JMeterEngineCache.runningEngine.remove(request.getReportId());
}
} }
} }
} }

View File

@ -3,7 +3,6 @@ package io.metersphere.api.jmeter;
import io.metersphere.api.exec.queue.PoolExecBlockingQueueUtil; import io.metersphere.api.exec.queue.PoolExecBlockingQueueUtil;
import io.metersphere.api.jmeter.utils.ReportStatusUtil; import io.metersphere.api.jmeter.utils.ReportStatusUtil;
import io.metersphere.cache.JMeterEngineCache;
import io.metersphere.commons.constants.CommonConstants; import io.metersphere.commons.constants.CommonConstants;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.FileUtils; import io.metersphere.commons.utils.FileUtils;
@ -121,9 +120,6 @@ public class MsApiBackendListener extends AbstractBackendListenerClient implemen
LoggerUtil.info("进入监听开始关闭CSV", dto.getReportId()); LoggerUtil.info("进入监听开始关闭CSV", dto.getReportId());
FileServer.getFileServer().closeCsv(dto.getReportId()); FileServer.getFileServer().closeCsv(dto.getReportId());
} }
if (JMeterEngineCache.runningEngine.containsKey(dto.getReportId())) {
JMeterEngineCache.runningEngine.remove(dto.getReportId());
}
} }
} }

View File

@ -16,7 +16,6 @@ import io.metersphere.base.mapper.ApiScenarioReportMapper;
import io.metersphere.base.mapper.ApiTestCaseMapper; import io.metersphere.base.mapper.ApiTestCaseMapper;
import io.metersphere.base.mapper.plan.TestPlanApiCaseMapper; import io.metersphere.base.mapper.plan.TestPlanApiCaseMapper;
import io.metersphere.base.mapper.plan.TestPlanApiScenarioMapper; import io.metersphere.base.mapper.plan.TestPlanApiScenarioMapper;
import io.metersphere.cache.JMeterEngineCache;
import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.enums.ApiReportStatus; import io.metersphere.commons.enums.ApiReportStatus;
import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.BeanUtils;
@ -153,9 +152,6 @@ public class RemakeReportService {
dto.setQueueId(request.getQueueId()); dto.setQueueId(request.getQueueId());
dto.setTestId(request.getTestId()); dto.setTestId(request.getTestId());
if (JMeterEngineCache.runningEngine.containsKey(dto.getReportId())) {
JMeterEngineCache.runningEngine.remove(dto.getReportId());
}
LoggerUtil.info("进入异常结果处理:" + dto.getRunMode() + " 整体处理完成", dto.getReportId()); LoggerUtil.info("进入异常结果处理:" + dto.getRunMode() + " 整体处理完成", dto.getReportId());
// 全局并发队列 // 全局并发队列
PoolExecBlockingQueueUtil.offer(dto.getReportId()); PoolExecBlockingQueueUtil.offer(dto.getReportId());

View File

@ -1064,9 +1064,11 @@ public class ApiTestCaseService {
} }
try { try {
List<String> envIds = environments.stream().filter(t -> StringUtils.isNotBlank(t.getValue()) && !StringUtils.equalsIgnoreCase(t.getValue(), "null")).map(ParamsDTO::getValue).collect(Collectors.toList()); List<String> envIds = environments.stream().filter(t -> StringUtils.isNotBlank(t.getValue()) && !StringUtils.equalsIgnoreCase(t.getValue(), "null")).map(ParamsDTO::getValue).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(envIds)) {
ApiTestEnvironmentExample example = new ApiTestEnvironmentExample(); ApiTestEnvironmentExample example = new ApiTestEnvironmentExample();
example.createCriteria().andIdIn(envIds); example.createCriteria().andIdIn(envIds);
List<ApiTestEnvironment> environmentList = apiTestEnvironmentMapper.selectByExample(example); List<ApiTestEnvironment> environmentList = apiTestEnvironmentMapper.selectByExample(example);
if (CollectionUtils.isEmpty(environmentList)) { if (CollectionUtils.isEmpty(environmentList)) {
return null; return null;
} }
@ -1079,6 +1081,7 @@ public class ApiTestCaseService {
} }
}); });
return caseEnvMap; return caseEnvMap;
}
} catch (Exception e) { } catch (Exception e) {
LogUtil.error("api case environmentId incorrect parsing", e); LogUtil.error("api case environmentId incorrect parsing", e);
} }

View File

@ -4,21 +4,20 @@ import io.metersphere.api.exec.queue.ExecThreadPoolExecutor;
import io.metersphere.api.exec.queue.PoolExecBlockingQueueUtil; import io.metersphere.api.exec.queue.PoolExecBlockingQueueUtil;
import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.JMeterThreadUtils; import io.metersphere.api.jmeter.JMeterThreadUtils;
import io.metersphere.service.ApiExecutionQueueService;
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.ApiScenarioReportMapper; import io.metersphere.base.mapper.ApiScenarioReportMapper;
import io.metersphere.base.mapper.TestResourceMapper; import io.metersphere.base.mapper.TestResourceMapper;
import io.metersphere.base.mapper.TestResourcePoolMapper; import io.metersphere.base.mapper.TestResourcePoolMapper;
import io.metersphere.base.mapper.ext.BaseTaskMapper;
import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper;
import io.metersphere.base.mapper.ext.ExtApiScenarioReportMapper; import io.metersphere.base.mapper.ext.ExtApiScenarioReportMapper;
import io.metersphere.base.mapper.ext.BaseTaskMapper;
import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.enums.ApiReportStatus; import io.metersphere.commons.enums.ApiReportStatus;
import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.dto.NodeDTO; import io.metersphere.dto.NodeDTO;
import io.metersphere.jmeter.LocalRunner; import io.metersphere.service.ApiExecutionQueueService;
import io.metersphere.task.dto.TaskCenterDTO; import io.metersphere.task.dto.TaskCenterDTO;
import io.metersphere.task.dto.TaskCenterRequest; import io.metersphere.task.dto.TaskCenterRequest;
import io.metersphere.task.dto.TaskRequestDTO; import io.metersphere.task.dto.TaskRequestDTO;
@ -213,7 +212,6 @@ public class ExtApiTaskService extends TaskService {
}}); }});
} }
} else { } else {
new LocalRunner().stop(reportId);
JMeterThreadUtils.stop(reportId); JMeterThreadUtils.stop(reportId);
} }
} }

View File

@ -1,13 +0,0 @@
package io.metersphere.cache;
import org.apache.jmeter.engine.StandardJMeterEngine;
import java.util.concurrent.ConcurrentHashMap;
public class JMeterEngineCache {
/**
* 执行中的线程池
*/
public static ConcurrentHashMap<String, StandardJMeterEngine> runningEngine = new ConcurrentHashMap<>();
}

View File

@ -1,15 +1,10 @@
package io.metersphere.jmeter; package io.metersphere.jmeter;
import io.metersphere.cache.JMeterEngineCache;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.engine.JMeterEngineException; import org.apache.jmeter.engine.JMeterEngineException;
import org.apache.jmeter.engine.StandardJMeterEngine; import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import java.util.List;
public class LocalRunner { public class LocalRunner {
private HashTree jmxTree; private HashTree jmxTree;
@ -26,31 +21,8 @@ public class LocalRunner {
try { try {
LoggerUtil.info("LocalRunner 开始执行报告",report); LoggerUtil.info("LocalRunner 开始执行报告",report);
engine.runTest(); engine.runTest();
JMeterEngineCache.runningEngine.put(report, engine);
} catch (JMeterEngineException e) { } catch (JMeterEngineException e) {
engine.stopTest(true); engine.stopTest(true);
} }
} }
public void stop(List<String> reports) {
if (CollectionUtils.isNotEmpty(reports)) {
for (String report : reports) {
StandardJMeterEngine engine = JMeterEngineCache.runningEngine.get(report);
if (engine != null) {
engine.stopTest();
JMeterEngineCache.runningEngine.remove(report);
}
}
}
}
public void stop(String report) {
if (StringUtils.isNotEmpty(report)) {
StandardJMeterEngine engine = JMeterEngineCache.runningEngine.get(report);
if (engine != null) {
engine.stopTest();
JMeterEngineCache.runningEngine.remove(report);
}
}
}
} }

View File

@ -1,63 +0,0 @@
package io.metersphere.service.utils;
import io.metersphere.commons.utils.LogUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class MsClassLoader {
public final static ConcurrentHashMap<String, MsURLClassLoader> LOADER_CACHE = new ConcurrentHashMap<>();
public MsURLClassLoader loadJar(String path) {
try {
String jarName = path.substring(path.indexOf("_") + 1);
if (StringUtils.isNotEmpty(jarName) && jarName.endsWith(".jar")) {
jarName = jarName.substring(0, jarName.length() - 4);
}
MsURLClassLoader urlClassLoader = LOADER_CACHE.get(jarName);
if (urlClassLoader != null) {
return urlClassLoader;
}
urlClassLoader = new MsURLClassLoader();
File jarFile = new File(path);
URL jarUrl = jarFile.toURI().toURL();
urlClassLoader.addURLFile(jarUrl);
LOADER_CACHE.put(jarName, urlClassLoader);
return urlClassLoader;
} catch (Exception ex) {
LogUtil.error("加载JAR包失败" + ex.getMessage());
}
return null;
}
public Class loadClass(String jarName, String name) throws ClassNotFoundException {
if (StringUtils.isNotEmpty(jarName) && jarName.endsWith(".jar")) {
jarName = jarName.substring(0, jarName.length() - 4);
}
MsURLClassLoader urlClassLoader = LOADER_CACHE.get(jarName);
if (urlClassLoader == null) {
return null;
}
return urlClassLoader.loadClass(name);
}
public void unloadJarFile(String path) throws MalformedURLException {
String jarName = path.substring(path.indexOf("_") + 1);
if (StringUtils.isNotEmpty(jarName) && jarName.endsWith(".jar")) {
jarName = jarName.substring(0, jarName.length() - 4);
}
MsURLClassLoader urlClassLoader = LOADER_CACHE.get(jarName);
if (urlClassLoader == null) {
return;
}
urlClassLoader.unloadJarFile(path);
urlClassLoader = null;
LOADER_CACHE.remove(jarName);
}
}

View File

@ -1,68 +0,0 @@
package io.metersphere.service.utils;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
public class MsURLClassLoader extends URLClassLoader {
private JarURLConnection cachedJarFile = null;
public MsURLClassLoader() {
super(new URL[]{}, findParentClassLoader());
}
/**
* 将指定的文件url添加到类加载器的classpath中去并缓存jar connection方便以后卸载jar
* 一个可想类加载器的classpath中添加的文件url
*
* @param
*/
public void addURLFile(URL file) {
try {
// 打开并缓存文件url连接
URLConnection uc = file.openConnection();
if (uc instanceof JarURLConnection) {
uc.setUseCaches(true);
((JarURLConnection) uc).getManifest();
cachedJarFile = (JarURLConnection) uc;
}
} catch (Exception e) {
System.err.println("Failed to cache plugin JAR file: " + file.toExternalForm());
}
addURL(file);
}
public void unloadJarFile(String url) {
JarURLConnection jarURLConnection = cachedJarFile;
if (jarURLConnection == null) {
return;
}
try {
System.err.println("Unloading plugin JAR file " + jarURLConnection.getJarFile().getName());
jarURLConnection.getJarFile().close();
jarURLConnection = null;
} catch (Exception e) {
System.err.println("Failed to unload JAR file\n" + e);
}
}
/**
* 定位基于当前上下文的父类加载器
*
* @return 返回可用的父类加载器.
*/
private static ClassLoader findParentClassLoader() {
ClassLoader parent = ClassLoader.getSystemClassLoader();
if (parent == null) {
parent = MsURLClassLoader.class.getClassLoader();
}
if (parent == null) {
parent = ClassLoader.getSystemClassLoader();
}
return parent;
}
}