1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package net.sf.bluecove;
26
27 import org.bluecove.tester.log.Logger;
28 import org.bluecove.tester.util.RuntimeDetect;
29
30
31
32
33 public class TestTimeOutMonitor implements Runnable {
34
35 private boolean testFinished = false;
36
37 private boolean shutdownCalled = false;
38
39 private CanShutdown testThread;
40
41 private String name;
42
43 int gracePeriodSeconds = 0;
44
45 private Thread monitorThread;
46
47 private TestTimeOutMonitor(String name, CanShutdown testThread, int gracePeriodSeconds) {
48 this.name = name;
49 this.testThread = testThread;
50 this.gracePeriodSeconds = gracePeriodSeconds;
51 }
52
53 public static TestTimeOutMonitor create(String name, CanShutdown testThread, int gracePeriodSeconds) {
54 TestTimeOutMonitor monitor = new TestTimeOutMonitor(name, testThread, gracePeriodSeconds);
55
56 if (gracePeriodSeconds != 0) {
57 monitor.monitorThread = RuntimeDetect.cldcStub.createNamedThread(monitor, name + "Monitor");
58 monitor.monitorThread.start();
59 }
60 return monitor;
61 }
62
63 private int getTestGracePeriod() {
64 if (testThread instanceof ConnectionHolder) {
65 return ((ConnectionHolder) testThread).getTestTimeOutSec();
66 } else {
67 return 0;
68 }
69 }
70
71 public void run() {
72 if (gracePeriodSeconds == 0) {
73 return;
74 }
75
76 while ((!testFinished)
77 && (System.currentTimeMillis() < (testThread.lastActivityTime() + (getTestGracePeriod() + this.gracePeriodSeconds) * 1000))) {
78 try {
79 Thread.sleep(10 * 1000);
80 } catch (InterruptedException e) {
81 return;
82 }
83 }
84
85 if (!testFinished) {
86 shutdownCalled = true;
87 Logger.info("shutdown " + name + " by TimeOut");
88 testThread.shutdown();
89 }
90 }
91
92 public void finish() {
93 testFinished = true;
94 }
95
96 public boolean isShutdownCalled() {
97 return shutdownCalled;
98 }
99 }