feat(环境配置): 环境配置通用配置增加超时时间配置 #1002297

https://www.tapd.cn/55049933/prong/stories/view/1155049933001002297
This commit is contained in:
song-tianyang 2021-08-18 17:23:15 +08:00 committed by 刘瑞斌
parent 5800b41ba2
commit 94d546c924
5 changed files with 164 additions and 69 deletions

View File

@ -14,6 +14,7 @@ import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.HttpConfig;
import io.metersphere.api.dto.scenario.HttpConfigCondition;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.CommonConfig;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.dto.ssl.KeyStoreConfig;
import io.metersphere.api.dto.ssl.KeyStoreFile;
@ -191,8 +192,6 @@ public class MsHTTPSamplerProxy extends MsTestElement {
sampler.setMethod(this.getMethod());
sampler.setContentEncoding("UTF-8");
sampler.setConnectTimeout(this.getConnectTimeout() == null ? "6000" : this.getConnectTimeout());
sampler.setResponseTimeout(this.getResponseTimeout() == null ? "6000" : this.getResponseTimeout());
sampler.setFollowRedirects(this.isFollowRedirects());
sampler.setUseKeepAlive(true);
sampler.setDoMultipart(this.isDoMultipartPost());
@ -204,8 +203,13 @@ public class MsHTTPSamplerProxy extends MsTestElement {
compatible(config);
this.initConnectAndResponseTimeout(config);
sampler.setConnectTimeout(this.getConnectTimeout() == null ? "60000" : this.getConnectTimeout());
sampler.setResponseTimeout(this.getResponseTimeout() == null ? "60000" : this.getResponseTimeout());
HttpConfig httpConfig = getHttpConfig(config);
setSamplerPath(config, httpConfig, sampler);
// 请求体
@ -271,6 +275,28 @@ public class MsHTTPSamplerProxy extends MsTestElement {
}
private void initConnectAndResponseTimeout(ParameterConfig config) {
if (config.isEffective(this.getProjectId())) {
String useEvnId = config.getConfig().get(this.getProjectId()).getApiEnvironmentid();
if (StringUtils.isNotEmpty(useEvnId) && !StringUtils.equals(useEvnId, this.getEnvironmentId())) {
this.setEnvironmentId(useEvnId);
}
CommonConfig commonConfig = config.getConfig().get(this.getProjectId()).getCommonConfig();
if(commonConfig != null){
if(this.getConnectTimeout() == null || StringUtils.equals(this.getConnectTimeout(),"60000")){
if(commonConfig.getRequestTimeout() != 0){
this.setConnectTimeout(String.valueOf(commonConfig.getRequestTimeout()));
}
}
if(this.getResponseTimeout() == null || StringUtils.equals(this.getResponseTimeout(),"60000")){
if(commonConfig.getResponseTimeout() != 0){
this.setResponseTimeout(String.valueOf(commonConfig.getResponseTimeout()));
}
}
}
}
}
private EnvironmentConfig getEnvironmentConfig(ParameterConfig config) {
return config.getConfig().get(this.getProjectId());
}

View File

@ -10,4 +10,6 @@ public class CommonConfig {
private List<KeyValue> variables;
private boolean enableHost;
private List<Host> hosts;
private int requestTimeout;
private int responseTimeout;
}

View File

@ -18,6 +18,7 @@ public class TCPPool {
public static String createTcp(int port){
String returnString = "";
if(port > 0){
TCPServer tcpServer = null;
if(serverSockedMap.containsKey(port)){
tcpServer = serverSockedMap.get(port);
@ -36,6 +37,7 @@ public class TCPPool {
e.printStackTrace();
MSException.throwException(e.getMessage());
}
}
return returnString;
}

View File

@ -35,6 +35,7 @@ import io.metersphere.track.service.TestPlanProjectService;
import io.metersphere.track.service.TestPlanService;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -89,6 +90,9 @@ public class ProjectService {
@Resource
private ScheduleService scheduleService;
@Value("${tcp.mock.port}")
private String tcpMockPorts;
public Project addProject(Project project) {
if (StringUtils.isBlank(project.getName())) {
MSException.throwException(Translator.get("project_name_is_null"));
@ -273,6 +277,10 @@ public class ProjectService {
lastTcpNum = oldData.getMockTcpPort().intValue();
}
if(project.getMockTcpPort().intValue() > 0){
this.checkMockTcpPort(project.getMockTcpPort().intValue());
}
this.checkProjectTcpPort(project);
project.setCreateTime(null);
@ -294,6 +302,39 @@ public class ProjectService {
}
}
private void checkMockTcpPort(int port) {
if(StringUtils.isNotEmpty(this.tcpMockPorts)){
try {
if(this.tcpMockPorts.contains("-")){
String [] tcpMockPortArr = this.tcpMockPorts.split("-");
int num1 = Integer.parseInt(tcpMockPortArr[0]);
int num2 = Integer.parseInt(tcpMockPortArr[1]);
int startNum = num1 > num2 ? num2 : num1;
int endNum = num1 < num2 ? num2 : num1;
if(port < startNum || port > endNum){
MSException.throwException("Tcp port is not in ["+this.tcpMockPorts+"]");
}
}else {
int tcpPortConfigNum = Integer.parseInt(this.tcpMockPorts);
if(port != tcpPortConfigNum){
MSException.throwException("Tcp port is not equals ["+this.tcpMockPorts+"]");
}
}
}catch (Exception e){
String errorMsg = e.getMessage();
if(!errorMsg.startsWith("Tcp")){
MSException.throwException("Tcp port config is error!");
}else {
MSException.throwException(errorMsg);
}
}
}else {
MSException.throwException("Tcp port config is error!");
}
}
private void checkProjectTcpPort(Project project) {
//判断端口是否重复
if (project.getMockTcpPort() != null && project.getMockTcpPort().intValue() != 0) {

View File

@ -8,6 +8,12 @@
<el-form-item>
<el-switch v-model="commonConfig.enableHost" active-text="Hosts"/>
</el-form-item>
<el-form-item>
<span>{{$t('api_test.environment.request_timeout')}}:</span>
<el-input-number style="margin-left: 20px" v-model="commonConfig.requestTimeout">{{$t('api_test.environment.globalVariable')}}</el-input-number>
<span style="margin-left: 30px">{{$t('api_test.environment.response_timeout')}}:</span>
<el-input-number style="margin-left: 20px" v-model="commonConfig.responseTimeout">{{$t('api_test.environment.globalVariable')}}</el-input-number>
</el-form-item>
<ms-api-host-table v-if="commonConfig.enableHost" :hostTable="commonConfig.hosts" ref="refHostTable"/>
</el-form>
</div>
@ -35,6 +41,24 @@
},
}
},
created() {
if(!this.commonConfig.requestTimeout){
this.commonConfig.requestTimeout = 60000;
}
if(!this.commonConfig.responseTimeout){
this.commonConfig.responseTimeout = 60000;
}
},
watch: {
commonConfig(){
if(!this.commonConfig.requestTimeout){
this.commonConfig.requestTimeout = 60000;
}
if(!this.commonConfig.responseTimeout){
this.commonConfig.responseTimeout = 60000;
}
}
},
methods: {
validate() {
let isValidate = false;