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