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.HttpConfig;
import io.metersphere.api.dto.scenario.HttpConfigCondition; import io.metersphere.api.dto.scenario.HttpConfigCondition;
import io.metersphere.api.dto.scenario.KeyValue; 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.scenario.environment.EnvironmentConfig;
import io.metersphere.api.dto.ssl.KeyStoreConfig; import io.metersphere.api.dto.ssl.KeyStoreConfig;
import io.metersphere.api.dto.ssl.KeyStoreFile; import io.metersphere.api.dto.ssl.KeyStoreFile;
@ -191,8 +192,6 @@ public class MsHTTPSamplerProxy extends MsTestElement {
sampler.setMethod(this.getMethod()); sampler.setMethod(this.getMethod());
sampler.setContentEncoding("UTF-8"); 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.setFollowRedirects(this.isFollowRedirects());
sampler.setUseKeepAlive(true); sampler.setUseKeepAlive(true);
sampler.setDoMultipart(this.isDoMultipartPost()); sampler.setDoMultipart(this.isDoMultipartPost());
@ -204,8 +203,13 @@ public class MsHTTPSamplerProxy extends MsTestElement {
compatible(config); 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); HttpConfig httpConfig = getHttpConfig(config);
setSamplerPath(config, httpConfig, sampler); 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) { private EnvironmentConfig getEnvironmentConfig(ParameterConfig config) {
return config.getConfig().get(this.getProjectId()); return config.getConfig().get(this.getProjectId());
} }

View File

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

View File

@ -18,23 +18,25 @@ public class TCPPool {
public static String createTcp(int port){ public static String createTcp(int port){
String returnString = ""; String returnString = "";
TCPServer tcpServer = null; if(port > 0){
if(serverSockedMap.containsKey(port)){ TCPServer tcpServer = null;
tcpServer = serverSockedMap.get(port); if(serverSockedMap.containsKey(port)){
}else { tcpServer = serverSockedMap.get(port);
tcpServer = new TCPServer(port); }else {
serverSockedMap.put(port,tcpServer); tcpServer = new TCPServer(port);
} serverSockedMap.put(port,tcpServer);
try { }
if(!tcpServer.isSocketOpen()){ try {
Thread t = new Thread(tcpServer); if(!tcpServer.isSocketOpen()){
t.start(); Thread t = new Thread(tcpServer);
t.start();
}
returnString = "OK";
}catch (Exception e){
returnString = e.getMessage();
e.printStackTrace();
MSException.throwException(e.getMessage());
} }
returnString = "OK";
}catch (Exception e){
returnString = e.getMessage();
e.printStackTrace();
MSException.throwException(e.getMessage());
} }
return returnString; return returnString;

View File

@ -35,6 +35,7 @@ import io.metersphere.track.service.TestPlanProjectService;
import io.metersphere.track.service.TestPlanService; import io.metersphere.track.service.TestPlanService;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -89,6 +90,9 @@ public class ProjectService {
@Resource @Resource
private ScheduleService scheduleService; private ScheduleService scheduleService;
@Value("${tcp.mock.port}")
private String tcpMockPorts;
public Project addProject(Project project) { public Project addProject(Project project) {
if (StringUtils.isBlank(project.getName())) { if (StringUtils.isBlank(project.getName())) {
MSException.throwException(Translator.get("project_name_is_null")); MSException.throwException(Translator.get("project_name_is_null"));
@ -273,6 +277,10 @@ public class ProjectService {
lastTcpNum = oldData.getMockTcpPort().intValue(); lastTcpNum = oldData.getMockTcpPort().intValue();
} }
if(project.getMockTcpPort().intValue() > 0){
this.checkMockTcpPort(project.getMockTcpPort().intValue());
}
this.checkProjectTcpPort(project); this.checkProjectTcpPort(project);
project.setCreateTime(null); 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) { private void checkProjectTcpPort(Project project) {
//判断端口是否重复 //判断端口是否重复
if (project.getMockTcpPort() != null && project.getMockTcpPort().intValue() != 0) { if (project.getMockTcpPort() != null && project.getMockTcpPort().intValue() != 0) {

View File

@ -1,65 +1,89 @@
<template> <template>
<div> <div>
<el-form :model="commonConfig" :rules="rules" ref="commonConfig" :disabled="isReadOnly"> <el-form :model="commonConfig" :rules="rules" ref="commonConfig" :disabled="isReadOnly">
<span>{{$t('api_test.environment.globalVariable')}}</span> <span>{{$t('api_test.environment.globalVariable')}}</span>
<ms-api-scenario-variables :show-copy="false" :items="commonConfig.variables"/> <ms-api-scenario-variables :show-copy="false" :items="commonConfig.variables"/>
<el-form-item> <el-form-item>
<el-switch v-model="commonConfig.enableHost" active-text="Hosts"/> <el-switch v-model="commonConfig.enableHost" active-text="Hosts"/>
</el-form-item> </el-form-item>
<ms-api-host-table v-if="commonConfig.enableHost" :hostTable="commonConfig.hosts" ref="refHostTable"/> <el-form-item>
</el-form> <span>{{$t('api_test.environment.request_timeout')}}:</span>
</div> <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>
</template> </template>
<script> <script>
import {CommonConfig, Environment} from "../../model/EnvironmentModel"; import {CommonConfig, Environment} from "../../model/EnvironmentModel";
import MsApiScenarioVariables from "../ApiScenarioVariables"; import MsApiScenarioVariables from "../ApiScenarioVariables";
import MsApiHostTable from "../ApiHostTable"; import MsApiHostTable from "../ApiHostTable";
export default { export default {
name: "MsEnvironmentCommonConfig", name: "MsEnvironmentCommonConfig",
components: {MsApiHostTable, MsApiScenarioVariables}, components: {MsApiHostTable, MsApiScenarioVariables},
props: { props: {
commonConfig: new CommonConfig(), commonConfig: new CommonConfig(),
isReadOnly: { isReadOnly: {
type: Boolean, type: Boolean,
default: false default: false
}, },
}, },
data() { data() {
return { return {
rules: { rules: {
},
}
}, },
methods: { }
validate() { },
let isValidate = false; created() {
this.$refs['commonConfig'].validate((valid) => { if(!this.commonConfig.requestTimeout){
if (valid) { this.commonConfig.requestTimeout = 60000;
// host }
let valHost = true; if(!this.commonConfig.responseTimeout){
if (this.commonConfig.enableHost) { this.commonConfig.responseTimeout = 60000;
for (let i = 0; i < this.commonConfig.hosts.length; i++) { }
valHost = this.$refs['refHostTable'].confirm(this.commonConfig.hosts[i]); },
} watch: {
} commonConfig(){
if (valHost) { if(!this.commonConfig.requestTimeout){
isValidate = true; this.commonConfig.requestTimeout = 60000;
} else { }
isValidate = false; if(!this.commonConfig.responseTimeout){
} this.commonConfig.responseTimeout = 60000;
} else {
isValidate = false;
}
});
return isValidate;
}
} }
} }
},
methods: {
validate() {
let isValidate = false;
this.$refs['commonConfig'].validate((valid) => {
if (valid) {
// host
let valHost = true;
if (this.commonConfig.enableHost) {
for (let i = 0; i < this.commonConfig.hosts.length; i++) {
valHost = this.$refs['refHostTable'].confirm(this.commonConfig.hosts[i]);
}
}
if (valHost) {
isValidate = true;
} else {
isValidate = false;
}
} else {
isValidate = false;
}
});
return isValidate;
}
}
}
</script> </script>
<style scoped> <style scoped>