Skip to content

Commit

Permalink
add max_retries count, move shell methods to ProcUtils and add win me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
fguallini committed Mar 31, 2021
1 parent 5688014 commit b29e488
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 50 deletions.
30 changes: 24 additions & 6 deletions test/jdk/javax/net/ssl/TLSCommon/interop/BaseInteropTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public abstract class BaseInteropTest<U extends UseCase> {

protected final Product serverProduct;
protected final Product clientProduct;
private static final int MAX_SERVER_RETRIES = 3;

public BaseInteropTest(Product serverProduct, Product clientProduct) {
this.serverProduct = serverProduct;
Expand Down Expand Up @@ -226,16 +227,26 @@ protected Status runTestCase(TestCase<U> testCase) throws Exception {

/*
* Return a server once it is properly started to avoid client connection issues.
* Retry operation if needed, server may fail to bind a port
*/
protected AbstractServer startAndGetServer(U useCase, ExecutorService executor)
throws Exception {
AbstractServer server = createServer(useCase, executor);
if (!Utilities.waitFor(Server::isAlive, server)) {
// Retry operation, server might have failed to bind a port
server.signalStop();
int maxRetries = getServerMaxRetries();
boolean serverAlive;
AbstractServer server;

do {
server = createServer(useCase, executor);
if (!Utilities.waitFor(Server::isAlive, server))
throw new RuntimeException("Server failed to start");
serverAlive = Utilities.waitFor(Server::isAlive, server);
if (!serverAlive) {
server.signalStop();
}

maxRetries--;
} while (!serverAlive && maxRetries > 0);

if (!serverAlive) {
throw new RuntimeException("Server failed to start");
}

return server;
Expand Down Expand Up @@ -297,6 +308,13 @@ protected AbstractClient.Builder createClientBuilder(U useCase)
.setCertTuple(useCase.getCertTuple());
}

/*
* Returns the maximum number of attempts to start a server.
*/
protected int getServerMaxRetries() {
return MAX_SERVER_RETRIES;
}

/*
* Determines the negotiated application protocol.
* Generally, using JDK client to get this value.
Expand Down
79 changes: 77 additions & 2 deletions test/jdk/javax/net/ssl/TLSCommon/interop/ProcUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
* questions.
*/

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

Expand All @@ -40,15 +42,15 @@ public class ProcUtils {
*/
public static OutputAnalyzer java(Path javaPath, Class<?> clazz,
Map<String, String> props) {
ProcessBuilder pb = createProcessBuilder(javaPath, clazz, props);
ProcessBuilder pb = createJavaProcessBuilder(javaPath, clazz, props);
try {
return ProcessTools.executeCommand(pb);
} catch (Throwable e) {
throw new RuntimeException("Executes java program failed!", e);
}
}

private static ProcessBuilder createProcessBuilder(Path javaPath,
private static ProcessBuilder createJavaProcessBuilder(Path javaPath,
Class<?> clazz, Map<String, String> props) {
List<String> cmds = new ArrayList<>();
cmds.add(javaPath.toString());
Expand All @@ -66,4 +68,77 @@ private static ProcessBuilder createProcessBuilder(Path javaPath,
pb.redirectErrorStream(true);
return pb;
}

/*
* Executes a shell command and return a OutputAnalyzer wrapping the process.
*/
public static OutputAnalyzer shell(String command, Map<String, String> env)
throws IOException {
Process process = shellProc(command, null, env);
return getProcessOutput(process);
}

/*
* Executes win command and return a OutputAnalyzer wrapping the process.
*/
public static OutputAnalyzer win(String command, Map<String, String> env)
throws IOException {
Process process = winProc(command, null, env);
return getProcessOutput(process);
}

/*
* Executes a shell command and return the process.
*/
public static Process shellProc(String command, Path outputPath,
Map<String, String> env) throws IOException {
String[] cmds = new String[3];
cmds[0] = "sh";
cmds[1] = "-c";
cmds[2] = command;
return startAndGetProc(cmds, outputPath, env);
}

/*
* Executes a win command and returns the process.
*/
public static Process winProc(String command, Path outputPath,
Map<String, String> env)
throws IOException {
String[] cmds = new String[3];
cmds[0] = "cmd.exe";
cmds[1] = "/C";
cmds[2] = command;
return startAndGetProc(cmds, outputPath, env);
}

/*
* Returns a OutputAnalyzer wrapping the process.
*/
private static OutputAnalyzer getProcessOutput (Process process) throws IOException {
OutputAnalyzer oa = new OutputAnalyzer(process);
try {
process.waitFor();
return oa;
} catch (InterruptedException e) {
throw new RuntimeException("Process is interrupted!", e);
}
}

/*
* Executes a command, redirects the output to a local file and returns the process.
*/
private static Process startAndGetProc(String[] cmds, Path outputPath, Map<String,
String> env) throws IOException {
System.out.println("command to run: " + Arrays.toString(cmds));
ProcessBuilder pb = new ProcessBuilder(cmds);
if (env != null) {
pb.environment().putAll(env);
}
pb.redirectErrorStream(true);
if (outputPath != null) {
pb.redirectOutput(outputPath.toFile());
}
return pb.start();
}
}
42 changes: 0 additions & 42 deletions test/jdk/javax/net/ssl/TLSCommon/interop/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,48 +279,6 @@ public static <T extends Enum<T>> T[] strToEnums(Class<T> enumType,
(T[]) Array.newInstance(enumType, 0));
}

/*
* Executes shell command and return a OutputAnalyzer wrapping the process.
*/
public static OutputAnalyzer shell(String command) throws IOException {
Process process = shellProc(command);
OutputAnalyzer oa = new OutputAnalyzer(process);
try {
process.waitFor();
return oa;
} catch (InterruptedException e) {
throw new RuntimeException("Shell process is interruptted!", e);
}
}

/*
* Executes shell command and redirect the output to a local file,
* and return the process.
*/
public static Process shellProc(String command, Path outputPath)
throws IOException {
String[] cmds = new String[3];
cmds[0] = "sh";
cmds[1] = "-c";
cmds[2] = command;
if (DEBUG) {
System.out.println("[sh -c " + command + "]");
}
ProcessBuilder pb = new ProcessBuilder(cmds);
pb.redirectErrorStream(true);
if (outputPath != null) {
pb.redirectOutput(outputPath.toFile());
}
return pb.start();
}

/*
* Executes shell command and return the process.
*/
public static Process shellProc(String command) throws IOException {
return shellProc(command, null);
}

/*
* Determines if the specified process is alive.
*/
Expand Down

0 comments on commit b29e488

Please sign in to comment.