Skip to content

Commit

Permalink
Add method with configurable timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Mar 6, 2024
1 parent a1fcea1 commit d1cce9f
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,19 @@ public static ServerSocket getServerSocketOnFreePort() throws IOException {
return new ServerSocket(0);
}

public static <T> Pair<List<String>, T> executeServerCallable(ServerCallable srvCall, Callable<T> clientCallable) 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();
try {
return Pair.of(future.get(5, TimeUnit.SECONDS), rs);
return Pair.of(future.get(timeoutSeconds, TimeUnit.SECONDS), rs);
} finally {
CommonUtils.shutdownExecutorService(serverExecutor);
}
}

public static <T> Pair<List<String>, T> executeServerCallable(ServerCallable srvCall, Callable<T> clientCallable) throws Exception {
return executeServerCallable(srvCall, clientCallable, 5L);
}
}

0 comments on commit d1cce9f

Please sign in to comment.