fix(接口测试): 修复单条执行用例偶发获取不到结果问题

--bug=1009232 --user=赵勇 【接口测试】执行测试用例还是一直执行中【+github#8807】 https://www.tapd.cn/55049933/s/1089188
This commit is contained in:
fit2-zhao 2021-12-31 15:55:34 +08:00 committed by fit2-zhao
parent e4633d707a
commit 7dbb81f9a7
4 changed files with 47 additions and 42 deletions

View File

@ -285,12 +285,12 @@ public class MsHTTPSamplerProxy extends MsTestElement {
HashTreeUtil hashTreeUtil = new HashTreeUtil(); HashTreeUtil hashTreeUtil = new HashTreeUtil();
//增加误报断言 //增加误报断言
if (httpConfig != null) { if (httpConfig != null) {
if(CollectionUtils.isNotEmpty(httpConfig.getErrorReportAssertions())){ if (CollectionUtils.isNotEmpty(httpConfig.getErrorReportAssertions())) {
for (MsAssertions assertion : httpConfig.getErrorReportAssertions()) { for (MsAssertions assertion : httpConfig.getErrorReportAssertions()) {
assertion.toHashTree(httpSamplerTree, assertion.getHashTree(), config); assertion.toHashTree(httpSamplerTree, assertion.getHashTree(), config);
} }
} }
if(CollectionUtils.isNotEmpty(httpConfig.getAssertions())){ if (CollectionUtils.isNotEmpty(httpConfig.getAssertions())) {
for (MsAssertions assertion : httpConfig.getAssertions()) { for (MsAssertions assertion : httpConfig.getAssertions()) {
assertion.toHashTree(httpSamplerTree, assertion.getHashTree(), config); assertion.toHashTree(httpSamplerTree, assertion.getHashTree(), config);
} }
@ -407,7 +407,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
httpConfig.setPostProcessor(environmentConfig.getPostProcessor()); httpConfig.setPostProcessor(environmentConfig.getPostProcessor());
httpConfig.setGlobalScriptConfig(environmentConfig.getGlobalScriptConfig()); httpConfig.setGlobalScriptConfig(environmentConfig.getGlobalScriptConfig());
httpConfig.setAssertions(environmentConfig.getAssertions()); httpConfig.setAssertions(environmentConfig.getAssertions());
if(environmentConfig.isUseErrorCode()){ if (environmentConfig.isUseErrorCode()) {
httpConfig.setErrorReportAssertions(HashTreeUtil.getErrorReportByProjectId(this.getProjectId())); httpConfig.setErrorReportAssertions(HashTreeUtil.getErrorReportByProjectId(this.getProjectId()));
} }
return httpConfig; return httpConfig;
@ -749,22 +749,22 @@ public class MsHTTPSamplerProxy extends MsTestElement {
list.stream(). list.stream().
filter(KeyValue::isValid). filter(KeyValue::isValid).
filter(KeyValue::isEnable).forEach(keyValue -> { filter(KeyValue::isEnable).forEach(keyValue -> {
try { try {
String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue(); String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue();
HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value); HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value);
if (keyValue.getValue() == null) { if (keyValue.getValue() == null) {
httpArgument.setValue(""); httpArgument.setValue("");
}
httpArgument.setAlwaysEncoded(keyValue.isUrlEncode());
if (StringUtils.isNotBlank(keyValue.getContentType())) {
httpArgument.setContentType(keyValue.getContentType());
}
arguments.addArgument(httpArgument);
} catch (Exception e) {
}
} }
); httpArgument.setAlwaysEncoded(keyValue.isUrlEncode());
if (StringUtils.isNotBlank(keyValue.getContentType())) {
httpArgument.setContentType(keyValue.getContentType());
}
arguments.addArgument(httpArgument);
} catch (Exception e) {
}
}
);
return arguments; return arguments;
} }
@ -836,21 +836,19 @@ public class MsHTTPSamplerProxy extends MsTestElement {
} }
} else { } else {
apiDefinition = apiDefinitionService.get(this.getId()); apiDefinition = apiDefinitionService.get(this.getId());
if (apiDefinition == null) { ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseService.get(this.getId());
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseService.get(this.getId()); if (apiTestCaseWithBLOBs == null) {
if (apiTestCaseWithBLOBs == null) { apiTestCaseWithBLOBs = apiTestCaseService.get(this.getName());
apiTestCaseWithBLOBs = apiTestCaseService.get(this.getName()); }
} if (apiTestCaseWithBLOBs != null) {
if (apiTestCaseWithBLOBs != null) { apiDefinition = apiDefinitionService.get(apiTestCaseWithBLOBs.getApiDefinitionId());
apiDefinition = apiDefinitionService.get(apiTestCaseWithBLOBs.getApiDefinitionId()); } else {
} else { TestPlanApiCaseService testPlanApiCaseService = CommonBeanFactory.getBean(TestPlanApiCaseService.class);
TestPlanApiCaseService testPlanApiCaseService = CommonBeanFactory.getBean(TestPlanApiCaseService.class); TestPlanApiCase testPlanApiCase = testPlanApiCaseService.getById(this.getId());
TestPlanApiCase testPlanApiCase = testPlanApiCaseService.getById(this.getId()); if (testPlanApiCase != null) {
if (testPlanApiCase != null) { ApiTestCaseWithBLOBs caseWithBLOBs = apiTestCaseService.get(testPlanApiCase.getApiCaseId());
ApiTestCaseWithBLOBs caseWithBLOBs = apiTestCaseService.get(testPlanApiCase.getApiCaseId()); if (caseWithBLOBs != null) {
if (caseWithBLOBs != null) { apiDefinition = apiDefinitionService.get(caseWithBLOBs.getApiDefinitionId());
apiDefinition = apiDefinitionService.get(caseWithBLOBs.getApiDefinitionId());
}
} }
} }
} }

View File

@ -38,8 +38,11 @@ public class APISingleResultListener extends MsExecListener {
dto.setConsole(CommonBeanFactory.getBean(MsResultService.class).getJmeterLogger(dto.getReportId())); dto.setConsole(CommonBeanFactory.getBean(MsResultService.class).getJmeterLogger(dto.getReportId()));
// 整体执行结束更新资源状态 // 整体执行结束更新资源状态
CommonBeanFactory.getBean(TestResultService.class).testEnded(dto); CommonBeanFactory.getBean(TestResultService.class).testEnded(dto);
LoggerUtil.info("执行队列处理:" + dto.getQueueId()); LoggerUtil.info("执行队列处理:" + dto.getQueueId());
CommonBeanFactory.getBean(ApiExecutionQueueService.class).queueNext(dto); if (StringUtils.isNotEmpty(dto.getQueueId())) {
CommonBeanFactory.getBean(ApiExecutionQueueService.class).queueNext(dto);
}
} catch (Exception e) { } catch (Exception e) {
LoggerUtil.error(e); LoggerUtil.error(e);
} }

View File

@ -3,7 +3,6 @@ package io.metersphere.api.service;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.cache.TestPlanReportExecuteCatch; import io.metersphere.api.cache.TestPlanReportExecuteCatch;
import io.metersphere.api.dto.ErrorReportLibraryParseDTO;
import io.metersphere.api.dto.datacount.ExecutedCaseInfoResult; import io.metersphere.api.dto.datacount.ExecutedCaseInfoResult;
import io.metersphere.base.domain.*; import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ApiDefinitionExecResultMapper;
@ -13,7 +12,6 @@ import io.metersphere.base.mapper.TestCaseReviewApiCaseMapper;
import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper;
import io.metersphere.commons.constants.*; import io.metersphere.commons.constants.*;
import io.metersphere.commons.utils.DateUtils; import io.metersphere.commons.utils.DateUtils;
import io.metersphere.commons.utils.ErrorReportLibraryUtil;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.SessionUtils; import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.dto.RequestResult; import io.metersphere.dto.RequestResult;
@ -66,6 +64,7 @@ public class ApiDefinitionExecResultService {
private TestPlanTestCaseService testPlanTestCaseService; private TestPlanTestCaseService testPlanTestCaseService;
@Resource @Resource
private ApiTestCaseService apiTestCaseService; private ApiTestCaseService apiTestCaseService;
public void saveApiResult(List<RequestResult> requestResults, ResultDTO dto) { public void saveApiResult(List<RequestResult> requestResults, ResultDTO dto) {
boolean isFirst = true; boolean isFirst = true;
int count = requestResults.stream() int count = requestResults.stream()
@ -322,7 +321,6 @@ public class ApiDefinitionExecResultService {
private ApiDefinitionExecResult save(RequestResult item, String reportId, String console, int expectProcessResultCount, String type, String testId, boolean isFirst) { private ApiDefinitionExecResult save(RequestResult item, String reportId, String console, int expectProcessResultCount, String type, String testId, boolean isFirst) {
if (!StringUtils.startsWithAny(item.getName(), "PRE_PROCESSOR_ENV_", "POST_PROCESSOR_ENV_")) { if (!StringUtils.startsWithAny(item.getName(), "PRE_PROCESSOR_ENV_", "POST_PROCESSOR_ENV_")) {
ErrorReportLibraryParseDTO errorCodeDTO = ErrorReportLibraryUtil.parseAssertions(item);
ApiDefinitionExecResult saveResult = apiDefinitionExecResultMapper.selectByPrimaryKey(reportId); ApiDefinitionExecResult saveResult = apiDefinitionExecResultMapper.selectByPrimaryKey(reportId);
if (saveResult == null) { if (saveResult == null) {
saveResult = new ApiDefinitionExecResult(); saveResult = new ApiDefinitionExecResult();
@ -334,10 +332,6 @@ public class ApiDefinitionExecResultService {
saveResult.setType(type); saveResult.setType(type);
saveResult.setCreateTime(item.getStartTime()); saveResult.setCreateTime(item.getStartTime());
String status = item.isSuccess() ? ExecuteResult.success.name() : ExecuteResult.error.name(); String status = item.isSuccess() ? ExecuteResult.success.name() : ExecuteResult.error.name();
if(errorCodeDTO.getErrorCodeList() != null){
status = ExecuteResult.errorCode.name();
saveResult.setErrorCode(errorCodeDTO.getErrorCodeStr());
}
saveResult.setName(editStatus(type, status, saveResult.getCreateTime(), saveResult.getId(), testId)); saveResult.setName(editStatus(type, status, saveResult.getCreateTime(), saveResult.getId(), testId));
saveResult.setStatus(status); saveResult.setStatus(status);
saveResult.setResourceId(item.getName()); saveResult.setResourceId(item.getName());

View File

@ -1,8 +1,12 @@
package io.metersphere.websocket; package io.metersphere.websocket;
import com.alibaba.fastjson.JSON;
import io.metersphere.api.dto.APIReportResult; import io.metersphere.api.dto.APIReportResult;
import io.metersphere.api.jmeter.TestResult;
import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.api.service.ApiDefinitionService;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -87,6 +91,7 @@ public class ApiReportWebSocket {
this.runMode = runMode; this.runMode = runMode;
} }
@SneakyThrows
@Override @Override
public void run() { public void run() {
try { try {
@ -95,10 +100,15 @@ public class ApiReportWebSocket {
return; return;
} }
if (report != null) { if (report != null) {
session.getBasicRemote().sendText(report.getContent()); if (StringUtils.isNotEmpty(report.getContent())) {
session.getBasicRemote().sendText(report.getContent());
} else {
session.getBasicRemote().sendText(JSON.toJSONString(new TestResult()));
}
session.close(); session.close();
} }
} catch (Exception e) { } catch (Exception e) {
session.close();
LogUtil.error(e.getMessage(), e); LogUtil.error(e.getMessage(), e);
} }
} }