1 | |
package org.apache.maven.surefire.junitcore.pc; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.junit.runner.Computer; |
23 | |
import org.junit.runner.Description; |
24 | |
|
25 | |
import java.util.Collection; |
26 | |
import java.util.concurrent.Callable; |
27 | |
import java.util.concurrent.Executors; |
28 | |
import java.util.concurrent.Future; |
29 | |
import java.util.concurrent.ScheduledExecutorService; |
30 | |
import java.util.concurrent.TimeUnit; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 29 | public abstract class ParallelComputer extends Computer |
41 | |
{ |
42 | |
private ScheduledExecutorService shutdownScheduler; |
43 | |
|
44 | |
public abstract Collection<Description> shutdown( boolean shutdownNow ); |
45 | |
|
46 | |
protected final void afterRunQuietly() |
47 | |
{ |
48 | 29 | if ( shutdownScheduler != null ) |
49 | |
{ |
50 | 0 | shutdownScheduler.shutdownNow(); |
51 | |
} |
52 | 29 | } |
53 | |
|
54 | |
public Future<Collection<Description>> scheduleShutdown( int timeout, TimeUnit unit ) |
55 | |
{ |
56 | 0 | return getShutdownScheduler().schedule( createShutdownTask( false ), timeout, unit ); |
57 | |
} |
58 | |
|
59 | |
public Future<Collection<Description>> scheduleForcedShutdown( int timeout, TimeUnit unit ) |
60 | |
{ |
61 | 0 | return getShutdownScheduler().schedule( createShutdownTask( true ), timeout, unit ); |
62 | |
} |
63 | |
|
64 | |
private ScheduledExecutorService getShutdownScheduler() |
65 | |
{ |
66 | 0 | if ( shutdownScheduler == null ) |
67 | |
{ |
68 | 0 | shutdownScheduler = Executors.newScheduledThreadPool( 2 ); |
69 | |
} |
70 | 0 | return shutdownScheduler; |
71 | |
} |
72 | |
|
73 | |
private Callable<Collection<Description>> createShutdownTask( final boolean isForced ) |
74 | |
{ |
75 | 0 | return new Callable<Collection<Description>>() |
76 | 0 | { |
77 | |
public Collection<Description> call() throws Exception |
78 | |
{ |
79 | 0 | return shutdown( isForced ); |
80 | |
} |
81 | |
}; |
82 | |
} |
83 | |
} |