fix(接口测试): 修复嵌套循环超时时间错误问题

--bug=1013782 --user=赵勇 【接口测试】github#14006,场景中多层嵌套的循环,内层循环执行完后,超时时间没清0 https://www.tapd.cn/55049933/s/1172842
This commit is contained in:
fit2-zhao 2022-06-02 16:19:03 +08:00 committed by f2c-ci-robot[bot]
parent b88af68e1a
commit 7d4859fe5f
3 changed files with 28 additions and 5 deletions

View File

@ -233,6 +233,17 @@ public class MsLoopController extends MsTestElement {
return script;
}
private void addPreProc(HashTree hashTree) {
JSR223Sampler sampler = new JSR223Sampler();
sampler.setName("MS_CLEAR_LOOPS_VAR_" + ms_current_timer);
sampler.setProperty(TestElement.TEST_CLASS, JSR223Sampler.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
sampler.setProperty("scriptLanguage", "beanshell");
ScriptFilter.verify("beanshell", this.getName(), script());
sampler.setProperty("script", "vars.put(\"" + ms_current_timer + "\", null);");
hashTree.add(sampler);
}
private HashTree controller(HashTree tree) {
if (StringUtils.equals(this.loopType, LoopConstants.WHILE.name()) && this.whileController != null) {
RunTime runTime = new RunTime();
@ -249,6 +260,8 @@ public class MsLoopController extends MsTestElement {
String ifCondition = "${__jexl3(" + condition + ")}";
String whileCondition = "${__jexl3(" + condition + " && \"${" + ms_current_timer + "}\" !=\"stop\")}";
HashTree ifHashTree = tree.add(ifController(ifCondition));
addPreProc(ifHashTree);
HashTree hashTree = ifHashTree.add(initWhileController(whileCondition));
// 添加超时处理防止死循环
JSR223PreProcessor jsr223PreProcessor = new JSR223PreProcessor();

View File

@ -39,6 +39,7 @@ public class APISingleResultListener implements MsExecListener {
// dto.setConsole(FixedCapacityUtils.getJmeterLogger(dto.getReportId()));
// JMeterBase.resultFormatting(results, dto);
// CommonBeanFactory.getBean(TestResultService.class).saveResults(dto);
this.clearLoops(results);
queues.addAll(results);
}
@ -112,4 +113,13 @@ public class APISingleResultListener implements MsExecListener {
results.addAll(list);
}
}
private void clearLoops(List<SampleResult> results) {
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(results)) {
results = results.stream().filter(sampleResult ->
StringUtils.isNotEmpty(sampleResult.getSampleLabel())
&& !sampleResult.getSampleLabel().startsWith("MS_CLEAR_LOOPS_VAR_"))
.collect(Collectors.toList());
}
}
}

View File

@ -83,10 +83,10 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
return getPropertyAsBoolean(SUCCESS_ONLY_LOGGING, false);
}
public boolean isSampleWanted(boolean success) {
public boolean isSampleWanted(boolean success, SampleResult result) {
boolean errorOnly = isErrorLogging();
boolean successOnly = isSuccessOnlyLogging();
return isSampleWanted(success, errorOnly, successOnly);
return isSampleWanted(success, errorOnly, successOnly) && !StringUtils.containsIgnoreCase(result.getSampleLabel(), "MS_CLEAR_LOOPS_VAR_");
}
public static boolean isSampleWanted(boolean success, boolean errorOnly,
@ -135,7 +135,7 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
LoggerUtil.debug("send. " + this.getName());
WebSocketUtils.sendMessageSingle(dto);
} catch (Exception ex) {
LoggerUtil.error("消息推送失败:" , ex);
LoggerUtil.error("消息推送失败:", ex);
}
}
@ -147,7 +147,7 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
public void sampleOccurred(SampleEvent event) {
SampleResult result = event.getResult();
this.setVars(result);
if (isSampleWanted(result.isSuccessful()) && !StringUtils.equals(result.getSampleLabel(), RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME)) {
if (isSampleWanted(result.isSuccessful(), result) && !StringUtils.equals(result.getSampleLabel(), RunningParamKeys.RUNNING_DEBUG_SAMPLER_NAME)) {
RequestResult requestResult = JMeterBase.getRequestResult(result);
if (requestResult != null && ResultParseUtil.isNotAutoGenerateSampler(requestResult)) {
MsgDto dto = new MsgDto();
@ -155,7 +155,7 @@ public class MsDebugListener extends AbstractListenerElement implements SampleLi
dto.setReportId("send." + this.getName());
dto.setToReport(this.getName());
String console = FixedCapacityUtils.getJmeterLogger(this.getName(),false);
String console = FixedCapacityUtils.getJmeterLogger(this.getName(), false);
if (StringUtils.isNotEmpty(requestResult.getName()) && requestResult.getName().startsWith("Transaction=")) {
requestResult.getSubRequestResults().forEach(transactionResult -> {
transactionResult.getResponseResult().setConsole(console);