Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Apr 13, 2021
1 parent b6445aa commit da46d00
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ public class TestConfig {
@ConfigItem(defaultValue = "false")
public boolean disableColor;

/**
* If test results and status should be displayed in the console.
*
* If this is false results can still be viewed in the dev console.
*/
@ConfigItem(defaultValue = "true")
public boolean console;

/**
* Duration to wait for the native image to built during testing
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ private RuntimeUpdatesProcessor setupRuntimeCompilation(DevModeContext context,
}
QuarkusCompiler compiler = new QuarkusCompiler(curatedApplication, compilationProviders, context);
TestSupport testSupport = new TestSupport(curatedApplication, compilationProviders, context);

RuntimeUpdatesProcessor processor = new RuntimeUpdatesProcessor(appRoot, context, compiler,
devModeType, this::restartCallback, null, new BiFunction<String, byte[], byte[]>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

import io.quarkus.bootstrap.runner.Timing;
import io.quarkus.changeagent.ClassChangeAgent;
import io.quarkus.deployment.dev.testing.TestListener;
import io.quarkus.deployment.dev.testing.TestSupport;
import io.quarkus.deployment.dev.testing.runner.TestRunner;
import io.quarkus.deployment.util.FSWatchUtil;
Expand Down Expand Up @@ -125,19 +126,18 @@ public RuntimeUpdatesProcessor(Path applicationRoot, DevModeContext context, Qua
this.copyResourceNotification = copyResourceNotification;
this.classTransformers = classTransformers;
this.testSupport = testSupport;
testSupport.addStartListener(new Runnable() {
testSupport.addListener(new TestListener() {
@Override
public void run() {
public void testsEnabled() {
if (!firstTestScanComplete) {
checkForChangedTestClasses(true);
firstTestScanComplete = true;
}
startTestScanningTimer();
}
});
testSupport.addStopListener(new Runnable() {

@Override
public void run() {
public void testsDisabled() {
synchronized (RuntimeUpdatesProcessor.this) {
if (timer != null) {
timer.cancel();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.quarkus.deployment.dev.testing;

import io.quarkus.deployment.dev.testing.runner.TestState;

public interface TestController {

TestState currentState();

void runAllTests();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.deployment.dev.testing;

import java.util.function.Consumer;

public interface TestListener {

default void listenerRegistered(TestController testController) {

}

default void testsEnabled() {
}

default void testsDisabled() {

}

default void testRunStarted(Consumer<TestRunListener> listenerConsumer) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.deployment.dev.testing;

import org.junit.platform.launcher.TestIdentifier;

public interface TestRunListener {

default void runStarted(long toRun) {

}

default void testComplete(TestResult result) {

}

default void runComplete(TestRunResults results) {

}

default void runAborted() {

}

default void testStarted(TestIdentifier testIdentifier, String className) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
import io.quarkus.deployment.dev.testing.runner.TestState;
import io.quarkus.dev.testing.ContinuousTestingWebsocketListener;

public class TestSupport {
public class TestSupport implements TestController {

private static final Logger log = Logger.getLogger(TestSupport.class);

final CuratedApplication curatedApplication;
final List<CompilationProvider> compilationProviders;
final DevModeContext context;
final List<Runnable> startListeners = new ArrayList<>();
final List<Runnable> stopListeners = new ArrayList<>();
final List<TestListener> testListeners = new ArrayList<>();
final TestState testState = new TestState();

volatile CuratedApplication testCuratedApplication;
Expand Down Expand Up @@ -55,6 +54,10 @@ public boolean isRunning() {
return testRunner.isRunning();
}

public List<TestListener> getTestListeners() {
return testListeners;
}

/**
* Gets the results for an already completed test run, whoes ID is equal to or larger than the provided id.
*/
Expand Down Expand Up @@ -129,8 +132,8 @@ public void start() {
if (context.getApplicationRoot().getTest().isPresent()) {
started = true;
init();
for (Runnable i : startListeners) {
i.run();
for (TestListener i : testListeners) {
i.testsEnabled();
}
testRunner.enable();
}
Expand Down Expand Up @@ -191,8 +194,8 @@ public void accept(TestRunResults testRunResults) {
public synchronized void stop() {
if (started) {
started = false;
for (Runnable i : stopListeners) {
i.run();
for (TestListener i : testListeners) {
i.testsDisabled();
}
}
if (testRunner != null) {
Expand All @@ -201,24 +204,21 @@ public synchronized void stop() {
}
}

public void addStartListener(Runnable runnable) {
public void addListener(TestListener listener) {
boolean run = false;
synchronized (this) {
startListeners.add(runnable);
testListeners.add(listener);
if (started) {
run = true;
}
}
listener.listenerRegistered(this);
if (run) {
//run outside lock
runnable.run();
listener.testsEnabled();
}
}

public synchronized void addStopListener(Runnable runnable) {
stopListeners.add(runnable);
}

public boolean isStarted() {
return started;
}
Expand Down Expand Up @@ -269,6 +269,16 @@ public void setPatterns(String include, String exclude) {
testRunner.setPatterns(include, exclude);
}

@Override
public TestState currentState() {
return getTestRunner().getResults();
}

@Override
public void runAllTests() {
getTestRunner().runTests();
}

public static class RunStatus {

final long lastRun;
Expand All @@ -287,4 +297,5 @@ public long getRunning() {
return running;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import io.quarkus.deployment.dev.DevModeContext;
import io.quarkus.deployment.dev.testing.TestClassResult;
import io.quarkus.deployment.dev.testing.TestResult;
import io.quarkus.deployment.dev.testing.TestRunListener;
import io.quarkus.deployment.dev.testing.TestRunResults;
import io.quarkus.dev.testing.ContinuousTestingLogHandler;
import io.quarkus.dev.testing.TracingHandler;
Expand All @@ -81,7 +82,7 @@ public class JunitTestRunner {
private final ClassScanResult classScanResult;
private final TestClassUsages testClassUsages;
private final TestState testState;
private final TestListener listener;
private final List<TestRunListener> listeners;
private final Set<String> includeTags;
private final Set<String> excludeTags;
private final Pattern include;
Expand All @@ -97,7 +98,7 @@ public JunitTestRunner(Builder builder) {
this.testApplication = builder.testApplication;
this.classScanResult = builder.classScanResult;
this.testClassUsages = builder.testClassUsages;
this.listener = builder.listener;
this.listeners = builder.listeners;
this.testState = builder.testState;
this.includeTags = new HashSet<>(builder.includeTags);
this.excludeTags = new HashSet<>(builder.excludeTags);
Expand Down Expand Up @@ -140,7 +141,9 @@ public void runTests() {
return;
}
long toRun = testPlan.countTestIdentifiers(TestIdentifier::isTest);
listener.runStarted(toRun);
for (TestRunListener listener : listeners) {
listener.runStarted(toRun);
}
log.debug("Starting test run with " + quarkusTestClasses.size() + " test cases");
TestLogCapturingHandler logHandler = new TestLogCapturingHandler();
ContinuousTestingLogHandler.setLogHandler(logHandler);
Expand Down Expand Up @@ -176,7 +179,9 @@ public void executionStarted(TestIdentifier testIdentifier) {
className = ((ClassSource) testIdentifier.getSource().get()).getClassName();
}
}
listener.testStarted(testIdentifier, className);
for (TestRunListener listener : listeners) {
listener.testStarted(testIdentifier, className);
}
waitTillResumed();
touchedClasses.push(Collections.synchronizedSet(new HashSet<>()));
}
Expand Down Expand Up @@ -232,7 +237,9 @@ public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult
logHandler.captureOutput(), testIdentifier.isTest(), runId);
results.put(id, result);
if (result.isTest()) {
listener.testComplete(result);
for (TestRunListener listener : listeners) {
listener.testComplete(result);
}
}
}
if (testExecutionResult.getStatus() == TestExecutionResult.Status.FAILED) {
Expand Down Expand Up @@ -281,8 +288,11 @@ public void reportingEntryPublished(TestIdentifier testIdentifier, ReportEntry e

ContinuousTestingLogHandler.setLogHandler(null);
List<TestResult> historicFailures = testState.getHistoricFailures(resultsByClass);
listener.runComplete(new TestRunResults(runId, classScanResult, classScanResult == null, start,
System.currentTimeMillis(), toResultsMap(historicFailures, resultsByClass)));

for (TestRunListener listener : listeners) {
listener.runComplete(new TestRunResults(runId, classScanResult, classScanResult == null, start,
System.currentTimeMillis(), toResultsMap(historicFailures, resultsByClass)));
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
Expand All @@ -292,7 +302,9 @@ public void reportingEntryPublished(TestIdentifier testIdentifier, ReportEntry e
}

public synchronized void abort() {
listener.runAborted();
for (TestRunListener listener : listeners) {
listener.runAborted();
}
aborted = true;
notifyAll();
}
Expand Down Expand Up @@ -422,19 +434,6 @@ public boolean isRunning() {
return testsRunning;
}

public interface TestListener {

void runStarted(long toRun);

void testComplete(TestResult result);

void runComplete(TestRunResults results);

void runAborted();

void testStarted(TestIdentifier testIdentifier, String className);
}

private class TestLogCapturingHandler implements Predicate<LogRecord> {

private final List<LogRecord> logOutput;
Expand Down Expand Up @@ -489,7 +488,7 @@ static class Builder {
private CuratedApplication testApplication;
private ClassScanResult classScanResult;
private TestClassUsages testClassUsages;
private TestListener listener;
private List<TestRunListener> listeners = new ArrayList<>();
private List<String> includeTags = Collections.emptyList();
private List<String> excludeTags = Collections.emptyList();
private Pattern include;
Expand Down Expand Up @@ -530,8 +529,8 @@ public Builder setTestClassUsages(TestClassUsages testClassUsages) {
return this;
}

public Builder setListener(TestListener listener) {
this.listener = listener;
public Builder addListener(TestRunListener listener) {
this.listeners.add(listener);
return this;
}

Expand All @@ -555,7 +554,6 @@ public JunitTestRunner build() {
Objects.requireNonNull(testClassUsages, "testClassUsages");
Objects.requireNonNull(testApplication, "testApplication");
Objects.requireNonNull(testState, "testState");
Objects.requireNonNull(listener, "listener");
return new JunitTestRunner(this);
}

Expand Down
Loading

0 comments on commit da46d00

Please sign in to comment.