fix(接口测试): 修复同名数据源执行过程产生大量错误日志问题

--bug=1016451 --user=赵勇 [接口测试]github#17539我把metersphere的接口自动化中的50个场景导入到一个新场景中运行时控制台报错。这些场景单独运行都是没问题的,就是导入到一个新的场景中控制台就报这个错了,并且运行时也有大量报错了 https://www.tapd.cn/55049933/s/1306709
This commit is contained in:
fit2-zhao 2022-11-28 17:34:34 +08:00 committed by fit2-zhao
parent a498e0463b
commit 6c134fb9fc
4 changed files with 25 additions and 16 deletions

View File

@ -981,4 +981,8 @@ public class ElementUtil {
}
return assertions;
}
public static String getDataSourceName(String name) {
return StringUtils.join(name, "-", UUID.randomUUID().toString());
}
}

View File

@ -146,8 +146,9 @@ public class MsJDBCPostProcessor extends MsTestElement {
MSException.throwException(StringUtils.isNotEmpty(this.getName()) ? this.getName() + "" + message : message);
}
}
final HashTree samplerHashTree = tree.add(jdbcPostProcessor(config));
tree.add(jdbcDataSource());
JDBCPostProcessor jdbcPostProcessor = jdbcPostProcessor(config);
final HashTree samplerHashTree = tree.add(jdbcPostProcessor);
tree.add(jdbcDataSource(jdbcPostProcessor.getDataSource()));
Arguments arguments = arguments(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "Arguments", this.getVariables());
if (arguments != null) {
tree.add(arguments);
@ -260,7 +261,8 @@ public class MsJDBCPostProcessor extends MsTestElement {
ElementUtil.setBaseParams(jdbcPostProcessor, this.getParent(), config, this.getId(), this.getIndex());
// request.getDataSource() 是ID需要转换为Name
jdbcPostProcessor.setProperty("dataSource", this.dataSource.getName());
jdbcPostProcessor.setDataSource(ElementUtil.getDataSourceName(this.dataSource.getName()));
jdbcPostProcessor.setProperty("dataSource", jdbcPostProcessor.getDataSource());
jdbcPostProcessor.setProperty("query", this.getQuery());
jdbcPostProcessor.setProperty("queryTimeout", String.valueOf(this.getQueryTimeout()));
jdbcPostProcessor.setProperty("resultVariable", this.getResultVariable());
@ -270,7 +272,7 @@ public class MsJDBCPostProcessor extends MsTestElement {
return jdbcPostProcessor;
}
private DataSourceElement jdbcDataSource() {
private DataSourceElement jdbcDataSource(String sourceName) {
DataSourceElement dataSourceElement = new DataSourceElement();
dataSourceElement.setEnabled(true);
dataSourceElement.setName(this.getName() + " JDBCDataSource");
@ -279,7 +281,7 @@ public class MsJDBCPostProcessor extends MsTestElement {
dataSourceElement.setProperty("autocommit", true);
dataSourceElement.setProperty("keepAlive", true);
dataSourceElement.setProperty("preinit", false);
dataSourceElement.setProperty("dataSource", dataSource.getName());
dataSourceElement.setProperty("dataSource", sourceName);
dataSourceElement.setProperty("dbUrl", dataSource.getDbUrl());
dataSourceElement.setProperty("driver", dataSource.getDriver());
dataSourceElement.setProperty("username", dataSource.getUsername());

View File

@ -134,8 +134,9 @@ public class MsJDBCPreProcessor extends MsTestElement {
MSException.throwException(StringUtils.isNotEmpty(this.getName()) ? this.getName() + "" + message : message);
}
}
final HashTree samplerHashTree = tree.add(jdbcPreProcessor(config));
tree.add(jdbcDataSource());
JDBCPreProcessor jdbcPreProcessor = jdbcPreProcessor(config);
final HashTree samplerHashTree = tree.add(jdbcPreProcessor);
tree.add(jdbcDataSource(jdbcPreProcessor.getDataSource()));
Arguments arguments = arguments(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "Arguments", this.getVariables());
if (arguments != null) {
tree.add(arguments);
@ -247,8 +248,8 @@ public class MsJDBCPreProcessor extends MsTestElement {
jdbcPreProcessor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
ElementUtil.setBaseParams(jdbcPreProcessor, this.getParent(), config, this.getId(), this.getIndex());
jdbcPreProcessor.setProperty("dataSource", this.dataSource.getName());
jdbcPreProcessor.setDataSource(ElementUtil.getDataSourceName(this.dataSource.getName()));
jdbcPreProcessor.setProperty("dataSource", jdbcPreProcessor.getDataSource());
jdbcPreProcessor.setProperty("query", this.getQuery());
jdbcPreProcessor.setProperty("queryTimeout", String.valueOf(this.getQueryTimeout()));
jdbcPreProcessor.setProperty("resultVariable", this.getResultVariable());
@ -258,7 +259,7 @@ public class MsJDBCPreProcessor extends MsTestElement {
return jdbcPreProcessor;
}
private DataSourceElement jdbcDataSource() {
private DataSourceElement jdbcDataSource(String sourceName) {
DataSourceElement dataSourceElement = new DataSourceElement();
dataSourceElement.setEnabled(true);
dataSourceElement.setName(this.getName() + " JDBCDataSource");
@ -267,7 +268,7 @@ public class MsJDBCPreProcessor extends MsTestElement {
dataSourceElement.setProperty("autocommit", true);
dataSourceElement.setProperty("keepAlive", true);
dataSourceElement.setProperty("preinit", false);
dataSourceElement.setProperty("dataSource", dataSource.getName());
dataSourceElement.setProperty("dataSource", sourceName);
dataSourceElement.setProperty("dbUrl", dataSource.getDbUrl());
dataSourceElement.setProperty("driver", dataSource.getDriver());
dataSourceElement.setProperty("username", dataSource.getUsername());

View File

@ -142,8 +142,9 @@ public class MsJDBCSampler extends MsTestElement {
String message = "数据源为空请选择数据源";
MSException.throwException(StringUtils.isNotEmpty(this.getName()) ? this.getName() + "" + message : message);
}
final HashTree samplerHashTree = tree.add(jdbcSampler(config));
tree.add(jdbcDataSource());
JDBCSampler jdbcSampler = jdbcSampler(config);
final HashTree samplerHashTree = tree.add(jdbcSampler);
tree.add(jdbcDataSource(jdbcSampler.getDataSource()));
Arguments arguments = arguments(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "Arguments", this.getVariables());
if (arguments != null) {
tree.add(arguments);
@ -287,7 +288,8 @@ public class MsJDBCSampler extends MsTestElement {
sampler.setProperty(TestElement.TEST_CLASS, JDBCSampler.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
ElementUtil.setBaseParams(sampler, this.getParent(), config, this.getId(), this.getIndex());
sampler.setProperty("dataSource", this.dataSource.getName());
sampler.setDataSource(ElementUtil.getDataSourceName(this.dataSource.getName()));
sampler.setProperty("dataSource", sampler.getDataSource());
sampler.setProperty("query", this.getQuery());
sampler.setProperty("queryTimeout", String.valueOf(this.getQueryTimeout()));
sampler.setProperty("resultVariable", this.getResultVariable());
@ -297,7 +299,7 @@ public class MsJDBCSampler extends MsTestElement {
return sampler;
}
private DataSourceElement jdbcDataSource() {
private DataSourceElement jdbcDataSource(String dataSourceName) {
DataSourceElement dataSourceElement = new DataSourceElement();
dataSourceElement.setEnabled(true);
dataSourceElement.setName(this.getName() + " JDBCDataSource");
@ -306,7 +308,7 @@ public class MsJDBCSampler extends MsTestElement {
dataSourceElement.setProperty("autocommit", true);
dataSourceElement.setProperty("keepAlive", true);
dataSourceElement.setProperty("preinit", false);
dataSourceElement.setProperty("dataSource", dataSource.getName());
dataSourceElement.setProperty("dataSource", dataSourceName);
dataSourceElement.setProperty("dbUrl", dataSource.getDbUrl());
dataSourceElement.setProperty("driver", dataSource.getDriver());
dataSourceElement.setProperty("username", dataSource.getUsername());