fix(接口测试): 失败重试,间隔时间不生效

--bug=1044137 --user=陈建星 【测试计划】测试规划中开启失败重试-设置的重试间隔时间不生效 https://www.tapd.cn/55049933/s/1550172
This commit is contained in:
AgAngle 2024-07-18 10:10:35 +08:00 committed by 刘瑞斌
parent 0e06314dbb
commit 8db5c9f411
2 changed files with 10 additions and 6 deletions

View File

@ -18,5 +18,5 @@ public class ApiRunRetryConfig implements Serializable {
/** /**
* 失败重试间隔(单位: ms) * 失败重试间隔(单位: ms)
*/ */
private Integer retryInterval; private Integer retryInterval = 0;
} }

View File

@ -8,6 +8,7 @@ import io.metersphere.plugin.api.spi.AbstractMsProtocolTestElement;
import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.plugin.api.spi.JmeterElementConvertInterceptor; import io.metersphere.plugin.api.spi.JmeterElementConvertInterceptor;
import io.metersphere.plugin.api.spi.MsTestElement; import io.metersphere.plugin.api.spi.MsTestElement;
import io.metersphere.sdk.dto.api.task.ApiRunRetryConfig;
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.apache.jmeter.control.WhileController; import org.apache.jmeter.control.WhileController;
@ -29,6 +30,7 @@ public class RetryInterceptor implements JmeterElementConvertInterceptor {
try { try {
String retryValueName = "VARS_" + retryId; String retryValueName = "VARS_" + retryId;
String retryTimes = "%s"; String retryTimes = "%s";
String retryInterval = "%s";
if (prev.isSuccess()) { if (prev.isSuccess()) {
vars.put(retryId, "STOPPED"); vars.put(retryId, "STOPPED");
} }
@ -36,6 +38,7 @@ public class RetryInterceptor implements JmeterElementConvertInterceptor {
vars.put(retryValueName, "0"); vars.put(retryValueName, "0");
} else { } else {
int retryNum = Integer.parseInt(vars.get(retryValueName)); int retryNum = Integer.parseInt(vars.get(retryValueName));
sleep(Integer.parseInt(retryInterval));
retryNum++; retryNum++;
log.info("重试:" + retryNum); log.info("重试:" + retryNum);
prev.setSampleLabel("MsRetry_" + retryNum + "_" + prev.getSampleLabel()); prev.setSampleLabel("MsRetry_" + retryNum + "_" + prev.getSampleLabel());
@ -55,12 +58,12 @@ public class RetryInterceptor implements JmeterElementConvertInterceptor {
AbstractMsTestElement abstractMsTestElement = (AbstractMsTestElement) element; AbstractMsTestElement abstractMsTestElement = (AbstractMsTestElement) element;
ApiParamConfig apiParamConfig = (ApiParamConfig) config; ApiParamConfig apiParamConfig = (ApiParamConfig) config;
if (isRetryEnable(apiParamConfig) && isRetryElement(element) && !isInLoop(abstractMsTestElement)) { if (isRetryEnable(apiParamConfig) && isRetryElement(element) && !isInLoop(abstractMsTestElement)) {
return addRetryWhileController(tree, abstractMsTestElement.getName(), apiParamConfig.getRetryConfig().getRetryTimes()); return addRetryWhileController(tree, abstractMsTestElement.getName(), apiParamConfig.getRetryConfig());
} }
return tree; return tree;
} }
public HashTree addRetryWhileController(HashTree tree, String name, int retryTimes) { public HashTree addRetryWhileController(HashTree tree, String name, ApiRunRetryConfig retryConfig) {
String retryId = UUID.randomUUID().toString(); String retryId = UUID.randomUUID().toString();
String whileCondition = String.format(""" String whileCondition = String.format("""
${__jexl3("${%s}" != "STOPPED")} ${__jexl3("${%s}" != "STOPPED")}
@ -72,7 +75,7 @@ public class RetryInterceptor implements JmeterElementConvertInterceptor {
postProcessor.setProperty(TestElement.TEST_CLASS, JSR223Listener.class.getName()); postProcessor.setProperty(TestElement.TEST_CLASS, JSR223Listener.class.getName());
postProcessor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(JmeterAlias.TEST_BEAN_GUI)); postProcessor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(JmeterAlias.TEST_BEAN_GUI));
postProcessor.setProperty("scriptLanguage", "groovy"); postProcessor.setProperty("scriptLanguage", "groovy");
postProcessor.setProperty("script", getRetryScript(retryId, retryTimes)); postProcessor.setProperty("script", getRetryScript(retryId, retryConfig));
hashTree.add(postProcessor); hashTree.add(postProcessor);
return hashTree; return hashTree;
} }
@ -90,10 +93,11 @@ public class RetryInterceptor implements JmeterElementConvertInterceptor {
return controller; return controller;
} }
private String getRetryScript(String retryId, int retryTimes) { private String getRetryScript(String retryId, ApiRunRetryConfig retryConfig) {
return String.format(template, return String.format(template,
retryId, retryId,
retryTimes retryConfig.getRetryTimes(),
retryConfig.getRetryInterval()
); );
} }