aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/junit/experimental/ParallelComputer.java
diff options
context:
space:
mode:
authorPaul Duffin <paulduffin@google.com>2016-12-14 11:49:43 +0000
committerPaul Duffin <paulduffin@google.com>2016-12-20 15:52:52 +0000
commitaeb93fc33cae3aadbb9b46083350ad2dc9aea645 (patch)
treeb316db7dee11d1aeee3510562e036fd41705b8b5 /src/main/java/org/junit/experimental/ParallelComputer.java
parent26401927b83770db45f00706ccc589955644c6c2 (diff)
downloadjunit-aeb93fc33cae3aadbb9b46083350ad2dc9aea645.tar.gz
Upgrade to JUnit 4.12
The license has changed from Common Public License v1.0 to Eclipse Public License v1.0. This will not compile as it is because it is intended to be built against Hamcrest 1.3 or later but it is being built against Hamcrest 1.1. A follow on patch will fix the compilation errors so that it builds against Hamcrest 1.1. That allows Hamcrest to be upgraded separately. The patch can be reverted once Hamcrest has been upgraded. There are also some Android specific issues that will also be fixed in follow on patches. Bug: 33613916 Test: make checkbuild Change-Id: Ic2c983a030399e3ace1a14927cb143fbd8307b4f
Diffstat (limited to 'src/main/java/org/junit/experimental/ParallelComputer.java')
-rw-r--r--src/main/java/org/junit/experimental/ParallelComputer.java99
1 files changed, 44 insertions, 55 deletions
diff --git a/src/main/java/org/junit/experimental/ParallelComputer.java b/src/main/java/org/junit/experimental/ParallelComputer.java
index fccb97c..97da0f7 100644
--- a/src/main/java/org/junit/experimental/ParallelComputer.java
+++ b/src/main/java/org/junit/experimental/ParallelComputer.java
@@ -1,11 +1,8 @@
package org.junit.experimental;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
import org.junit.runner.Computer;
import org.junit.runner.Runner;
@@ -15,64 +12,56 @@ import org.junit.runners.model.RunnerBuilder;
import org.junit.runners.model.RunnerScheduler;
public class ParallelComputer extends Computer {
- private final boolean fClasses;
+ private final boolean classes;
- private final boolean fMethods;
+ private final boolean methods;
- public ParallelComputer(boolean classes, boolean methods) {
- fClasses= classes;
- fMethods= methods;
- }
+ public ParallelComputer(boolean classes, boolean methods) {
+ this.classes = classes;
+ this.methods = methods;
+ }
- public static Computer classes() {
- return new ParallelComputer(true, false);
- }
+ public static Computer classes() {
+ return new ParallelComputer(true, false);
+ }
- public static Computer methods() {
- return new ParallelComputer(false, true);
- }
+ public static Computer methods() {
+ return new ParallelComputer(false, true);
+ }
- private static <T> Runner parallelize(Runner runner) {
- if (runner instanceof ParentRunner<?>) {
- ((ParentRunner<?>) runner).setScheduler(new RunnerScheduler() {
- private final List<Future<Object>> fResults= new ArrayList<Future<Object>>();
+ private static Runner parallelize(Runner runner) {
+ if (runner instanceof ParentRunner) {
+ ((ParentRunner<?>) runner).setScheduler(new RunnerScheduler() {
+ private final ExecutorService fService = Executors.newCachedThreadPool();
- private final ExecutorService fService= Executors
- .newCachedThreadPool();
+ public void schedule(Runnable childStatement) {
+ fService.submit(childStatement);
+ }
- public void schedule(final Runnable childStatement) {
- fResults.add(fService.submit(new Callable<Object>() {
- public Object call() throws Exception {
- childStatement.run();
- return null;
- }
- }));
- }
+ public void finished() {
+ try {
+ fService.shutdown();
+ fService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
+ } catch (InterruptedException e) {
+ e.printStackTrace(System.err);
+ }
+ }
+ });
+ }
+ return runner;
+ }
- public void finished() {
- for (Future<Object> each : fResults)
- try {
- each.get();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- return runner;
- }
+ @Override
+ public Runner getSuite(RunnerBuilder builder, java.lang.Class<?>[] classes)
+ throws InitializationError {
+ Runner suite = super.getSuite(builder, classes);
+ return this.classes ? parallelize(suite) : suite;
+ }
- @Override
- public Runner getSuite(RunnerBuilder builder, java.lang.Class<?>[] classes)
- throws InitializationError {
- Runner suite= super.getSuite(builder, classes);
- return fClasses ? parallelize(suite) : suite;
- }
-
- @Override
- protected Runner getRunner(RunnerBuilder builder, Class<?> testClass)
- throws Throwable {
- Runner runner= super.getRunner(builder, testClass);
- return fMethods ? parallelize(runner) : runner;
- }
+ @Override
+ protected Runner getRunner(RunnerBuilder builder, Class<?> testClass)
+ throws Throwable {
+ Runner runner = super.getRunner(builder, testClass);
+ return methods ? parallelize(runner) : runner;
+ }
}