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 AtomicInteger threadNumber = new AtomicInteger(0);
22
23 protected final Log log = LogFactory.getLog(getClass());
24
25 protected final long delay;
26 protected final TimeUnit unit;
27 protected final TestCaseWatchdogTimeoutHandler handler;
28
29 public TestCaseWatchdog(long delay, TimeUnit unit, TestCaseWatchdogTimeoutHandler timeoutHandler)
30 {
31 super("WatchdogThread-" + threadNumber.getAndIncrement());
32 this.setDaemon(true);
33 this.delay = delay;
34 this.unit = unit;
35 this.handler = timeoutHandler;
36 }
37
38 public void run()
39 {
40 long millisToWait = this.unit.toMillis(this.delay);
41 if (log.isDebugEnabled())
42 {
43 log.debug("Starting with " + millisToWait + "ms timeout.");
44 }
45
46 try
47 {
48 Thread.sleep(millisToWait);
49 handler.handleTimeout(delay, unit);
50 }
51 catch (InterruptedException interrupted)
52 {
53 if (log.isDebugEnabled())
54 {
55 log.debug("Watchdog stopped.");
56 }
57 }
58 }
59
60 public void cancel()
61 {
62 this.interrupt();
63 }
64
65 }