1 | |
package org.apache.maven.surefire.junitcore; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Collections; |
24 | |
import java.util.List; |
25 | |
import java.util.concurrent.ExecutionException; |
26 | |
import java.util.concurrent.ExecutorService; |
27 | |
import java.util.concurrent.Executors; |
28 | |
import java.util.concurrent.Future; |
29 | |
import org.apache.maven.surefire.util.NestedRuntimeException; |
30 | |
|
31 | |
import org.junit.runners.model.RunnerScheduler; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public class AsynchronousRunner |
37 | |
implements RunnerScheduler |
38 | |
{ |
39 | 6011 | private final List<Future<Object>> futures = Collections.synchronizedList( new ArrayList<Future<Object>>() ); |
40 | |
|
41 | |
private final ExecutorService fService; |
42 | |
|
43 | |
public AsynchronousRunner( ExecutorService fService ) |
44 | 6011 | { |
45 | 6011 | this.fService = fService; |
46 | 6011 | } |
47 | |
|
48 | |
public void schedule( final Runnable childStatement ) |
49 | |
{ |
50 | 20026 | futures.add( fService.submit( Executors.callable( childStatement ) ) ); |
51 | 20026 | } |
52 | |
|
53 | |
|
54 | |
public void finished() |
55 | |
{ |
56 | |
try |
57 | |
{ |
58 | 6011 | waitForCompletion(); |
59 | |
} |
60 | 0 | catch ( ExecutionException e ) |
61 | |
{ |
62 | 0 | throw new NestedRuntimeException( e ); |
63 | 6011 | } |
64 | 6011 | } |
65 | |
|
66 | |
public void waitForCompletion() |
67 | |
throws ExecutionException |
68 | |
{ |
69 | 9011 | for ( Future<Object> each : futures ) |
70 | |
{ |
71 | |
try |
72 | |
{ |
73 | 29026 | each.get(); |
74 | |
} |
75 | 0 | catch ( InterruptedException e ) |
76 | |
{ |
77 | 0 | throw new NestedRuntimeException( e ); |
78 | 29026 | } |
79 | 29026 | } |
80 | 9011 | } |
81 | |
} |