fix(接口测试): 保持线程变量的生命周期一致性

This commit is contained in:
fit2-zhao 2020-09-28 15:41:38 +08:00
parent ce7eb2391e
commit bc94f1cedb
2 changed files with 12 additions and 5 deletions

View File

@ -39,7 +39,6 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
public String runMode = ApiRunMode.RUN.name();
private JMeterVars variables;
// 测试ID
private String testId;
@ -155,9 +154,9 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
responseResult.setResponseTime(result.getTime());
responseResult.setResponseMessage(result.getResponseMessage());
if (variables.get(result.hashCode()) != null) {
if (JMeterVars.get(result.hashCode()) != null) {
List<String> vars = new LinkedList<>();
variables.get(result.hashCode()).entrySet().parallelStream().reduce(vars, (first, second) -> {
JMeterVars.get(result.hashCode()).entrySet().parallelStream().reduce(vars, (first, second) -> {
first.add(second.getKey() + "" + second.getValue());
return first;
}, (first, second) -> {
@ -168,6 +167,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
return first;
});
responseResult.setVars(StringUtils.join(vars, "\n"));
JMeterVars.remove(result.hashCode());
}
for (AssertionResult assertionResult : result.getAssertionResults()) {
ResponseAssertionResult responseAssertionResult = getResponseAssertionResult(assertionResult);
@ -199,7 +199,6 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
if (StringUtils.isBlank(this.runMode)) {
this.runMode = ApiRunMode.RUN.name();
}
variables = new JMeterVars();
}
private ResponseAssertionResult getResponseAssertionResult(AssertionResult assertionResult) {

View File

@ -13,6 +13,9 @@ import java.util.*;
public class JMeterVars {
private JMeterVars() { }
// 数据和线程变量保持一致
private static Map<Integer, JMeterVariables> variables = new HashMap<>();
// 线程执行过程调用提取变量值
@ -59,7 +62,12 @@ public class JMeterVars {
}
}
public JMeterVariables get(Integer key) {
public static JMeterVariables get(Integer key) {
return variables.get(key);
}
public static void remove(Integer key) {
variables.remove(key);
}
}