ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TimeUnitTest.java
(Generate patch)

Comparing jsr166/src/test/tck/TimeUnitTest.java (file contents):
Revision 1.1 by dl, Sun Aug 31 19:24:56 2003 UTC vs.
Revision 1.23 by jsr166, Tue Sep 24 16:48:52 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines