Commit f491addf authored by Lubomir Bulej's avatar Lubomir Bulej

src-test: deduplicated and regularized the runner classes.

parent dca02fb2
......@@ -377,9 +377,11 @@
<junit haltonfailure="no" haltonerror="no" failureproperty="junit.failure">
<classpath refid="classpath" />
<formatter type="brief" usefile="false" />
<!-- allow properties starting with "-Ddisl." to be passed to tests -->
<!-- allow "disl.", "dislserver.", and "dislreserver." properties to be passed to tests -->
<syspropertyset>
<propertyref prefix="disl" />
<propertyref prefix="disl." />
<propertyref prefix="dislserver." />
<propertyref prefix="dislreserver." />
</syspropertyset>
<batchtest fork="yes">
<fileset dir="${src.test}">
......
......@@ -25,7 +25,7 @@ public class DispatchTest {
r.destroyIfRunningAndFlushOutputs ();
}
r.assertIsSuccessful ();
r.assertEvaluationOut ("evaluation.out.resource");
r.assertShadowOut ("evaluation.out.resource");
r.assertRestOutErrNull ();
}
......
......@@ -26,7 +26,7 @@ public class Dispatch2Test {
}
r.assertIsSuccessful ();
r.assertClientOut ("client.out.resource");
r.assertEvaluationOut ("evaluation.out.resource");
r.assertShadowOut ("evaluation.out.resource");
r.assertRestOutErrNull ();
}
......
......@@ -26,7 +26,7 @@ public class DispatchMPTest {
}
r.assertIsSuccessful ();
r.assertClientOut ("client.out.resource");
r.assertEvaluationOut ("evaluation.out.resource");
r.assertShadowOut ("evaluation.out.resource");
r.assertRestOutErrNull ();
}
......
package ch.usi.dag.disl.test.utils;
import java.util.concurrent.TimeUnit;
public final class Duration {
private final long __value;
private final TimeUnit __unit;
//
private Duration (final long value, final TimeUnit unit) {
__value = value;
__unit = unit;
}
//
public final long value () {
return __value;
}
public final TimeUnit unit () {
return __unit;
}
public final long to (final TimeUnit targetUnit) {
return targetUnit.convert (__value, __unit);
}
//
public final void timedWait (final Object object) throws InterruptedException {
__unit.timedWait (object, __value);
}
public final void timedJoin (final Thread thread) throws InterruptedException {
__unit.timedJoin (thread, __value);
}
//
public final void sleep () throws InterruptedException {
__unit.sleep (__value);
}
public final void softSleep () {
try {
__unit.sleep (__value);
} catch (final InterruptedException ie) {
// return if interrupted, keeping the interrupted() status
}
}
//
public static Duration of (final long duration, final TimeUnit timeUnit) {
return new Duration (duration, timeUnit);
}
}
package ch.usi.dag.disl.test.utils;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import ch.usi.dag.dislreserver.DiSLREServer;
import ch.usi.dag.dislserver.DiSLServer;
public abstract class Runner {
protected static final Duration _INIT_TIME_LIMIT_ = Duration.of (3, SECONDS);
protected static final Duration _TEST_TIME_LIMIT_ = Duration.of (60, SECONDS);
protected static final String _ENV_JAVA_HOME_ = "JAVA_HOME";
protected static final String _JAVA_COMMAND_ = __getJavaCommand ();
protected static final File _DISL_BUILD_DIR_ = new File ("build");
protected static final File _DISL_AGENT_JAR_ = new File (_DISL_BUILD_DIR_, "disl-agent.jar");
protected static final File _DISL_AGENT_LIB_ = new File (_DISL_BUILD_DIR_, "libdislagent.so");
protected static final File _DISL_SERVER_JAR_ = new File (_DISL_BUILD_DIR_, "disl-server.jar");
protected static final Class <?> _DISL_SERVER_MAIN_CLASS_ = DiSLServer.class;
protected static final File _DISL_RE_AGENT_LIB_ = new File (_DISL_BUILD_DIR_, "libdislreagent.so");
protected static final File _DISL_RE_DISPATCH_JAR_ = new File (_DISL_BUILD_DIR_, "dislre-dispatch.jar");
protected static final File _DISL_RE_SERVER_JAR_ = new File (_DISL_BUILD_DIR_, "dislre-server.jar");
protected static final Class <?> _DISL_RE_SERVER_MAIN_CLASS_ = DiSLREServer.class;
protected static final File _TEST_BUILD_DIR_ = new File ("build-test");
//
private final String __testName;
private final Class <?> __testClass;
//
Runner (final Class <?> testClass) {
__testClass = testClass;
__testName = __extractTestName (testClass);
}
private static String __getJavaCommand () {
final String javaHome = System.getenv (_ENV_JAVA_HOME_);
if (javaHome != null) {
final File jreBinDir = new File (new File (javaHome, "jre"), "bin");
return new File (jreBinDir, "java").toString ();
} else {
return "java";
}
}
private static String __extractTestName (final Class <?> testClass) {
final String [] packages = testClass.getPackage ().getName ().split ("\\.");
return packages [packages.length - 2];
}
//
public void start () throws IOException {
final File testInstrJar = new File (
_TEST_BUILD_DIR_, String.format ("disl-instr-%s.jar", __testName)
);
final File testAppJar = new File (
_TEST_BUILD_DIR_, String.format ("disl-app-%s.jar", __testName)
);
_start (testInstrJar, testAppJar);
}
protected abstract void _start (
final File testInstrJar, final File testAppJar
) throws IOException;
//
public final boolean waitFor () {
return _waitFor (_TEST_TIME_LIMIT_);
}
protected abstract boolean _waitFor (final Duration duration);
//
static void writeString (
final String content, final String fileName
) throws FileNotFoundException {
final PrintWriter out = new PrintWriter (fileName);
try {
out.print (content);
} finally {
out.close ();
}
}
static <E> List <E> newLinkedList (final E ... elements) {
return new LinkedList <E> (Arrays.asList (elements));
}
static String makeClassPath (final File ... paths) {
final StringBuilder builder = new StringBuilder ();
String effectiveSeparator = "";
for (final File path : paths) {
builder.append (effectiveSeparator);
builder.append (path);
effectiveSeparator = File.pathSeparator;
}
return builder.toString ();
}
protected String _testName () {
return __testName;
}
//
protected String _readResource (final String fileName) throws IOException {
return __readResource (__testClass, fileName);
}
private static String __readResource (
final Class <?> refClass, final String fileName
) throws IOException {
BufferedReader reader = null;
try {
reader = new BufferedReader (new InputStreamReader (
refClass.getResourceAsStream (fileName), "UTF-8"
));
final StringBuffer buffer = new StringBuffer ();
for (int c = reader.read (); c != -1; c = reader.read ()) {
buffer.append ((char) c);
}
return buffer.toString ();
} finally {
if(reader != null) {
reader.close ();
}
}
}
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));
}
}
}
}
return result;
}
protected void _destroyIfRunningAndDumpOutputs (
final Job job, final String prefix
) throws IOException {
if (job.isRunning ()) {
job.destroy ();
}
Runner.writeString (
job.getOutput (),
String.format ("tmp.%s.%s.out.txt", __testName, prefix)
);
Runner.writeString (
job.getError (),
String.format ("tmp.%s.%s.err.txt", __testName, prefix)
);
}
}
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