1 package org.xmlBlaster.test.classtest;
2
3 import java.util.logging.Logger;
4 import org.xmlBlaster.util.Timestamp;
5 import org.xmlBlaster.util.Timeout;
6 import org.xmlBlaster.util.I_Timeout;
7 import org.xmlBlaster.util.XmlBlasterException;
8
9 import junit.framework.*;
10
11 /**
12 * Test Timeout class (scheduling for timeouts).
13 * <p />
14 * All methods starting with 'test' and without arguments are invoked automatically
15 * <p />
16 * Invoke: java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.classtest.TimeoutTest
17 * @see org.xmlBlaster.util.Timeout
18 */
19 public class TimeoutTest extends TestCase {
20 private String ME = "TimeoutTest";
21 private static Logger log = Logger.getLogger(TimeoutTest.class.getName());
22 private boolean event = false;
23 private int counter = 0;
24
25 public TimeoutTest(String name) {
26 super(name);
27
28 }
29
30 /**
31 * Test a simple timeout
32 */
33 public void testTimeout() {
34 System.out.println("***TimeoutTest: testTimeout ...");
35
36 {
37 event = false;
38 Timeout timeout = new Timeout(ME);
39 Timestamp timeoutHandle = timeout.addTimeoutListener(new I_Timeout() {
40 public void timeout(Object userData) {
41 event = true;
42 log.info("Timeout happened after 0 millisec");
43 }
44 },
45 0L, null);
46
47 try { Thread.sleep(100L); } catch (InterruptedException e) {}
48 assertEquals("Timeout not occurred after 0 msec.", true, event);
49 }
50
51 {
52 event = false;
53 Timeout timeout = new Timeout(ME);
54 Timestamp timeoutHandle = timeout.addTimeoutListener(new I_Timeout() {
55 public void timeout(Object userData) {
56 event = true;
57 log.info("Timeout happened after 500 millisec");
58 }
59 },
60 500L, null);
61
62 try { Thread.sleep(800L); } catch (InterruptedException e) {}
63 assertEquals("Timeout not occurred after 1 sec.", true, event);
64 }
65
66 {
67 event = false;
68 Timeout timeout = new Timeout(ME);
69 Timestamp timeoutHandle = timeout.addTimeoutListener(new I_Timeout() {
70 public void timeout(Object userData) {
71 event = true;
72 log.severe("Timeout happened after 1 sec");
73 }
74 },
75 1000L, null);
76
77 try { Thread.sleep(200L); } catch (InterruptedException e) {}
78 assertEquals("Timeout occurred unexpected", false, event);
79 timeout.removeTimeoutListener(timeoutHandle);
80 try { Thread.sleep(1000L); } catch (InterruptedException e) {}
81 assertEquals("Timeout occurred unexpected", false, event);
82 }
83
84 System.out.println("***TimeoutTest: testTimeout [SUCCESS]");
85 }
86
87 /**
88 * Testing basic functionality
89 */
90 public void testFunctionality() {
91 System.out.println("***TimeoutTest: testFunctionality ...");
92
93 Timeout timeout = new Timeout();
94 counter = 0;
95
96 // Test to remove invalid keys
97 timeout.removeTimeoutListener(null);
98 timeout.removeTimeoutListener(new Timestamp(12));
99
100 // We have the internal knowledge that the key is the scheduled timeout in millis since 1970
101 // so we use it here for testing ...
102 final Timestamp[] keyArr = new Timestamp[4];
103 class Dummy1 implements I_Timeout {
104 private String ME = "Dummy1";
105 public void timeout(Object userData) {
106 long time = System.currentTimeMillis();
107 long diff = time - keyArr[counter].getMillis();
108 if (Math.abs(diff) < 40)
109 // Allow 40 millis wrong notification (for code execution etc.) ...
110 log.info("Timeout occurred for " + userData.toString() + " at " + time + " millis, real time failure=" + diff + " millis.");
111 else {
112 System.err.println("*****ERROR: Wrong timeout occurred for " + userData.toString() + " at " + time + " millis, scheduled was " + keyArr[counter] + " , real time failure=" + diff + " millis.");
113 fail("*****ERROR: Wrong timeout occurred for " + userData.toString() + " at " + time + " millis, scheduled was " + keyArr[counter] + " , real time failure=" + diff + " millis.");
114 }
115 counter++;
116 }
117 }
118 Dummy1 dummy = new Dummy1();
119 keyArr[2] = timeout.addTimeoutListener(dummy, 4000L, "timer-4000");
120
121 keyArr[3] = timeout.addTimeoutListener(dummy, 2000L, "timer-5500");
122 try { keyArr[3] = timeout.refreshTimeoutListener(keyArr[3], 5500L); } catch (XmlBlasterException e) { fail("Refresh failed: " + e.getMessage()); }
123 long diffT = keyArr[3].getMillis() - System.currentTimeMillis();
124 assertTrue("ERROR: refresh failed", (Math.abs(5500L - diffT) <= 30));
125
126 keyArr[0] = timeout.addTimeoutListener(dummy, 1000L, "timer-1000");
127 keyArr[1] = timeout.addTimeoutListener(dummy, 1000L, "timer-1000");
128
129 long span = timeout.spanToTimeout(keyArr[2]);
130 assertTrue("*****ERROR: This short span to timeout = " + span + " is probably wrong, or you have a very slow computer.", span >= 3000L);
131
132 Timestamp key = timeout.addTimeoutListener(dummy, 1000L, "timer-1000");
133 timeout.removeTimeoutListener(key);
134 try { key = timeout.refreshTimeoutListener(key, 1500L); } catch (XmlBlasterException e) { log.info("Refresh failed which is OK (it is a test): " + e.getMessage()); }
135
136 assertEquals("Should not be expired", false, timeout.isExpired(keyArr[2]));
137
138 try { Thread.sleep(7000L); } catch (Exception e) { fail("*****ERROR: main interrupt: " + e.toString()); }
139
140 assertEquals("Should be expired", true, timeout.isExpired(keyArr[2]));
141
142 System.out.println("***TimeoutTest: testFunctionality [SUCCESS]");
143 }
144
145 /**
146 * We test a big load
147 */
148 public void testStressLoad() {
149 System.out.println("***TimeoutTest: testStressLoad ...");
150
151 String ME = "Timeout-Tester";
152 Timeout timeout = new Timeout();
153
154 final int numTimers = 10000;
155 timeout.shutdown();
156 timeout = new Timeout(); // get a new handle
157 class Dummy2 implements I_Timeout {
158 private String ME = "Dummy2";
159 private long start = 0L;
160 public void timeout(Object userData) {
161 if (counter == 0) {
162 start = System.currentTimeMillis();
163 }
164 counter++;
165 if (counter == numTimers) {
166 long diff = System.currentTimeMillis() - start;
167 assertTrue("Error testing " + numTimers + " timers, all updates needed " + diff + " millis", diff < 4000L);
168 log.info("Success, tested " + numTimers + " timers, all updates came in " + diff + " millis");
169 }
170 }
171 }
172 Dummy2 dummy2 = new Dummy2();
173 long start = System.currentTimeMillis();
174 for (int ii = 0; ii < numTimers; ii++) {
175 timeout.addTimeoutListener(dummy2, 6000L, "timer-" + ii);
176 }
177 assertEquals("Expected " + numTimers + " instead of " + timeout.getSize() + " active timers", numTimers, timeout.getSize());
178
179 log.info("Feeding of " + numTimers + " done, " + (long) (1000 * (double) numTimers / (System.currentTimeMillis() - start)) + " adds/sec");
180
181 while (counter != numTimers) {
182 try { Thread.sleep(500L); } catch (Exception e) { fail("*****ERROR:main interrupt: " + e.toString()); }
183 }
184
185 System.out.println("***TimeoutTest: testStressLoad [SUCCESS]");
186 }
187 }
syntax highlighted by Code2HTML, v. 0.9.1