1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
+ |
import static java.util.concurrent.TimeUnit.DAYS; |
10 |
+ |
import static java.util.concurrent.TimeUnit.HOURS; |
11 |
+ |
import static java.util.concurrent.TimeUnit.MICROSECONDS; |
12 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
+ |
import static java.util.concurrent.TimeUnit.MINUTES; |
14 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
15 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
16 |
|
|
17 |
< |
import junit.framework.*; |
18 |
< |
import java.util.concurrent.*; |
11 |
< |
|
12 |
< |
public class TimeUnitTest extends TestCase { |
13 |
< |
static public boolean DEBUG = false; |
17 |
> |
import java.util.concurrent.CountDownLatch; |
18 |
> |
import java.util.concurrent.TimeUnit; |
19 |
|
|
20 |
+ |
import junit.framework.Test; |
21 |
+ |
import junit.framework.TestSuite; |
22 |
|
|
23 |
+ |
public class TimeUnitTest extends JSR166TestCase { |
24 |
|
public static void main(String[] args) { |
25 |
< |
junit.textui.TestRunner.run(suite()); |
25 |
> |
junit.textui.TestRunner.run(suite()); |
26 |
|
} |
27 |
< |
|
27 |
> |
|
28 |
|
public static Test suite() { |
29 |
< |
return new TestSuite(TimeUnitTest.class); |
29 |
> |
return new TestSuite(TimeUnitTest.class); |
30 |
|
} |
31 |
|
|
32 |
+ |
// (loops to 88888 check increments at all time divisions.) |
33 |
+ |
|
34 |
+ |
/** |
35 |
+ |
* convert correctly converts sample values across the units |
36 |
+ |
*/ |
37 |
|
public void testConvert() { |
38 |
< |
for (long t = 0; t < 10; ++t) { |
39 |
< |
assertEquals(t, |
40 |
< |
TimeUnit.SECONDS.convert(t, |
41 |
< |
TimeUnit.SECONDS)); |
42 |
< |
assertEquals(t, |
43 |
< |
TimeUnit.SECONDS.convert(1000 * t, |
44 |
< |
TimeUnit.MILLISECONDS)); |
45 |
< |
assertEquals(t, |
46 |
< |
TimeUnit.SECONDS.convert(1000000 * t, |
47 |
< |
TimeUnit.MICROSECONDS)); |
48 |
< |
assertEquals(t, |
49 |
< |
TimeUnit.SECONDS.convert(1000000000 * t, |
50 |
< |
TimeUnit.NANOSECONDS)); |
51 |
< |
assertEquals(1000 * t, |
52 |
< |
TimeUnit.MILLISECONDS.convert(t, |
53 |
< |
TimeUnit.SECONDS)); |
54 |
< |
assertEquals(t, |
55 |
< |
TimeUnit.MILLISECONDS.convert(t, |
56 |
< |
TimeUnit.MILLISECONDS)); |
57 |
< |
assertEquals(t, |
58 |
< |
TimeUnit.MILLISECONDS.convert(1000 * t, |
59 |
< |
TimeUnit.MICROSECONDS)); |
60 |
< |
assertEquals(t, |
61 |
< |
TimeUnit.MILLISECONDS.convert(1000000 * t, |
62 |
< |
TimeUnit.NANOSECONDS)); |
63 |
< |
assertEquals(1000000 * t, |
64 |
< |
TimeUnit.MICROSECONDS.convert(t, |
65 |
< |
TimeUnit.SECONDS)); |
66 |
< |
assertEquals(1000 * t, |
67 |
< |
TimeUnit.MICROSECONDS.convert(t, |
68 |
< |
TimeUnit.MILLISECONDS)); |
69 |
< |
assertEquals(t, |
70 |
< |
TimeUnit.MICROSECONDS.convert(t, |
71 |
< |
TimeUnit.MICROSECONDS)); |
72 |
< |
assertEquals(t, |
73 |
< |
TimeUnit.MICROSECONDS.convert(1000 * t, |
74 |
< |
TimeUnit.NANOSECONDS)); |
75 |
< |
assertEquals(1000000000 * t, |
76 |
< |
TimeUnit.NANOSECONDS.convert(t, |
77 |
< |
TimeUnit.SECONDS)); |
78 |
< |
assertEquals(1000000 * t, |
79 |
< |
TimeUnit.NANOSECONDS.convert(t, |
80 |
< |
TimeUnit.MILLISECONDS)); |
81 |
< |
assertEquals(1000 * t, |
82 |
< |
TimeUnit.NANOSECONDS.convert(t, |
83 |
< |
TimeUnit.MICROSECONDS)); |
84 |
< |
assertEquals(t, |
85 |
< |
TimeUnit.NANOSECONDS.convert(t, |
86 |
< |
TimeUnit.NANOSECONDS)); |
38 |
> |
for (long t = 0; t < 88888; ++t) { |
39 |
> |
assertEquals(t*60*60*24, |
40 |
> |
SECONDS.convert(t, DAYS)); |
41 |
> |
assertEquals(t*60*60, |
42 |
> |
SECONDS.convert(t, HOURS)); |
43 |
> |
assertEquals(t*60, |
44 |
> |
SECONDS.convert(t, MINUTES)); |
45 |
> |
assertEquals(t, |
46 |
> |
SECONDS.convert(t, SECONDS)); |
47 |
> |
assertEquals(t, |
48 |
> |
SECONDS.convert(1000L*t, MILLISECONDS)); |
49 |
> |
assertEquals(t, |
50 |
> |
SECONDS.convert(1000000L*t, MICROSECONDS)); |
51 |
> |
assertEquals(t, |
52 |
> |
SECONDS.convert(1000000000L*t, NANOSECONDS)); |
53 |
> |
|
54 |
> |
assertEquals(1000L*t*60*60*24, |
55 |
> |
MILLISECONDS.convert(t, DAYS)); |
56 |
> |
assertEquals(1000L*t*60*60, |
57 |
> |
MILLISECONDS.convert(t, HOURS)); |
58 |
> |
assertEquals(1000L*t*60, |
59 |
> |
MILLISECONDS.convert(t, MINUTES)); |
60 |
> |
assertEquals(1000L*t, |
61 |
> |
MILLISECONDS.convert(t, SECONDS)); |
62 |
> |
assertEquals(t, |
63 |
> |
MILLISECONDS.convert(t, MILLISECONDS)); |
64 |
> |
assertEquals(t, |
65 |
> |
MILLISECONDS.convert(1000L*t, MICROSECONDS)); |
66 |
> |
assertEquals(t, |
67 |
> |
MILLISECONDS.convert(1000000L*t, NANOSECONDS)); |
68 |
> |
|
69 |
> |
assertEquals(1000000L*t*60*60*24, |
70 |
> |
MICROSECONDS.convert(t, DAYS)); |
71 |
> |
assertEquals(1000000L*t*60*60, |
72 |
> |
MICROSECONDS.convert(t, HOURS)); |
73 |
> |
assertEquals(1000000L*t*60, |
74 |
> |
MICROSECONDS.convert(t, MINUTES)); |
75 |
> |
assertEquals(1000000L*t, |
76 |
> |
MICROSECONDS.convert(t, SECONDS)); |
77 |
> |
assertEquals(1000L*t, |
78 |
> |
MICROSECONDS.convert(t, MILLISECONDS)); |
79 |
> |
assertEquals(t, |
80 |
> |
MICROSECONDS.convert(t, MICROSECONDS)); |
81 |
> |
assertEquals(t, |
82 |
> |
MICROSECONDS.convert(1000L*t, NANOSECONDS)); |
83 |
> |
|
84 |
> |
assertEquals(1000000000L*t*60*60*24, |
85 |
> |
NANOSECONDS.convert(t, DAYS)); |
86 |
> |
assertEquals(1000000000L*t*60*60, |
87 |
> |
NANOSECONDS.convert(t, HOURS)); |
88 |
> |
assertEquals(1000000000L*t*60, |
89 |
> |
NANOSECONDS.convert(t, MINUTES)); |
90 |
> |
assertEquals(1000000000L*t, |
91 |
> |
NANOSECONDS.convert(t, SECONDS)); |
92 |
> |
assertEquals(1000000L*t, |
93 |
> |
NANOSECONDS.convert(t, MILLISECONDS)); |
94 |
> |
assertEquals(1000L*t, |
95 |
> |
NANOSECONDS.convert(t, MICROSECONDS)); |
96 |
> |
assertEquals(t, |
97 |
> |
NANOSECONDS.convert(t, NANOSECONDS)); |
98 |
|
} |
99 |
|
} |
100 |
|
|
101 |
+ |
/** |
102 |
+ |
* toNanos correctly converts sample values in different units to |
103 |
+ |
* nanoseconds |
104 |
+ |
*/ |
105 |
|
public void testToNanos() { |
106 |
< |
for (long t = 0; t < 10; ++t) { |
107 |
< |
assertEquals(1000000000 * t, |
108 |
< |
TimeUnit.SECONDS.toNanos(t)); |
109 |
< |
|
110 |
< |
assertEquals(1000000 * t, |
111 |
< |
TimeUnit.MILLISECONDS.toNanos(t)); |
112 |
< |
assertEquals(1000 * t, |
113 |
< |
TimeUnit.MICROSECONDS.toNanos(t)); |
114 |
< |
assertEquals(t, |
115 |
< |
TimeUnit.SECONDS.NANOSECONDS.toNanos(t)); |
106 |
> |
for (long t = 0; t < 88888; ++t) { |
107 |
> |
assertEquals(t*1000000000L*60*60*24, |
108 |
> |
DAYS.toNanos(t)); |
109 |
> |
assertEquals(t*1000000000L*60*60, |
110 |
> |
HOURS.toNanos(t)); |
111 |
> |
assertEquals(t*1000000000L*60, |
112 |
> |
MINUTES.toNanos(t)); |
113 |
> |
assertEquals(1000000000L*t, |
114 |
> |
SECONDS.toNanos(t)); |
115 |
> |
assertEquals(1000000L*t, |
116 |
> |
MILLISECONDS.toNanos(t)); |
117 |
> |
assertEquals(1000L*t, |
118 |
> |
MICROSECONDS.toNanos(t)); |
119 |
> |
assertEquals(t, |
120 |
> |
NANOSECONDS.toNanos(t)); |
121 |
|
} |
122 |
|
} |
123 |
|
|
124 |
+ |
/** |
125 |
+ |
* toMicros correctly converts sample values in different units to |
126 |
+ |
* microseconds |
127 |
+ |
*/ |
128 |
+ |
public void testToMicros() { |
129 |
+ |
for (long t = 0; t < 88888; ++t) { |
130 |
+ |
assertEquals(t*1000000L*60*60*24, |
131 |
+ |
DAYS.toMicros(t)); |
132 |
+ |
assertEquals(t*1000000L*60*60, |
133 |
+ |
HOURS.toMicros(t)); |
134 |
+ |
assertEquals(t*1000000L*60, |
135 |
+ |
MINUTES.toMicros(t)); |
136 |
+ |
assertEquals(1000000L*t, |
137 |
+ |
SECONDS.toMicros(t)); |
138 |
+ |
assertEquals(1000L*t, |
139 |
+ |
MILLISECONDS.toMicros(t)); |
140 |
+ |
assertEquals(t, |
141 |
+ |
MICROSECONDS.toMicros(t)); |
142 |
+ |
assertEquals(t, |
143 |
+ |
NANOSECONDS.toMicros(t*1000L)); |
144 |
+ |
} |
145 |
+ |
} |
146 |
|
|
147 |
+ |
/** |
148 |
+ |
* toMillis correctly converts sample values in different units to |
149 |
+ |
* milliseconds |
150 |
+ |
*/ |
151 |
+ |
public void testToMillis() { |
152 |
+ |
for (long t = 0; t < 88888; ++t) { |
153 |
+ |
assertEquals(t*1000L*60*60*24, |
154 |
+ |
DAYS.toMillis(t)); |
155 |
+ |
assertEquals(t*1000L*60*60, |
156 |
+ |
HOURS.toMillis(t)); |
157 |
+ |
assertEquals(t*1000L*60, |
158 |
+ |
MINUTES.toMillis(t)); |
159 |
+ |
assertEquals(1000L*t, |
160 |
+ |
SECONDS.toMillis(t)); |
161 |
+ |
assertEquals(t, |
162 |
+ |
MILLISECONDS.toMillis(t)); |
163 |
+ |
assertEquals(t, |
164 |
+ |
MICROSECONDS.toMillis(t*1000L)); |
165 |
+ |
assertEquals(t, |
166 |
+ |
NANOSECONDS.toMillis(t*1000000L)); |
167 |
+ |
} |
168 |
+ |
} |
169 |
+ |
|
170 |
+ |
/** |
171 |
+ |
* toSeconds correctly converts sample values in different units to |
172 |
+ |
* seconds |
173 |
+ |
*/ |
174 |
+ |
public void testToSeconds() { |
175 |
+ |
for (long t = 0; t < 88888; ++t) { |
176 |
+ |
assertEquals(t*60*60*24, |
177 |
+ |
DAYS.toSeconds(t)); |
178 |
+ |
assertEquals(t*60*60, |
179 |
+ |
HOURS.toSeconds(t)); |
180 |
+ |
assertEquals(t*60, |
181 |
+ |
MINUTES.toSeconds(t)); |
182 |
+ |
assertEquals(t, |
183 |
+ |
SECONDS.toSeconds(t)); |
184 |
+ |
assertEquals(t, |
185 |
+ |
MILLISECONDS.toSeconds(t*1000L)); |
186 |
+ |
assertEquals(t, |
187 |
+ |
MICROSECONDS.toSeconds(t*1000000L)); |
188 |
+ |
assertEquals(t, |
189 |
+ |
NANOSECONDS.toSeconds(t*1000000000L)); |
190 |
+ |
} |
191 |
+ |
} |
192 |
+ |
|
193 |
+ |
/** |
194 |
+ |
* toMinutes correctly converts sample values in different units to |
195 |
+ |
* minutes |
196 |
+ |
*/ |
197 |
+ |
public void testToMinutes() { |
198 |
+ |
for (long t = 0; t < 88888; ++t) { |
199 |
+ |
assertEquals(t*60*24, |
200 |
+ |
DAYS.toMinutes(t)); |
201 |
+ |
assertEquals(t*60, |
202 |
+ |
HOURS.toMinutes(t)); |
203 |
+ |
assertEquals(t, |
204 |
+ |
MINUTES.toMinutes(t)); |
205 |
+ |
assertEquals(t, |
206 |
+ |
SECONDS.toMinutes(t*60)); |
207 |
+ |
assertEquals(t, |
208 |
+ |
MILLISECONDS.toMinutes(t*1000L*60)); |
209 |
+ |
assertEquals(t, |
210 |
+ |
MICROSECONDS.toMinutes(t*1000000L*60)); |
211 |
+ |
assertEquals(t, |
212 |
+ |
NANOSECONDS.toMinutes(t*1000000000L*60)); |
213 |
+ |
} |
214 |
+ |
} |
215 |
+ |
|
216 |
+ |
/** |
217 |
+ |
* toHours correctly converts sample values in different units to |
218 |
+ |
* hours |
219 |
+ |
*/ |
220 |
+ |
public void testToHours() { |
221 |
+ |
for (long t = 0; t < 88888; ++t) { |
222 |
+ |
assertEquals(t*24, |
223 |
+ |
DAYS.toHours(t)); |
224 |
+ |
assertEquals(t, |
225 |
+ |
HOURS.toHours(t)); |
226 |
+ |
assertEquals(t, |
227 |
+ |
MINUTES.toHours(t*60)); |
228 |
+ |
assertEquals(t, |
229 |
+ |
SECONDS.toHours(t*60*60)); |
230 |
+ |
assertEquals(t, |
231 |
+ |
MILLISECONDS.toHours(t*1000L*60*60)); |
232 |
+ |
assertEquals(t, |
233 |
+ |
MICROSECONDS.toHours(t*1000000L*60*60)); |
234 |
+ |
assertEquals(t, |
235 |
+ |
NANOSECONDS.toHours(t*1000000000L*60*60)); |
236 |
+ |
} |
237 |
+ |
} |
238 |
+ |
|
239 |
+ |
/** |
240 |
+ |
* toDays correctly converts sample values in different units to |
241 |
+ |
* days |
242 |
+ |
*/ |
243 |
+ |
public void testToDays() { |
244 |
+ |
for (long t = 0; t < 88888; ++t) { |
245 |
+ |
assertEquals(t, |
246 |
+ |
DAYS.toDays(t)); |
247 |
+ |
assertEquals(t, |
248 |
+ |
HOURS.toDays(t*24)); |
249 |
+ |
assertEquals(t, |
250 |
+ |
MINUTES.toDays(t*60*24)); |
251 |
+ |
assertEquals(t, |
252 |
+ |
SECONDS.toDays(t*60*60*24)); |
253 |
+ |
assertEquals(t, |
254 |
+ |
MILLISECONDS.toDays(t*1000L*60*60*24)); |
255 |
+ |
assertEquals(t, |
256 |
+ |
MICROSECONDS.toDays(t*1000000L*60*60*24)); |
257 |
+ |
assertEquals(t, |
258 |
+ |
NANOSECONDS.toDays(t*1000000000L*60*60*24)); |
259 |
+ |
} |
260 |
+ |
} |
261 |
+ |
|
262 |
+ |
/** |
263 |
+ |
* convert saturates positive too-large values to Long.MAX_VALUE |
264 |
+ |
* and negative to LONG.MIN_VALUE |
265 |
+ |
*/ |
266 |
|
public void testConvertSaturate() { |
267 |
|
assertEquals(Long.MAX_VALUE, |
268 |
< |
TimeUnit.NANOSECONDS.convert(Long.MAX_VALUE / 2, |
95 |
< |
TimeUnit.SECONDS)); |
268 |
> |
NANOSECONDS.convert(Long.MAX_VALUE / 2, SECONDS)); |
269 |
|
assertEquals(Long.MIN_VALUE, |
270 |
< |
TimeUnit.NANOSECONDS.convert(-Long.MAX_VALUE / 4, |
271 |
< |
TimeUnit.SECONDS)); |
272 |
< |
|
270 |
> |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, SECONDS)); |
271 |
> |
assertEquals(Long.MAX_VALUE, |
272 |
> |
NANOSECONDS.convert(Long.MAX_VALUE / 2, MINUTES)); |
273 |
> |
assertEquals(Long.MIN_VALUE, |
274 |
> |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, MINUTES)); |
275 |
> |
assertEquals(Long.MAX_VALUE, |
276 |
> |
NANOSECONDS.convert(Long.MAX_VALUE / 2, HOURS)); |
277 |
> |
assertEquals(Long.MIN_VALUE, |
278 |
> |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, HOURS)); |
279 |
> |
assertEquals(Long.MAX_VALUE, |
280 |
> |
NANOSECONDS.convert(Long.MAX_VALUE / 2, DAYS)); |
281 |
> |
assertEquals(Long.MIN_VALUE, |
282 |
> |
NANOSECONDS.convert(-Long.MAX_VALUE / 4, DAYS)); |
283 |
|
} |
284 |
|
|
285 |
+ |
/** |
286 |
+ |
* toNanos saturates positive too-large values to Long.MAX_VALUE |
287 |
+ |
* and negative to LONG.MIN_VALUE |
288 |
+ |
*/ |
289 |
|
public void testToNanosSaturate() { |
290 |
< |
assertEquals(Long.MAX_VALUE, |
291 |
< |
TimeUnit.MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
292 |
< |
assertEquals(Long.MIN_VALUE, |
293 |
< |
TimeUnit.MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
107 |
< |
|
290 |
> |
assertEquals(Long.MAX_VALUE, |
291 |
> |
MILLISECONDS.toNanos(Long.MAX_VALUE / 2)); |
292 |
> |
assertEquals(Long.MIN_VALUE, |
293 |
> |
MILLISECONDS.toNanos(-Long.MAX_VALUE / 3)); |
294 |
|
} |
295 |
|
|
296 |
< |
|
296 |
> |
/** |
297 |
> |
* toString returns name of unit |
298 |
> |
*/ |
299 |
|
public void testToString() { |
300 |
< |
String s = TimeUnit.SECONDS.toString(); |
301 |
< |
assertTrue(s.indexOf("econd") >= 0); |
300 |
> |
assertEquals("SECONDS", SECONDS.toString()); |
301 |
> |
} |
302 |
> |
|
303 |
> |
/** |
304 |
> |
* name returns name of unit |
305 |
> |
*/ |
306 |
> |
public void testName() { |
307 |
> |
assertEquals("SECONDS", SECONDS.name()); |
308 |
|
} |
309 |
|
|
116 |
– |
// Exception tests |
117 |
– |
|
310 |
|
/** |
311 |
< |
* This test specifically to catch the unreported exception |
311 |
> |
* Timed wait without holding lock throws |
312 |
> |
* IllegalMonitorStateException |
313 |
|
*/ |
314 |
< |
public void testTimedWaitForUnreportedIllegalMonitorException() { |
315 |
< |
//created a new thread with anonymous runnable |
314 |
> |
public void testTimedWait_IllegalMonitorException() { |
315 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
316 |
> |
public void realRun() throws InterruptedException { |
317 |
> |
Object o = new Object(); |
318 |
> |
TimeUnit tu = MILLISECONDS; |
319 |
> |
|
320 |
> |
try { |
321 |
> |
tu.timedWait(o, LONG_DELAY_MS); |
322 |
> |
threadShouldThrow(); |
323 |
> |
} catch (IllegalMonitorStateException success) {} |
324 |
> |
}}); |
325 |
|
|
326 |
< |
Thread t = new Thread(new Runnable() { |
327 |
< |
public void run() { |
328 |
< |
Object o = new Object(); |
329 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
330 |
< |
try { |
331 |
< |
tu.timedWait(o,40000); |
332 |
< |
fail("should throw"); |
326 |
> |
awaitTermination(t); |
327 |
> |
} |
328 |
> |
|
329 |
> |
/** |
330 |
> |
* timedWait throws InterruptedException when interrupted |
331 |
> |
*/ |
332 |
> |
public void testTimedWait_Interruptible() { |
333 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
334 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
335 |
> |
public void realRun() throws InterruptedException { |
336 |
> |
Object o = new Object(); |
337 |
> |
TimeUnit tu = MILLISECONDS; |
338 |
> |
|
339 |
> |
Thread.currentThread().interrupt(); |
340 |
> |
try { |
341 |
> |
synchronized (o) { |
342 |
> |
tu.timedWait(o, LONG_DELAY_MS); |
343 |
|
} |
344 |
< |
catch (InterruptedException ie) { |
345 |
< |
fail("should not throw IE here"); |
346 |
< |
} |
347 |
< |
catch(IllegalMonitorStateException success) { |
344 |
> |
shouldThrow(); |
345 |
> |
} catch (InterruptedException success) {} |
346 |
> |
assertFalse(Thread.interrupted()); |
347 |
> |
|
348 |
> |
pleaseInterrupt.countDown(); |
349 |
> |
try { |
350 |
> |
synchronized (o) { |
351 |
> |
tu.timedWait(o, LONG_DELAY_MS); |
352 |
|
} |
353 |
< |
|
354 |
< |
} |
355 |
< |
}); |
356 |
< |
t.start(); |
357 |
< |
try { |
358 |
< |
Thread.sleep(100); |
359 |
< |
t.interrupt(); |
360 |
< |
t.join(); |
361 |
< |
} catch(Exception e) { |
362 |
< |
fail("Unexpected exception"); |
363 |
< |
} |
364 |
< |
} |
365 |
< |
|
366 |
< |
/** |
367 |
< |
* Test to verify that timedWait will throw InterruptedException. |
368 |
< |
* Thread t waits on timedWait while the main thread interrupts it. |
369 |
< |
* Note: This does not throw IllegalMonitorException since timeWait |
370 |
< |
* is synchronized on o |
371 |
< |
*/ |
372 |
< |
public void testTimedWait() { |
373 |
< |
Thread t = new Thread(new Runnable() { |
374 |
< |
public void run() { |
375 |
< |
Object o = new Object(); |
376 |
< |
|
377 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
378 |
< |
try { |
379 |
< |
synchronized(o) { |
380 |
< |
tu.timedWait(o,1000); |
381 |
< |
} |
382 |
< |
fail("should throw"); |
383 |
< |
} |
384 |
< |
catch(InterruptedException success) {} |
385 |
< |
catch(IllegalMonitorStateException failure) { |
386 |
< |
fail("should not throw"); |
387 |
< |
} |
388 |
< |
} |
389 |
< |
}); |
390 |
< |
t.start(); |
391 |
< |
try { |
392 |
< |
Thread.sleep(100); |
393 |
< |
t.interrupt(); |
394 |
< |
t.join(); |
395 |
< |
} catch(Exception e) { |
396 |
< |
fail("Unexpected exception"); |
181 |
< |
} |
182 |
< |
} |
183 |
< |
|
184 |
< |
|
185 |
< |
/** |
186 |
< |
* Test to verify that timedJoin will throw InterruptedException. |
187 |
< |
* Thread t waits on timedJoin while the main thread interrupts it. |
188 |
< |
*/ |
189 |
< |
public void testTimedJoin() { |
190 |
< |
Thread t = new Thread(new Runnable() { |
191 |
< |
public void run() { |
192 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
193 |
< |
try { |
194 |
< |
Thread s = new Thread(new Runnable() { |
195 |
< |
public void run() { |
196 |
< |
try{ |
197 |
< |
Thread.sleep(1000); |
198 |
< |
}catch(InterruptedException success){} |
199 |
< |
} |
200 |
< |
}); |
201 |
< |
s.start(); |
202 |
< |
tu.timedJoin(s,1000); |
203 |
< |
fail("should throw"); |
204 |
< |
} |
205 |
< |
catch(Exception e) {} |
206 |
< |
} |
207 |
< |
}); |
208 |
< |
t.start(); |
209 |
< |
try { |
210 |
< |
Thread.sleep(100); |
211 |
< |
t.interrupt(); |
212 |
< |
t.join(); |
213 |
< |
} catch(Exception e) { |
214 |
< |
fail("Unexpected exception"); |
215 |
< |
} |
216 |
< |
} |
217 |
< |
|
218 |
< |
/** |
219 |
< |
* Test to verify that timedSleep will throw InterruptedException. |
220 |
< |
* Thread t waits on timedSleep while the main thread interrupts it. |
221 |
< |
*/ |
222 |
< |
public void testTimedSleep() { |
223 |
< |
//created a new thread with anonymous runnable |
224 |
< |
|
225 |
< |
Thread t = new Thread(new Runnable() { |
226 |
< |
public void run() { |
227 |
< |
TimeUnit tu = TimeUnit.MILLISECONDS; |
228 |
< |
try { |
229 |
< |
tu.sleep(1000); |
230 |
< |
fail("should throw"); |
231 |
< |
} |
232 |
< |
catch(InterruptedException success) {} |
233 |
< |
} |
234 |
< |
}); |
235 |
< |
t.start(); |
236 |
< |
try { |
237 |
< |
Thread.sleep(100); |
238 |
< |
t.interrupt(); |
239 |
< |
t.join(); |
240 |
< |
} catch(Exception e) { |
241 |
< |
fail("Unexpected exception"); |
242 |
< |
} |
353 |
> |
shouldThrow(); |
354 |
> |
} catch (InterruptedException success) {} |
355 |
> |
assertFalse(Thread.interrupted()); |
356 |
> |
}}); |
357 |
> |
|
358 |
> |
await(pleaseInterrupt); |
359 |
> |
assertThreadStaysAlive(t); |
360 |
> |
t.interrupt(); |
361 |
> |
awaitTermination(t); |
362 |
> |
} |
363 |
> |
|
364 |
> |
/** |
365 |
> |
* timedJoin throws InterruptedException when interrupted |
366 |
> |
*/ |
367 |
> |
public void testTimedJoin_Interruptible() { |
368 |
> |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
369 |
> |
final Thread s = newStartedThread(new CheckedInterruptedRunnable() { |
370 |
> |
public void realRun() throws InterruptedException { |
371 |
> |
Thread.sleep(LONG_DELAY_MS); |
372 |
> |
}}); |
373 |
> |
final Thread t = newStartedThread(new CheckedRunnable() { |
374 |
> |
public void realRun() throws InterruptedException { |
375 |
> |
TimeUnit tu = MILLISECONDS; |
376 |
> |
Thread.currentThread().interrupt(); |
377 |
> |
try { |
378 |
> |
tu.timedJoin(s, LONG_DELAY_MS); |
379 |
> |
shouldThrow(); |
380 |
> |
} catch (InterruptedException success) {} |
381 |
> |
assertFalse(Thread.interrupted()); |
382 |
> |
|
383 |
> |
pleaseInterrupt.countDown(); |
384 |
> |
try { |
385 |
> |
tu.timedJoin(s, LONG_DELAY_MS); |
386 |
> |
shouldThrow(); |
387 |
> |
} catch (InterruptedException success) {} |
388 |
> |
assertFalse(Thread.interrupted()); |
389 |
> |
}}); |
390 |
> |
|
391 |
> |
await(pleaseInterrupt); |
392 |
> |
assertThreadStaysAlive(t); |
393 |
> |
t.interrupt(); |
394 |
> |
awaitTermination(t); |
395 |
> |
s.interrupt(); |
396 |
> |
awaitTermination(s); |
397 |
|
} |
398 |
+ |
|
399 |
+ |
/** |
400 |
+ |
* timedSleep throws InterruptedException when interrupted |
401 |
+ |
*/ |
402 |
+ |
public void testTimedSleep_Interruptible() { |
403 |
+ |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
404 |
+ |
Thread t = newStartedThread(new CheckedRunnable() { |
405 |
+ |
public void realRun() throws InterruptedException { |
406 |
+ |
TimeUnit tu = MILLISECONDS; |
407 |
+ |
Thread.currentThread().interrupt(); |
408 |
+ |
try { |
409 |
+ |
tu.sleep(LONG_DELAY_MS); |
410 |
+ |
shouldThrow(); |
411 |
+ |
} catch (InterruptedException success) {} |
412 |
+ |
assertFalse(Thread.interrupted()); |
413 |
+ |
|
414 |
+ |
pleaseInterrupt.countDown(); |
415 |
+ |
try { |
416 |
+ |
tu.sleep(LONG_DELAY_MS); |
417 |
+ |
shouldThrow(); |
418 |
+ |
} catch (InterruptedException success) {} |
419 |
+ |
assertFalse(Thread.interrupted()); |
420 |
+ |
}}); |
421 |
+ |
|
422 |
+ |
await(pleaseInterrupt); |
423 |
+ |
assertThreadStaysAlive(t); |
424 |
+ |
t.interrupt(); |
425 |
+ |
awaitTermination(t); |
426 |
+ |
} |
427 |
+ |
|
428 |
+ |
/** |
429 |
+ |
* a deserialized serialized unit is the same instance |
430 |
+ |
*/ |
431 |
+ |
public void testSerialization() throws Exception { |
432 |
+ |
TimeUnit x = MILLISECONDS; |
433 |
+ |
assertSame(x, serialClone(x)); |
434 |
+ |
} |
435 |
+ |
|
436 |
|
} |