Skip to content

Commit

Permalink
start of integrating the progress bar monitor into the JUnit testing …
Browse files Browse the repository at this point in the history
…infrastructure
  • Loading branch information
jurgenvinju committed Apr 2, 2024
1 parent c26af31 commit 0ca2819
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 28 deletions.
16 changes: 14 additions & 2 deletions src/org/rascalmpl/debug/IRascalMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.rascalmpl.debug;

import java.util.function.Function;
import java.util.function.Supplier;

import io.usethesource.vallang.ISourceLocation;
Expand All @@ -31,17 +32,28 @@ default void jobStart(String name, int totalWork) {
jobStart(name, 1, totalWork);
}

default void job(String name, Supplier<Boolean> block) {
default void job(String name, int totalWork, Supplier<Boolean> block) {
boolean result = false;
try {
jobStart(name);
jobStart(name, totalWork);
result = block.get();
}
finally {
jobEnd(name, result);
}
}

default void job(String name, int totalWork, Function<String, Boolean> block) {
boolean result = false;
try {
jobStart(name, totalWork);
result = block.apply(name);
}
finally {
jobEnd(name, result);
}
}

/**
* Log the start of an event with the amount of work that will be done when it's finished.
* An event is finished when the next event is logged, or when endJob() is called.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
import org.rascalmpl.debug.IRascalMonitor;
import org.rascalmpl.interpreter.Evaluator;
import org.rascalmpl.interpreter.ITestResultListener;
import org.rascalmpl.interpreter.NullRascalMonitor;
Expand Down Expand Up @@ -210,31 +211,31 @@ public void run() {
private void runTests() {
try {
if (waitForRunSignal()) {
for (Description mod: testModules) {
Listener trl = new Listener(mod, stderr);

if (mod.getAnnotations().stream().anyMatch(t -> t instanceof CompilationFailed)) {
results.add(notifier -> {
notifier.fireTestStarted(mod);
notifier.fireTestFailure(new Failure(mod, new IllegalArgumentException(mod.getDisplayName() + " had import/compilation errors")));
});
continue;
}

long start = System.nanoTime();
TestEvaluator runner = new TestEvaluator(evaluator, trl);
runner.test(mod.getDisplayName());
long stop = System.nanoTime();
long duration = (stop - start) / 1000_000;
if (duration > 10_000) {
// longer that 10s
System.err.println("Testing module " + mod.getClassName() + " took: " + duration + "ms");
System.err.flush();
evaluator.job("Test runner for " + testModules.size() + " modules...", testModules.size(), (String jn) -> {
for (Description mod: testModules) {
evaluator.jobStep(jn, mod.getDisplayName(), 1);
Listener trl = new Listener(mod, stderr, evaluator);

if (mod.getAnnotations().stream().anyMatch(t -> t instanceof CompilationFailed)) {
results.add(notifier -> {
notifier.fireTestStarted(mod);
notifier.fireTestFailure(new Failure(mod, new IllegalArgumentException(mod.getDisplayName() + " had import/compilation errors")));
});
continue;
}


TestEvaluator runner = new TestEvaluator(evaluator, trl);
runner.test(mod.getDisplayName());

stdout.flush();
stderr.flush();
}
stdout.flush();
stderr.flush();
}

return true;
});
}

}
finally {
workersCompleted.release();
Expand Down Expand Up @@ -314,10 +315,13 @@ private void initializeEvaluator() {
public class Listener implements ITestResultListener {
private final PrintWriter stderr;
private final Description module;
private final IRascalMonitor monitor;
private String context = "";

public Listener(Description module, PrintWriter stderr) {
public Listener(Description module, PrintWriter stderr, IRascalMonitor monitor) {
this.module = module;
this.stderr = stderr;
this.monitor = monitor;
}

private Description getDescription(String name, ISourceLocation loc) {
Expand All @@ -334,11 +338,13 @@ private Description getDescription(String name, ISourceLocation loc) {

@Override
public void start(String context, int count) {
// starting a module
this.context = context;
monitor.jobStart(context, count);
}

@Override
public void done() {
// a module was done
monitor.jobEnd(context, true);
}

@Override
Expand All @@ -359,12 +365,15 @@ public void report(boolean successful, String test, ISourceLocation loc, String
notifier.fireTestFinished(desc);
}
});

monitor.jobStep(context, test);
}

@Override
public void ignored(String test, ISourceLocation loc) {
Description desc = getDescription(test, loc);
results.add(notifier -> notifier.fireTestIgnored(desc));
monitor.jobStep(context, "Ignore " + test);
}
}
}

0 comments on commit 0ca2819

Please sign in to comment.