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 { ...@@ -47,12 +47,38 @@ public class ClientServerEvaluationRunner extends Runner {
super (testClass); 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 ( private Job __startServer (
final File testInstJar, final File statusFile final File instJar, final File statusFile
) throws IOException { ) throws IOException {
final List <String> command = Lists.newLinkedList ( final List <String> command = Lists.newLinkedList (
__SERVER_JAVA_COMMAND__, __SERVER_JAVA_COMMAND__,
"-classpath", Runner.classPath (_DISL_SERVER_JAR_, testInstJar) "-classpath", Runner.classPath (_DISL_SERVER_JAR_, instJar)
); );
if (statusFile != null) { if (statusFile != null) {
...@@ -65,20 +91,16 @@ public class ClientServerEvaluationRunner extends Runner { ...@@ -65,20 +91,16 @@ public class ClientServerEvaluationRunner extends Runner {
command.addAll (propertiesStartingWith ("disl.")); command.addAll (propertiesStartingWith ("disl."));
command.add (_DISL_SERVER_CLASS_.getName ()); command.add (_DISL_SERVER_CLASS_.getName ());
// return new Job (command).start ();
final Job result = new Job (command);
result.start ();
return result;
} }
private Job __startShadow ( private Job __startShadow (
final File testInstJar, final File statusFile final File instJar, final File statusFile
) throws IOException { ) throws IOException {
final List <String> command = Lists.newLinkedList ( final List <String> command = Lists.newLinkedList (
__SHADOW_JAVA_COMMAND__, "-Xms1G", "-Xmx2G", __SHADOW_JAVA_COMMAND__, "-Xms1G", "-Xmx2G",
"-classpath", Runner.classPath (_SHVM_SERVER_JAR_, testInstJar) "-classpath", Runner.classPath (_SHVM_SERVER_JAR_, instJar)
); );
if (statusFile != null) { if (statusFile != null) {
...@@ -90,70 +112,30 @@ public class ClientServerEvaluationRunner extends Runner { ...@@ -90,70 +112,30 @@ public class ClientServerEvaluationRunner extends Runner {
command.addAll (propertiesStartingWith ("dislreserver.")); command.addAll (propertiesStartingWith ("dislreserver."));
command.add (_SHVM_SERVER_CLASS_.getName ()); command.add (_SHVM_SERVER_CLASS_.getName ());
// return new Job (command).start ();
final Job result = new Job (command);
result.start ();
return result;
} }
private Job __startClient ( private Job __startClient (
final File testInstJar, final File testAppJar final File instJar, final File appJar
) throws IOException { ) throws IOException {
final List <String> command = Lists.newLinkedList ( final List <String> command = Lists.newLinkedList (
__CLIENT_JAVA_COMMAND__, __CLIENT_JAVA_COMMAND__,
String.format ("-agentpath:%s", _DISL_AGENT_LIB_), String.format ("-agentpath:%s", _DISL_AGENT_LIB_),
String.format ("-agentpath:%s", _SHVM_AGENT_LIB_), String.format ("-agentpath:%s", _SHVM_AGENT_LIB_),
String.format ("-Xbootclasspath/a:%s", Runner.classPath ( 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 (propertiesStartingWith ("disl."));
command.addAll (Arrays.asList ( command.addAll (Arrays.asList (
"-jar", testAppJar.toString () "-jar", appJar.toString ()
)); ));
// //
final Job result = new Job (command); return new Job (command).start ();
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);
} }
...@@ -319,14 +301,6 @@ public class ClientServerEvaluationRunner extends Runner { ...@@ -319,14 +301,6 @@ public class ClientServerEvaluationRunner extends Runner {
@Override @Override
protected void _destroy () { protected void _destroy () {
if (__client != null) { killJobs ( __server, __shadow, __client );
__client.destroy ();
}
if (__shadow != null) {
__shadow.destroy ();
}
if (__server != null) {
__server.destroy ();
}
} }
} }
...@@ -40,22 +40,44 @@ public class ClientServerRunner extends Runner { ...@@ -40,22 +40,44 @@ public class ClientServerRunner extends Runner {
@Override @Override
protected void _start ( protected void _start (
final File testInstJar, final File testAppJar final File instJar, final File appJar
) throws IOException { ) throws IOException {
final File serverFile = File.createTempFile ("disl-", ".status"); final File serverFile = createStatusFile ("disl-");
serverFile.deleteOnExit (); __server = __startServer (instJar, serverFile);
ensureJobInitialized (__server, "server", serverFile);
__server = __startServer (testInstJar, serverFile); //
watchFile (serverFile, _INIT_TIME_LIMIT_); __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
) throws IOException {
final List <String> command = Lists.newLinkedList (
__SERVER_JAVA_COMMAND__,
"-classpath", Runner.classPath (_DISL_SERVER_JAR_, testInstJar)
);
if (! __server.isRunning ()) { if (statusFile != null) {
throw new IOException ("server failed: "+ __server.getError ()); 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 { ...@@ -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 @Override
protected boolean _waitFor (final Duration duration) { protected boolean _waitFor (final Duration duration) {
// FIXME Wait in parallel for the specified duration. // FIXME Wait in parallel for the specified duration.
...@@ -222,12 +220,6 @@ public class ClientServerRunner extends Runner { ...@@ -222,12 +220,6 @@ public class ClientServerRunner extends Runner {
@Override @Override
protected void _destroy () { protected void _destroy () {
if (__client != null) { killJobs (__server, __client);
__client.destroy ();
}
if (__server != null) {
__server.destroy ();
}
} }
} }
...@@ -4,7 +4,6 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS; ...@@ -4,7 +4,6 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
...@@ -78,33 +77,33 @@ public abstract class Runner { ...@@ -78,33 +77,33 @@ public abstract class Runner {
// //
public void start () throws IOException { public void start () {
final File testInstJar = new File ( final File instJar = __getTestJar ("instrumentation", "%s-inst.jar", __testName);
_TEST_LIB_DIR_, String.format ("%s-inst.jar", __testName) final File appJar = __getTestJar ("application", "%s-app.jar", __testName);
);
try {
_start (instJar, appJar);
if (!testInstJar.exists ()) { } catch (final IOException ioe) {
throw new FileNotFoundException (String.format ( throw new RunnerException (ioe, "failed to start runner");
"instrumentation jar not found: %s", testInstJar.toString ()
));
} }
}
final File testAppJar = new File ( private static File __getTestJar (
_TEST_LIB_DIR_, String.format ("%s-app.jar", __testName) final String jarType, final String format, final Object ... args
) {
final File result = new File (
_TEST_LIB_DIR_, String.format (format, args)
); );
if (!testAppJar.exists ()) { if (!result.exists ()) {
throw new FileNotFoundException (String.format ( throw new RunnerException ("%s jar not found: %s", jarType, result);
"application jar not found: %s", testAppJar.toString ()
));
} }
_start (testInstJar, testAppJar); return result;
} }
protected abstract void _start ( protected abstract void _start (File instJar, File appJar) throws IOException;
final File testInstJar, final File testAppJar
) throws IOException;
// //
...@@ -190,31 +189,42 @@ public abstract class Runner { ...@@ -190,31 +189,42 @@ public abstract class Runner {
// //
static List <String> propertiesStartingWith (final String prefix) { static File createStatusFile (final String prefix) throws IOException {
final List <String> result = new LinkedList <String> (); final File result = File.createTempFile (prefix, ".status");
result.deleteOnExit ();
return result;
}
for (final String key : System.getProperties ().stringPropertyNames ()) { static void killJobs (final Job ... jobs) {
if (key.startsWith (prefix)) { for (final Job job : jobs) {
final Object valueObject = System.getProperty (key); if (job != null) {
if (valueObject instanceof String) { job.destroy ();
final String value = (String) valueObject;
if (! value.isEmpty ()) {
result.add (String.format ("-D%s=%s", key, value));
}
}
} }
} }
return result;
} }
static void ensureJobInitialized (
final Job job, final String jobName, final File statusFile
) {
final boolean jobTimedOut = __awaitRemoval (statusFile, _INIT_TIME_LIMIT_);
static String classPath (final File ... paths) { if (! job.isRunning ()) {
return Strings.join (File.pathSeparator, (Object []) paths); throw new RunnerException (
"%s initialization failed:\n%s", jobName, getJobError (job)
);
}
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); final long watchEnd = System.nanoTime () + duration.to (TimeUnit.NANOSECONDS);
while (file.exists () && System.nanoTime () < watchEnd) { while (file.exists () && System.nanoTime () < watchEnd) {
_WATCH_DELAY_.sleepUninterruptibly (); _WATCH_DELAY_.sleepUninterruptibly ();
...@@ -223,4 +233,35 @@ public abstract class Runner { ...@@ -223,4 +233,35 @@ public abstract class Runner {
return file.exists (); 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