refactor(接口测试): CSV隔离加载的同时兼容CSVRead函数取值 #26821

Signed-off-by: fit2-zhao <yong.zhao@fit2cloud.com>
This commit is contained in:
fit2-zhao 2023-09-20 13:45:23 +08:00
parent 1770b7cc2d
commit 8b4c8f56c5
1 changed files with 34 additions and 21 deletions

View File

@ -236,6 +236,10 @@ public class FileServer {
reserveFile(filename, charsetName, alias, false);
}
private boolean hasThreadContext() {
return JMeterContextService.getContext() != null && JMeterContextService.getContext().getThread() != null;
}
/**
* Creates an association between a filename and a File inputOutputObject,
* and stores it for later use - unless it is already stored.
@ -259,11 +263,12 @@ public class FileServer {
log.error("file does not exist [ " + filename + " ]");
return "";
}
if (hasThreadContext()) {
String threadName = JMeterContextService.getContext().getThread().getThreadName();
if (!StringUtils.contains(alias, threadName)) {
alias = StringUtils.join(threadName, alias);
}
}
FileEntry fileEntry = files.get(alias);
if (fileEntry == null) {
fileEntry = new FileEntry(resolveFileFromPath(filename), null, charsetName);
@ -342,11 +347,12 @@ public class FileServer {
*/
public synchronized String readLine(String filename, boolean recycle,
boolean ignoreFirstLine) throws IOException {
if (hasThreadContext()) {
String threadName = JMeterContextService.getContext().getThread().getThreadName();
if (!StringUtils.contains(filename, threadName)) {
filename = StringUtils.join(threadName, filename);
}
}
FileEntry fileEntry = files.get(filename);
if (fileEntry != null) {
if (fileEntry.inputOutputObject == null) {
@ -398,10 +404,12 @@ public class FileServer {
* @return {@link BufferedReader}
*/
private BufferedReader getReader(String alias, boolean recycle, boolean ignoreFirstLine) throws IOException {
if (hasThreadContext()) {
String threadName = JMeterContextService.getContext().getThread().getThreadName();
if (!StringUtils.contains(alias, threadName)) {
alias = StringUtils.join(threadName, alias);
}
}
FileEntry fileEntry = files.get(alias);
if (fileEntry != null) {
@ -459,10 +467,12 @@ public class FileServer {
}
public synchronized void write(String filename, String value) throws IOException {
if (hasThreadContext()) {
String threadName = JMeterContextService.getContext().getThread().getThreadName();
if (!StringUtils.contains(filename, threadName)) {
filename = StringUtils.join(threadName, filename);
}
}
FileEntry fileEntry = files.get(filename);
if (fileEntry != null) {
if (fileEntry.inputOutputObject == null) {
@ -519,11 +529,12 @@ public class FileServer {
* @throws IOException when closing of the aliased file fails
*/
public synchronized void closeFile(String name) throws IOException {
if (hasThreadContext()) {
String threadName = JMeterContextService.getContext().getThread().getThreadName();
if (!StringUtils.contains(name, threadName)) {
name = StringUtils.join(threadName, name);
}
}
FileEntry fileEntry = files.get(name);
closeFile(name, fileEntry);
}
@ -580,10 +591,12 @@ public class FileServer {
// If path is absolute, then File constructor will simply return it
String alias = path;
if (hasThreadContext()) {
String threadName = JMeterContextService.getContext().getThread().getThreadName();
if (!StringUtils.contains(alias, threadName)) {
alias = StringUtils.join(threadName, path);
}
}
FileEntry fileEntry = files.containsKey(alias) ? files.get(alias) : files.get(path);
return fileEntry != null ? fileEntry.file : null;
}