refactor(测试跟踪): 优化测试计划失败重试验证规则

Signed-off-by: fit2-zhao <yong.zhao@fit2cloud.com>
This commit is contained in:
fit2-zhao 2023-07-18 17:31:22 +08:00 committed by fit2-zhao
parent fb81d7ec42
commit f1745bf26d
2 changed files with 50 additions and 25 deletions

View File

@ -20,9 +20,28 @@ import java.util.UUID;
public class MsRetryLoopController extends MsTestElement {
private String type = "RetryLoopController";
private String clazzName = MsRetryLoopController.class.getCanonicalName();
private final String template = """
try {
if (prev.isSuccess()) {
vars.put("%s", "STOPPED");
}
if (vars.get("%s") == null) {
vars.put("%s", "0");
} else {
int retryNum = Integer.parseInt(vars.get("%s"));
log.info("重试:" + (retryNum + 1));
prev.setSampleLabel("MsRetry_" + (retryNum + 1) + "_" + prev.getSampleLabel());
retryNum++;
vars.put("%s", String.valueOf(retryNum));
}
if (vars.get("%s").equals("%s")) {
vars.put("%s", "STOPPED");
}
} catch (Exception e) {
vars.put("%s", "STOPPED");
}""";
private long retryNum;
private String ms_current_timer = UUID.randomUUID().toString();
@Override
@ -52,36 +71,30 @@ public class MsRetryLoopController extends MsTestElement {
}
private String script() {
String script = "import org.apache.commons.lang3.StringUtils;" +
"\n// 失败重试控制" +
"\n" + "try{" +
"\n" + "\tint errorCount = prev.getErrorCount();" +
"\n" + "\tif(errorCount == 0 && StringUtils.isBlank(prev.getFirstAssertionFailureMessage()) ){" +
"\n" + "\t vars.put(\"" + ms_current_timer + "\", \"stop\");\n" + "\t}" +
"\n" + "\tif(vars.get(\"" + ms_current_timer + "_num\") == null){" +
"\n" + "\t\tvars.put(\"" + ms_current_timer + "_num\", \"0\");" +
"\n" + "\t}else{" +
"\n" + "\t\tint retryNum= Integer.parseInt(vars.get(\"" + ms_current_timer + "_num\"));" +
"\n" + "\t\tlog.info(\"重试:\"+ (retryNum + 1));" +
"\n" + " \tprev.setSampleLabel(\"MsRetry_\"+ (retryNum + 1) + \"_\" + prev.getSampleLabel());" +
"\n" + "\t\tretryNum =retryNum +1;\n" + "\t\tvars.put(\"" + ms_current_timer + "_num\",retryNum + \"\");\n" + "\t}" +
"\n" + "\tif(vars.get(\"" + ms_current_timer + "_num\").equals( \"" + retryNum + "\")){" +
"\n" + "\t\tvars.put(\"" + ms_current_timer + "\", \"stop\");\n" + "\t}" +
"\n" + "}catch (Exception e){" +
"\n" + "\tvars.put(\"" + ms_current_timer + "\", \"stop\");\n" + "}" +
"\n";
return script;
String stopStatus = ms_current_timer;
String retryNum = StringUtils.join("VARS_", stopStatus);
return String.format(template,
stopStatus,
retryNum,
retryNum,
retryNum,
retryNum,
retryNum,
this.retryNum,
stopStatus,
stopStatus
);
}
public HashTree controller(HashTree tree, String name) {
String whileCondition = "${__jexl3(" + "\"${" + ms_current_timer + "}\" !=\"stop\")}";
String whileCondition = "${__jexl3(" + "\"${" + ms_current_timer + "}\" !=\"STOPPED\")}";
HashTree hashTree = tree.add(initWhileController(whileCondition, name));
// 添加超时处理防止死循环
JSR223Listener postProcessor = new JSR223Listener();
postProcessor.setName("Retry-controller");
postProcessor.setProperty(TestElement.TEST_CLASS, JSR223Listener.class.getName());
postProcessor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
postProcessor.setProperty("scriptLanguage", "beanshell");
postProcessor.setProperty("scriptLanguage", "groovy");
postProcessor.setProperty("script", script());
hashTree.add(postProcessor);
return hashTree;

View File

@ -33,11 +33,10 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
// For unit tests, @see TestSampleResult
@ -1279,6 +1278,19 @@ public class SampleResult implements Serializable, Cloneable, Searchable {
return success ? 0 : 1;
}
public boolean isAssertionSuccessful() {
AssertionResult[] results = getAssertionResults();
boolean allPass = Optional.ofNullable(results)
.map(Stream::of)
.orElse(Stream.empty())
.allMatch(result -> (!result.isFailure() && !result.isError()));
return allPass;
}
public boolean isSuccess() {
return isAssertionSuccessful() && isSuccessful();
}
public void setErrorCount(int i) {// for reading from CSV files
// ignored currently
}