diff --git a/CHANGELOG.md b/CHANGELOG.md index 01d3656..fdc0333 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/main/java/com/epam/reportportal/util/test/SocketUtils.java b/src/main/java/com/epam/reportportal/util/test/SocketUtils.java index 872da6e..59128be 100644 --- a/src/main/java/com/epam/reportportal/util/test/SocketUtils.java +++ b/src/main/java/com/epam/reportportal/util/test/SocketUtils.java @@ -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; @@ -46,21 +45,14 @@ public static final class ServerCallable implements Callable> { private final ServerSocket ss; private final Map model; private final List responseFiles; - private Socket s; - public ServerCallable(@Nonnull ServerSocket serverSocket, @Nullable Socket socket, @Nonnull Map replacementValues, + public ServerCallable(@Nonnull ServerSocket serverSocket, @Nonnull Map replacementValues, @Nonnull List responseFilePaths) { ss = serverSocket; - s = socket; model = replacementValues; responseFiles = responseFilePaths; } - public ServerCallable(@Nonnull ServerSocket serverSocket, @Nonnull Map replacementValues, - @Nonnull List responseFilePaths) { - this(serverSocket, null, replacementValues, responseFilePaths); - } - public ServerCallable(@Nonnull ServerSocket serverSocket, @Nonnull Map replacementValues, @Nonnull String responseFilePath) { this(serverSocket, replacementValues, Collections.singletonList(responseFilePath)); @@ -68,37 +60,36 @@ public ServerCallable(@Nonnull ServerSocket serverSocket, @Nonnull Map call() throws Exception { - if (s == null) { - s = ss.accept(); - } - List 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 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; } } @@ -106,8 +97,8 @@ public static ServerSocket getServerSocketOnFreePort() throws IOException { return new ServerSocket(0); } - public static Pair, T> executeServerCallable(ServerCallable srvCall, Callable clientCallable, - long timeoutSeconds) throws Exception { + public static Pair, T> executeServerCallable(ServerCallable srvCall, Callable clientCallable, long timeoutSeconds) + throws Exception { ExecutorService serverExecutor = Executors.newSingleThreadExecutor(); Future> future = serverExecutor.submit(srvCall); T rs = clientCallable.call();