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 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 if ( shutdownScheduler != null )
49 {
50 shutdownScheduler.shutdownNow();
51 }
52 }
53
54 public Future<Collection<Description>> scheduleShutdown( int timeout, TimeUnit unit )
55 {
56 return getShutdownScheduler().schedule( createShutdownTask( false ), timeout, unit );
57 }
58
59 public Future<Collection<Description>> scheduleForcedShutdown( int timeout, TimeUnit unit )
60 {
61 return getShutdownScheduler().schedule( createShutdownTask( true ), timeout, unit );
62 }
63
64 private ScheduledExecutorService getShutdownScheduler()
65 {
66 if ( shutdownScheduler == null )
67 {
68 shutdownScheduler = Executors.newScheduledThreadPool( 2 );
69 }
70 return shutdownScheduler;
71 }
72
73 private Callable<Collection<Description>> createShutdownTask( final boolean isForced )
74 {
75 return new Callable<Collection<Description>>()
76 {
77 public Collection<Description> call() throws Exception
78 {
79 return shutdown( isForced );
80 }
81 };
82 }
83 }