Skip to content

Commit

Permalink
Socket object close after each response chain
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Apr 2, 2024
1 parent 59e0e0d commit 5d287da
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## [Unreleased]
### Removed
- Constructor with `Socket` argument in `SocketUtils.ServerCallable` class, by @HardNorth
### Added
- `Socket` object close after each response chain, by @HardNorth

## [0.0.5]
### Added
Expand Down
67 changes: 29 additions & 38 deletions src/main/java/com/epam/reportportal/util/test/SocketUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.commons.lang3.tuple.Pair;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Expand All @@ -46,68 +45,60 @@ public static final class ServerCallable implements Callable<List<String>> {
private final ServerSocket ss;
private final Map<String, Object> model;
private final List<String> responseFiles;
private Socket s;

public ServerCallable(@Nonnull ServerSocket serverSocket, @Nullable Socket socket, @Nonnull Map<String, Object> replacementValues,
public ServerCallable(@Nonnull ServerSocket serverSocket, @Nonnull Map<String, Object> replacementValues,
@Nonnull List<String> responseFilePaths) {
ss = serverSocket;
s = socket;
model = replacementValues;
responseFiles = responseFilePaths;
}

public ServerCallable(@Nonnull ServerSocket serverSocket, @Nonnull Map<String, Object> replacementValues,
@Nonnull List<String> responseFilePaths) {
this(serverSocket, null, replacementValues, responseFilePaths);
}

public ServerCallable(@Nonnull ServerSocket serverSocket, @Nonnull Map<String, Object> replacementValues,
@Nonnull String responseFilePath) {
this(serverSocket, replacementValues, Collections.singletonList(responseFilePath));
}

@Override
public List<String> call() throws Exception {
if (s == null) {
s = ss.accept();
}
List<String> results = new ArrayList<>();
for (String responseFile : responseFiles) {
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream(), StandardCharsets.UTF_8));
StringBuilder builder = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
if (line.isEmpty()) {
break;
}
builder.append(line);
builder.append(System.lineSeparator());
}
results.add(builder.toString());
String rs = ofNullable(getClass().getClassLoader().getResourceAsStream(responseFile)).flatMap(s -> {
try {
String responseStr = IOUtils.toString(s);
for (String k : model.keySet()) {
responseStr = responseStr.replace("{" + k + "}", model.get(k).toString());
try (Socket s = ss.accept()) {
List<String> results = new ArrayList<>();
for (String responseFile : responseFiles) {
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream(), StandardCharsets.UTF_8));
StringBuilder builder = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
if (line.isEmpty()) {
break;
}
return of(responseStr);
} catch (IOException ignore) {
return empty();
builder.append(line);
builder.append(System.lineSeparator());
}
}).orElseThrow(() -> new IOException("Unable to read file: " + responseFile));
IOUtils.write(rs, s.getOutputStream());
results.add(builder.toString());
String rs = ofNullable(getClass().getClassLoader().getResourceAsStream(responseFile)).flatMap(stream -> {
try {
String responseStr = IOUtils.toString(stream);
for (String k : model.keySet()) {
responseStr = responseStr.replace("{" + k + "}", model.get(k).toString());
}
return of(responseStr);
} catch (IOException ignore) {
return empty();
}
}).orElseThrow(() -> new IOException("Unable to read file: " + responseFile));
IOUtils.write(rs, s.getOutputStream());

}
return results;
}
return results;
}
}

public static ServerSocket getServerSocketOnFreePort() throws IOException {
return new ServerSocket(0);
}

public static <T> Pair<List<String>, T> executeServerCallable(ServerCallable srvCall, Callable<T> clientCallable,
long timeoutSeconds) throws Exception {
public static <T> Pair<List<String>, T> executeServerCallable(ServerCallable srvCall, Callable<T> clientCallable, long timeoutSeconds)
throws Exception {
ExecutorService serverExecutor = Executors.newSingleThreadExecutor();
Future<List<String>> future = serverExecutor.submit(srvCall);
T rs = clientCallable.call();
Expand Down

0 comments on commit 5d287da

Please sign in to comment.