Permalink
Browse files

Merge pull request #510 from Tibor17/junit.tests

Improved implementation of ParallelComputer
  • Loading branch information...
2 parents d48bf97 + 7898dd8 commit 30bc68c6877a8f3daa160b2c32bab107dae44154 @dsaff dsaff committed Sep 25, 2012
Showing with 12 additions and 23 deletions.
  1. +12 −23 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;
@@ -32,30 +29,22 @@ public static Computer methods() {
return new ParallelComputer(false, true);
}
- private static <T> Runner parallelize(Runner runner) {
- if (runner instanceof ParentRunner<?>) {
+ private static Runner parallelize(Runner runner) {
+ if (runner instanceof ParentRunner) {
((ParentRunner<?>) runner).setScheduler(new RunnerScheduler() {
- private final List<Future<Object>> fResults= new ArrayList<Future<Object>>();
+ private final ExecutorService fService= Executors.newCachedThreadPool();
- private final ExecutorService fService= Executors
- .newCachedThreadPool();
-
- public void schedule(final Runnable childStatement) {
- fResults.add(fService.submit(new Callable<Object>() {
- public Object call() throws Exception {
- childStatement.run();
- return null;
- }
- }));
+ public void schedule(Runnable childStatement) {
+ fService.submit(childStatement);
}
public void finished() {
- for (Future<Object> each : fResults)
- try {
- each.get();
- } catch (Exception e) {
- e.printStackTrace();
- }
+ try {
+ fService.shutdown();
+ fService.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
+ } catch (InterruptedException e) {
+ e.printStackTrace(System.err);
+ }
}
});
}

0 comments on commit 30bc68c

Please sign in to comment.