Commit eb15ff4f authored by Lubomir Bulej's avatar Lubomir Bulej

Runner: create support methods for common code in the subclasses.

ClientServerRunner, ClientServerEvaluationRunner: use support methods from the Runner base class in the _start() method.
ClientServerRunner, ClientServerEvaluationRunner: use RunnerException for Job related problems.
ClientServerRunner: cosmetic change in the order of the __startServer() and __startClient() methods.
parent 06ef393f
......@@ -47,12 +47,38 @@ public class ClientServerEvaluationRunner extends Runner {
super (testClass);
}
@Override
protected void _start (
final File instJar, final File appJar
) throws IOException {
final File serverFile = createStatusFile ("disl-");
__server = __startServer (instJar, serverFile);
ensureJobInitialized (__server, "server", serverFile);
//
final File shadowFile = createStatusFile ("shvm-");
__shadow = __startShadow (instJar, shadowFile);
ensureJobInitialized (__shadow, "shadow", shadowFile);
//
__client = __startClient (instJar, appJar);
if (! __client.isRunning ()) {
throw new RunnerException (
"client initialization failed:\n%s", getJobError (__client)
);
}
}
private Job __startServer (
final File testInstJar, final File statusFile
final File instJar, final File statusFile
) throws IOException {
final List <String> command = Lists.newLinkedList (
__SERVER_JAVA_COMMAND__,
"-classpath", Runner.classPath (_DISL_SERVER_JAR_, testInstJar)
"-classpath", Runner.classPath (_DISL_SERVER_JAR_, instJar)
);
if (statusFile != null) {
......@@ -65,20 +91,16 @@ public class ClientServerEvaluationRunner extends Runner {
command.addAll (propertiesStartingWith ("disl."));
command.add (_DISL_SERVER_CLASS_.getName ());
//
final Job result = new Job (command);
result.start ();
return result;
return new Job (command).start ();
}
private Job __startShadow (
final File testInstJar, final File statusFile
final File instJar, final File statusFile
) throws IOException {
final List <String> command = Lists.newLinkedList (
__SHADOW_JAVA_COMMAND__, "-Xms1G", "-Xmx2G",
"-classpath", Runner.classPath (_SHVM_SERVER_JAR_, testInstJar)
"-classpath", Runner.classPath (_SHVM_SERVER_JAR_, instJar)
);
if (statusFile != null) {
......@@ -90,70 +112,30 @@ public class ClientServerEvaluationRunner extends Runner {
command.addAll (propertiesStartingWith ("dislreserver."));
command.add (_SHVM_SERVER_CLASS_.getName ());
//
final Job result = new Job (command);
result.start ();
return result;
return new Job (command).start ();
}
private Job __startClient (
final File testInstJar, final File testAppJar
final File instJar, final File appJar
) throws IOException {
final List <String> command = Lists.newLinkedList (
__CLIENT_JAVA_COMMAND__,
String.format ("-agentpath:%s", _DISL_AGENT_LIB_),
String.format ("-agentpath:%s", _SHVM_AGENT_LIB_),
String.format ("-Xbootclasspath/a:%s", Runner.classPath (
_DISL_BYPASS_JAR_, _SHVM_DISPATCH_JAR_, testInstJar
_DISL_BYPASS_JAR_, _SHVM_DISPATCH_JAR_, instJar
))
);
command.addAll (propertiesStartingWith ("disl."));
command.addAll (Arrays.asList (
"-jar", testAppJar.toString ()
"-jar", appJar.toString ()
));
//
final Job result = new Job (command);
result.start ();
return result;
}
@Override
protected void _start (
final File testInstJar, final File testAppJar
) throws IOException {
final File serverFile = File.createTempFile ("disl-", ".status");
serverFile.deleteOnExit ();
__server = __startServer (testInstJar, serverFile);
watchFile (serverFile, _INIT_TIME_LIMIT_);
if (! __server.isRunning ()) {
throw new IOException ("server failed: "+ __server.getError ());
}
//
final File shadowFile = File.createTempFile ("shvm-", ".status");
shadowFile.deleteOnExit ();
__shadow = __startShadow (testInstJar, shadowFile);
watchFile (shadowFile, _INIT_TIME_LIMIT_);
if (! __shadow.isRunning ()) {
throw new IOException ("shadow failed: "+ __shadow.getError ());
}
//
__client = __startClient (testInstJar, testAppJar);
return new Job (command).start ();
}
......@@ -319,14 +301,6 @@ public class ClientServerEvaluationRunner extends Runner {
@Override
protected void _destroy () {
if (__client != null) {
__client.destroy ();
}
if (__shadow != null) {
__shadow.destroy ();
}
if (__server != null) {
__server.destroy ();
}
killJobs ( __server, __shadow, __client );
}
}
......@@ -40,22 +40,44 @@ public class ClientServerRunner extends Runner {
@Override
protected void _start (
final File testInstJar, final File testAppJar
final File instJar, final File appJar
) throws IOException {
final File serverFile = File.createTempFile ("disl-", ".status");
serverFile.deleteOnExit ();
final File serverFile = createStatusFile ("disl-");
__server = __startServer (instJar, serverFile);
ensureJobInitialized (__server, "server", serverFile);
__server = __startServer (testInstJar, serverFile);
//
__client = __startClient (instJar, appJar);
if (! __client.isRunning ()) {
throw new RunnerException (
"client initialization failed:\n%s", getJobError (__client)
);
}
}
watchFile (serverFile, _INIT_TIME_LIMIT_);
if (! __server.isRunning ()) {
throw new IOException ("server failed: "+ __server.getError ());
private Job __startServer (
final File testInstJar, final File statusFile
) throws IOException {
final List <String> command = Lists.newLinkedList (
__SERVER_JAVA_COMMAND__,
"-classpath", Runner.classPath (_DISL_SERVER_JAR_, testInstJar)
);
if (statusFile != null) {
command.add (String.format (
"-Dserver.status.file=%s", statusFile
));
}
command.addAll (propertiesStartingWith ("dislserver."));
command.addAll (propertiesStartingWith ("disl."));
command.add (_DISL_SERVER_CLASS_.getName ());
//
__client = __startClient (testInstJar, testAppJar);
return new Job (command).start ();
}
......@@ -81,30 +103,6 @@ public class ClientServerRunner extends Runner {
}
private Job __startServer (
final File testInstJar, final File statusFile
) throws IOException {
final List <String> command = Lists.newLinkedList (
__SERVER_JAVA_COMMAND__,
"-classpath", Runner.classPath (_DISL_SERVER_JAR_, testInstJar)
);
if (statusFile != null) {
command.add (String.format (
"-Dserver.status.file=%s", statusFile
));
}
command.addAll (propertiesStartingWith ("dislserver."));
command.addAll (propertiesStartingWith ("disl."));
command.add (_DISL_SERVER_CLASS_.getName ());
//
return new Job (command).start ();
}
@Override
protected boolean _waitFor (final Duration duration) {
// FIXME Wait in parallel for the specified duration.
......@@ -222,12 +220,6 @@ public class ClientServerRunner extends Runner {
@Override
protected void _destroy () {
if (__client != null) {
__client.destroy ();
}
if (__server != null) {
__server.destroy ();
}
killJobs (__server, __client);
}
}
......@@ -4,7 +4,6 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
......@@ -78,33 +77,33 @@ public abstract class Runner {
//
public void start () throws IOException {
final File testInstJar = new File (
_TEST_LIB_DIR_, String.format ("%s-inst.jar", __testName)
);
public void start () {
final File instJar = __getTestJar ("instrumentation", "%s-inst.jar", __testName);
final File appJar = __getTestJar ("application", "%s-app.jar", __testName);
try {
_start (instJar, appJar);
if (!testInstJar.exists ()) {
throw new FileNotFoundException (String.format (
"instrumentation jar not found: %s", testInstJar.toString ()
));
} catch (final IOException ioe) {
throw new RunnerException (ioe, "failed to start runner");
}
}
final File testAppJar = new File (
_TEST_LIB_DIR_, String.format ("%s-app.jar", __testName)
private static File __getTestJar (
final String jarType, final String format, final Object ... args
) {
final File result = new File (
_TEST_LIB_DIR_, String.format (format, args)
);
if (!testAppJar.exists ()) {
throw new FileNotFoundException (String.format (
"application jar not found: %s", testAppJar.toString ()
));
if (!result.exists ()) {
throw new RunnerException ("%s jar not found: %s", jarType, result);
}
_start (testInstJar, testAppJar);
return result;
}
protected abstract void _start (
final File testInstJar, final File testAppJar
) throws IOException;
protected abstract void _start (File instJar, File appJar) throws IOException;
//
......@@ -190,31 +189,42 @@ public abstract class Runner {
//
static List <String> propertiesStartingWith (final String prefix) {
final List <String> result = new LinkedList <String> ();
for (final String key : System.getProperties ().stringPropertyNames ()) {
if (key.startsWith (prefix)) {
final Object valueObject = System.getProperty (key);
if (valueObject instanceof String) {
final String value = (String) valueObject;
if (! value.isEmpty ()) {
result.add (String.format ("-D%s=%s", key, value));
static File createStatusFile (final String prefix) throws IOException {
final File result = File.createTempFile (prefix, ".status");
result.deleteOnExit ();
return result;
}
static void killJobs (final Job ... jobs) {
for (final Job job : jobs) {
if (job != null) {
job.destroy ();
}
}
}
return result;
}
static void ensureJobInitialized (
final Job job, final String jobName, final File statusFile
) {
final boolean jobTimedOut = __awaitRemoval (statusFile, _INIT_TIME_LIMIT_);
if (! job.isRunning ()) {
throw new RunnerException (
"%s initialization failed:\n%s", jobName, getJobError (job)
);
}
static String classPath (final File ... paths) {
return Strings.join (File.pathSeparator, (Object []) paths);
if (jobTimedOut) {
throw new RunnerException (
"%s initialization timed out", jobName, getJobError (job)
);
}
}
static boolean watchFile (final File file, final Duration duration) {
private static boolean __awaitRemoval (
final File file, final Duration duration
) {
final long watchEnd = System.nanoTime () + duration.to (TimeUnit.NANOSECONDS);
while (file.exists () && System.nanoTime () < watchEnd) {
_WATCH_DELAY_.sleepUninterruptibly ();
......@@ -223,4 +233,35 @@ public abstract class Runner {
return file.exists ();
}
static String getJobError (final Job job) {
try {
return job.getError ();
} catch (final IOException e) {
return "unknown error, failed to read job error output";
}
}
static List <String> propertiesStartingWith (final String prefix) {
final List <String> result = new LinkedList <String> ();
for (final String key : System.getProperties ().stringPropertyNames ()) {
if (key.startsWith (prefix)) {
final String value = System.getProperty (key);
if (value != null) {
result.add (String.format ("-D%s=%s", key, value));
}
}
}
return result;
}
static String classPath (final File ... paths) {
return Strings.join (File.pathSeparator, (Object []) paths);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment