1
2
3
4
5
6
7
8
9
10
11 package org.mule.tck;
12
13 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
14 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18
19 public class TestCaseWatchdog extends Thread
20 {
21 protected static final Log log = LogFactory.getLog(TestCaseWatchdog.class);
22 protected static final AtomicInteger threadNumber = new AtomicInteger(0);
23
24 private final long delay;
25 private final TimeUnit unit;
26
27 public TestCaseWatchdog(long delay, TimeUnit unit)
28 {
29 super("WatchdogThread-" + threadNumber.getAndIncrement());
30 this.setDaemon(true);
31 this.delay = delay;
32 this.unit = unit;
33 }
34
35 public void run()
36 {
37 long millisToWait = this.unit.toMillis(this.delay);
38 if (log.isDebugEnabled())
39 {
40 log.debug("Starting with " + millisToWait + "ms timeout.");
41 }
42
43 try
44 {
45 Thread.sleep(millisToWait);
46 log.fatal("Timeout of " + millisToWait + "ms exceeded - exiting VM!");
47
48
49 Runtime.getRuntime().halt(1);
50 }
51 catch (InterruptedException interrupted)
52 {
53 if (log.isDebugEnabled())
54 {
55 log.debug("Watchdog stopped.");
56 }
57 Thread.currentThread().interrupt();
58 }
59 }
60
61 public void cancel()
62 {
63 this.interrupt();
64 }
65 }